-
Notifications
You must be signed in to change notification settings - Fork 6
/
wireguard.go
125 lines (114 loc) · 2.68 KB
/
wireguard.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
package main
import (
"net"
"time"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
const (
defaultPersistentKeepaliveInterval = 25 * time.Second
defaultWireguardDeviceName = "wg0"
)
func newPeerConfig(publicKey string, presharedKey string, endpoint string, allowedIPs []string) (*wgtypes.PeerConfig, error) {
key, err := wgtypes.ParseKey(publicKey)
if err != nil {
return nil, err
}
t := defaultPersistentKeepaliveInterval
peer := &wgtypes.PeerConfig{PublicKey: key, PersistentKeepaliveInterval: &t}
if presharedKey != "" {
key, err := wgtypes.ParseKey(presharedKey)
if err != nil {
return nil, err
}
peer.PresharedKey = &key
}
if endpoint != "" {
addr, err := net.ResolveUDPAddr("udp4", endpoint)
if err != nil {
return nil, err
}
peer.Endpoint = addr
}
for _, ai := range allowedIPs {
_, network, err := net.ParseCIDR(ai)
if err != nil {
return nil, err
}
peer.AllowedIPs = append(peer.AllowedIPs, *network)
}
return peer, nil
}
func setPeers(deviceName string, peers []wgtypes.PeerConfig) error {
wg, err := wgctrl.New()
if err != nil {
return err
}
defer func() {
if err := wg.Close(); err != nil {
logger.Errorf(
"Failed to close wireguard client: %v", err)
}
}()
if deviceName == "" {
deviceName = defaultWireguardDeviceName
}
device, err := wg.Device(deviceName)
if err != nil {
return err
}
for _, ep := range device.Peers {
found := false
for i, np := range peers {
peers[i].ReplaceAllowedIPs = true
if ep.PublicKey.String() == np.PublicKey.String() {
found = true
break
}
}
if !found {
peers = append(peers, wgtypes.PeerConfig{PublicKey: ep.PublicKey, Remove: true})
}
}
return wg.ConfigureDevice(deviceName, wgtypes.Config{Peers: peers})
}
func setPrivateKey(deviceName string, privKey string) error {
wg, err := wgctrl.New()
if err != nil {
return err
}
defer func() {
if err := wg.Close(); err != nil {
logger.Errorf(
"Failed to close wireguard client: %v", err)
}
}()
if deviceName == "" {
deviceName = defaultWireguardDeviceName
}
key, err := wgtypes.ParseKey(privKey)
if err != nil {
return err
}
return wg.ConfigureDevice(deviceName, wgtypes.Config{PrivateKey: &key})
}
func getKeys(deviceName string) (string, string, error) {
wg, err := wgctrl.New()
if err != nil {
return "", "", err
}
defer func() {
if err := wg.Close(); err != nil {
logger.Errorf(
"Failed to close wireguard client: %v", err)
}
}()
if deviceName == "" {
deviceName = defaultWireguardDeviceName
}
dev, err := wg.Device(deviceName)
if err != nil {
return "", "", err
}
return dev.PublicKey.String(), dev.PrivateKey.String(), nil
}