-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxypool.go
172 lines (154 loc) · 4.15 KB
/
proxypool.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
package proxypool
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"h12.io/socks"
)
// Proxy represents a single SOCKS 4/5 proxy
type Proxy struct {
URL string
Alive bool
protocol string
ip string
port int
lastRequest time.Time
dial func(string, string) (net.Conn, error)
transport *http.Transport
client *http.Client
}
// ProxyPool manages a group of proxies.
// RateLimit is how many seconds to wait between requests, per proxy.
// RetestDelay is after how many seconds should a proxy be retested if its is unavailable (manually call Pool.Test()).
type ProxyPool struct {
proxies []*Proxy
TestURL string
RateLimit int
RetestDelay int
AliveCount int
AvailableCount int
}
// Create checks if its v4 or v5, constructs the dial, transport, client and finally tests it.
func (proxy *Proxy) Create(testURL string, wg *sync.WaitGroup) {
socksVersions := [2]int{5, 4}
for _, version := range socksVersions {
proxy.URL = fmt.Sprintf("socks%d://%s:%d", version, proxy.ip, proxy.port)
proxy.dial = socks.Dial(proxy.URL + "?timeout=20s")
proxy.transport = &http.Transport{
Dial: proxy.dial,
DisableKeepAlives: true,
}
proxy.client = &http.Client{Transport: proxy.transport, Timeout: 20 * time.Second}
_, err := proxy.Get(testURL)
if err == nil {
break
}
}
wg.Done()
}
// Get fetchs an URL with the given proxy and returns the body text
func (proxy *Proxy) Get(url string) ([]byte, error) {
proxy.lastRequest = time.Now()
resp, err := proxy.client.Get(url)
if err != nil {
proxy.Alive = false
fmt.Println(err)
return nil, fmt.Errorf("HTTP request failed: %s", err)
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
proxy.Alive = false
return nil, fmt.Errorf("can't read response: %s", err)
}
if resp.StatusCode != http.StatusOK {
proxy.Alive = false
return nil, fmt.Errorf("test URL replied with HTTP code %d", resp.StatusCode)
}
proxy.Alive = true
return buf, nil
}
// GetAvailableProxy returns an available proxy from the pool.
func (pool *ProxyPool) GetAvailableProxy() (*Proxy, error) {
for i := range pool.proxies {
if time.Since(pool.proxies[i].lastRequest).Seconds() > float64(pool.RetestDelay) && !pool.proxies[i].Alive {
go pool.proxies[i].Get(pool.TestURL)
}
if time.Since(pool.proxies[i].lastRequest).Seconds() > float64(pool.RateLimit) && pool.proxies[i].Alive {
return pool.proxies[i], nil
}
}
return nil, errors.New("no proxies available")
}
// NewProxyPool constructs a new ProxyPool instance
func NewProxyPool(testURL string, rateLimit int, retestDelay int) *ProxyPool {
pool := &ProxyPool{TestURL: testURL, RateLimit: rateLimit, RetestDelay: retestDelay}
return pool
}
// LoadProxies loads the pool with SOCKS4/5 proxies from a text file
func (pool *ProxyPool) LoadProxies(path string) error {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var proxies []*Proxy
scanner := bufio.NewScanner(file)
var wg sync.WaitGroup
for scanner.Scan() {
line := strings.Split(scanner.Text(), ":")
ip := line[0]
port, _ := strconv.Atoi(line[1])
proxy := Proxy{ip: ip, port: port}
proxies = append(proxies, &proxy)
wg.Add(1)
go proxy.Create(pool.TestURL, &wg)
}
wg.Wait()
for _, p := range proxies {
if p.Alive {
pool.proxies = append(pool.proxies, p)
}
}
if err := scanner.Err(); err != nil {
return err
}
pool.UpdateCounts()
return nil
}
// Get waits until a proxy from the pool is available and then fetchs the given URL.
func (pool *ProxyPool) Get(url string) []byte {
for {
proxy, err := pool.GetAvailableProxy()
if err == nil {
r, reqErr := proxy.Get(url)
if reqErr == nil {
return r
}
}
time.Sleep(time.Second)
}
}
// UpdateCounts updates the pool counters
func (pool *ProxyPool) UpdateCounts() {
var alive, available int
for i := range pool.proxies {
if pool.proxies[i].Alive {
alive++
if time.Since(pool.proxies[i].lastRequest).Seconds() > float64(pool.RateLimit) {
available++
}
}
}
pool.AliveCount = alive
pool.AvailableCount = available
}