@@ -24,8 +24,7 @@ echo "Starting vsock proxy..."
2424
2525TIME_SYNC_URL=" http://127.0.0.1:27015/getCurrentTime"
2626TIME_SYNC_PROXY=" socks5h://127.0.0.1:3305"
27- TIME_SYNC_INTERVAL_SECONDS=" 300"
28-
27+ TIME_SYNC_TRIGGER_PORT=" ${TIME_SYNC_TRIGGER_PORT:- 27100} "
2928TIME_SYNC_OFFSET_SECONDS=" ${TIME_SYNC_OFFSET_SECONDS:- 30} "
3029
3130sync_enclave_time_with_offset_once () {
@@ -50,43 +49,52 @@ sync_enclave_time_with_offset_once() {
5049sync_enclave_time_with_offset_once || true
5150
5251
53- enable_time_sync_timer () {
54- if ! command -v systemctl > /dev/null 2>&1 || [[ ! -d /run/systemd/system ]]; then
55- echo " Time sync: systemd not available; skipping timer setup" >&2
56- return 0
57- fi
5852
59- cat << EOF >/etc/systemd/system/uid2-time-sync.service
60- [Unit]
61- Description=UID2 enclave time sync
62-
63- [Service]
64- Type=oneshot
65- Environment=TIME_SYNC_URL=${TIME_SYNC_URL}
66- Environment=TIME_SYNC_PROXY=${TIME_SYNC_PROXY}
67- ExecStart=/bin/bash -c 'set -euo pipefail; current_time="$( curl -sSf -x " $TIME_SYNC_PROXY " " $TIME_SYNC_URL " ) "; date -u -s "$current_time "; echo "Time sync: updated enclave time to $current_time "'
68- EOF
69-
70- cat << EOF >/etc/systemd/system/uid2-time-sync.timer
71- [Unit]
72- Description=UID2 enclave time sync timer
73-
74- [Timer]
75- OnBootSec=300s
76- OnUnitActiveSec=${TIME_SYNC_INTERVAL_SECONDS} s
77- Unit=uid2-time-sync.service
78- Persistent=true
79- AccuracySec=1s
80-
81- [Install]
82- WantedBy=timers.target
83- EOF
84-
85- systemctl daemon-reload
86- systemctl enable --now uid2-time-sync.timer
53+ start_time_sync_server () {
54+ python3 - << 'PY ' &
55+ import os
56+ import subprocess
57+ from http.server import BaseHTTPRequestHandler, HTTPServer
58+
59+ TIME_SYNC_URL = os.environ.get("TIME_SYNC_URL", "http://127.0.0.1:27015/getCurrentTime")
60+ TIME_SYNC_PROXY = os.environ.get("TIME_SYNC_PROXY", "socks5h://127.0.0.1:3305")
61+ TIME_SYNC_TRIGGER_PORT = int(os.environ.get("TIME_SYNC_TRIGGER_PORT", "27100"))
62+
63+ def sync_time() -> str:
64+ current_time = subprocess.check_output(
65+ ["curl", "-sSf", "-x", TIME_SYNC_PROXY, TIME_SYNC_URL],
66+ text=True,
67+ ).strip()
68+ subprocess.check_call(["date", "-u", "-s", current_time])
69+ return current_time
70+
71+ class Handler(BaseHTTPRequestHandler):
72+ def do_GET(self) -> None:
73+ if self.path not in ("/", "/sync"):
74+ self.send_response(404)
75+ self.end_headers()
76+ return
77+ try:
78+ result = sync_time()
79+ print(f"Time sync: updated enclave time to {result}")
80+ self.send_response(200)
81+ self.end_headers()
82+ self.wfile.write(f"OK {result}\n".encode())
83+ except Exception as exc: # pragma: no cover - best effort logging
84+ print(f"Time sync error: {exc}")
85+ self.send_response(500)
86+ self.end_headers()
87+ self.wfile.write(f"ERROR {exc}\n".encode())
88+
89+ def log_message(self, format, *args): # noqa: N802 - match base class
90+ return
91+
92+ server = HTTPServer(("127.0.0.1", TIME_SYNC_TRIGGER_PORT), Handler)
93+ server.serve_forever()
94+ PY
8795}
8896
89- enable_time_sync_timer
97+ start_time_sync_server
9098
9199build_parameterized_config () {
92100 curl -s -f -o " ${PARAMETERIZED_CONFIG} " -x socks5h://127.0.0.1:3305 http://127.0.0.1:27015/getConfig
0 commit comments