-
Notifications
You must be signed in to change notification settings - Fork 9
/
godeclutter.go
205 lines (169 loc) · 5.53 KB
/
godeclutter.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
package main
import (
"bufio"
"flag"
"fmt"
"net"
"net/url"
"os"
"strings"
"github.com/PuerkitoBio/purell"
)
// var strFlag = flag.String("long-string", "", "Description")
var preferHttpsFlag = flag.Bool("p", false, "Prefer HTTPS - If there's a https url present, don't print the http for it. (since it will probably just redirect to https)")
var normalizeURLFlag = flag.Bool("c", true, "Clean URLs - Aggressively clean/normalize URLs before outputting them.")
var blacklistExtensionsFlag = flag.Bool("be", true, "Blacklist Extensions - clean some uninteresting extensions.")
var customBlacklistExtensionsFlag = flag.String("bec", "", "Blacklist Extensions - Specify additional extensions separated by commas to be cleared along the default ones.")
var blacklistWordsFlag = flag.Bool("bw", true, "Blacklist Words - clean some uninteresting words.")
var blacklistedPresetFlag = flag.String("bwl", "minimal", "Blacklist Words - Defines the level of word blocking. Values can be: minimal,aggressive")
var customBlacklistWordsFlag = flag.String("bwc", "", "Blacklist Words - Specify additional words separated by commas to be cleared along the default ones.")
var blacklistedExtensions = []string{"css", "scss", "png", "jpg", "jpeg", "img", "svg", "ico", "webp", "webm", "tif", "ttf", "tiff", "otf", "woff", "woff2", "gif", "pdf", "bmp", "eot", "mp3", "mp4", "m4a", "m4p", "avi", "flv", "swf", "eot"}
func iterInput(c chan string) {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
c <- scanner.Text()
}
close(c)
}
func removeDuplicateStr(strSlice []string) []string {
allKeys := make(map[string]bool)
list := []string{}
for _, item := range strSlice {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}
func stringInSlice(a string, list []string) (int, bool) {
for i, b := range list {
if b == a {
return i, true
}
}
return 0, false
}
func remove(s []string, i int) []string {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
func normalizeURL(url string) string {
normalized := purell.MustNormalizeURLString(url, purell.FlagLowercaseScheme|purell.FlagLowercaseHost|purell.FlagUppercaseEscapes|purell.FlagDecodeUnnecessaryEscapes|purell.FlagEncodeNecessaryEscapes|purell.FlagRemoveDefaultPort|purell.FlagRemoveEmptyQuerySeparator|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagSortQuery)
return normalized
}
func main() {
flag.Parse()
var blacklistedWords []string
if *blacklistedPresetFlag == "aggressive" {
blacklistedWords = []string{"/node_modules/", "wp-includes", "jquery", "/bootstrap", "/webpack-runtime", "accessChatPublic"}
} else if *blacklistedPresetFlag == "minimal" {
blacklistedWords = []string{"/node_modules/", "/wp-includes/", "/jquery", "/webpack-runtime", "accessChatPublic"}
}
if *customBlacklistWordsFlag != "" {
additionalWords := strings.Split(*customBlacklistWordsFlag, ",")
for _, word := range additionalWords {
blacklistedWords = append(blacklistedWords, word)
}
}
if *customBlacklistExtensionsFlag != "" {
additionalExtensions := strings.Split(*customBlacklistExtensionsFlag, ",")
for _, extension := range additionalExtensions {
blacklistedExtensions = append(blacklistedExtensions, extension)
}
}
var c chan string = make(chan string)
go iterInput(c)
var processedUrls []string
processedUrlMap := make(map[string]string)
for line := range c {
u, err := url.ParseRequestURI(line)
if err != nil {
continue
}
// Remove http-on-https false-positives
if u.Scheme == "http" && u.Port() == "443" {
continue
}
if u.Scheme == "https" && u.Port() == "80" {
continue
}
// Escape redundant port syntax (for default ports)
if strings.Contains(u.Host, ":") {
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
continue
}
if port == "443" && u.Scheme == "https" {
u.Host = host
} else if port == "80" && u.Scheme == "http" {
u.Host = host
} else if port == "" {
fmt.Print(host)
u.Host = host
} else {
u.Host = host + ":" + port
}
}
// Prefer https
if *preferHttpsFlag {
if u.Scheme == "https" {
scheme, hostname_found := processedUrlMap[u.Host]
if hostname_found {
if scheme == "http" {
check_u, _ := url.Parse(u.String())
check_u.Scheme = "http"
found_index, found := stringInSlice(check_u.String(), processedUrls)
if found {
processedUrls = remove(processedUrls, found_index)
}
processedUrlMap[u.Host] = u.Scheme
}
} else {
processedUrlMap[u.Host] = u.Scheme
}
} else if u.Scheme == "http" {
_, hostname_found := processedUrlMap[u.Host]
if hostname_found {
continue
} else {
processedUrlMap[u.Host] = u.Scheme
}
}
}
if *blacklistExtensionsFlag {
foundBlacklistedExtension := false
for _, ext := range blacklistedExtensions {
if strings.HasSuffix(u.Path, ext) {
foundBlacklistedExtension = true
break
}
}
if foundBlacklistedExtension {
continue
}
}
if *blacklistWordsFlag {
foundBlacklistedWord := false
for _, word := range blacklistedWords {
if strings.Contains(u.Path, word) {
foundBlacklistedWord = true
break
}
}
if foundBlacklistedWord {
continue
}
}
if *normalizeURLFlag {
u_str := normalizeURL(u.String())
processedUrls = append(processedUrls, u_str)
} else {
processedUrls = append(processedUrls, u.String())
}
}
filteredProcessedUrls := removeDuplicateStr(processedUrls)
for _, url := range filteredProcessedUrls {
fmt.Println(url)
}
}