-
Notifications
You must be signed in to change notification settings - Fork 11
/
ntp.go
124 lines (115 loc) · 2.89 KB
/
ntp.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package path
import (
"fmt"
"sort"
"time"
"github.com/KusakabeSi/EtherGuard-VPN/mtypes"
orderedmap "github.com/KusakabeSi/EtherGuard-VPN/orderdmap"
"github.com/beevik/ntp"
)
var forever = time.Hour * 99999
func (g *IG) InitNTP() {
if g.ntp_info.UseNTP {
if len(g.ntp_info.Servers) == 0 {
g.ntp_info.UseNTP = false
return
}
g.ntp_servers = *orderedmap.New()
for _, url := range g.ntp_info.Servers {
g.ntp_servers.Set(url, ntp.Response{
RTT: forever,
})
}
g.SyncTimeMultiple(-1)
go g.RoutineSyncTime()
} else {
if g.loglevel.LogNTP {
fmt.Println("NTP: NTP sync disabled")
}
}
}
func (g *IG) RoutineSyncTime() {
if !g.ntp_info.UseNTP {
return
}
for {
g.SyncTimeMultiple(g.ntp_info.MaxServerUse)
time.Sleep(mtypes.S2TD(g.ntp_info.SyncTimeInterval))
}
}
type ByDuration []time.Duration
func (a ByDuration) Len() int { return len(a) }
func (a ByDuration) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDuration) Less(i, j int) bool { return a[i] < a[j] }
func (g *IG) SyncTimeMultiple(count int) {
var url2sync []string
if count < 0 {
count = len(g.ntp_servers.Keys())
}
if count > len(g.ntp_servers.Keys()) {
count = len(g.ntp_servers.Keys())
}
for index, url := range g.ntp_servers.Keys() {
if index < count {
url2sync = append(url2sync, url)
} else {
break
}
}
for _, url := range url2sync {
g.ntp_wg.Add(1)
go g.SyncTime(url, mtypes.S2TD(g.ntp_info.NTPTimeout))
}
g.ntp_wg.Wait()
g.ntp_servers.Sort(func(a *orderedmap.Pair, b *orderedmap.Pair) bool {
return a.Value().(ntp.Response).RTT < b.Value().(ntp.Response).RTT
})
results := make([]time.Duration, count)
for _, url := range g.ntp_servers.Keys() {
val, has := g.ntp_servers.Get(url)
if !has {
continue
}
result := val.(ntp.Response)
if result.RTT < forever {
results = append(results, result.ClockOffset)
}
}
if g.loglevel.LogNTP {
fmt.Println("NTP: All done")
}
sort.Sort(ByDuration(results))
if len(results) > 3 {
results = results[1 : len(results)-1]
}
var totaltime time.Duration
for _, result := range results {
totaltime += result
}
avgtime := totaltime / time.Duration(len(results))
if g.loglevel.LogNTP {
fmt.Println("NTP: Arvage offset: " + avgtime.String())
}
g.ntp_offset = avgtime
}
func (g *IG) SyncTime(url string, timeout time.Duration) {
if g.loglevel.LogNTP {
fmt.Println("NTP: Starting syncing with NTP server :" + url)
}
options := ntp.QueryOptions{Timeout: timeout}
response, err := ntp.QueryWithOptions(url, options)
if err == nil {
if g.loglevel.LogNTP {
fmt.Println("NTP: NTP server :" + url + "\tResult:" + response.ClockOffset.String() + " RTT:" + response.RTT.String())
}
g.ntp_servers.Set(url, *response)
} else {
if g.loglevel.LogNTP {
fmt.Println("NTP: NTP server :" + url + "\tFailed :" + err.Error())
}
g.ntp_servers.Set(url, ntp.Response{
RTT: forever,
})
}
g.ntp_wg.Done()
}