-
Notifications
You must be signed in to change notification settings - Fork 26
/
local_unix.go
378 lines (358 loc) · 10.6 KB
/
local_unix.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
//go:build linux || darwin || windows
package main
import (
"context"
"fmt"
"net"
"net/rpc/jsonrpc"
"path/filepath"
"github.com/nexodus-io/nexodus/internal/api"
"github.com/urfave/cli/v3"
)
func init() {
additionalPlatformCommands = append(additionalPlatformCommands, &cli.Command{
Name: "nexd",
Usage: "Commands for interacting with the local instance of nexd",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "unix-socket",
Usage: "Path to the unix socket nexd is listening against",
Value: api.UnixSocketPath,
Destination: &api.UnixSocketPath,
DefaultText: api.UnixSocketPath,
Required: false,
},
},
Before: func(ctx context.Context, command *cli.Command) error {
if err := hasPrivileges(); err != nil {
return err
}
return nil
},
Commands: []*cli.Command{
{
Name: "version",
Usage: "Display the nexd version",
Action: cmdLocalVersion,
},
{
Name: "status",
Usage: "Display the nexd status",
Action: cmdLocalStatus,
},
{
Name: "get",
Usage: "Get a value from the local nexd instance",
Commands: []*cli.Command{
{
Name: "tunnelip",
Usage: "Get the tunnel IP address",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "ipv6",
Aliases: []string{"6"},
Usage: "Get the IPv6 tunnel IP address",
Value: false,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
var result string
var err error
if err := checkVersion(); err != nil {
return err
}
if command.Bool("ipv6") {
result, err = callNexd("GetTunnelIPv6", "")
} else {
result, err = callNexd("GetTunnelIPv4", "")
}
if err != nil {
fmt.Printf("%s\n", err)
return err
}
fmt.Printf("%s\n", result)
return nil
},
},
{
Name: "debug",
Usage: "Get the debug logging status",
Action: func(ctx context.Context, command *cli.Command) error {
if err := checkVersion(); err != nil {
return err
}
result, err := callNexd("GetDebug", "")
if err != nil {
fmt.Printf("%s\n", err)
return err
}
fmt.Printf("%s\n", result)
return nil
},
},
},
},
{
Name: "set",
Usage: "Set a value on the local nexd instance",
Commands: []*cli.Command{
{
Name: "debug",
Usage: "Set debug logging on or off",
Commands: []*cli.Command{
{
Name: "on",
Usage: "Turn debug logging on",
Action: func(ctx context.Context, command *cli.Command) error {
if err := checkVersion(); err != nil {
return err
}
result, err := callNexd("SetDebugOn", "")
if err != nil {
fmt.Printf("%s\n", err)
return err
}
fmt.Printf("%s\n", result)
return nil
},
},
{
Name: "off",
Usage: "Turn debug logging off",
Action: func(ctx context.Context, command *cli.Command) error {
if err := checkVersion(); err != nil {
return err
}
result, err := callNexd("SetDebugOff", "")
if err != nil {
fmt.Printf("%s\n", err)
return err
}
fmt.Printf("%s\n", result)
return nil
},
},
},
},
},
},
{
Name: "proxy",
Usage: "Commands for interacting nexd's proxy configuration",
Commands: []*cli.Command{
{
Name: "list",
Usage: "List the nexd proxy rules",
Action: func(ctx context.Context, command *cli.Command) error {
if err := checkVersion(); err != nil {
return err
}
result, err := callNexd("ProxyList", "")
if err != nil {
fmt.Printf("%s\n", err)
return err
}
fmt.Printf("%s", result)
return nil
},
},
{
Name: "add",
Usage: "Add one or more proxy rules to nexd",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "ingress",
Usage: "Forward connections from the Nexodus network made to [port] on this proxy instance to port [destination_port] at [destination_ip] via a locally accessible network using a `value` in the form: protocol:port:destination_ip:destination_port. All fields are required.",
Required: false,
},
&cli.StringSliceFlag{
Name: "egress",
Usage: "Forward connections from a locally accessible network made to [port] on this proxy instance to port [destination_port] at [destination_ip] via the Nexodus network using a `value` in the form: protocol:port:destination_ip:destination_port. All fields are required.",
Required: false,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
return proxyAddRemove(ctx, command, true)
},
},
{
Name: "remove",
Usage: "remove one or more proxy rules to nexd",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "ingress",
Usage: "Forward connections from the Nexodus network made to [port] on this proxy instance to port [destination_port] at [destination_ip] via a locally accessible network using a `value` in the form: protocol:port:destination_ip:destination_port. All fields are required.",
Required: false,
},
&cli.StringSliceFlag{
Name: "egress",
Usage: "Forward connections from a locally accessible network made to [port] on this proxy instance to port [destination_port] at [destination_ip] via the Nexodus network using a `value` in the form: protocol:port:destination_ip:destination_port. All fields are required.",
Required: false,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
return proxyAddRemove(ctx, command, false)
},
},
},
},
{
Name: "peers",
Usage: "Commands for interacting with nexd peer connectivity",
Commands: []*cli.Command{
{
Name: "list",
Usage: "list the nexd peers for this device",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "full",
Aliases: []string{"f"},
Usage: "display the full set of peer details",
Value: false,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
return cmdListPeers(ctx, command)
},
},
{
Name: "ping",
Usage: "run a test to check the nexd IPv4 peer connectivity (host firewalls or security groups may block the ICMP probes)",
Action: func(ctx context.Context, command *cli.Command) error {
return cmdConnStatus(ctx, command, v4)
},
},
{
Name: "ping6",
Usage: "run a test to check the nexd IPv6 peer connectivity (host firewalls or security groups may block the ICMP probes)",
Action: func(ctx context.Context, command *cli.Command) error {
return cmdConnStatus(ctx, command, v6)
},
},
},
},
{
Name: "exit-node",
Usage: "Commands for interacting nexd exit node configuration",
Commands: []*cli.Command{
{
Name: "list",
Usage: "list exit nodes",
Action: func(ctx context.Context, command *cli.Command) error {
encodeOut := command.String("output")
return listExitNodes(ctx, command, encodeOut)
},
},
{
Name: "enable",
Usage: "Enable the device to use an exit node in the current organization. Warning: this will funnel all traffic through the exit node if one exists and will likely cause your device to be unreachable outside of the nexodus peer network.",
Action: func(ctx context.Context, command *cli.Command) error {
return enableExitNodeClient(ctx, command)
},
},
{
Name: "disable",
Usage: "Disable the device from using an exit node. Traffic will return to using the device's default gateway and direct peers in the nexodus peer network.",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "client",
Usage: "disable the use of an exit node on this device and remove any exit node client configuration if one exists.",
Required: false,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
return disableExitNodeClient(ctx, command)
},
},
},
},
},
})
}
func callNexd(method string, arg string) (string, error) {
conn, err := net.Dial("unix", api.UnixSocketPath)
if err != nil {
conn, err = net.Dial("unix", filepath.Base(api.UnixSocketPath))
if err != nil {
return "", fmt.Errorf("Failed to connect to nexd: %w\n", err)
}
}
defer conn.Close()
client := jsonrpc.NewClient(conn)
var result string
err = client.Call("NexdCtl."+method, arg, &result)
if err != nil {
return "", fmt.Errorf("Failed to execute method (%s): %w\n", method, err)
}
return result, nil
}
func checkVersion() error {
result, err := callNexd("Version", "")
if err != nil {
return fmt.Errorf("Failed to get nexd version: %w\n", err)
}
if Version != result {
errMsg := fmt.Sprintf("Version mismatch: nexctl(%s) nexd(%s)\n", Version, result)
return fmt.Errorf("%s", errMsg)
}
return nil
}
func cmdLocalVersion(ctx context.Context, command *cli.Command) error {
fmt.Printf("nexctl version: %s\n", Version)
result, err := callNexd("Version", "")
if err == nil {
fmt.Printf("nexd version: %s\n", result)
}
return err
}
func cmdLocalStatus(ctx context.Context, command *cli.Command) error {
if err := checkVersion(); err != nil {
return err
}
result, err := callNexd("Status", "")
if err != nil {
return err
}
fmt.Printf("%s", result)
return nil
}
func proxyAddRemove(ctx context.Context, command *cli.Command, add bool) error {
if err := checkVersion(); err != nil {
return err
}
ingress := command.StringSlice("ingress")
egress := command.StringSlice("egress")
if len(ingress) == 0 && len(egress) == 0 {
return fmt.Errorf("No rules provided")
}
var method string
addStr := "adding"
if add {
method = "ProxyAddIngress"
} else {
method = "ProxyRemoveIngress"
addStr = "removing"
}
for _, rule := range ingress {
result, err := callNexd(method, rule)
if err != nil {
fmt.Printf("Error %s ingress rule (%s): %s\n", addStr, rule, err)
continue
}
fmt.Printf("%s", result)
}
if add {
method = "ProxyAddEgress"
} else {
method = "ProxyRemoveEgress"
}
for _, rule := range egress {
result, err := callNexd(method, rule)
if err != nil {
fmt.Printf("Error %s egress rule (%s): %s\n", addStr, rule, err)
continue
}
fmt.Printf("%s", result)
}
return nil
}