-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
237 lines (204 loc) · 7.16 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"math"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
)
type requestData struct {
client http.Client
cookie string
headers []string
headersFile string
headerFromFile string
cookieFromFile string
}
type duplicateFlags []string
func (dupes *duplicateFlags) String() string {
return ""
}
func (dupes *duplicateFlags) Set(value string) error {
*dupes = append(*dupes, strings.TrimSpace(value))
return nil
}
func getUserAgent() string {
userAgents := []string{
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:40.0) Gecko/20100101 Firefox/40.0",
"Mozilla/5.0 (iPad; CPU OS 8_1_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B466 Safari/600.1.4",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36",
"Mozilla/5.0 (iPad; CPU OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B440 Safari/600.1.4",
"Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; KFTT Build/IML74K) AppleWebKit/537.36 (KHTML, like Gecko) Silk/3.68 like Chrome/39.0.2171.93 Safari/537.36",
"Mozilla/5.0 (iPad; CPU OS 8_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12D508 Safari/600.1.4",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.94 Chrome/37.0.2062.94 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
"Mozilla/5.0 (iPad; CPU OS 8_4_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H321 Safari/600.1.4",
}
rand.Seed(time.Now().UnixNano())
index := rand.Intn(len(userAgents))
userAgent := userAgents[index]
return userAgent
}
func getCookieFromFile(file string) string {
cookie := ""
if file != "" {
f, err := os.Open(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
defer f.Close()
value, err := ioutil.ReadAll(f)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
cookie = string(value)
}
return cookie
}
func (r requestData) makeRequest(url string) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return
}
req.Header.Set("User-Agent", getUserAgent())
if r.cookie != "" {
req.Header.Set("Cookie", r.cookie)
}
if r.cookieFromFile != "" {
req.Header.Set("Cookie", r.cookieFromFile)
}
if len(r.headers) > 0 {
for _, header := range r.headers {
if strings.Contains(header, ":") {
//Split by first occurrence of colon(:), any other colons after that will be ignored
name, value := header[:strings.IndexByte(header, ':')], header[strings.IndexByte(header, ':')+1:]
req.Header.Set(strings.TrimSpace(name), strings.TrimSpace(value))
}
}
}
if r.headerFromFile != "" {
if strings.Contains(r.headerFromFile, ":") {
//Split by first occurrence of colon(:), any other colons after that will be ignored
name, value := r.headerFromFile[:strings.IndexByte(r.headerFromFile, ':')], r.headerFromFile[strings.IndexByte(r.headerFromFile, ':')+1:]
req.Header.Set(strings.TrimSpace(name), strings.TrimSpace(value))
}
}
resp, err := r.client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return
}
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
}
func main() {
var (
concurrency int
proxy string
cookie string
cookieFile string
headers duplicateFlags
headersFile string
timeout int
)
flag.Var(&headers, "h", "specify header to include in request")
flag.IntVar(&concurrency, "c", 1, "set the concurrency")
flag.StringVar(&proxy, "p", "", "specify http proxy")
flag.StringVar(&cookie, "b", "", "specify cookie VALUE to include in request")
flag.StringVar(&cookieFile, "B", "", "specify file that contains the cookie VALUE to include in request")
flag.StringVar(&headersFile, "H", "", "specify list of headers. This sends each header 1 at a time to the same request")
flag.IntVar(&timeout, "t", 1000, "set the timeout in milliseconds")
flag.Parse()
sc := bufio.NewScanner(os.Stdin)
proxyUrl, err := url.Parse(proxy)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
to := time.Duration(timeout * 100000)
client := &http.Client{
Timeout: to,
Transport: &http.Transport{
MaxIdleConns: concurrency,
MaxIdleConnsPerHost: concurrency,
MaxConnsPerHost: int(math.Round(float64(concurrency) * 0.10)),
DisableKeepAlives: true,
DialContext: (&net.Dialer{
Timeout: to,
}).DialContext,
IdleConnTimeout: time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyURL(proxyUrl),
},
//Ignore redirects
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
req := requestData{
client: *client,
headers: headers,
headersFile: headersFile,
cookie: cookie,
cookieFromFile: getCookieFromFile(cookieFile),
}
//Create buffer(queue) and set the size to the concurrency value
queue := make(chan bool, concurrency)
defer close(queue)
//For each url that is read from stdin, we want to add it to the buffer(queue)
//If the concurrency value is 5, then 5 requests will be added to the buffer(queue)
//This limits the throughput to 5 requests. In otherwords only 5 urls at a time will
//be passed to the proxy tool
for sc.Scan() {
url := sc.Text()
//For example we set the concurrency value to 5, so the buffer(queue) has 5 values
//this means that the buffer(queue) is full and it will wait until a request
//has given up its position in the buffer(queue)
queue <- true
go func(url string) {
// 'defer func() { <-queue }()' will empty this request's position in the buffer(queue)
// and send a new request to the buffer(queue)
defer func() { <-queue }()
//Open headers file for reading, and resend the request but with the next header in line
if req.headersFile != "" {
file, err := os.Open(req.headersFile)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
defer file.Close()
sc := bufio.NewScanner(file)
//Resend the same url(request) but with the next header in the headers file(-H flag)
for sc.Scan() {
req.headerFromFile = sc.Text()
req.makeRequest(url)
}
if err := sc.Err(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
}
req.makeRequest(url)
}(url)
}
//flush queue
for i := 0; i < concurrency; i++ {
queue <- true
}
}