chore: ntp service support dialer-proxy

This commit is contained in:
wwqgtxx 2023-09-25 09:11:20 +08:00
parent c0ba798708
commit 0dfe696300
3 changed files with 24 additions and 12 deletions

View file

@ -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

View file

@ -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,
)
}
}

View file

@ -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)