-
Notifications
You must be signed in to change notification settings - Fork 124
/
main.go
477 lines (402 loc) · 11.7 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"net/url"
"os"
"regexp"
"strings"
parser "github.com/Cgboal/DomainParser"
)
var extractor parser.Parser
func init() {
extractor = parser.NewDomainParser()
}
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,
"apex": apexes,
"apexes": apexes,
"json": jsonFormat,
"format": format,
}[mode]
if !ok {
fmt.Fprintf(os.Stderr, "unknown mode: %s\n", mode)
flag.Usage()
return
}
sc := bufio.NewScanner(os.Stdin)
buf := make([]byte, 0, 64*1024)
sc.Buffer(buf, 1024*1024)
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
type UrlStruct struct {
Scheme string `json:"scheme"`
Opaque string `json:"opaque"` // encoded opaque data scheme:opaque[?query][#fragment]
User string `json:"user"` // username and password information
Host string `json:"host"` // host or host:port [scheme:][//[userinfo@]host][/]path[?query][#fragment]
Path string `json:"path"` // path (relative paths may omit leading slash)
RawPath string `json:"raw_path"` // encoded path hint (see EscapedPath method)
RawQuery string `json:"raw_query"` // encoded query values, without '?'
Fragment string `json:"fragment"` // fragment for references, without '#'
Parameters []KeyValue `json:"parameters"` //
Url string `json:"url"` //
Domain string `json:"domain"` // The domain (e.g. sub.example.com)\n"
Subdomain string `json:"subdomain"` // The subdomain (e.g. sub)\n"
Root string `json:"root"` // The root of domain (e.g. example)\n"
TLD string `json:"tld"` // The TLD (e.g. com)\n"
Apex string `json:"apex"` //apex domain test.google.co.uk google.co.uk
Port string `json:"port"` // The port (e.g. 8080)\n"
PathExtension string `json:"extension"` // The path's file extension (e.g. jpg, html)\n"
}
type KeyValue struct {
Key string `json:"key"`
Value string `json:"value"`
}
func jsonFormat(u *url.URL, _ string) []string {
parameters := make([]KeyValue, 0)
for key, vals := range u.Query() {
for _, val := range vals {
parameters = append(parameters, KeyValue{Key: key, Value: val})
}
}
extractApexs := format(u, "%r.%t")
apex := ""
if len(extractApexs) == 1 {
apex = extractApexs[0]
}
domain := ""
extractDomains := format(u, "%d")
if len(extractDomains) == 1 {
domain = extractDomains[0]
}
subdomain := ""
extractSubdomains := format(u, "%S")
if len(extractSubdomains) == 1 {
subdomain = extractSubdomains[0]
}
root := ""
extractRoots := format(u, "%r")
if len(extractRoots) == 1 {
root = extractRoots[0]
}
tld := ""
extractTLDs := format(u, "%t")
if len(extractTLDs) == 1 {
tld = extractTLDs[0]
}
port := ""
extractPorts := format(u, "%P")
if len(extractPorts) == 1 {
port = extractPorts[0]
}
extension := ""
extractExtensions := format(u, "%e")
if len(extractExtensions) == 1 {
extension = extractExtensions[0]
}
newstructure := UrlStruct{
Scheme: u.Scheme,
Opaque: u.Opaque,
User: u.User.String(),
Host: u.Host,
Path: u.Path,
RawPath: u.RawPath,
RawQuery: u.RawQuery,
Fragment: u.Fragment,
Parameters: parameters,
Apex: apex,
Url: u.String(),
Domain: domain,
Subdomain: subdomain,
Root: root,
TLD: tld,
Port: port,
PathExtension: extension,
}
outBytes, err := json.Marshal(newstructure)
if err == nil {
out := bytes.NewBuffer(outBytes).String()
return []string{out}
}
return []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")
}
// apexes return the apex portion of the URL. e.g.
// for http://sub.example.com/path it will return
// []string{"example.com"}
func apexes(u *url.URL, f string) []string {
return format(u, "%r.%t")
}
// paths 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 paths's file extension
case 'e':
paths := strings.Split(u.EscapedPath(), "/")
if len(paths) > 1 {
parts := strings.Split(paths[len(paths)-1], ".")
if len(parts) > 1 {
out.WriteString(parts[len(parts)-1])
}
} else {
parts := strings.Split(u.EscapedPath(), ".")
if len(parts) > 1 {
out.WriteString(parts[len(parts)-1])
}
}
// 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:pass@example.com: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 {
// remove the port before parsing
portRe := regexp.MustCompile(`(?m):\d+$`)
domain := portRe.ReplaceAllString(u.Host, "")
switch selection {
case "subdomain":
return extractor.GetSubdomain(domain)
case "root":
return extractor.GetDomain(domain)
case "tld":
return extractor.GetTld(domain)
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 += " apexes The apex domain (e.g. example.com from sub.example.com)\n"
h += " json JSON encoded url/format objects\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 += " %e The path's file extension (e.g. jpg, html)\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)
}
}