-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (129 loc) · 4.12 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
/*
Copyright © 2025 Seednode <seednode@seedno.de>
*/
package main
import (
"errors"
"fmt"
"log"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
ReleaseVersion string = "1.21.1"
)
var (
all bool
bind string
exitOnError bool
maxDiceRolls int
maxDiceSides int
ouiFile string
dns bool
dnsResolver string
hashing bool
httpStatus bool
ip bool
mac bool
qr bool
qrSize int
roll bool
subnet bool
timezones bool
tlsCert string
tlsKey string
port uint16
profile bool
verbose bool
version bool
requiredArgs = []string{
"all",
"dns",
"hash",
"http-status",
"ip",
"mac",
"qr",
"roll",
"time",
}
)
func main() {
cmd := &cobra.Command{
Use: "query",
Short: "Serves a variety of web-based utilities.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initializeConfig(cmd)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
switch {
case tlsCert == "" && tlsKey != "" || tlsCert != "" && tlsKey == "":
return errors.New("TLS certificate and keyfile must both be specified to enable HTTPS")
case qrSize < 256 || qrSize > 2048:
return ErrInvalidQRSize
case maxDiceRolls < 1:
return ErrInvalidMaxDiceCount
case maxDiceSides < 1:
return ErrInvalidMaxDiceSides
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return servePage()
},
}
cmd.Flags().BoolVar(&all, "all", false, "enable all features")
cmd.Flags().StringVarP(&bind, "bind", "b", "0.0.0.0", "address to bind to")
cmd.Flags().BoolVar(&dns, "dns", false, "enable DNS lookup")
cmd.Flags().StringVar(&dnsResolver, "dns-resolver", "", "custom DNS server IP and port to query (e.g. 8.8.8.8:53)")
cmd.Flags().BoolVar(&exitOnError, "exit-on-error", false, "shut down webserver on error, instead of just printing the error")
cmd.Flags().BoolVar(&hashing, "hash", false, "enable hashing")
cmd.Flags().BoolVar(&httpStatus, "http-status", false, "enable HTTP response status codes")
cmd.Flags().BoolVar(&ip, "ip", false, "enable IP lookups")
cmd.Flags().BoolVar(&mac, "mac", false, "enable MAC lookups")
cmd.Flags().IntVar(&maxDiceRolls, "max-dice-rolls", 1024, "maximum number of dice per roll")
cmd.Flags().IntVar(&maxDiceSides, "max-dice-sides", 1024, "maximum number of sides per die")
cmd.Flags().StringVar(&ouiFile, "oui-file", "", "path to Wireshark manufacturer database file")
cmd.Flags().Uint16VarP(&port, "port", "p", 8080, "port to listen on")
cmd.Flags().BoolVar(&profile, "profile", false, "register net/http/pprof handlers")
cmd.Flags().BoolVar(&qr, "qr", false, "enable QR code generation")
cmd.Flags().IntVar(&qrSize, "qr-size", 256, "height/width of PNG-encoded QR codes (in pixels)")
cmd.Flags().BoolVar(&roll, "roll", false, "enable dice rolls")
cmd.Flags().BoolVar(&subnet, "subnet", false, "enable subnet calculator")
cmd.Flags().BoolVar(&timezones, "time", false, "enable time lookup")
cmd.Flags().StringVar(&tlsCert, "tls-cert", "", "path to TLS certificate")
cmd.Flags().StringVar(&tlsKey, "tls-key", "", "path to TLS keyfile")
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "log tool usage to stdout")
cmd.Flags().BoolVarP(&version, "version", "V", false, "display version and exit")
cmd.Flags().SetInterspersed(true)
cmd.CompletionOptions.HiddenDefaultCmd = true
cmd.MarkFlagsOneRequired(requiredArgs...)
cmd.SilenceErrors = true
cmd.SetHelpCommand(&cobra.Command{
Hidden: true,
})
cmd.SetVersionTemplate("query v{{.Version}}\n")
cmd.Version = ReleaseVersion
err := cmd.Execute()
if err != nil {
log.Fatal(err)
}
}
func initializeConfig(cmd *cobra.Command) {
v := viper.New()
v.SetEnvPrefix("query")
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.AutomaticEnv()
bindFlags(cmd, v)
}
func bindFlags(cmd *cobra.Command, v *viper.Viper) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
configName := strings.ReplaceAll(f.Name, "-", "_")
if !f.Changed && v.IsSet(configName) {
val := v.Get(configName)
cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
}
})
}