Fix time service

This commit is contained in:
世界 2024-12-14 22:41:58 +08:00
parent a2b6c367ee
commit f08861185a
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
2 changed files with 34 additions and 4 deletions

16
box.go
View File

@ -14,6 +14,7 @@ import (
"github.com/sagernet/sing-box/adapter/outbound" "github.com/sagernet/sing-box/adapter/outbound"
"github.com/sagernet/sing-box/common/dialer" "github.com/sagernet/sing-box/common/dialer"
"github.com/sagernet/sing-box/common/taskmonitor" "github.com/sagernet/sing-box/common/taskmonitor"
"github.com/sagernet/sing-box/common/tls"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental" "github.com/sagernet/sing-box/experimental"
"github.com/sagernet/sing-box/experimental/cachefile" "github.com/sagernet/sing-box/experimental/cachefile"
@ -149,6 +150,14 @@ func New(options Options) (*Box, error) {
if err != nil { if err != nil {
return nil, E.Cause(err, "initialize router") return nil, E.Cause(err, "initialize router")
} }
ntpOptions := common.PtrValueOrDefault(options.NTP)
var timeService *tls.TimeServiceWrapper
if ntpOptions.Enabled {
timeService = new(tls.TimeServiceWrapper)
service.MustRegister[ntp.TimeService](ctx, timeService)
}
for i, endpointOptions := range options.Endpoints { for i, endpointOptions := range options.Endpoints {
var tag string var tag string
if endpointOptions.Tag != "" { if endpointOptions.Tag != "" {
@ -254,13 +263,12 @@ func New(options Options) (*Box, error) {
service.MustRegister[adapter.V2RayServer](ctx, v2rayServer) service.MustRegister[adapter.V2RayServer](ctx, v2rayServer)
} }
} }
ntpOptions := common.PtrValueOrDefault(options.NTP)
if ntpOptions.Enabled { if ntpOptions.Enabled {
ntpDialer, err := dialer.New(ctx, ntpOptions.DialerOptions) ntpDialer, err := dialer.New(ctx, ntpOptions.DialerOptions)
if err != nil { if err != nil {
return nil, E.Cause(err, "create NTP service") return nil, E.Cause(err, "create NTP service")
} }
timeService := ntp.NewService(ntp.Options{ ntpService := ntp.NewService(ntp.Options{
Context: ctx, Context: ctx,
Dialer: ntpDialer, Dialer: ntpDialer,
Logger: logFactory.NewLogger("ntp"), Logger: logFactory.NewLogger("ntp"),
@ -268,8 +276,8 @@ func New(options Options) (*Box, error) {
Interval: time.Duration(ntpOptions.Interval), Interval: time.Duration(ntpOptions.Interval),
WriteToSystem: ntpOptions.WriteToSystem, WriteToSystem: ntpOptions.WriteToSystem,
}) })
service.MustRegister[ntp.TimeService](ctx, timeService) timeService.TimeService = ntpService
services = append(services, adapter.NewLifecycleService(timeService, "ntp service")) services = append(services, adapter.NewLifecycleService(ntpService, "ntp service"))
} }
return &Box{ return &Box{
network: networkManager, network: networkManager,

View File

@ -0,0 +1,22 @@
package tls
import (
"time"
"github.com/sagernet/sing/common/ntp"
)
type TimeServiceWrapper struct {
ntp.TimeService
}
func (w *TimeServiceWrapper) TimeFunc() func() time.Time {
if w.TimeService == nil {
return nil
}
return w.TimeService.TimeFunc()
}
func (w *TimeServiceWrapper) Upstream() any {
return w.TimeService
}