Просмотр исходного кода

Add possibility to define your own ntp servers

tags/v1.0.3^2^2
9seconds 6 лет назад
Родитель
Сommit
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,6 +68,8 @@ const (
68 68
 
69 69
 	OptionTypeMultiplexPerConnection
70 70
 
71
+	OptionTypeNTPServers
72
+
71 73
 	OptionTypeSecret
72 74
 	OptionTypeAdtag
73 75
 )
@@ -96,6 +98,7 @@ type Config struct {
96 98
 	Verbose    bool       `json:"verbose"`
97 99
 	SecretMode SecretMode `json:"secret_mode"`
98 100
 	PreferIP   PreferIP   `json:"prefer_ip"`
101
+	NTPServers []string   `json:"ntp_servers"`
99 102
 
100 103
 	Secret []byte `json:"secret"`
101 104
 	AdTag  []byte `json:"adtag"`
@@ -165,6 +168,11 @@ func Init(options ...Opt) error { // nolint: gocyclo, funlen
165 168
 			C.AntiReplayMaxSize = int(opt.Value.(units.Base2Bytes))
166 169
 		case OptionTypeMultiplexPerConnection:
167 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 176
 		case OptionTypeSecret:
169 177
 			C.Secret = opt.Value.([]byte)
170 178
 		case OptionTypeAdtag:

+ 6
- 0
main.go Просмотреть файл

@@ -115,6 +115,11 @@ var (
115 115
 		Envar("MTG_MULTIPLEX_PERCONNECTION").
116 116
 		Default("50").
117 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 123
 	runSecret = runCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes()
119 124
 	runAdtag  = runCommand.Arg("adtag", "ADTag of the proxy.").HexBytes()
120 125
 )
@@ -149,6 +154,7 @@ func main() {
149 154
 			config.Opt{Option: config.OptionTypeCloakPort, Value: *runTLSCloakPort},
150 155
 			config.Opt{Option: config.OptionTypeAntiReplayMaxSize, Value: *runAntiReplayMaxSize},
151 156
 			config.Opt{Option: config.OptionTypeMultiplexPerConnection, Value: *runMultiplexPerConnection},
157
+			config.Opt{Option: config.OptionTypeNTPServers, Value: *runNTPServers},
152 158
 			config.Opt{Option: config.OptionTypeSecret, Value: *runSecret},
153 159
 			config.Opt{Option: config.OptionTypeAdtag, Value: *runAdtag},
154 160
 		)

+ 3
- 8
ntp/ntp.go Просмотреть файл

@@ -7,20 +7,15 @@ import (
7 7
 
8 8
 	"github.com/beevik/ntp"
9 9
 	"go.uber.org/zap"
10
+
11
+	"github.com/9seconds/mtg/config"
10 12
 )
11 13
 
12 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 16
 // Fetch fetches the data on time drift.
22 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 20
 	resp, err := ntp.Query(url)
26 21
 	if err != nil {

Загрузка…
Отмена
Сохранить