-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntpman.go
216 lines (173 loc) · 4.2 KB
/
ntpman.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"bufio"
"fmt"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"gopkg.in/alecthomas/kingpin.v2"
"net"
"os"
"strconv"
"strings"
"time"
)
const Version = "0.0.3"
const JAN_1970 = 2208988800
var (
configfile = kingpin.Arg("configfile", "config file path").Required().String()
port = kingpin.Flag("port", "source port").Short('p').String()
)
type Ntpman struct {
ConfAddr string
UDPAddr *net.UDPAddr
ServerAddr *net.UDPAddr
Domains []string
SendTime time.Time
RecvTime time.Time
}
func Run() int {
addrList, err := LoadConf(*configfile)
if err != nil {
fmt.Println(err)
return 1
}
laddrStr := ":"
if 0 < len(*port) {
laddrStr = laddrStr + *port
}
laddr, err := net.ResolveUDPAddr("udp", laddrStr)
if err != nil {
fmt.Println(err)
return 1
}
conn, err := net.ListenUDP("udp", laddr)
if err != nil {
fmt.Println(err)
return 1
}
defer conn.Close()
app := tview.NewApplication()
header := tview.NewTable()
table := tview.NewTable()
grid := tview.NewGrid().
SetRows(1, 0).
AddItem(header, 0, 0, 1, 3, 0, 0, false).
AddItem(table, 1, 0, 1, 3, 0, 0, true)
defer app.Stop()
header.SetCell(0, 0, tview.NewTableCell("ntpman"))
header.SetCell(0, 1, tview.NewTableCell(Version))
header.SetCell(0, 2, tview.NewTableCell(conn.LocalAddr().String()))
go func() {
for c, hdr := range []string{" ", "Conf", "Server", "Domain", "Ref", "RTT", "S", "V"} {
table.SetCell(0, c, tview.NewTableCell(hdr))
}
for {
for i, ntpman := range addrList {
table.SetCell(i+1, 0, tview.NewTableCell(">"))
table.SetCell(i+1, 1, tview.NewTableCell(ntpman.ConfAddr))
nh, err := SendQuery(conn, ntpman)
if err != nil {
table.SetCell(i+1, 2, tview.NewTableCell(err.Error()).SetTextColor(tcell.ColorRed))
} else {
table.SetCell(i+1, 2, tview.NewTableCell(ntpman.ServerAddr.String()))
if len(ntpman.Domains) > 0 {
table.SetCell(i+1, 3, tview.NewTableCell(ntpman.Domains[0]))
}
table.SetCell(i+1, 4, tview.NewTableCell(nh.RefidStr()))
table.SetCell(i+1, 5, tview.NewTableCell(
ntpman.RecvTime.
Sub(ntpman.SendTime).
Truncate(time.Microsecond*10).
String()))
table.SetCell(i+1, 6, tview.NewTableCell(strconv.Itoa(int(nh.Stratum))))
table.SetCell(i+1, 7, tview.NewTableCell(strconv.Itoa(int(nh.Version))))
}
app.Draw()
time.Sleep(1 * time.Second)
table.SetCell(i+1, 0, tview.NewTableCell(" "))
}
}
}()
if err = app.SetRoot(grid, true).Run(); err != nil {
fmt.Println(err)
return 1
}
return 0
}
func SendQuery(conn *net.UDPConn, ntpman *Ntpman) (*NtpHeader, error) {
now := time.Now()
sec := now.Unix()
nsec := now.UnixNano() - (sec * 1000000000)
fraction := (float64(nsec) / 1000000000) * 4294967296
ntpman.SendTime = now
xmt := uint64(
(uint64(sec+JAN_1970) << 32) | uint64(fraction))
ntp := NtpHeader{
Leap: NtpLeapUnknown,
Version: 4,
Mode: NtpModeClient,
Stratum: 0,
Poll: 0,
Precision: 0,
Rootdelay: 0x00000000,
Rootdisp: 0x00000000,
Refid: 0,
Refts: 0,
Orgts: 0,
Rects: 0,
Xmtts: xmt,
}
msg, _ := ntp.Marshal()
err := conn.SetDeadline(now.Add(time.Second))
if err != nil {
return nil, err
}
n, err := conn.WriteToUDP(msg, ntpman.UDPAddr)
if err != nil {
return nil, err
}
buf := make([]byte, 9000)
n, uaddr, err := conn.ReadFromUDP(buf)
if err != nil {
return nil, err
}
ntpman.ServerAddr = uaddr
ntpman.RecvTime = time.Now()
var nh NtpHeader
err = nh.Unmarshal(buf[:n])
if err != nil {
return nil, err
}
ntpman.Domains, _ = net.LookupAddr(uaddr.IP.String())
return &nh, nil
}
func LoadConf(filename string) ([]*Ntpman, error) {
list := make([]*Ntpman, 0)
fp, err := os.Open(filename)
if err != nil {
return nil, err
}
defer fp.Close()
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
if len(line) < 1 {
continue
}
ntpman := &Ntpman{ConfAddr: line}
ntpman.UDPAddr, err = net.ResolveUDPAddr("udp", line)
if err != nil {
return nil, err
}
list = append(list, ntpman)
}
return list, nil
}
func main() {
kingpin.Version(Version)
kingpin.Parse()
os.Exit(Run())
}