-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
495 lines (409 loc) · 9.18 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package dswg
import (
"fmt"
"net"
"time"
"errors"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
type Client struct {
db DB
wg *wgctrl.Client
ns *netlink.Handle
}
// Adds link to database.
// If link.Enable is set we try to activate the link
func (c *Client) AddLink(link Link) error {
if ln, _ := c.db.GetLink(link.Name); ln != nil {
return fmt.Errorf("Link name \"%v\" already exists in database", link.Name)
}
if ln, _ := c.ns.LinkByName(link.Name); ln != nil {
return fmt.Errorf(
"Link name already exists in the kernel, " +
"please delete it first using `ip link delete %v`", link.Name)
}
if err := validLink(link); err != nil {
return err
}
err := c.db.AddLink(link)
if err != nil {
return err
}
if link.Enable {
err := c.ActivateLink(link.Name)
if err != nil {
return err
}
}
return nil
}
// Removes the link from the kernel and the database with all its peers.
// The link must exist in the database.
func (c *Client) RemoveLink(name string) error {
link, err := c.db.GetLink(name)
if err != nil {
return err
}
if c.isLoaded(name) {
err = c.ns.LinkDel(*link)
if err != nil {
return err
}
}
// link cascades its deletion to its peers
err = c.db.RemoveLink(name)
if err != nil {
return err
}
return nil
}
// Activates link and applies wireguard configurations (including adding peers).
// If link is not loaded in the kernel, it gets loaded first.
// The link must exist in the database.
func (c *Client) ActivateLink(name string) error {
link, err := c.db.GetLink(name)
if err != nil {
return err
}
if !c.isLoaded(name) {
err := c.ns.LinkAdd(link)
if err != nil {
return err
}
}
link.Enable = true
err = c.setLinkSystemConfig(link.Name, *link)
if err != nil {
return err
}
// TODO: Execute PostUp commands here
err = c.db.UpdateLink(link.Name, *link)
if err != nil {
return err
}
peers, err := c.db.GetLinkPeers(link.Name)
if err != nil {
return err
}
for _, peer := range peers {
if peer.Enable {
err := c.ActivatePeer(link.Name, peer.Name)
if err != nil {
return err
}
}
}
return nil
}
// Deactivates link.
// Equivalent to `ip link set {name} down`.
// The link must exist in the database.
func (c *Client) DeactivateLink(name string) error {
link, err := c.db.GetLink(name)
if err != nil {
return err
}
if c.isLoaded(name) {
err = c.ns.LinkSetDown(*link)
if err != nil {
return err
}
}
link.Enable = false
err = c.db.UpdateLink(link.Name, *link)
if err != nil {
return err
}
return nil
}
// Updates link in database and updates link system
// configurations, if it is loaded in the kernel.
func (c *Client) UpdateLink(name string, link Link) error {
if err := validLink(link); err != nil {
return err
}
err := c.db.UpdateLink(name, link)
if err != nil {
return err
}
if c.isLoaded(name) {
err := c.setLinkSystemConfig(name, link)
if err != nil {
return err
}
}
if !c.isLoaded(name) && link.Enable {
err := c.ActivateLink(link.Name)
if err != nil {
return err
}
}
return nil
}
// Update link configuration in the kernel, including wireguard configuration.
// Link must exist in the kernel.
// NOTE: This function does not add peers or affect the database
func (c *Client) setLinkSystemConfig(name string, link Link) error {
if err := validLink(link); err != nil {
return err
}
netInterface, err := c.ns.LinkByName(name)
if err != nil {
return fmt.Errorf("Couldn't find link %v in the kernel", link.Name)
}
if netInterface.Type() != "wireguard" {
return errors.New("Link must be of type wireguard")
}
// Interface must be down when changes are applied
err = c.ns.LinkSetDown(netInterface)
if err != nil {
return err
}
// Override wireguard configuration
devConfig := wgtypes.Config{
PrivateKey: &link.PrivateKey.Key,
ListenPort: &link.ListenPort,
FirewallMark: &link.FirewallMark,
}
err = c.wg.ConfigureDevice(name, devConfig)
if err != nil {
return err
}
// TODO: Isolate in a separate function ex. updateLinkAddr(link)
// Delete older addreses associated with the link
addrList, err := c.ns.AddrList(netInterface, netlink.FAMILY_ALL)
if err != nil {
return err
}
for _, addr := range addrList {
err := c.ns.AddrDel(netInterface, &addr)
if err != nil {
return err
}
}
// Add link addresses
if link.AddressIPv4 != nil {
addr := &netlink.Addr{
IPNet: &link.AddressIPv4.IPNet,
}
err := c.ns.AddrAdd(netInterface, addr)
if err != nil {
return err
}
}
if link.AddressIPv6 != nil {
addr := &netlink.Addr{
IPNet: &link.AddressIPv6.IPNet,
}
err := c.ns.AddrAdd(netInterface, addr)
if err != nil {
return err
}
}
err = c.ns.LinkSetMTU(netInterface, link.MTU)
if err != nil {
return err
}
err = c.ns.LinkSetName(netInterface, link.Name)
if err != nil {
return err
}
if link.Enable {
err := c.ns.LinkSetUp(netInterface)
if err != nil {
return err
}
}
return nil
}
func (c *Client) AddPeer(linkName string, peer Peer) error {
if p, _ := c.db.GetPeer(linkName, peer.Name); p != nil {
return fmt.Errorf("Peer name \"%v\" already exists in database", peer.Name)
}
if err := validPeer(peer); err != nil {
return err
}
err := c.db.AddPeer(linkName, peer)
if err != nil {
return err
}
if c.isLoaded(linkName) && peer.Enable {
err = c.ActivatePeer(linkName, peer.Name)
if err != nil {
return err
}
}
return nil
}
func (c *Client) RemovePeer(linkName, peerName string) error {
if c.isLoaded(linkName) {
err := c.DeactivatePeer(linkName, peerName)
if err != nil {
return err
}
}
err := c.db.RemovePeer(linkName, peerName)
if err != nil {
return err
}
return nil
}
func (c *Client) ActivatePeer(linkName, peerName string) error {
if !c.isLoaded(linkName) {
return fmt.Errorf("Couldn't find wireguard link %v in the kernel", linkName)
}
peer, err := c.db.GetPeer(linkName, peerName)
if err != nil {
return err
}
var preshared *wgtypes.Key
if peer.PresharedKey != nil {
preshared = &peer.PresharedKey.Key
}
keepalive := time.Duration(peer.PersistentKeepalive)
allowedIPs := make([]net.IPNet, len(peer.AllowedIPs))
for i := range peer.AllowedIPs {
allowedIPs[i] = peer.AllowedIPs[i].IPNet
}
peerConfig := wgtypes.PeerConfig{
PublicKey: peer.PublicKey.Key,
PresharedKey: preshared,
Endpoint: &peer.Endpoint.UDPAddr,
PersistentKeepaliveInterval: &keepalive,
ReplaceAllowedIPs: true,
AllowedIPs: allowedIPs,
}
devConfig := wgtypes.Config{
Peers: []wgtypes.PeerConfig{peerConfig},
}
err = c.wg.ConfigureDevice(linkName, devConfig)
if err != nil {
return err
}
netInterface, err := c.ns.LinkByName(linkName)
if err != nil {
return err
}
for _, ip := range allowedIPs {
route := &netlink.Route{
LinkIndex: netInterface.Attrs().Index,
Scope: netlink.SCOPE_LINK,
Dst: &ip,
}
err := c.ns.RouteAdd(route)
if err != nil {
return err
}
}
peer.Enable = true
err = c.db.UpdatePeer(linkName, peerName, *peer)
if err != nil {
return err
}
return nil
}
func (c *Client) DeactivatePeer(linkName, peerName string) error {
if !c.isLoaded(linkName) {
return fmt.Errorf("Couldn't find wireguard link %v in the kernel", linkName)
}
peer, err := c.db.GetPeer(linkName, peerName)
if err != nil {
return err
}
peerConfig := wgtypes.PeerConfig{
PublicKey: peer.PublicKey.Key,
Remove: true,
}
devConfig := wgtypes.Config{
Peers: []wgtypes.PeerConfig{peerConfig},
}
err = c.wg.ConfigureDevice(linkName, devConfig)
if err != nil {
return err
}
peer.Enable = false
err = c.db.UpdatePeer(linkName, peerName, *peer)
if err != nil {
return err
}
return nil
}
func (c *Client) UpdatePeer(linkName, peerName string, peer Peer) error {
if err := validPeer(peer); err != nil {
return err
}
if c.isLoaded(linkName) {
err := c.DeactivatePeer(linkName, peerName)
if err != nil {
return err
}
}
err := c.db.UpdatePeer(linkName, peerName, peer)
if err != nil {
return err
}
if c.isLoaded(linkName) && peer.Enable {
err = c.ActivatePeer(linkName, peer.Name)
if err != nil {
return err
}
}
return nil
}
func validLink(link Link) error {
if len(link.Name) == 0 {
return errors.New("Link name cannot be empty")
}
if link.AddressIPv4 == nil && link.AddressIPv6 == nil {
return errors.New("Link must be assigned at least one address address")
}
return nil
}
func validPeer(peer Peer) error {
if len(peer.Name) == 0 {
return errors.New("Peer name cannot be empty")
}
return nil
}
// Indicates whether the link is added to the kernel or not.
func (c *Client) isLoaded(name string) bool {
netInterface, _ := c.ns.LinkByName(name)
return netInterface != nil && netInterface.Type() == "wireguard"
}
func (c *Client) Close() error {
if c.db != nil {
err := c.db.Close()
if err != nil {
return err
}
}
if c.wg != nil {
err := c.wg.Close()
if err != nil {
return err
}
}
if c.ns != nil {
c.ns.Delete()
}
return nil
}
func NewClient(db DB) (*Client, error) {
// Use current network namespace
handle, err := netlink.NewHandle()
if err != nil {
return nil, err
}
wg, err := wgctrl.New()
if err != nil {
return nil, err
}
client := &Client{
db: db,
wg: wg,
ns: handle,
}
return client, nil
}