-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
189 lines (156 loc) · 4.54 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
package main
import (
"fmt"
"net"
"os"
"strconv"
"time"
"github.com/coreos/go-iptables/iptables"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
const (
ExitSetupSuccess = 0
ExitSetupFailed = 1
)
func main() {
interfaceName := "chip0"
serverPortString := os.Getenv("SERVER_PORT")
if serverPortString == "" {
fmt.Printf("SERVER_PORT is not set\n")
os.Exit(ExitSetupFailed)
}
serverPort, err := strconv.Atoi(serverPortString)
if err != nil {
fmt.Printf("SERVER_PORT is not an integer\n")
os.Exit(ExitSetupFailed)
}
hostPeerIp := os.Getenv("HOST_PEER_IP")
if hostPeerIp == "" {
fmt.Printf("HOST_PEER_IP is not set\n")
os.Exit(ExitSetupFailed)
}
vmPeerIp := os.Getenv("VM_PEER_IP")
if vmPeerIp == "" {
fmt.Printf("VM_PEER_IP is not set\n")
os.Exit(ExitSetupFailed)
}
hostPublicKeyString := os.Getenv("HOST_PUBLIC_KEY")
if hostPublicKeyString == "" {
fmt.Printf("HOST_PUBLIC_KEY is not set\n")
os.Exit(ExitSetupFailed)
}
vmPrivateKeyString := os.Getenv("VM_PRIVATE_KEY")
if vmPrivateKeyString == "" {
fmt.Printf("VM_PRIVATE_KEY is not set\n")
os.Exit(ExitSetupFailed)
}
links, err := netlink.LinkList()
if err != nil {
fmt.Printf("Could not list links: %v\n", err)
os.Exit(ExitSetupFailed)
}
for _, link := range links {
if link.Attrs().Name == interfaceName {
fmt.Printf("Interface %s already exists. Removing.\n", interfaceName)
err = netlink.LinkDel(link)
if err != nil {
fmt.Printf("Could not delete link %s: %v\n", interfaceName, err)
os.Exit(ExitSetupFailed)
}
}
}
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = interfaceName
fmt.Printf("Creating WireGuard interface %s\n", interfaceName)
wireguard := &netlink.Wireguard{LinkAttrs: linkAttrs}
err = netlink.LinkAdd(wireguard)
if err != nil {
fmt.Printf("Could not add link %s: %v\n", linkAttrs.Name, err)
}
vmIpNet, err := netlink.ParseIPNet(vmPeerIp + "/32")
if err != nil {
fmt.Printf("Could not parse VM peer IPNet: %v\n", err)
}
hostIpNet, err := netlink.ParseIPNet(hostPeerIp + "/32")
if err != nil {
fmt.Printf("Could not parse host peer IPNet: %v\n", err)
}
fmt.Println("Assigning IP to WireGuard interface")
addr := netlink.Addr{IPNet: vmIpNet, Peer: hostIpNet}
netlink.AddrAdd(wireguard, &addr)
c, err := wgctrl.New()
if err != nil {
fmt.Printf("Failed to create wgctrl client: %v\n", err)
os.Exit(ExitSetupFailed)
}
defer c.Close()
vmPrivateKey, err := wgtypes.ParseKey(vmPrivateKeyString)
if err != nil {
fmt.Printf("Failed to parse VM private key: %v\n", err)
os.Exit(ExitSetupFailed)
}
hostPublicKey, err := wgtypes.ParseKey(hostPublicKeyString)
if err != nil {
fmt.Printf("Failed to parse host public key: %v\n", err)
os.Exit(ExitSetupFailed)
}
wildcardIpNet, err := netlink.ParseIPNet("0.0.0.0/0")
if err != nil {
fmt.Printf("Failed to parse wildcard IPNet: %v\n", err)
os.Exit(ExitSetupFailed)
}
ips, err := net.LookupIP("host.docker.internal")
if err != nil || len(ips) == 0 {
fmt.Printf("Failed to lookup IP: %v\n", err)
os.Exit(ExitSetupFailed)
}
persistentKeepaliveInterval, err := time.ParseDuration("25s")
if err != nil {
fmt.Printf("Failed to parse duration: %v\n", err)
os.Exit(ExitSetupFailed)
}
peer := wgtypes.PeerConfig{
PublicKey: hostPublicKey,
Endpoint: &net.UDPAddr{IP: ips[0], Port: serverPort},
PersistentKeepaliveInterval: &persistentKeepaliveInterval,
AllowedIPs: []net.IPNet{
*wildcardIpNet,
*hostIpNet,
},
}
fmt.Println("Configuring WireGuard device")
err = c.ConfigureDevice(interfaceName, wgtypes.Config{
PrivateKey: &vmPrivateKey,
Peers: []wgtypes.PeerConfig{peer},
})
if err != nil {
fmt.Printf("Failed to configure wireguard device: %v\n", err)
os.Exit(ExitSetupFailed)
}
err = netlink.LinkSetUp(wireguard)
if err != nil {
fmt.Printf("Failed to set wireguard link to up: %v\n", err)
os.Exit(ExitSetupFailed)
}
ipt, err := iptables.New()
if err != nil {
fmt.Printf("Failed to create new iptables client: %v\n", err)
os.Exit(ExitSetupFailed)
}
fmt.Println("Adding iptables NAT rule for host WireGuard IP")
// Add iptables NAT rule to translate incoming packet's
// source IP to the respective Docker network interface IP.
// Required to route reply packets back through correct
// container interface.
err = ipt.AppendUnique(
"nat", "POSTROUTING",
"-s", hostPeerIp,
"-j", "MASQUERADE",
)
if err != nil {
fmt.Printf("Failed to add iptables nat rule: %v\n", err)
os.Exit(ExitSetupFailed)
}
}