-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathproxy.go
More file actions
205 lines (167 loc) · 4.09 KB
/
Copy pathproxy.go
File metadata and controls
205 lines (167 loc) · 4.09 KB
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"
"context"
"crypto/tls"
"errors"
"io"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"os"
"sync"
"time"
"github.com/cheggaaa/pb"
"golang.org/x/net/proxy"
)
const timeout = 5 * time.Second
// proxiedRequest makes a request to an endpoint using a proxy.
func proxiedRequest(proxyURL string, endpoint string) (*result, error) {
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
var start, connect, tlsHandshake time.Time
res := &result{
Proxy: proxyURL,
Endpoint: endpoint,
}
// Measure response times
trace := &httptrace.ClientTrace{
TLSHandshakeStart: func() { tlsHandshake = time.Now() },
TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
res.Latency.TLSHandshake = time.Since(tlsHandshake).Nanoseconds() / 1000000 //nolint:gomnd
},
ConnectStart: func(network, addr string) { connect = time.Now() },
ConnectDone: func(network, addr string, err error) {
res.Latency.Connect = time.Since(connect).Nanoseconds() / 1000000 //nolint:gomnd
},
GotFirstResponseByte: func() {
res.Latency.TTFB = time.Since(start).Nanoseconds() / 1000000 //nolint:gomnd
},
}
// Create a new request with the trace context
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
start = time.Now()
p, err := url.Parse(proxyURL)
if err != nil {
return nil, err
}
tr := http.Transport{
TLSHandshakeTimeout: timeout,
ResponseHeaderTimeout: timeout,
ExpectContinueTimeout: timeout,
DisableKeepAlives: true,
MaxConnsPerHost: 0,
}
// Create a client based on the proxy scheme
switch p.Scheme {
case "http", "https":
// HTTP/HTTPS proxy
tr.Proxy = http.ProxyURL(p)
case "socks5", "socks5h":
// SOCKS5 proxy
dialer, err := proxy.FromURL(p, proxy.Direct)
if err != nil {
return nil, err
}
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}
default:
return nil, errors.New("unsupported proxy scheme: " + p.Scheme)
}
r, err := tr.RoundTrip(req)
if err != nil {
res.StatusCode = -1
return res, err
}
defer r.Body.Close()
res.StatusCode = r.StatusCode
// Read response body
if includeResponseBody && r.Body != nil {
b, err := io.ReadAll(r.Body)
if err != nil {
res.ResponseBody = "ERROR PARSING RESPONSE"
} else {
res.ResponseBody = string(b)
}
}
return res, nil
}
// testProxies tests a list of proxies.
func testProxies(proxies []string) {
// Start the progress bar
if output == "plaintext" {
bar = pb.StartNew(len(proxies))
}
proxiesCh := make(chan string, maxThreads)
resultsCh := make(chan *result)
done := make(chan struct{})
var wg sync.WaitGroup
for w := 1; w <= maxThreads; w++ {
// Run concurrent workers
wg.Add(1)
go func(proxies chan string, results chan *result) {
defer wg.Done()
for proxy := range proxies {
time.Sleep(time.Duration(delay) * time.Millisecond)
res, err := proxiedRequest(proxy, testURL)
if res == nil {
res = &result{}
}
if err != nil {
res.Err = err
}
results <- res
}
}(proxiesCh, resultsCh)
}
go func(resultsCh chan *result) {
for res := range resultsCh {
results = append(results, res)
if output == "plaintext" {
bar.Increment()
}
}
// Stop the progress bar
if output == "plaintext" {
bar.Finish()
}
// Display the report
displayReport(results)
done <- struct{}{}
}(resultsCh)
for _, proxy := range proxies {
proxiesCh <- proxy
}
close(proxiesCh)
wg.Wait()
close(resultsCh)
<-done
}
// testProxiesFromFile tests proxies from a file.
func testProxiesFromFile(fileName string) {
// Read proxy file
file, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// Validate format
_, err := url.Parse(scanner.Text())
if err != nil {
panic("Invalid proxy format: " + scanner.Text())
}
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
panic(err)
}
// Test proxies
testProxies(lines)
}