chore: ntp service support dialer-proxy

This commit is contained in:
gVisor bot 2023-09-25 09:11:20 +08:00
parent a92874f409
commit 07b0e869b4
3 changed files with 24 additions and 12 deletions

View file

@ -93,10 +93,11 @@ type Controller struct {
// NTP config // NTP config
type NTP struct { type NTP struct {
Enable bool `yaml:"enable"` Enable bool `yaml:"enable"`
WriteToSystem bool `yaml:"write-to-system"`
Server string `yaml:"server"` Server string `yaml:"server"`
Port int `yaml:"port"` Port int `yaml:"port"`
Interval int `yaml:"interval"` Interval int `yaml:"interval"`
DialerProxy string `yaml:"dialer-proxy"`
WriteToSystem bool `yaml:"write-to-system"`
} }
// DNS config // DNS config
@ -183,10 +184,11 @@ type Config struct {
type RawNTP struct { type RawNTP struct {
Enable bool `yaml:"enable"` Enable bool `yaml:"enable"`
WriteToSystem bool `yaml:"write-to-system"`
Server string `yaml:"server"` Server string `yaml:"server"`
ServerPort int `yaml:"server-port"` ServerPort int `yaml:"server-port"`
Interval int `yaml:"interval"` Interval int `yaml:"interval"`
DialerProxy string `yaml:"dialer-proxy"`
WriteToSystem bool `yaml:"write-to-system"`
} }
type RawDNS struct { type RawDNS struct {
@ -1200,6 +1202,7 @@ func paresNTP(rawCfg *RawConfig) *NTP {
Server: cfg.Server, Server: cfg.Server,
Port: cfg.ServerPort, Port: cfg.ServerPort,
Interval: cfg.Interval, Interval: cfg.Interval,
DialerProxy: cfg.DialerProxy,
WriteToSystem: cfg.WriteToSystem, WriteToSystem: cfg.WriteToSystem,
} }
return ntpCfg return ntpCfg

View file

@ -190,8 +190,12 @@ func updateExperimental(c *config.Config) {
func updateNTP(c *config.NTP) { func updateNTP(c *config.NTP) {
if c.Enable { if c.Enable {
ntp.ReCreateNTPService(net.JoinHostPort(c.Server, strconv.Itoa(c.Port)), ntp.ReCreateNTPService(
time.Duration(c.Interval), c.WriteToSystem) net.JoinHostPort(c.Server, strconv.Itoa(c.Port)),
time.Duration(c.Interval),
c.DialerProxy,
c.WriteToSystem,
)
} }
} }

View file

@ -2,12 +2,15 @@ package ntp
import ( import (
"context" "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" "sync"
"time" "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 var offset time.Duration
@ -15,6 +18,7 @@ var service *Service
type Service struct { type Service struct {
server M.Socksaddr server M.Socksaddr
dialer proxydialer.SingDialer
ticker *time.Ticker ticker *time.Ticker
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
@ -23,16 +27,17 @@ type Service struct {
running bool 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 { if service != nil {
service.Stop() service.Stop()
} }
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
service = &Service{ service = &Service{
server: M.ParseSocksaddr(server),
dialer: proxydialer.NewByNameSingDialer(dialerProxy, dialer.NewDialer()),
ticker: time.NewTicker(interval * time.Minute),
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
server: M.ParseSocksaddr(server),
ticker: time.NewTicker(interval * time.Minute),
syncSystemTime: syncSystemTime, syncSystemTime: syncSystemTime,
} }
service.Start() service.Start()
@ -70,7 +75,7 @@ func (srv *Service) update() {
var response *ntp.Response var response *ntp.Response
var err error var err error
for i := 0; i < 3; i++ { 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 err != nil {
if i == 2 { if i == 2 {
log.Errorln("Initialize NTP time failed: %s", err) log.Errorln("Initialize NTP time failed: %s", err)