-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
214 lines (173 loc) · 4.64 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
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
package main
import (
"bufio"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/michael1026/sessionManager"
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/projectdiscovery/fastdialer/fastdialer"
)
var (
urlMap cmap.ConcurrentMap[string, bool]
jsonResults cmap.ConcurrentMap[string, string]
client *http.Client
threads int
)
type CookieInfo map[string]string
type Response struct {
*http.Response
url string
err error
}
type Request struct {
*http.Request
url string
}
func AddAndPrintIfUnique(urlMap cmap.ConcurrentMap[string, bool], key string, url string, contentType string) {
if _, ok := urlMap.Get(key); !ok {
fmt.Println(url)
urlMap.Set(key, true)
jsonResults.Set(url, contentType)
}
}
func buildHttpClient(jar *cookiejar.Jar) (c *http.Client) {
fastdialerOpts := fastdialer.DefaultOptions
fastdialerOpts.EnableFallback = true
dialer, err := fastdialer.NewDialer(fastdialerOpts)
if err != nil {
log.Fatal("Error building HTTP client")
return nil
}
transport := &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
MaxConnsPerHost: 100,
IdleConnTimeout: time.Second * 10,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
Renegotiation: tls.RenegotiateOnceAsClient,
},
DisableKeepAlives: false,
DialContext: dialer.Dial,
}
re := func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
client := &http.Client{
Transport: transport,
CheckRedirect: re,
Timeout: time.Second * 5,
Jar: jar,
}
return client
}
func main() {
urlMap = cmap.New[bool]()
cookieFile := flag.String("C", "", "File containing cookie")
flag.IntVar(&threads, "t", 5, "Number of concurrent threads")
outputJson := flag.String("json", "", "Output as json")
jsonResults = cmap.New[string]()
flag.Parse()
jar := sessionManager.ReadCookieJson(*cookieFile)
urls := []string{}
client = buildHttpClient(jar)
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
urls = append(urls, s.Text())
}
reqChan := make(chan Request)
done := make(chan bool)
go producer(urls, reqChan)
for i := 0; i < threads; i++ {
go consumer(reqChan, done)
}
<-done
if *outputJson != "" {
jsonFile, err := json.Marshal(jsonResults)
if err != nil {
fmt.Printf("Error marshalling JSON: %s\n", err)
return
}
err = ioutil.WriteFile(*outputJson, jsonFile, 0644)
if err != nil {
fmt.Printf("Error writing JSON to file: %s\n", err)
}
}
}
func printUniqueContentURLs(resp http.Response, rawUrl string) {
if resp.StatusCode == http.StatusOK {
resource := ""
if len(resp.Header.Get("content-type")) >= 9 && resp.Header.Get("content-type")[:9] == "text/html" {
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return
}
doc.Find("script[src]").Each(func(index int, item *goquery.Selection) {
src, _ := item.Attr("src")
srcurl, err := url.Parse(src)
if err != nil {
return
}
srcurl.RawQuery = ""
resource += srcurl.String()
})
AddAndPrintIfUnique(urlMap, resource, rawUrl, "text/html")
} else if len(resp.Header.Get("content-type")) >= 16 && resp.Header.Get("content-type")[:16] == "application/json" {
var resultMap map[string]interface{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal([]byte(body), &resultMap)
if err != nil {
return
}
resource = mapKeysToString(resultMap)
AddAndPrintIfUnique(urlMap, resource, rawUrl, "application/json")
}
}
}
func mapKeysToString(jsonMap map[string]interface{}) string {
finalString := ""
for k := range jsonMap {
finalString += k
}
return finalString
}
func producer(urls []string, reqChan chan Request) {
defer close(reqChan)
for _, url := range urls {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
continue
}
req.Close = true
req.Header.Add("Connection", "close")
req.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0")
req.Header.Add("Accept-Language", "en-US,en;q=0.9")
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
reqChan <- Request{req, url}
}
}
func consumer(reqChan chan Request, done chan bool) {
for req := range reqChan {
if req.Request != nil {
resp, err := client.Do(req.Request)
r := Response{resp, req.url, err}
if r.Response != nil {
printUniqueContentURLs(*r.Response, r.url)
}
}
}
done <- true
}