forked from tomnomnom/unfurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
345 lines (281 loc) · 7.84 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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"net/url"
"os"
"regexp"
"github.com/jakewarren/tldomains"
)
func main() {
var unique bool
flag.BoolVar(&unique, "u", false, "")
flag.BoolVar(&unique, "unique", false, "")
var verbose bool
flag.BoolVar(&verbose, "v", false, "")
flag.BoolVar(&verbose, "verbose", false, "")
flag.Parse()
mode := flag.Arg(0)
fmtStr := flag.Arg(1)
procFn, ok := map[string]urlProc{
"keys": keys,
"values": values,
"keypairs": keyPairs,
"domains": domains,
"domain": domains,
"paths": paths,
"path": paths,
"format": format,
}[mode]
if !ok {
fmt.Fprintf(os.Stderr, "unknown mode: %s\n", mode)
return
}
sc := bufio.NewScanner(os.Stdin)
seen := make(map[string]bool)
for sc.Scan() {
u, err := parseURL(sc.Text())
if err != nil {
if verbose {
fmt.Fprintf(os.Stderr, "parse failure: %s\n", err)
}
continue
}
// some urlProc functions return multiple things,
// so it's just easier to always get a slice and
// loop over it instead of having two kinds of
// urlProc functions.
for _, val := range procFn(u, fmtStr) {
// you do see empty values sometimes
if val == "" {
continue
}
if seen[val] && unique {
continue
}
fmt.Println(val)
// no point using up memory if we're outputting dupes
if unique {
seen[val] = true
}
}
}
if err := sc.Err(); err != nil {
fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err)
}
}
// parseURL parses a string as a URL and returns a *url.URL
// or any error that occured. If the initially parsed URL
// has no scheme, http:// is prepended and the string is
// re-parsed
func parseURL(raw string) (*url.URL, error) {
u, err := url.Parse(raw)
if err != nil {
return nil, err
}
if u.Scheme == "" {
return url.Parse("http://" + raw)
}
return u, nil
}
// a urlProc is any function that accepts a URL and some
// kind of format string (which may not actually be used
// by some functions), and returns a slice of strings
// derived from that URL. It's not uncommon for a urlProc
// function to return a slice of length 1, but the return
// type remains a slice because *some* functions need to
// return multiple strings; e.g. the keys function.
type urlProc func(*url.URL, string) []string
// keys returns all of the keys used in the query string
// portion of the URL. E.g. for /?one=1&two=2&three=3 it
// will return []string{"one", "two", "three"}
func keys(u *url.URL, _ string) []string {
out := make([]string, 0)
for key, _ := range u.Query() {
out = append(out, key)
}
return out
}
// values returns all of the values in the query string
// portion of the URL. E.g. for /?one=1&two=2&three=3 it
// will return []string{"1", "2", "3"}
func values(u *url.URL, _ string) []string {
out := make([]string, 0)
for _, vals := range u.Query() {
for _, val := range vals {
out = append(out, val)
}
}
return out
}
// keyPairs returns all the key=value pairs in
// the query string portion of the URL. E.g for
// /?one=1&two=2&three=3 it will return
// []string{"one=1", "two=2", "three=3"}
func keyPairs(u *url.URL, _ string) []string {
out := make([]string, 0)
for key, vals := range u.Query() {
for _, val := range vals {
out = append(out, fmt.Sprintf("%s=%s", key, val))
}
}
return out
}
// domains returns the domain portion of the URL. e.g.
// for http://sub.example.com/path it will return
// []string{"sub.example.com"}
func domains(u *url.URL, f string) []string {
return format(u, "%d")
}
// domains returns the path portion of the URL. e.g.
// for http://sub.example.com/path it will return
// []string{"/path"}
func paths(u *url.URL, f string) []string {
return format(u, "%p")
}
// format is a little bit like a special sprintf for
// URLs; it will return a single formatted string
// based on the URL and the format string. e.g. for
// http://example.com/path and format string "%d%p"
// it will return example.com/path
func format(u *url.URL, f string) []string {
out := &bytes.Buffer{}
inFormat := false
for _, r := range f {
if r == '%' && !inFormat {
inFormat = true
continue
}
if !inFormat {
out.WriteRune(r)
continue
}
switch r {
// a literal percent rune
case '%':
out.WriteRune('%')
// the scheme; e.g. http
case 's':
out.WriteString(u.Scheme)
// the userinfo; e.g. user:pass
case 'u':
if u.User != nil {
out.WriteString(u.User.String())
}
// the domain; e.g. sub.example.com
case 'd':
out.WriteString(u.Hostname())
// the port; e.g. 8080
case 'P':
out.WriteString(u.Port())
// the subdomain; e.g. www
case 'S':
out.WriteString(extractFromDomain(u, "subdomain"))
// the root; e.g. example
case 'r':
out.WriteString(extractFromDomain(u, "root"))
// the tld; e.g. com
case 't':
out.WriteString(extractFromDomain(u, "tld"))
// the path; e.g. /users
case 'p':
out.WriteString(u.EscapedPath())
// the query string; e.g. one=1&two=2
case 'q':
out.WriteString(u.RawQuery)
// the fragment / hash value; e.g. section-1
case 'f':
out.WriteString(u.Fragment)
// an @ if user info is specified
case '@':
if u.User != nil {
out.WriteRune('@')
}
// a colon if a port is specified
case ':':
if u.Port() != "" {
out.WriteRune(':')
}
// a question mark if there's a query string
case '?':
if u.RawQuery != "" {
out.WriteRune('?')
}
// a hash if there is a fragment
case '#':
if u.Fragment != "" {
out.WriteRune('#')
}
// the authority; e.g. user:[email protected]:8080
case 'a':
out.WriteString(format(u, "%u%@%d%:%P")[0])
// default to literal
default:
// output untouched
out.WriteRune('%')
out.WriteRune(r)
}
inFormat = false
}
return []string{out.String()}
}
func extractFromDomain(u *url.URL, selection string) string {
cache := os.TempDir() + "/tld.cache"
extract, err := tldomains.New(cache)
if err != nil {
fmt.Fprintf(os.Stderr, "failed initialize tldomains: %s\n", err)
}
// remove the port before parsing
portRe := regexp.MustCompile(`(?m):\d+$`)
host := extract.Parse(portRe.ReplaceAllString(u.Host, ""))
switch selection {
case "subdomain":
return host.Subdomain
case "root":
return host.Root
case "tld":
return host.Suffix
default:
return ""
}
}
func init() {
flag.Usage = func() {
h := "Format URLs provided on stdin\n\n"
h += "Usage:\n"
h += " unfurl [OPTIONS] [MODE] [FORMATSTRING]\n\n"
h += "Options:\n"
h += " -u, --unique Only output unique values\n"
h += " -v, --verbose Verbose mode (output URL parse errors)\n\n"
h += "Modes:\n"
h += " keys Keys from the query string (one per line)\n"
h += " values Values from the query string (one per line)\n"
h += " keypairs Key=value pairs from the query string (one per line)\n"
h += " domains The hostname (e.g. sub.example.com)\n"
h += " paths The request path (e.g. /users)\n"
h += " format Specify a custom format (see below)\n\n"
h += "Format Directives:\n"
h += " %% A literal percent character\n"
h += " %s The request scheme (e.g. https)\n"
h += " %u The user info (e.g. user:pass)\n"
h += " %d The domain (e.g. sub.example.com)\n"
h += " %S The subdomain (e.g. sub)\n"
h += " %r The root of domain (e.g. example)\n"
h += " %t The TLD (e.g. com)\n"
h += " %P The port (e.g. 8080)\n"
h += " %p The path (e.g. /users)\n"
h += " %q The raw query string (e.g. a=1&b=2)\n"
h += " %f The page fragment (e.g. page-section)\n"
h += " %@ Inserts an @ if user info is specified\n"
h += " %: Inserts a colon if a port is specified\n"
h += " %? Inserts a question mark if a query string exists\n"
h += " %# Inserts a hash if a fragment exists\n"
h += " %a Authority (alias for %u%@%d%:%P)\n\n"
h += "Examples:\n"
h += " cat urls.txt | unfurl keys\n"
h += " cat urls.txt | unfurl format %s://%d%p?%q\n"
fmt.Fprint(os.Stderr, h)
}
}