generated from pavelpascari/go-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weber.go
196 lines (162 loc) · 4.56 KB
/
weber.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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"time"
)
var (
x = flag.String("X", "", "")
h = flag.String("H", "", "")
o = flag.String("o", "", "")
c = flag.String("c", "", "")
t = flag.Int("t", 15, "")
v = flag.Bool("v", false, "")
q = flag.Bool("q", false, "")
supportedMethods = []string{
http.MethodDelete,
http.MethodGet,
http.MethodHead,
http.MethodOptions,
http.MethodPatch,
http.MethodPost,
http.MethodPut,
}
)
const usage = `Usage: weber [OPTIONS] <url>
Options:
-X <method> Comma-separated list of HTTP methods to watch for (GET, POST, OPTIONS, PUT, DELETE). Default behavior is to consider all methods.
-H <string> Comma-separated list of hostname or IP address to watch for. Default behavior is to consider all hosts.
-o <file> Write the response to a file. CSV is the default and only supported format.
-c <string> Comma-separated list of columns to write to the output file. Default is "url,method,status".
Available columns are any valid response header, plus: url, method, status, hostname.
-t <int> Time after which the program will give up waiting for another request.
Every new processed request will restart the timer. Default is 15 seconds.
-v Enable verbose logging to observe all browser events.
-q Disable all logging.
-h Show this help message.
Examples:
# Watch for all requests to https://example.com
weber -o output.csv https://example.com
# Watch for all requests to https://example.com> and https://example.org
weber -H example.com,example.org -o output.csv https://example.com
# Watch for GET requests on example.org and output the URL, request method, the status code, and cache-control header
weber -X GET -H example.org -o output.csv -c "url,method,status,Cache-Control" https://example.com
# Get all URLs that for resources of a website
weber -o output.csv -c hostname https://example.com
`
type config struct {
// verbose enables verbose logging to observe all browser events
verbose bool
// quiet disables all logging
quiet bool
// giveUpAfter is the time after which the program will give up waiting for another request
giveUpAfter time.Duration
// methods is a list of HTTP methods to watch for
methods []string
// domains is a list of domains to watch for
domains []string
// url is the URL we observe being loaded
url string
// outputPath is the path to the file where the output will be written
outputPath string
// outputCols is the list of columns to write to the output file
outputCols []string
}
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, usage)
}
cfg, err := flagsToConfig()
if err != nil {
usageAndExit(err.Error())
}
log := logger(cfg)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
log("\nInterrupted. Exiting...")
log("\nThe stored data may be incomplete...")
cancel()
}()
if err := WatchNetworkFor(ctx, cfg.url, cfg, log); err != nil {
errAndExit(err.Error())
}
log("Done.")
}
type logF func(string, ...any)
func logger(cfg config) logF {
return func(msg string, args ...any) {
if cfg.quiet {
return
}
fmt.Fprintf(os.Stderr, msg, args...)
fmt.Fprint(os.Stderr, "\n")
}
}
func usageAndExit(msg string) {
if msg != "" {
fmt.Fprint(os.Stderr, msg)
fmt.Fprint(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprint(os.Stderr, "\n")
os.Exit(1)
}
func errAndExit(msg string) {
fmt.Fprint(os.Stderr, msg)
fmt.Fprint(os.Stderr, "\n")
os.Exit(1)
}
func flagsToConfig() (config, error) {
flag.Parse()
if flag.NArg() < 1 {
return config{}, fmt.Errorf("missing required argument: url")
}
// Make sure the host is provided
url := flag.Args()[0]
if url == "" {
return config{}, fmt.Errorf("provided url argument is empty")
}
cfg := config{
url: url,
giveUpAfter: time.Duration(*t) * time.Second,
outputPath: *o,
outputCols: []string{"url", "method", "status"},
}
if *x != "" {
methods := strings.Split(*x, ",")
if len(methods) > 0 {
for _, m := range methods {
if !contains(supportedMethods, m) {
return config{}, fmt.Errorf("unsupported HTTP method: %s", m)
}
cfg.methods = append(cfg.methods, m)
}
}
}
if *h != "" {
cfg.domains = strings.Split(*h, ",")
}
if *c != "" {
cols := strings.Split(*c, ",")
if len(cols) > 0 {
cfg.outputCols = cols
}
}
if *v {
cfg.verbose = true
}
if *q {
cfg.quiet = true
cfg.verbose = false
}
return cfg, nil
}