-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdsieve.go
293 lines (264 loc) · 6.93 KB
/
dsieve.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
package main
import (
"bufio"
"flag"
"fmt"
"net/url"
"os"
"sort"
"strconv"
"strings"
"golang.org/x/net/publicsuffix"
)
var (
inputUrl *string
inputFilePath *string
filterLevel *string
outputFilePath *string
filterTLD *bool
top *int
topDomainsPerLevel = make(map[int]*map[string]int)
topDomainsPerLevelFiltered = make(map[int][]string)
)
func fail(text string) {
fmt.Fprintln(os.Stderr, text)
os.Exit(1)
}
func check(err error) {
if err != nil {
fail(err.Error())
}
}
func parseFilter(filter string) (int, int) {
if filter == "" {
return -1, -1
}
vMin := -1
vMax := -1
var err error
minMax := strings.Split(filter, ":")
if len(minMax) == 1 {
vMin, err = strconv.Atoi(minMax[0])
vMax = vMin
check(err)
} else if len(minMax) == 2 {
if minMax[0] != "" {
vMin, err = strconv.Atoi(minMax[0])
check(err)
}
if minMax[1] != "" {
vMax, err = strconv.Atoi(minMax[1])
check(err)
}
} else {
fail("Invalid filter value: " + filter)
}
return vMin, vMax
}
func writeResults(domains *[]string) {
_, err := os.Create(*outputFilePath)
check(err)
if len(*domains) > 0 {
file, _ := os.OpenFile(*outputFilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
defer file.Close()
writer := bufio.NewWriter(file)
for _, domain := range *domains {
_, _ = fmt.Fprintln(writer, domain)
}
_ = writer.Flush()
}
}
func parseUrl(rawUrl string, lMin, lMax int) []string {
domains := make([]string, 0)
if !strings.HasPrefix(rawUrl, "http") {
rawUrl = "http://" + rawUrl
}
u, err := url.Parse(rawUrl)
if err != nil {
return domains
}
domainLevels := strings.Split(u.Host, ".")
suffixes := make([]string, 0)
eTLD, icann := publicsuffix.PublicSuffix(u.Host)
if icann {
if eTLD != u.Host {
suffixes = append(suffixes, eTLD)
}
}
if len(suffixes) > 0 {
sort.Slice(suffixes, func(i, j int) bool {
return len(suffixes[i]) > len(suffixes[j])
})
tld := suffixes[0]
tldLength := strings.Count(tld, ".") + 1
domainLevels = domainLevels[:len(domainLevels)-tldLength]
domainLevels = append(domainLevels, tld)
}
if lMin <= len(domainLevels) {
if lMax == -1 || lMax > len(domainLevels) {
lMax = len(domainLevels)
}
for i := lMax; i > 0 && i >= lMin; i-- {
domain := strings.Join(domainLevels[len(domainLevels)-i:], ".")
if domain != "" {
domains = append(domains, domain)
if *top > 0 {
if topDomainsPerLevel[i] == nil {
levelMap := make(map[string]int)
topDomainsPerLevel[i] = &levelMap
}
levelMap := topDomainsPerLevel[i]
(*levelMap)[domain] = (*levelMap)[domain] + 1
}
}
}
}
return domains
}
func main() {
inputUrl = flag.String("i", "", "Input url or domain")
inputFilePath = flag.String("if", "", "Input file path, one url/domain per line.")
filterLevel = flag.String("f", "", "Filter domain level. "+
"Use python slice notation to select range. \nExample input: foo.bar.baz.tld \n"+
" \033[3m-f 3 \033[0m bar.baz.tld \n"+
" \033[3m-f 3: \033[0m bar.baz.tld, foo.bar.baz.tld\n"+
" \033[3m-f 2:4\033[0m baz.tld, bar.baz.tld\n"+
" \033[3m-f :3 \033[0m tld, baz.tld")
outputFilePath = flag.String("o", "", "Output file path, optional")
//filterTLD = flag.Bool("t", true, "Filter invalid domains according to Mozilla's publicsuffix list.")
top = flag.Int("top", 0, "Only consider top X subdomains of a certain level and return all their subdomains")
flag.Parse()
// set filterTLD to true by default while removing the flag
t := true
filterTLD = &t
inputUrls := make([]string, 0)
if *inputUrl != "" {
inputUrls = append(inputUrls, *inputUrl)
}
if *inputFilePath != "" {
inputFile, err := os.Open(*inputFilePath)
check(err)
defer inputFile.Close()
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
inputUrls = append(inputUrls, scanner.Text())
}
}
// TODO: Consider program main execution to be in goroutine.
if *inputUrl == "" && *inputFilePath == "" {
// Let's get input from Stdin
// Check for stdin input
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
fmt.Fprintln(os.Stderr, "No domains detected. Hint: cat domains.txt | dsieve -f 2")
os.Exit(1)
}
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
inputUrls = append(inputUrls, sc.Text())
}
}
// Leaving this in, for incase all/any input process gives un Zero URLs.
if len(inputUrls) == 0 {
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "\nError: No input.")
os.Exit(1)
}
lMin, lMax := parseFilter(*filterLevel)
domainMap := make(map[string]bool)
domains := make([]string, 0)
for _, inputURL := range inputUrls {
for _, domain := range parseUrl(inputURL, lMin, lMax) {
if _, dup := domainMap[domain]; !dup {
eTLD, icann := publicsuffix.PublicSuffix(domain)
if *filterTLD {
if icann {
if eTLD != domain {
if *top == 0 {
fmt.Println(domain)
}
domainMap[domain] = true
domains = append(domains, domain)
}
}
} else {
if *top == 0 {
fmt.Println(domain)
}
domainMap[domain] = true
domains = append(domains, domain)
}
}
}
}
if *top > 0 {
if lMax < 0 {
for lvl := range topDomainsPerLevel {
if lvl > lMax {
lMax = lvl
}
}
}
for i := lMax; i > 0 && i >= lMin; i-- {
domainsForLevel := make([]string, 0)
levelMap := topDomainsPerLevel[i]
if levelMap == nil {
continue
}
for _, domain := range domains {
if _, ok := (*levelMap)[domain]; ok {
domainsForLevel = append(domainsForLevel, domain)
}
}
if len(domainsForLevel) > 0 {
sort.Slice(domainsForLevel, func(i, j int) bool {
return (*levelMap)[domainsForLevel[i]] > (*levelMap)[domainsForLevel[j]]
})
if len(domainsForLevel) >= *top {
domainsForLevel = domainsForLevel[:*top]
}
topDomainsPerLevelFiltered[i] = domainsForLevel
}
}
maxLevel := 0
for level := range topDomainsPerLevelFiltered {
if level > maxLevel {
maxLevel = level
}
}
if strings.Contains(*filterLevel, ":") {
if strings.HasSuffix(*filterLevel, ":") {
lvl, err := strconv.Atoi(strings.TrimSuffix(*filterLevel, ":"))
if err != nil {
check(err)
}
maxLevel = lvl
} else {
split := strings.Split(*filterLevel, ":")
lvl, err := strconv.Atoi(split[len(split)-1])
if err != nil {
check(err)
}
maxLevel = lvl - 1
}
filteredDomains := make([]string, 0)
for _, inputURL := range inputUrls {
for _, d := range topDomainsPerLevelFiltered[maxLevel] {
if strings.HasSuffix(inputURL, d) {
filteredDomains = append(filteredDomains, inputURL)
fmt.Println(inputURL)
}
}
}
domains = filteredDomains
} else {
for _, d := range topDomainsPerLevelFiltered[maxLevel] {
fmt.Println(d)
}
domains = topDomainsPerLevelFiltered[maxLevel]
}
}
if *outputFilePath != "" && len(domains) > 0 {
writeResults(&domains)
}
}