@@ -23,39 +23,46 @@ echo "Starting vsock proxy..."
2323/app/vsockpx --config /app/proxies.nitro.yaml --daemon --workers $(( ( $(nproc) + 3 ) / 4 )) --log-level 3
2424
2525TIME_SYNC_URL=" http://127.0.0.1:27015/getCurrentTime"
26- TIME_SYNC_INTERVAL_SECONDS=" ${TIME_SYNC_INTERVAL_SECONDS:- 86400} "
27-
28- sync_enclave_time () {
29- local current_time
30- local parent_epoch
31- local enclave_epoch
32- local drift_seconds
33- if current_time=$( curl -s -f -x socks5h://127.0.0.1:3305 " ${TIME_SYNC_URL} " ) ; then
34- parent_epoch=$( date -u -d " ${current_time} " +%s 2> /dev/null || true)
35- enclave_epoch=$( date -u +%s)
36- if [[ -n " ${parent_epoch} " ]]; then
37- drift_seconds=$(( enclave_epoch - parent_epoch))
38- echo " Time sync: drift seconds (enclave - parent) = ${drift_seconds} "
39- fi
40- if ! date -u -s " ${current_time} " ; then
41- echo " Time sync: failed to set enclave time from '${current_time} '"
42- return 1
43- fi
44- echo " Time sync: updated enclave time to ${current_time} "
45- else
46- echo " Time sync: failed to fetch time from parent instance"
47- return 1
26+ TIME_SYNC_PROXY=" socks5h://127.0.0.1:3305"
27+ TIME_SYNC_INTERVAL_SECONDS=" 300"
28+
29+ enable_time_sync_timer () {
30+ if ! command -v systemctl > /dev/null 2>&1 || [[ ! -d /run/systemd/system ]]; then
31+ echo " Time sync: systemd not available; skipping timer setup" >&2
32+ return 0
4833 fi
49- }
5034
51- start_time_sync_loop () {
52- while true ; do
53- sleep " ${TIME_SYNC_INTERVAL_SECONDS} "
54- sync_enclave_time || true
55- done
35+ cat << EOF >/etc/systemd/system/uid2-time-sync.service
36+ [Unit]
37+ Description=UID2 enclave time sync
38+
39+ [Service]
40+ Type=oneshot
41+ Environment=TIME_SYNC_URL=${TIME_SYNC_URL}
42+ Environment=TIME_SYNC_PROXY=${TIME_SYNC_PROXY}
43+ ExecStart=/bin/bash -c 'set -euo pipefail; curl -sSf -x "$TIME_SYNC_PROXY " "$TIME_SYNC_URL " | xargs -I{} date -u -s "{}"; echo "Time sync: updated enclave time to $current_time "'
44+ EOF
45+
46+ cat << EOF >/etc/systemd/system/uid2-time-sync.timer
47+ [Unit]
48+ Description=UID2 enclave time sync timer
49+
50+ [Timer]
51+ OnBootSec=30s
52+ OnUnitActiveSec=${TIME_SYNC_INTERVAL_SECONDS} s
53+ Unit=uid2-time-sync.service
54+ Persistent=true
55+ AccuracySec=1s
56+
57+ [Install]
58+ WantedBy=timers.target
59+ EOF
60+
61+ systemctl daemon-reload
62+ systemctl enable --now uid2-time-sync.timer
5663}
5764
58- start_time_sync_loop &
65+ enable_time_sync_timer
5966
6067build_parameterized_config () {
6168 curl -s -f -o " ${PARAMETERIZED_CONFIG} " -x socks5h://127.0.0.1:3305 http://127.0.0.1:27015/getConfig
0 commit comments