/
client.go
61 lines (54 loc) · 1.49 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ntp
import (
"errors"
"math"
"time"
ntpServer "github.com/beevik/ntp"
"github.com/cyrildever/go-utls/common/logger"
)
const (
MIN_LIMIT float64 = 100. // in milliseconds
)
var (
defaultHost string
limit float64
timeServer func(string) (time.Time, error)
)
// Initialize defines the default NTP host and maximum time leap (in milliseconds) between the current machine and the NTP server time
// NB: Must be initialized before
func Initialize(host string, timeLeapMillis float64) error {
if host == "" {
return errors.New("invalid empty NTP host")
}
defaultHost = host
if timeLeapMillis < MIN_LIMIT {
limit = MIN_LIMIT
} else {
limit = timeLeapMillis
}
return nil
}
// Time ...
func Time(host string) (time.Time, error) {
if timeServer == nil {
if host == "" {
host = defaultHost
}
if host == "" || limit == 0 {
return time.Now(), errors.New("NTP wasn't initialized")
}
local := time.Now()
if server, err := ntpServer.Time(host); err == nil &&
math.Abs(float64(local.UnixNano()/1e6)-float64(server.UnixNano()/1e6)) > limit {
log := logger.Init("ntp", "Time")
log.Crit("Using remote server because time leap is too high", "local", local.UnixNano(), "server", server.UnixNano(), "diff", math.Abs(float64(local.UnixNano()/1e6)-float64(server.UnixNano()/1e6)), "limit", limit)
timeServer = ntpServer.Time
} else {
timeServer = localTime
}
}
return timeServer(host)
}
func localTime(noHost string) (time.Time, error) {
return time.Now(), nil
}