-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
111 lines (102 loc) · 2.47 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
package main
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"flag"
"fmt"
"io"
"log"
"log/slog"
"net"
"os"
"strings"
"github.com/mjl-/sconf"
"golang.org/x/mod/sumdb/note"
)
func usage() {
log.Println("usage: gobuild config")
log.Println(" gobuild testconfig gobuild.conf")
log.Println(" gobuild serve [flags] [gobuild.conf]")
log.Println(" gobuild genkey name")
log.Println(" gobuild get [flags] module[@version/package]")
log.Println(" gobuild sum < file")
flag.PrintDefaults()
os.Exit(2)
}
func main() {
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) == 0 {
usage()
}
cmd, args := args[0], args[1:]
switch cmd {
default:
usage()
case "config":
if len(args) != 0 {
usage()
}
if err := sconf.Describe(os.Stdout, &config); err != nil {
log.Fatalf("describing config: %v", err)
}
case "testconfig":
if len(args) != 1 {
usage()
}
if err := parseConfig(args[0], &config); err != nil {
log.Fatalf("parsing config file: %v", err)
}
log.Printf("config OK")
case "serve":
serve(args)
case "genkey":
if len(args) != 1 {
usage()
}
if skey, vkey, err := note.GenerateKey(rand.Reader, args[0]); err != nil {
log.Fatalf("generating key: %v", err)
} else {
log.Printf("Signer key: %s", skey)
log.Printf("Verifier key: %s", vkey)
log.Printf(`Configure the signer key in your server config file, and use the verifier key with the "get" subcommand.`)
}
case "get":
get(args)
case "sum":
if len(args) != 0 {
usage()
}
sha := sha256.New()
if _, err := io.Copy(sha, os.Stdin); err != nil {
log.Fatalf("read: %v", err)
} else if _, err := fmt.Println("0" + base64.RawURLEncoding.EncodeToString(sha.Sum(nil)[:20])); err != nil {
log.Fatalf("write: %v", err)
}
}
}
func parseConfig(p string, c *Config) error {
if err := sconf.ParseFile(p, c); err != nil {
return err
}
c.loglevel = &slog.LevelVar{}
if err := c.loglevel.UnmarshalText([]byte(c.LogLevel)); err != nil {
return fmt.Errorf("parsing loglevel %q: %v", c.LogLevel, err)
}
for i, cp := range c.BadClients {
cp.UserAgent = strings.ToLower(cp.UserAgent)
cp.HostnameSuffix = strings.ToLower(cp.HostnameSuffix)
for _, ipnetstr := range cp.Networks {
if _, ipnet, err := net.ParseCIDR(ipnetstr); err != nil {
return fmt.Errorf("parsing network %q: %v", ipnetstr, err)
} else {
cp.ipnets = append(cp.ipnets, *ipnet)
}
}
c.BadClients[i] = cp
}
return nil
}