forked from trap-bytes/gourlex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gourlex.go
199 lines (179 loc) · 5.16 KB
/
gourlex.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
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"golang.org/x/net/html"
)
func main() {
var filePath, targetURL, cookie, customHeader, proxyFlag string
var urlOnly, pathOnly, silentMode bool
flag.StringVar(&filePath, "f", "", "Specify file containing URLs")
flag.StringVar(&targetURL, "t", "", "Specify a single target URL")
flag.StringVar(&cookie, "c", "", "Specify cookies")
flag.StringVar(&customHeader, "r", "", "Specify headers")
flag.StringVar(&proxyFlag, "p", "", "Specify the proxy URL")
flag.BoolVar(&urlOnly, "uO", false, "Extract only URLs")
flag.BoolVar(&pathOnly, "pO", false, "Extract only paths")
flag.BoolVar(&silentMode, "s", false, "Silent mode")
helpFlag := flag.Bool("h", false, "Display help")
flag.Parse()
if *helpFlag {
printHelp()
return
}
if !silentMode {
printBanner()
}
client := createHTTPClient(proxyFlag, silentMode)
if filePath != "" {
processFile(filePath, client, cookie, customHeader, urlOnly, pathOnly, silentMode)
} else if targetURL != "" {
processURL(targetURL, client, cookie, customHeader, urlOnly, pathOnly, silentMode)
} else {
fmt.Println("Error: No input provided. Use -f for a file or -t for a single URL.")
}
}
func processFile(filePath string, client *http.Client, cookie, customHeader string, urlOnly, pathOnly, silentMode bool) {
file, err := os.Open(filePath)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
processURL(scanner.Text(), client, cookie, customHeader, urlOnly, pathOnly, silentMode)
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading from file: %v\n", err)
}
}
func processURL(url string, client *http.Client, cookie, customHeader string, urlOnly, pathOnly, silentMode bool) {
if url == "" {
return
}
validUrl, err := validateUrl(url)
if err != nil {
fmt.Printf("Error validating URL: %v\n", err)
return
}
req, err := http.NewRequest("GET", validUrl, nil)
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
return
}
setupRequestHeaders(req, cookie, customHeader)
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error making HTTP request: %v\n", err)
return
}
defer resp.Body.Close()
urls, paths, err := extractURLsAndPaths(resp)
if err != nil {
fmt.Printf("Error extracting URLs and paths: %v\n", err)
return
}
printResults(urls, paths, urlOnly, pathOnly, silentMode)
}
func createHTTPClient(proxyFlag string, silentMode bool) *http.Client {
if proxyFlag != "" {
proxyURL, err := url.Parse(proxyFlag)
if err != nil {
fmt.Printf("Error parsing proxy URL: %v\n", err)
return &http.Client{}
}
if !silentMode {
fmt.Printf("Using proxy: %s\n", proxyFlag)
}
return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
return &http.Client{}
}
func setupRequestHeaders(req *http.Request, cookie, customHeader string) {
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
if cookie != "" {
req.Header.Set("Cookie", cookie)
}
if customHeader != "" {
parts := strings.SplitN(customHeader, ":", 2)
if len(parts) == 2 {
req.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
}
}
}
func extractURLsAndPaths(resp *http.Response) ([]string, []string, error) {
tokenizer := html.NewTokenizer(resp.Body)
var urls, paths []string
for {
tokenType := tokenizer.Next()
if tokenType == html.ErrorToken {
return urls, paths, nil
}
if tokenType == html.StartTagToken || tokenType == html.SelfClosingTagToken {
token := tokenizer.Token()
for _, attr := range token.Attr {
if attr.Key == "href" || attr.Key == "src" {
if u, err := url.Parse(attr.Val); err == nil && (u.Scheme == "http" || u.Scheme == "https") {
urls = append(urls, u.String())
} else {
paths = append(paths, attr.Val)
}
}
}
}
}
}
func printResults(urls, paths []string, urlOnly, pathOnly, silentMode bool) {
if !silentMode {
fmt.Printf("Extracted URLs from page:\n")
}
if !pathOnly {
for _, url := range urls {
fmt.Println(url)
}
}
if !urlOnly {
fmt.Println("\nPaths found on the page:")
for _, path := range paths {
fmt.Println(path)
}
}
}
func validateUrl(inputURL string) (string, error) {
u, err := url.Parse(inputURL)
if err != nil {
return "", err
}
if u.Scheme == "" {
u.Scheme = "https"
}
return u.String(), nil
}
func printHelp() {
fmt.Println("Usage: gourlex -f <file_path> or -t <target_url> [options]")
fmt.Println("Options:")
fmt.Println(" -f string Specify file containing URLs")
fmt.Println(" -t string Specify a single target URL")
fmt.Println(" -c string Specify cookies")
fmt.Println(" -r string Specify custom headers")
fmt.Println(" -p string Specify the proxy URL")
fmt.Println(" -uO Extract only URLs")
fmt.Println(" -pO Extract only paths")
fmt.Println(" -s Silent mode (suppress output)")
fmt.Println(" -h Display this help and exit")
}
func printBanner() {
fmt.Println("Gourlex - WebPage Urls Extractor Tool\n")
}