Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
99 lines (86 loc) · 2.48 KB
/
Copy pathmain.go
File metadata and controls
99 lines (86 loc) · 2.48 KB
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
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
)
const (
defaultAddr = "localhost:3000"
defaultReloadInterval = 30 * time.Second
minReloadInterval = time.Second
)
var errPathRequired = errors.New("-path is required")
type config struct {
addr string
nodesPath string
reloadInterval time.Duration
allowEmpty bool
}
type serveFunc func(context.Context, config, *slog.Logger) error
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
exitCode := runCLI(ctx, os.Args[1:], os.Stderr, logger, serve)
stop()
os.Exit(exitCode)
}
func runCLI(ctx context.Context, args []string, stderr io.Writer, logger *slog.Logger, serveFn serveFunc) int {
cfg, err := parseConfig(args, stderr)
if errors.Is(err, flag.ErrHelp) {
return 0
}
if err != nil {
writeDiagnostic(stderr, "error: %v\n", err)
return 2
}
if err := serveFn(ctx, cfg, logger); err != nil {
writeDiagnostic(stderr, "error: %v\n", err)
return 1
}
return 0
}
func parseConfig(args []string, stderr io.Writer) (config, error) {
fs := flag.NewFlagSet("tailscale-derp-client-verifier", flag.ContinueOnError)
fs.SetOutput(stderr)
cfg := config{reloadInterval: defaultReloadInterval}
fs.StringVar(&cfg.addr, "addr", defaultAddr, "address to listen on")
fs.StringVar(&cfg.nodesPath, "path", "", "path to nodes.json (required)")
fs.DurationVar(&cfg.reloadInterval, "reload-interval", defaultReloadInterval, "interval between nodes.json reloads (minimum 1s)")
fs.BoolVar(&cfg.allowEmpty, "allow-empty", false, "allow an empty nodes list that denies every client")
fs.Usage = func() {
writeDiagnostic(stderr, "Usage: %s -path /path/to/nodes.json [options]\n", fs.Name())
fs.PrintDefaults()
}
if err := fs.Parse(args); err != nil {
return config{}, err
}
if fs.NArg() != 0 {
return config{}, fmt.Errorf("unexpected positional arguments: %q", fs.Args())
}
if err := cfg.validate(); err != nil {
if errors.Is(err, errPathRequired) {
fs.Usage()
}
return config{}, err
}
return cfg, nil
}
func (cfg config) validate() error {
if cfg.nodesPath == "" {
return errPathRequired
}
if cfg.reloadInterval < minReloadInterval {
return fmt.Errorf("-reload-interval must be at least %s", minReloadInterval)
}
return nil
}
func writeDiagnostic(w io.Writer, format string, args ...any) {
_, _ = fmt.Fprintf(w, format, args...)
}