-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathconfigure.go
377 lines (335 loc) · 14.2 KB
/
configure.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
package cmd
import (
"fmt"
"net"
"net/netip"
"os"
"strings"
"wiretap/peer"
"github.com/atotto/clipboard"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
type configureCmdConfig struct {
allowedIPs []string
endpoint string
outbound bool
port int
nickname string
configFileRelay string
configFileE2EE string
configFileServer string
writeToClipboard bool
simple bool
clientAddr4Relay string
clientAddr6Relay string
clientAddr4E2EE string
clientAddr6E2EE string
serverAddr4Relay string
serverAddr6Relay string
apiAddr string
apiv4Addr string
keepalive int
mtu int
disableV6 bool
localhostIP string
}
// Defaults for configure command.
// See root command for shared defaults.
var configureCmdArgs = configureCmdConfig{
allowedIPs: []string{""},
endpoint: Endpoint,
outbound: false,
port: USE_ENDPOINT_PORT,
nickname: "",
configFileRelay: ConfigRelay,
configFileE2EE: ConfigE2EE,
configFileServer: ConfigServer,
writeToClipboard: false,
simple: false,
clientAddr4Relay: ClientRelaySubnet4.Addr().Next().String() + "/32",
clientAddr6Relay: ClientRelaySubnet6.Addr().Next().String() + "/128",
clientAddr4E2EE: ClientE2EESubnet4.Addr().Next().String() + "/32",
clientAddr6E2EE: ClientE2EESubnet6.Addr().Next().String() + "/128",
serverAddr4Relay: RelaySubnets4.Addr().Next().Next().String() + "/32",
serverAddr6Relay: RelaySubnets6.Addr().Next().Next().String() + "/128",
apiAddr: ApiSubnets.Addr().Next().Next().String() + "/128",
apiv4Addr: ApiV4Subnets.Addr().Next().Next().String() + "/32",
keepalive: Keepalive,
mtu: MTU,
disableV6: false,
localhostIP: "",
}
// configureCmd represents the configure command.
var configureCmd = &cobra.Command{
Use: "configure",
Short: "Build wireguard config",
Long: `Build wireguard config and print command line arguments for deployment`,
Run: func(cmd *cobra.Command, args []string) {
configureCmdArgs.Run()
},
}
// Add command and set flags.
func init() {
rootCmd.AddCommand(configureCmd)
configureCmd.Flags().StringSliceVarP(&configureCmdArgs.allowedIPs, "routes", "r", configureCmdArgs.allowedIPs, "[REQUIRED] CIDR IP ranges that will be routed through wiretap (example \"10.0.0.1/24\")")
configureCmd.Flags().StringVarP(&configureCmdArgs.endpoint, "endpoint", "e", configureCmdArgs.endpoint, "[REQUIRED] IP:PORT (or [IP]:PORT for IPv6) of wireguard listener that server will connect to (example \"1.2.3.4:51820\")")
configureCmd.Flags().BoolVar(&configureCmdArgs.outbound, "outbound", configureCmdArgs.outbound, "client will initiate handshake to server; --endpoint now specifies server's listening socket instead of client's, and --port assigns the server's listening port instead of client's")
configureCmd.Flags().IntVarP(&configureCmdArgs.port, "port", "p", configureCmdArgs.port, "listener port for wireguard relay. Default is to copy the --endpoint port. If --outbound, sets port for the server; else for the client.")
configureCmd.Flags().StringVarP(&configureCmdArgs.nickname, "nickname", "n", configureCmdArgs.nickname, "Server nickname to display in 'status' command")
configureCmd.Flags().StringVarP(&configureCmdArgs.localhostIP, "localhost-ip", "i", configureCmdArgs.localhostIP, "[EXPERIMENTAL] Redirect wiretap packets destined for this IPv4 address to server's localhost")
configureCmd.Flags().StringVarP(&configureCmdArgs.configFileRelay, "relay-output", "", configureCmdArgs.configFileRelay, "wireguard relay config output filename")
configureCmd.Flags().StringVarP(&configureCmdArgs.configFileE2EE, "e2ee-output", "", configureCmdArgs.configFileE2EE, "wireguard E2EE config output filename")
configureCmd.Flags().StringVarP(&configureCmdArgs.configFileServer, "server-output", "s", configureCmdArgs.configFileServer, "wiretap server config output filename")
configureCmd.Flags().BoolVarP(&configureCmdArgs.writeToClipboard, "clipboard", "c", configureCmdArgs.writeToClipboard, "copy configuration args to clipboard")
configureCmd.Flags().BoolVarP(&configureCmdArgs.simple, "simple", "", configureCmdArgs.simple, "disable multihop and multiclient features for a simpler setup")
configureCmd.Flags().StringVarP(&configureCmdArgs.apiAddr, "api", "0", configureCmdArgs.apiAddr, "address of server API service")
configureCmd.Flags().IntVarP(&configureCmdArgs.keepalive, "keepalive", "k", configureCmdArgs.keepalive, "tunnel keepalive in seconds, only applies to outbound handshakes")
configureCmd.Flags().IntVarP(&configureCmdArgs.mtu, "mtu", "m", configureCmdArgs.mtu, "tunnel MTU")
configureCmd.Flags().BoolVarP(&configureCmdArgs.disableV6, "disable-ipv6", "", configureCmdArgs.disableV6, "disables IPv6")
configureCmd.Flags().StringVarP(&configureCmdArgs.clientAddr4Relay, "ipv4-relay", "", configureCmdArgs.clientAddr4Relay, "ipv4 relay address")
configureCmd.Flags().StringVarP(&configureCmdArgs.clientAddr6Relay, "ipv6-relay", "", configureCmdArgs.clientAddr6Relay, "ipv6 relay address")
configureCmd.Flags().StringVarP(&configureCmdArgs.clientAddr4E2EE, "ipv4-e2ee", "", configureCmdArgs.clientAddr4E2EE, "ipv4 e2ee address")
configureCmd.Flags().StringVarP(&configureCmdArgs.clientAddr6E2EE, "ipv6-e2ee", "", configureCmdArgs.clientAddr6E2EE, "ipv6 e2ee address")
configureCmd.Flags().StringVarP(&configureCmdArgs.serverAddr4Relay, "ipv4-relay-server", "", configureCmdArgs.serverAddr4Relay, "ipv4 relay address of server")
configureCmd.Flags().StringVarP(&configureCmdArgs.serverAddr6Relay, "ipv6-relay-server", "", configureCmdArgs.serverAddr6Relay, "ipv6 relay address of server")
err := configureCmd.MarkFlagRequired("routes")
check("failed to mark flag required", err)
err = configureCmd.MarkFlagRequired("endpoint")
check("failed to mark flag required", err)
configureCmd.Flags().SortFlags = false
helpFunc := configureCmd.HelpFunc()
configureCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
if !ShowHidden {
for _, f := range []string{
"api",
"ipv4-relay",
"ipv6-relay",
"ipv4-e2ee",
"ipv6-e2ee",
"ipv4-relay-server",
"ipv6-relay-server",
"keepalive",
"mtu",
"disable-ipv6",
"relay-output",
"e2ee-output",
"server-output",
} {
err := cmd.Flags().MarkHidden(f)
if err != nil {
fmt.Printf("Failed to hide flag %v: %v\n", f, err)
}
}
}
helpFunc(cmd, args)
})
}
// Run builds Wireguard relay and E2EE configs, and prints/writes them to a file.
// Also prints out a command to paste into a remote machine.
func (c configureCmdConfig) Run() {
var err error
if c.localhostIP != "" {
c.allowedIPs = append(c.allowedIPs, c.localhostIP+"/32")
}
if c.disableV6 && netip.MustParsePrefix(c.apiAddr).Addr().Is6() {
c.apiAddr = c.apiv4Addr
}
c.allowedIPs = append(c.allowedIPs, c.apiAddr)
// Generate client and server configs.
serverConfigRelayArgs := peer.ConfigArgs{}
serverConfigE2EEArgs := peer.ConfigArgs{}
serverConfigRelay, err := peer.GetConfig(serverConfigRelayArgs)
check("failed to generate server relay config", err)
serverConfigE2EE, err := peer.GetConfig(serverConfigE2EEArgs)
check("failed to generate server E2EE config", err)
// Parse first client relay subnet.
relaySubnet4, err := netip.ParsePrefix(c.serverAddr4Relay)
check("invalid cidr range", err)
relaySubnet6, err := netip.ParsePrefix(c.serverAddr6Relay)
check("invalid cidr range", err)
relaySubnet4 = netip.PrefixFrom(relaySubnet4.Addr(), SubnetV4Bits).Masked()
relaySubnet6 = netip.PrefixFrom(relaySubnet6.Addr(), SubnetV6Bits).Masked()
relaySubnets := []netip.Prefix{relaySubnet4}
if !c.disableV6 {
relaySubnets = append(relaySubnets, relaySubnet6)
}
clientRelayAddrs := []string{c.clientAddr4Relay}
if !c.disableV6 {
clientRelayAddrs = append(clientRelayAddrs, c.clientAddr6Relay)
}
clientE2EEAddrs := []string{c.clientAddr4E2EE}
if !c.disableV6 {
clientE2EEAddrs = append(clientE2EEAddrs, c.clientAddr6E2EE)
}
if c.port == USE_ENDPOINT_PORT {
c.port = portFromEndpoint(c.endpoint)
}
// We only configure one of these (based on --outbound or not)
// The other must be manually changed in the configs/command/envs
var clientPort int
var serverPort int
if c.outbound {
clientPort = Port
serverPort = c.port
} else {
clientPort = c.port
serverPort = Port
}
err = serverConfigRelay.SetPort(serverPort)
check("failed to set port", err)
clientConfigRelayArgs := peer.ConfigArgs{
ListenPort: clientPort,
Peers: []peer.PeerConfigArgs{
{
PublicKey: serverConfigRelay.GetPublicKey(),
AllowedIPs: func() []string {
if c.simple {
return c.allowedIPs
} else {
return func() []string {
var s []string
for _, r := range relaySubnets {
s = append(s, r.String())
}
return s
}()
}
}(),
Endpoint: func() string {
if c.outbound {
return c.endpoint
} else {
return ""
}
}(),
PersistentKeepaliveInterval: func() int {
if c.outbound {
return c.keepalive
} else {
return 0
}
}(),
},
},
Addresses: clientRelayAddrs,
}
clientConfigE2EEArgs := peer.ConfigArgs{
ListenPort: E2EEPort,
Peers: []peer.PeerConfigArgs{
{
PublicKey: serverConfigE2EE.GetPublicKey(),
AllowedIPs: c.allowedIPs,
Endpoint: net.JoinHostPort(relaySubnet4.Addr().Next().Next().String(), fmt.Sprint(E2EEPort)),
Nickname: c.nickname,
},
},
Addresses: clientE2EEAddrs,
MTU: c.mtu - 80,
}
clientConfigRelay, err := peer.GetConfig(clientConfigRelayArgs)
check("failed to generate client relay config", err)
clientConfigE2EE, err := peer.GetConfig(clientConfigE2EEArgs)
check("failed to generate client E2EE config", err)
clientPeerConfigRelay, err := clientConfigRelay.AsPeer()
check("failed to parse relay config as peer", err)
clientPeerConfigE2EE, err := clientConfigE2EE.AsPeer()
check("failed to parse e2ee config as peer", err)
if len(c.endpoint) > 0 {
if !c.outbound {
err = clientPeerConfigRelay.SetEndpoint(c.endpoint)
check("failed to set endpoint", err)
}
err = clientPeerConfigE2EE.SetEndpoint(net.JoinHostPort(clientConfigRelay.GetAddresses()[0].IP.String(), fmt.Sprint(E2EEPort)))
check("failed to set endpoint", err)
}
serverConfigRelay.AddPeer(clientPeerConfigRelay)
serverConfigE2EE.AddPeer(clientPeerConfigE2EE)
if c.mtu != MTU {
err = serverConfigRelay.SetMTU(c.mtu)
check("failed to set mtu", err)
}
if c.localhostIP != "" {
err = serverConfigRelay.SetLocalhostIP(c.localhostIP)
check("failed to set localhost IP", err)
}
// Add number to filename if it already exists.
c.configFileRelay = peer.FindAvailableFilename(c.configFileRelay)
c.configFileE2EE = peer.FindAvailableFilename(c.configFileE2EE)
c.configFileServer = peer.FindAvailableFilename(c.configFileServer)
if c.simple {
c.configFileRelay = c.configFileE2EE
}
// Write config file and get status string.
var fileStatusRelay string
err = os.WriteFile(c.configFileRelay, []byte(clientConfigRelay.AsFile()), 0600)
if err != nil {
fileStatusRelay = fmt.Sprintf("%s %s", RedBold("config:"), Red(fmt.Sprintf("error writing config file: %v", err)))
} else {
fileStatusRelay = fmt.Sprintf("%s %s", GreenBold("config:"), Green(c.configFileRelay))
}
// Write config file and get status string.
var fileStatusE2EE string
if !c.simple {
err = os.WriteFile(c.configFileE2EE, []byte(clientConfigE2EE.AsFile()), 0600)
if err != nil {
fileStatusE2EE = fmt.Sprintf("%s %s", RedBold("config:"), Red(fmt.Sprintf("error writing config file: %v", err)))
} else {
fileStatusE2EE = fmt.Sprintf("%s %s", GreenBold("config:"), Green(c.configFileE2EE))
}
}
// Write server config file and get status string.
var fileStatusServer string
err = os.WriteFile(c.configFileServer, []byte(peer.CreateServerFile(serverConfigRelay, serverConfigE2EE)), 0600)
if err != nil {
fileStatusServer = fmt.Sprintf("%s %s", RedBold("server config:"), Red(fmt.Sprintf("error writing config file: %v", err)))
} else {
fileStatusServer = fmt.Sprintf("%s %s", GreenBold("server config:"), Green(c.configFileServer))
}
// Make config file string
serverConfigFile := fmt.Sprintf("./wiretap serve -f %s", c.configFileServer)
if c.simple {
serverConfigFile = fmt.Sprintf("%s --simple", serverConfigFile)
}
if c.disableV6 {
serverConfigFile = fmt.Sprintf("%s --disable-ipv6", serverConfigFile)
}
// Copy to clipboard if requested.
var clipboardStatus string
if c.writeToClipboard {
err = clipboard.WriteAll(peer.CreateServerCommand(serverConfigRelay, serverConfigE2EE, peer.POSIX, c.simple, c.disableV6))
if err != nil {
clipboardStatus = fmt.Sprintf("%s %s", RedBold("clipboard:"), Red(fmt.Sprintf("error copying to clipboard: %v", err)))
} else {
clipboardStatus = fmt.Sprintf("%s %s", GreenBold("clipboard:"), Green("successfully copied"))
}
}
// Write and format output.
fmt.Fprintln(color.Output)
fmt.Fprintln(color.Output, "Configurations successfully generated.")
fmt.Fprintln(color.Output, "Import the config(s) into WireGuard locally and pass the arguments below to Wiretap on the remote machine.")
fmt.Fprintln(color.Output)
fmt.Fprintln(color.Output, fileStatusRelay)
fmt.Fprintln(color.Output, Green(strings.Repeat("─", 32)))
fmt.Fprint(color.Output, WhiteBold(clientConfigRelay.AsFile()))
fmt.Fprintln(color.Output, Green(strings.Repeat("─", 32)))
fmt.Fprintln(color.Output)
if !c.simple {
fmt.Fprintln(color.Output, fileStatusE2EE)
fmt.Fprintln(color.Output, Green(strings.Repeat("─", 32)))
fmt.Fprint(color.Output, WhiteBold(clientConfigE2EE.AsFile()))
fmt.Fprintln(color.Output, Green(strings.Repeat("─", 32)))
fmt.Fprintln(color.Output)
}
fmt.Fprintln(color.Output, fileStatusServer)
fmt.Fprintln(color.Output)
fmt.Fprintln(color.Output, GreenBold("server command:"))
fmt.Fprintln(color.Output, Cyan("POSIX Shell: "), Green(peer.CreateServerCommand(serverConfigRelay, serverConfigE2EE, peer.POSIX, c.simple, c.disableV6)))
fmt.Fprintln(color.Output, Cyan(" PowerShell: "), Green(peer.CreateServerCommand(serverConfigRelay, serverConfigE2EE, peer.PowerShell, c.simple, c.disableV6)))
fmt.Fprintln(color.Output, Cyan("Config File: "), Green(serverConfigFile))
fmt.Fprintln(color.Output)
if c.writeToClipboard {
fmt.Fprintln(color.Output, clipboardStatus)
fmt.Fprintln(color.Output)
}
}