-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
114 lines (104 loc) · 2.66 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
package main
import (
"bufio"
"flag"
"fmt"
"net/url"
"os"
"path"
"sort"
"strings"
)
var (
urlMappings = make(map[string]map[string]map[string]string)
staticExtensions = map[string]struct{}{
"css": {}, "svg": {}, "png": {}, "mp3": {}, "jpg": {}, "pdf": {},
"woff2": {}, "bmp": {}, "ico": {}, "mp4": {}, "woff": {}, "jpeg": {},
"ttf": {}, "avi": {}, "webp": {}, "ppt": {}, "eot": {}, "otf": {}, "gif": {},
}
)
var parametersFlag = flag.Bool("params", false, "Only output URLs with parameters")
func parametersToNameSet(params string) map[string]struct{} {
res := make(map[string]struct{})
for _, pair := range strings.Split(params, "&") {
if strings.Contains(pair, "=") {
parts := strings.SplitN(pair, "=", 2)
key := parts[0]
if key != "" {
res[key] = struct{}{}
}
}
}
return res
}
func paramNamesToString(paramNames map[string]struct{}) string {
if len(paramNames) == 0 {
return ""
}
keys := make([]string, 0, len(paramNames))
for k := range paramNames {
keys = append(keys, k)
}
sort.Strings(keys)
return strings.Join(keys, "&")
}
func hasBadExtension(pathStr string) bool {
extension := strings.TrimPrefix(path.Ext(pathStr), ".")
if extension == "" {
return false
}
_, exists := staticExtensions[extension]
return exists
}
func isContentPath(pathStr string) bool {
for _, part := range strings.Split(pathStr, "/") {
if strings.Count(part, "-") > 3 {
return true
}
}
return false
}
func main() {
flag.Parse()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
parsed, err := url.Parse(line)
if err != nil {
continue
}
if parsed.Scheme == "" {
parsed.Scheme = "http"
}
host := parsed.Scheme + "://" + parsed.Host
if _, exists := urlMappings[host]; !exists {
urlMappings[host] = make(map[string]map[string]string)
}
pathStr := parsed.Path
if hasBadExtension(pathStr) || isContentPath(pathStr) {
continue
}
paramNames := parametersToNameSet(parsed.RawQuery)
paramNamesStr := paramNamesToString(paramNames)
if *parametersFlag && paramNamesStr == "" {
// Skip URLs without parameters when -params flag is set
continue
}
if _, exists := urlMappings[host][pathStr]; !exists {
urlMappings[host][pathStr] = make(map[string]string)
}
// Use paramNamesStr as key for deduplication
if _, exists := urlMappings[host][pathStr][paramNamesStr]; !exists {
// Store the full URL (including parameter values) for output
fullURL := parsed.String()
urlMappings[host][pathStr][paramNamesStr] = fullURL
}
}
for _, paths := range urlMappings {
for _, paramMap := range paths {
for _, fullURL := range paramMap {
fmt.Println(fullURL)
}
}
}
}