-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
389 lines (313 loc) · 9.86 KB
/
main.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Command wgctrl is a testing utility for interacting with WireGuard via package
// wgctrl.
package main
import (
"bytes"
"context"
"encoding/binary"
"errors"
"flag"
"fmt"
"hash/crc32"
"net"
"net/netip"
"os"
"os/signal"
"sync"
"syscall"
"time"
"uw/ulog"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
var emptyPublicKey = make([]byte, wgtypes.KeyLen)
type Device struct {
InterfaceName string // 接口名称
ListenAddr string // 本地监听地址
UpstreamAddr string // 上游服务器地址
IgnorePublicKeys []string // 忽略变更的公钥
IgnorePublicKeysMap map[string]bool // 忽略变更的公钥
Interval time.Duration // 发起查询间隔
MaxHandshakeTime time.Duration // 最大请求握手时间
Timeout time.Duration // 请求超时时间
Client *wgctrl.Client // wgctrl 客户端
ListenConn *net.UDPConn // 监听连接
IntervalTicker *time.Ticker // 查询定时器
Context context.Context // context
}
func main() {
c, e := wgctrl.New()
if e != nil {
ulog.Fatal("failed to open client: %s", e)
}
defer c.Close()
d := &Device{Client: c}
flag.StringVar(&d.InterfaceName, "i", "wg0", "wireguard `interface` name")
flag.StringVar(&d.ListenAddr, "l", ":51220", "wgsd `listen` address (empty for no listen)")
flag.StringVar(&d.UpstreamAddr, "u", "", "`upstream` server address (empty for no upstream)")
flag.DurationVar(&d.Interval, "d", 60*time.Second, "query `interval`")
flag.DurationVar(&d.MaxHandshakeTime, "t", 5*time.Minute, "max `handshake` time")
flag.DurationVar(&d.Timeout, "o", 5*time.Second, "request `timeout`")
flag.Usage = func() {
ulog.Info("Usage: %s [options] [ignore public keys]", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
d.IgnorePublicKeys = flag.Args()
d.IgnorePublicKeysMap = make(map[string]bool, len(d.IgnorePublicKeys))
for i := 0; i < len(d.IgnorePublicKeys); i++ {
d.IgnorePublicKeysMap[d.IgnorePublicKeys[i]] = true
}
ulog.Debug("interface name: %s, listen address: %s, upstream address: %s, "+
"query interval: %s, max handshake time: %s, ignore public keys: %v",
d.InterfaceName, d.ListenAddr, d.UpstreamAddr, d.Interval,
d.MaxHandshakeTime, d.IgnorePublicKeys)
device, e := c.Device(d.InterfaceName)
if e != nil {
ulog.Fatal("failed to get device %q: %s", d.InterfaceName, e)
}
ulog.Info("local public key: %s, listen port: %d", device.PublicKey, device.ListenPort)
wg := &sync.WaitGroup{}
if d.ListenAddr != "" {
wg.Add(1)
go func(device *Device) {
defer wg.Done()
if e := device.listenService(); e != nil {
ulog.Fatal("listen service: %s", e)
}
}(d)
}
if d.UpstreamAddr != "" {
wg.Add(1)
go func(device *Device) {
defer wg.Done()
if e := device.queryService(); e != nil {
ulog.Fatal("query service: %s", e)
}
}(d)
}
wg.Add(1)
go func(d *Device) {
defer wg.Done()
var cancel func()
d.Context, cancel = context.WithCancel(context.Background())
defer cancel()
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
for {
switch <-ch {
case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
ulog.Info("signal: exit")
if d.ListenConn != nil {
if e := d.ListenConn.Close(); e != nil {
ulog.Error("failed to close udp: %s", e)
}
}
if d.IntervalTicker != nil {
d.IntervalTicker.Stop()
}
cancel()
return
}
}
}(d)
ulog.Info("full started")
wg.Wait()
}
const basePacketSize = 4 + wgtypes.KeyLen
type packet struct {
_ uint32 // CRC 校验位
PublicKey wgtypes.Key // 公钥
Addr []byte // 地址
}
func (m *packet) String() string {
ap := &netip.AddrPort{}
if len(m.Addr) > 1 {
if e := ap.UnmarshalBinary(m.Addr); e != nil {
return fmt.Sprintf("public key: %s, addr unmarshal error: %s", m.PublicKey.String(), e)
}
}
return fmt.Sprintf("public key: %s, addr: %s", m.PublicKey.String(), ap.String())
}
func (m *packet) MarshalBinary() []byte {
b := make([]byte, basePacketSize)
copy(b[4:36], m.PublicKey[:])
b = append(b, m.Addr...)
binary.LittleEndian.PutUint32(b[:4], crc32.ChecksumIEEE(b[4:]))
return b
}
func (m *packet) UnmarshalBinary(data []byte) error {
if len(data) < basePacketSize {
return fmt.Errorf("invalid message packet length: %d", len(data))
}
if binary.LittleEndian.Uint32(data[:4]) != crc32.ChecksumIEEE(data[4:]) {
return errors.New("invalid message packet checksum")
}
copy(m.PublicKey[:], data[4:36])
m.Addr = data[36:]
return nil
}
func (d *Device) listenService() (e error) {
laddr, e := net.ResolveUDPAddr("udp", d.ListenAddr)
if e != nil {
ulog.Fatal("failed to resolve udp address %q: %s", d.ListenAddr, e)
}
ulog.Info("resolved udp address: %s", laddr)
d.ListenConn, e = net.ListenUDP("udp", laddr)
if e != nil {
return fmt.Errorf("failed to listen udp: %w", e)
}
defer d.ListenConn.Close()
laddrString := d.ListenConn.LocalAddr()
ulog.Info("listen udp: %s", laddrString)
for {
m, buf := &packet{}, make([]byte, 4096)
n, raddr, e := d.ListenConn.ReadFromUDP(buf)
if e != nil {
if errors.Is(e, net.ErrClosed) {
return nil
}
ulog.Warn("failed to read udp: %s", e)
continue
}
if e := m.UnmarshalBinary(buf[:n]); e != nil {
ulog.Warn("failed to unmarshal message packet: %s", e)
continue
}
ulog.Debug("packet(%s <<< %s): %s", laddrString,
raddr.String(), m.String())
if !bytes.Equal(m.PublicKey[:], emptyPublicKey) {
endpoint, e := d.queryEndpoint(m.PublicKey)
if e != nil {
ulog.Warn("failed to query endpoint: %s", e)
} else {
ulog.Info("query endpoint: %s", endpoint.String())
if m.Addr, e = endpoint.AddrPort().MarshalBinary(); e != nil {
ulog.Warn("failed to marshal binary: %s", e)
}
}
}
ulog.Debug("packet(%s >>> %s): %s", laddrString,
raddr.String(), m.String())
if _, e := d.ListenConn.WriteToUDP(m.MarshalBinary(), raddr); e != nil {
ulog.Warn("failed to write udp: %s", e)
}
}
}
func (d *Device) queryEndpoint(publicKey wgtypes.Key) (*net.UDPAddr, error) {
device, e := d.Client.Device(d.InterfaceName)
if e != nil {
return nil, fmt.Errorf("failed to get device %q: %w", d.InterfaceName, e)
}
for i := 0; i < len(device.Peers); i++ {
if bytes.Equal(device.Peers[i].PublicKey[:], publicKey[:]) {
return device.Peers[i].Endpoint, nil
}
}
return nil, fmt.Errorf("peer not found: %s", publicKey.String())
}
func (d *Device) queryService() error {
d.IntervalTicker = time.NewTicker(d.Interval)
defer d.IntervalTicker.Stop()
d.queryFunc()
for {
select {
case <-d.IntervalTicker.C:
d.queryFunc()
case <-d.Context.Done():
return nil
}
}
}
func (d *Device) queryFunc() {
device, e := d.Client.Device(d.InterfaceName)
if e != nil {
ulog.Error("failed to get device %q: %s", d.InterfaceName, e)
return
}
ulog.Debug("device: %+v", device)
conf := &wgtypes.Config{
PrivateKey: &device.PrivateKey,
ListenPort: &device.ListenPort,
FirewallMark: &device.FirewallMark,
ReplacePeers: true,
Peers: make([]wgtypes.PeerConfig, len(device.Peers)),
}
for i := 0; i < len(device.Peers); i++ {
conf.Peers[i] = wgtypes.PeerConfig{
PublicKey: device.Peers[i].PublicKey,
Remove: false,
UpdateOnly: false,
PresharedKey: &device.Peers[i].PresharedKey,
Endpoint: device.Peers[i].Endpoint,
PersistentKeepaliveInterval: &device.Peers[i].PersistentKeepaliveInterval,
ReplaceAllowedIPs: true,
AllowedIPs: device.Peers[i].AllowedIPs,
}
if time.Since(device.Peers[i].LastHandshakeTime) < d.MaxHandshakeTime ||
d.IgnorePublicKeysMap[device.Peers[i].PublicKey.String()] {
continue
}
addr, e := d.queryUpstream(device.Peers[i].PublicKey)
if e != nil {
ulog.Warn("failed to query upstream: %s", e)
continue
}
conf.Peers[i].Endpoint = addr
}
ulog.Debug("configuring: %+v", conf)
if e := d.Client.ConfigureDevice(device.Name, *conf); e != nil {
ulog.Warn("failed to configure device: %s", e)
}
}
func (d *Device) queryUpstream(publicKey wgtypes.Key) (*net.UDPAddr, error) {
if len(d.UpstreamAddr) < 1 {
return nil, errors.New("upstream address is empty")
}
raddr, e := net.ResolveUDPAddr("udp", d.UpstreamAddr)
if e != nil {
return nil, fmt.Errorf("failed to resolve udp address %q: %w", d.UpstreamAddr, e)
}
ulog.Info("resolved udp address: %s", raddr)
conn, e := net.DialUDP("udp", nil, raddr)
if e != nil {
return nil, fmt.Errorf("failed to dial udp: %w", e)
}
defer conn.Close()
laddrString := conn.LocalAddr()
m := &packet{PublicKey: publicKey}
ulog.Debug("packet(%s >>> %s): %s", laddrString,
raddr.String(), m.String())
if e := conn.SetWriteDeadline(time.Now().Add(d.Timeout)); e != nil {
return nil, fmt.Errorf("failed to set write deadline: %w", e)
}
if _, e := conn.Write(m.MarshalBinary()); e != nil {
return nil, fmt.Errorf("failed to write udp: %w", e)
}
if e := conn.SetReadDeadline(time.Now().Add(d.Timeout)); e != nil {
return nil, fmt.Errorf("failed to set read deadline: %w", e)
}
buf := make([]byte, 4096)
n, e := conn.Read(buf)
if e != nil {
return nil, fmt.Errorf("failed to read udp: %w", e)
}
if e := m.UnmarshalBinary(buf[:n]); e != nil {
return nil, fmt.Errorf("failed to unmarshal message packet: %w", e)
}
ulog.Debug("packet(%s <<< %s): %s", laddrString,
raddr.String(), m.String())
if len(m.Addr) < 2 {
return nil, errors.New("address is empty")
}
ap := &netip.AddrPort{}
if e := ap.UnmarshalBinary(m.Addr); e != nil {
return nil, fmt.Errorf("failed to unmarshal binary: %w", e)
}
ulog.Debug("address: %s", ap.String())
addr, e := net.ResolveUDPAddr("udp", ap.String())
if e != nil {
return nil, fmt.Errorf("failed to resolve udp address %q: %w", ap.String(), e)
}
return addr, nil
}