forked from ameenmaali/wordlistgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (82 loc) · 2.23 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
package main
import (
"bufio"
"flag"
"fmt"
"net/url"
"os"
"strings"
)
type cliOptions struct {
IncludeQsValues bool
FilterQs bool
}
var options cliOptions
func main() {
flag.BoolVar(&options.IncludeQsValues, "qv", false, "If enabled, include query string values (e.g. if enabled /?q=123 - 123 would be included in results")
flag.BoolVar(&options.FilterQs, "fq", false, "If enabled, filter out query strings (e.g. if enabled /?q=123 - q would NOT be included in results")
flag.Parse()
allComponents := make(map[string]bool)
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
host := strings.ToLower(sc.Text())
components := getUrlComponents(host)
for _, component := range components {
if allComponents[component] {
continue
}
allComponents[component] = true
fmt.Println(component)
}
}
if err := sc.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Cannot read input: %s\n", err)
}
}
func getUrlComponents(host string) []string {
var components []string
if !strings.HasPrefix(host, "/") && !strings.HasPrefix(host, "http") {
host = "http://" + host
}
u, err := url.Parse(host)
// If URL can't be parsed, ignore and move on
if err != nil {
return components
}
path := u.Path
pathFragments := strings.Split(path, "/")
// Remove first item from the slice as it will be blank
if len(pathFragments) > 0 {
pathFragments = pathFragments[1:]
}
// If query strings can't be parsed, set query strings as empty
queryStrings, err := url.ParseQuery(u.RawQuery)
if err != nil {
queryStrings = nil
}
domain := u.Host
domainFragments := strings.Split(domain, ".")
// Remove last item from the slice as it will be the extension (.com, .net, .etc)
if len(domainFragments) > 0 {
domainFragments = domainFragments[:len(domainFragments)-1]
}
for _, fragment := range domainFragments {
components = append(components, fragment)
}
for _, pathFragment := range pathFragments {
components = append(components, pathFragment)
}
if queryStrings != nil {
for qs, values := range queryStrings {
if !options.FilterQs {
components = append(components, qs)
}
if options.IncludeQsValues {
for _, val := range values {
components = append(components, val)
}
}
}
}
return components
}