@@ -10,6 +10,8 @@ import (
1010 "github.com/sagernet/sing/common/logger"
1111 M "github.com/sagernet/sing/common/metadata"
1212 N "github.com/sagernet/sing/common/network"
13+ "github.com/sagernet/sing/service"
14+ "github.com/sagernet/sing/service/pause"
1315)
1416
1517const TimeLayout = "2006-01-02 15:04:05 -0700"
@@ -19,23 +21,26 @@ type TimeService interface {
1921}
2022
2123type Options struct {
22- Context context.Context
23- Server M.Socksaddr
24- Interval time.Duration
25- Dialer N.Dialer
26- Logger logger.Logger
24+ Context context.Context
25+ Dialer N.Dialer
26+ Logger logger.Logger
27+ Server M.Socksaddr
28+ Interval time.Duration
29+ WriteToSystem bool
2730}
2831
2932var _ TimeService = (* Service )(nil )
3033
3134type Service struct {
32- ctx context.Context
33- cancel common.ContextCancelCauseFunc
34- server M.Socksaddr
35- dialer N.Dialer
36- logger logger.Logger
37- ticker * time.Ticker
38- clockOffset time.Duration
35+ ctx context.Context
36+ cancel common.ContextCancelCauseFunc
37+ dialer N.Dialer
38+ logger logger.Logger
39+ server M.Socksaddr
40+ writeToSystem bool
41+ ticker * time.Ticker
42+ clockOffset time.Duration
43+ pause pause.Manager
3944}
4045
4146func NewService (options Options ) * Service {
@@ -47,9 +52,12 @@ func NewService(options Options) *Service {
4752 destination := options .Server
4853 if ! destination .IsValid () {
4954 destination = M.Socksaddr {
50- Fqdn : "time.google .com" ,
55+ Fqdn : "time.apple .com" ,
5156 }
5257 }
58+ if options .Logger == nil {
59+ options .Logger = logger .NOP ()
60+ }
5361 if destination .Port == 0 {
5462 destination .Port = 123
5563 }
@@ -66,12 +74,14 @@ func NewService(options Options) *Service {
6674 dialer = N .SystemDialer
6775 }
6876 return & Service {
69- ctx : ctx ,
70- cancel : cancel ,
71- server : destination ,
72- dialer : dialer ,
73- logger : options .Logger ,
74- ticker : time .NewTicker (interval ),
77+ ctx : ctx ,
78+ cancel : cancel ,
79+ dialer : dialer ,
80+ logger : options .Logger ,
81+ writeToSystem : options .WriteToSystem ,
82+ server : destination ,
83+ ticker : time .NewTicker (interval ),
84+ pause : service.FromContext [pause.Manager ](ctx ),
7585 }
7686}
7787
@@ -104,6 +114,14 @@ func (s *Service) loopUpdate() {
104114 return
105115 case <- s .ticker .C :
106116 }
117+ if s .pause != nil {
118+ s .pause .WaitActive ()
119+ select {
120+ case <- s .ctx .Done ():
121+ return
122+ default :
123+ }
124+ }
107125 err := s .update ()
108126 if err == nil {
109127 s .logger .Debug ("updated time: " , s .TimeFunc ()().Local ().Format (TimeLayout ))
@@ -119,5 +137,11 @@ func (s *Service) update() error {
119137 return err
120138 }
121139 s .clockOffset = response .ClockOffset
140+ if s .writeToSystem {
141+ writeErr := SetSystemTime (s .TimeFunc ()())
142+ if writeErr != nil {
143+ s .logger .Warn ("write time to system: " , writeErr )
144+ }
145+ }
122146 return nil
123147}
0 commit comments