Remove init

vmess-time
风扇滑翔翼 2025-09-06 19:21:29 +00:00 committed by GitHub
parent 30e3fa690e
commit 033d2ba2b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 16 deletions

View File

@ -3,8 +3,10 @@ package time
import ( import (
"context" "context"
"net/http" "net/http"
"sync"
"sync/atomic" "sync/atomic"
"time" "time"
"runtime"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
@ -13,22 +15,7 @@ import (
) )
var timeOffset atomic.Pointer[time.Duration] var timeOffset atomic.Pointer[time.Duration]
var initOnce sync.Once
func init() {
timeOffset.Store(new(time.Duration))
domain := platform.NewEnvFlag("xray.vmess.time.domain").GetValue(func() string { return "https://apple.com" })
if domain == "" {
errors.LogError(context.Background(), "vmess time domain is empty, skip time sync")
return
}
err := updateTime(domain)
if err != nil {
errors.LogError(context.Background(), err)
}
errors.LogWarning(context.Background(), "Initial time offset for vmess:", timeOffset.Load())
// only one sync should be enough, so disable periodic update for now
//go updateTimeMonitor(context.TODO(), domain)
}
func updateTimeMonitor(ctx context.Context, domain string) { func updateTimeMonitor(ctx context.Context, domain string) {
for { for {
@ -76,6 +63,24 @@ func updateTime(domain string) error {
} }
func Now() time.Time { func Now() time.Time {
initOnce.Do(func() {
timeOffset.Store(new(time.Duration))
go func() {
domain := platform.NewEnvFlag("xray.vmess.time.domain").GetValue(func() string { return "https://apple.com" })
if domain == "" {
errors.LogError(context.Background(), "vmess time domain is empty, skip time sync")
return
}
err := updateTime(domain)
if err != nil {
errors.LogError(context.Background(), err)
}
errors.LogWarning(context.Background(), "Initial time offset for vmess:", timeOffset.Load())
// only one sync should be enough, so disable periodic update for now
//go updateTimeMonitor(context.TODO(), domain)
}()
runtime.Gosched()
})
offset := timeOffset.Load() offset := timeOffset.Load()
return time.Now().Add(*offset) return time.Now().Add(*offset)
} }