From 0dfe696300d70eef66dcebf718bb2571d0dc05bd Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Mon, 25 Sep 2023 09:11:20 +0800 Subject: [PATCH] chore: ntp service support `dialer-proxy` --- config/config.go | 7 +++++-- hub/executor/executor.go | 8 ++++++-- ntp/service.go | 21 +++++++++++++-------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index a1ddde81..e240f9b9 100644 --- a/config/config.go +++ b/config/config.go @@ -93,10 +93,11 @@ type Controller struct { // NTP config type NTP struct { Enable bool `yaml:"enable"` - WriteToSystem bool `yaml:"write-to-system"` Server string `yaml:"server"` Port int `yaml:"port"` Interval int `yaml:"interval"` + DialerProxy string `yaml:"dialer-proxy"` + WriteToSystem bool `yaml:"write-to-system"` } // DNS config @@ -183,10 +184,11 @@ type Config struct { type RawNTP struct { Enable bool `yaml:"enable"` - WriteToSystem bool `yaml:"write-to-system"` Server string `yaml:"server"` ServerPort int `yaml:"server-port"` Interval int `yaml:"interval"` + DialerProxy string `yaml:"dialer-proxy"` + WriteToSystem bool `yaml:"write-to-system"` } type RawDNS struct { @@ -1200,6 +1202,7 @@ func paresNTP(rawCfg *RawConfig) *NTP { Server: cfg.Server, Port: cfg.ServerPort, Interval: cfg.Interval, + DialerProxy: cfg.DialerProxy, WriteToSystem: cfg.WriteToSystem, } return ntpCfg diff --git a/hub/executor/executor.go b/hub/executor/executor.go index d1636754..1831584f 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -190,8 +190,12 @@ func updateExperimental(c *config.Config) { func updateNTP(c *config.NTP) { if c.Enable { - ntp.ReCreateNTPService(net.JoinHostPort(c.Server, strconv.Itoa(c.Port)), - time.Duration(c.Interval), c.WriteToSystem) + ntp.ReCreateNTPService( + net.JoinHostPort(c.Server, strconv.Itoa(c.Port)), + time.Duration(c.Interval), + c.DialerProxy, + c.WriteToSystem, + ) } } diff --git a/ntp/service.go b/ntp/service.go index 3234180d..c5506197 100644 --- a/ntp/service.go +++ b/ntp/service.go @@ -2,12 +2,15 @@ package ntp import ( "context" - "github.com/Dreamacro/clash/log" - M "github.com/sagernet/sing/common/metadata" - N "github.com/sagernet/sing/common/network" - "github.com/sagernet/sing/common/ntp" "sync" "time" + + "github.com/Dreamacro/clash/component/dialer" + "github.com/Dreamacro/clash/component/proxydialer" + "github.com/Dreamacro/clash/log" + + M "github.com/sagernet/sing/common/metadata" + "github.com/sagernet/sing/common/ntp" ) var offset time.Duration @@ -15,6 +18,7 @@ var service *Service type Service struct { server M.Socksaddr + dialer proxydialer.SingDialer ticker *time.Ticker ctx context.Context cancel context.CancelFunc @@ -23,16 +27,17 @@ type Service struct { running bool } -func ReCreateNTPService(server string, interval time.Duration, syncSystemTime bool) { +func ReCreateNTPService(server string, interval time.Duration, dialerProxy string, syncSystemTime bool) { if service != nil { service.Stop() } ctx, cancel := context.WithCancel(context.Background()) service = &Service{ + server: M.ParseSocksaddr(server), + dialer: proxydialer.NewByNameSingDialer(dialerProxy, dialer.NewDialer()), + ticker: time.NewTicker(interval * time.Minute), ctx: ctx, cancel: cancel, - server: M.ParseSocksaddr(server), - ticker: time.NewTicker(interval * time.Minute), syncSystemTime: syncSystemTime, } service.Start() @@ -70,7 +75,7 @@ func (srv *Service) update() { var response *ntp.Response var err error for i := 0; i < 3; i++ { - response, err = ntp.Exchange(context.Background(), N.SystemDialer, srv.server) + response, err = ntp.Exchange(context.Background(), srv.dialer, srv.server) if err != nil { if i == 2 { log.Errorln("Initialize NTP time failed: %s", err)