浏览代码

Add possibility to define your own ntp servers

tags/v1.0.3^2^2
9seconds 6 年前
父节点
当前提交
8e29ff78ba
共有 3 个文件被更改,包括 17 次插入8 次删除
  1. 8
    0
      config/config.go
  2. 6
    0
      main.go
  3. 3
    8
      ntp/ntp.go

+ 8
- 0
config/config.go 查看文件

68
 
68
 
69
 	OptionTypeMultiplexPerConnection
69
 	OptionTypeMultiplexPerConnection
70
 
70
 
71
+	OptionTypeNTPServers
72
+
71
 	OptionTypeSecret
73
 	OptionTypeSecret
72
 	OptionTypeAdtag
74
 	OptionTypeAdtag
73
 )
75
 )
96
 	Verbose    bool       `json:"verbose"`
98
 	Verbose    bool       `json:"verbose"`
97
 	SecretMode SecretMode `json:"secret_mode"`
99
 	SecretMode SecretMode `json:"secret_mode"`
98
 	PreferIP   PreferIP   `json:"prefer_ip"`
100
 	PreferIP   PreferIP   `json:"prefer_ip"`
101
+	NTPServers []string   `json:"ntp_servers"`
99
 
102
 
100
 	Secret []byte `json:"secret"`
103
 	Secret []byte `json:"secret"`
101
 	AdTag  []byte `json:"adtag"`
104
 	AdTag  []byte `json:"adtag"`
165
 			C.AntiReplayMaxSize = int(opt.Value.(units.Base2Bytes))
168
 			C.AntiReplayMaxSize = int(opt.Value.(units.Base2Bytes))
166
 		case OptionTypeMultiplexPerConnection:
169
 		case OptionTypeMultiplexPerConnection:
167
 			C.MultiplexPerConnection = int(opt.Value.(uint))
170
 			C.MultiplexPerConnection = int(opt.Value.(uint))
171
+		case OptionTypeNTPServers:
172
+			C.NTPServers = opt.Value.([]string)
173
+			if len(C.NTPServers) == 0 {
174
+				return errors.New("ntp server list is empty")
175
+			}
168
 		case OptionTypeSecret:
176
 		case OptionTypeSecret:
169
 			C.Secret = opt.Value.([]byte)
177
 			C.Secret = opt.Value.([]byte)
170
 		case OptionTypeAdtag:
178
 		case OptionTypeAdtag:

+ 6
- 0
main.go 查看文件

115
 		Envar("MTG_MULTIPLEX_PERCONNECTION").
115
 		Envar("MTG_MULTIPLEX_PERCONNECTION").
116
 		Default("50").
116
 		Default("50").
117
 		Uint()
117
 		Uint()
118
+	runNTPServers = runCommand.Flag("ntp-server",
119
+		"A list of NTP servers to use.").
120
+		Envar("MTG_NTP_SERVERS").
121
+		Default("0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org").
122
+		Strings()
118
 	runSecret = runCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes()
123
 	runSecret = runCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes()
119
 	runAdtag  = runCommand.Arg("adtag", "ADTag of the proxy.").HexBytes()
124
 	runAdtag  = runCommand.Arg("adtag", "ADTag of the proxy.").HexBytes()
120
 )
125
 )
149
 			config.Opt{Option: config.OptionTypeCloakPort, Value: *runTLSCloakPort},
154
 			config.Opt{Option: config.OptionTypeCloakPort, Value: *runTLSCloakPort},
150
 			config.Opt{Option: config.OptionTypeAntiReplayMaxSize, Value: *runAntiReplayMaxSize},
155
 			config.Opt{Option: config.OptionTypeAntiReplayMaxSize, Value: *runAntiReplayMaxSize},
151
 			config.Opt{Option: config.OptionTypeMultiplexPerConnection, Value: *runMultiplexPerConnection},
156
 			config.Opt{Option: config.OptionTypeMultiplexPerConnection, Value: *runMultiplexPerConnection},
157
+			config.Opt{Option: config.OptionTypeNTPServers, Value: *runNTPServers},
152
 			config.Opt{Option: config.OptionTypeSecret, Value: *runSecret},
158
 			config.Opt{Option: config.OptionTypeSecret, Value: *runSecret},
153
 			config.Opt{Option: config.OptionTypeAdtag, Value: *runAdtag},
159
 			config.Opt{Option: config.OptionTypeAdtag, Value: *runAdtag},
154
 		)
160
 		)

+ 3
- 8
ntp/ntp.go 查看文件

7
 
7
 
8
 	"github.com/beevik/ntp"
8
 	"github.com/beevik/ntp"
9
 	"go.uber.org/zap"
9
 	"go.uber.org/zap"
10
+
11
+	"github.com/9seconds/mtg/config"
10
 )
12
 )
11
 
13
 
12
 const autoUpdatePeriod = time.Minute
14
 const autoUpdatePeriod = time.Minute
13
 
15
 
14
-var ntpEndpoints = [...]string{
15
-	"0.pool.ntp.org",
16
-	"1.pool.ntp.org",
17
-	"2.pool.ntp.org",
18
-	"3.pool.ntp.org",
19
-}
20
-
21
 // Fetch fetches the data on time drift.
16
 // Fetch fetches the data on time drift.
22
 func Fetch() (time.Duration, error) {
17
 func Fetch() (time.Duration, error) {
23
-	url := ntpEndpoints[rand.Intn(len(ntpEndpoints))]
18
+	url := config.C.NTPServers[rand.Intn(len(config.C.NTPServers))]
24
 
19
 
25
 	resp, err := ntp.Query(url)
20
 	resp, err := ntp.Query(url)
26
 	if err != nil {
21
 	if err != nil {

正在加载...
取消
保存