-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
481 lines (441 loc) · 12.5 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
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
// SPDX-License-Identifier: MPL-2.0
/*
* Copyright (C) 2024 The Noisy Sockets Authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"net/netip"
"os"
"runtime"
"time"
"github.com/adrg/xdg"
"github.com/dpeckett/telemetry"
"github.com/dpeckett/telemetry/v1alpha1"
"github.com/noisysockets/network"
"github.com/noisysockets/noisysockets/config"
configtypes "github.com/noisysockets/noisysockets/config/types"
configcmd "github.com/noisysockets/nsh/cmd/config"
dnscmd "github.com/noisysockets/nsh/cmd/dns"
peercmd "github.com/noisysockets/nsh/cmd/peer"
routecmd "github.com/noisysockets/nsh/cmd/route"
upcmd "github.com/noisysockets/nsh/cmd/up"
"github.com/noisysockets/nsh/internal/constants"
"github.com/noisysockets/nsh/internal/service"
"github.com/noisysockets/nsh/internal/util"
"github.com/urfave/cli/v2"
)
func main() {
var conf configtypes.Config
configPath, err := xdg.ConfigFile("nsh/noisysockets.yaml")
if err != nil {
slog.Error("Error getting config file path", slog.Any("error", err))
os.Exit(1)
}
sharedFlags := []cli.Flag{
&cli.GenericFlag{
Name: "log-level",
Usage: "Set the log verbosity level",
Value: util.FromSlogLevel(slog.LevelInfo),
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Noisy Sockets configuration file",
Value: configPath,
},
}
initLogger := func(c *cli.Context) error {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: (*slog.Level)(c.Generic("log-level").(*util.LevelFlag)),
})))
return nil
}
loadConfig := func(c *cli.Context) error {
configPath := c.String("config")
slog.Debug("Loading config", slog.String("path", configPath))
configFile, err := os.Open(configPath)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("config file %q does not exist, run `nsh config init` to create one", configPath)
}
return fmt.Errorf("failed to open config file: %w", err)
}
defer configFile.Close()
conf, err = config.FromYAML(configFile)
if err != nil {
return fmt.Errorf("failed to read config: %w", err)
}
return nil
}
// Collect anonymous usage statistics.
var telemetryReporter *telemetry.Reporter
initTelemetry := func(c *cli.Context) error {
// Compatibility with the old environment variable.
if os.Getenv("NSH_NO_TELEMETRY") != "" {
os.Setenv("DO_NOT_TRACK", "1")
}
telemetryReporter = telemetry.NewReporter(c.Context, slog.Default(), telemetry.Configuration{
BaseURL: constants.TelemetryURL,
Tags: []string{"nsh"},
})
// Some basic system information.
info := map[string]string{
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"num_cpu": fmt.Sprintf("%d", runtime.NumCPU()),
"version": constants.Version,
}
// Are we running in a container?
if os.Getenv("container") != "" {
info["container"] = os.Getenv("container")
}
telemetryReporter.ReportEvent(&v1alpha1.TelemetryEvent{
Kind: v1alpha1.TelemetryEventKindInfo,
Name: "ApplicationStart",
Values: info,
})
return nil
}
shutdownTelemetry := func(c *cli.Context) error {
if telemetryReporter == nil {
return nil
}
telemetryReporter.ReportEvent(&v1alpha1.TelemetryEvent{
Kind: v1alpha1.TelemetryEventKindInfo,
Name: "ApplicationStop",
})
// Don't want to block the shutdown of the application for too long.
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := telemetryReporter.Shutdown(ctx); err != nil {
slog.Error("Failed to close telemetry reporter", slog.Any("error", err))
}
return nil
}
app := &cli.App{
Name: "nsh",
Usage: "The Noisy Sockets CLI",
Version: constants.Version,
Commands: []*cli.Command{
{
Name: "config",
Usage: "Manage configuration",
Subcommands: []*cli.Command{
{
Name: "init",
Usage: "Create a new configuration",
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "The name of the peer",
},
&cli.IntFlag{
Name: "listen-port",
Aliases: []string{"l"},
Usage: "The port to listen on",
},
&cli.StringSliceFlag{
Name: "ip",
Usage: "The IP address/s to assign to the peer, if not set a random IPv6 address will be assigned",
},
&cli.StringFlag{
Name: "domain",
Aliases: []string{"d"},
Usage: "The network domain",
},
}, sharedFlags...),
Before: beforeAll(initLogger, initTelemetry),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
return configcmd.Init(
c.String("config"),
c.String("name"),
c.Int("listen-port"),
c.StringSlice("ip"),
c.String("domain"))
},
},
{
Name: "import",
Usage: "Import existing WireGuard configuration",
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "input",
Aliases: []string{"i"},
Usage: "The path to read the WireGuard formatted configuration",
Value: "-",
},
}, sharedFlags...),
Before: beforeAll(initLogger, initTelemetry),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
return configcmd.Import(
c.String("config"),
c.String("input"))
},
},
{
Name: "export",
Usage: "Export WireGuard configuration",
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "The path to write the WireGuard formatted configuration",
Value: "-",
},
&cli.BoolFlag{
Name: "stripped",
Usage: "Remove wg-quick specific fields",
Value: false,
},
}, sharedFlags...),
Before: beforeAll(initLogger, initTelemetry, loadConfig),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
return configcmd.Export(
conf,
c.String("output"),
c.Bool("stripped"))
},
},
{
Name: "show",
Usage: "Show the current configuration",
Flags: sharedFlags,
Args: true,
ArgsUsage: "query",
Before: beforeAll(initLogger, initTelemetry, loadConfig),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
if c.Args().Len() != 1 {
_ = cli.ShowSubcommandHelp(c)
return errors.New("expected jq syntax query as argument")
}
return configcmd.Show(c.Context, conf, c.Args().First())
},
},
},
},
{
Name: "peer",
Usage: "Manage peers",
Subcommands: []*cli.Command{
{
Name: "add",
Usage: "Add a peer",
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "The name of the peer",
},
&cli.StringFlag{
Name: "public-key",
Aliases: []string{"k"},
Usage: "The public key of the peer",
Required: true,
},
&cli.StringFlag{
Name: "endpoint",
Aliases: []string{"e"},
Usage: "The peer's public address/port (if available)",
},
&cli.StringSliceFlag{
Name: "ip",
Usage: "The IP address/s to assign to the peer",
Required: true,
},
}, sharedFlags...),
Before: beforeAll(initLogger, initTelemetry, loadConfig),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
return peercmd.Add(
c.String("config"),
c.String("name"),
c.String("public-key"),
c.String("endpoint"),
c.StringSlice("ip"),
)
},
},
{
Name: "remove",
Usage: "Remove a peer",
Flags: sharedFlags,
Args: true,
ArgsUsage: "name | public-key",
Before: beforeAll(initLogger, initTelemetry, loadConfig),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
if c.Args().Len() != 1 {
_ = cli.ShowSubcommandHelp(c)
return errors.New("expected name or public-key as argument")
}
return peercmd.Remove(
c.String("config"),
c.Args().First(),
)
},
},
},
},
{
Name: "route",
Usage: "Manage network routes",
Subcommands: []*cli.Command{
{
Name: "add",
Usage: "Add a route",
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "destination",
Aliases: []string{"d"},
Usage: "The destination CIDR",
Required: true,
},
&cli.StringFlag{
Name: "via",
Aliases: []string{"v"},
Usage: "The router peer name or public key",
Required: true,
},
}, sharedFlags...),
Before: beforeAll(initLogger, initTelemetry, loadConfig),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
return routecmd.Add(
c.String("config"),
c.String("destination"),
c.String("via"),
)
},
},
{
Name: "remove",
Usage: "Remove a route",
Flags: sharedFlags,
Args: true,
ArgsUsage: "destination",
Before: beforeAll(initLogger, initTelemetry, loadConfig),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
if c.Args().Len() != 1 {
_ = cli.ShowSubcommandHelp(c)
return errors.New("expected destination as argument")
}
return routecmd.Remove(
c.String("config"),
c.Args().First(),
)
},
},
},
},
{
Name: "dns",
Usage: "Manage DNS configuration",
Subcommands: []*cli.Command{
{
Name: "server",
Usage: "Manage DNS servers",
Subcommands: []*cli.Command{
{
Name: "add",
Usage: "Add a DNS server",
Args: true,
ArgsUsage: "address",
Flags: sharedFlags,
Before: beforeAll(initLogger, initTelemetry, loadConfig),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
if c.Args().Len() != 1 {
_ = cli.ShowSubcommandHelp(c)
return errors.New("expected DNS server address as argument")
}
return dnscmd.AddServer(
c.String("config"),
c.Args().First(),
)
},
},
},
},
},
},
{
Name: "up",
Usage: "Start Noisy Sockets",
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "enable-dns",
Usage: "Enable DNS service",
},
&cli.BoolFlag{
Name: "enable-router",
Usage: "Enable router service",
},
&cli.BoolFlag{
Name: "nat64",
Usage: "Enable DNS64/NAT64 (IPv6 to IPv4 translation)",
Value: true,
},
&cli.StringFlag{
Name: "nat64-prefix",
Usage: "The DNS64/NAT64 prefix",
Value: "64:ff9b::/96",
},
&cli.StringSliceFlag{
Name: "dns-public-upstream",
Usage: "Upstream DNS servers to use for public queries",
},
}, sharedFlags...),
Before: beforeAll(initLogger, initTelemetry, loadConfig),
After: shutdownTelemetry,
Action: func(c *cli.Context) error {
enableNAT64 := c.Bool("nat64")
nat64Prefix, err := netip.ParsePrefix(c.String("nat64-prefix"))
if err != nil {
return fmt.Errorf("failed to parse NAT64 prefix: %w", err)
}
var services []service.Service
if c.Bool("enable-dns") {
services = append(services, service.DNS(enableNAT64, nat64Prefix, c.StringSlice("dns-public-upstream")))
}
if c.Bool("enable-router") {
services = append(services, service.Router(network.Host(), enableNAT64, nat64Prefix))
}
// If all services are disabled, then throw an error.
if len(services) == 0 {
_ = cli.ShowSubcommandHelp(c)
return errors.New("at least one service must be enabled")
}
return upcmd.Up(c.Context, conf, services)
},
},
},
}
if err := app.Run(os.Args); err != nil {
slog.Error("Error", slog.Any("error", err))
os.Exit(1)
}
}
func beforeAll(funcs ...cli.BeforeFunc) cli.BeforeFunc {
return func(c *cli.Context) error {
for _, f := range funcs {
if err := f(c); err != nil {
return err
}
}
return nil
}
}