This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
203 lines (187 loc) · 3.96 KB
/
client.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
package taobao
import (
"net/http"
"net/url"
"regexp"
"sync"
"time"
"github.com/reusee/hcutil"
)
type ClientSet struct {
sync.RWMutex
in chan<- *http.Client
out <-chan *http.Client
kill chan struct{}
good map[*http.Client]int
Logger func(ClientInfo, ClientState)
infos map[*http.Client]ClientInfo
}
type ClientInfo struct {
HttpProxyAddr string
}
func NewClientSet() *ClientSet {
in, out, kill := NewClientsChan()
s := &ClientSet{
in: in,
out: out,
kill: kill,
good: make(map[*http.Client]int),
infos: make(map[*http.Client]ClientInfo),
}
// local ss proxies
go func() {
proxies := []string{
"8022",
"8032",
"8080",
"8010",
"8031",
"8030",
"8023",
"8020",
"8021",
"8040",
"8015",
}
for _, addr := range proxies {
client, err := hcutil.NewClientSocks5("localhost:" + addr)
if err != nil {
continue
}
client.Timeout = time.Second * 32
in <- client
}
}()
// free proxies
go s.provideFreeProxyClients()
return s
}
type ClientState uint8
const (
Good ClientState = iota
Bad
)
func (s *ClientSet) Do(fn func(client *http.Client) ClientState) {
loop:
for {
client := <-s.out
s.Lock()
if _, ok := s.good[client]; !ok {
s.good[client] = 32
}
s.Unlock()
switch fn(client) {
case Good:
s.in <- client
s.Lock()
s.good[client]++
if s.Logger != nil {
s.Logger(s.infos[client], Good)
}
s.Unlock()
break loop
case Bad:
s.Lock()
if s.good[client] > 0 {
s.good[client]--
s.in <- client
}
if s.Logger != nil {
s.Logger(s.infos[client], Bad)
}
s.Unlock()
}
}
}
func (s *ClientSet) Close() {
close(s.kill)
}
func (s *ClientSet) provideFreeProxyClients() {
entryPattern := regexp.MustCompile(`<tr><b><td>[0-9]+</td><td>([0-9.]+)</td><td>([0-9]+)</td>`)
for page := 1; page <= 10; page++ {
pageUrl := sp("http://www.proxy.com.ru/list_%d.html", page)
var bs []byte
var err error
s.Do(func(client *http.Client) ClientState {
bs, err = getBytes(client, pageUrl)
if err != nil {
return Bad
}
return Good
})
if err != nil {
pt("error getting %s\n", pageUrl)
continue
}
res := entryPattern.FindAllSubmatch(bs, -1)
for _, match := range res {
addr := sp("http://%s:%s", match[1], match[2])
proxyUrl, err := url.Parse(addr)
ce(err, sp("parse http proxy url %s", addr))
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
},
Timeout: time.Second * 32,
}
s.Lock()
s.infos[client] = ClientInfo{
HttpProxyAddr: addr,
}
s.Unlock()
time.Sleep(time.Millisecond * 200)
s.in <- client
}
}
/*
for page := 1; page <= 300; page++ {
pageUrl := sp("http://www.kuaidaili.com/proxylist/%d/", page)
bs, err := getBytes(http.DefaultClient, pageUrl)
if err != nil {
pt("error getting %s: %v\n", pageUrl, err)
continue
}
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(bs))
if err != nil {
pt("error parsing %s: %v\n", pageUrl, err)
continue
}
doc.Find("div#list table tbody tr").Each(func(i int, se *goquery.Selection) {
tds := se.Find("td")
addr := sp("%s:%s", tds.Get(0).FirstChild.Data, tds.Get(1).FirstChild.Data)
proxyUrl, err := url.Parse(addr)
ce(err, sp("parse http proxy url %s", addr))
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
},
Timeout: time.Second * 32,
}
if testClient(client, addr) {
clients <- client
}
})
}
*/
}
func testClient(client *http.Client, addr string) bool {
pt("testing proxy %s\n", addr)
done := make(chan struct{})
go func() {
_, err := getBytes(client, "http://www.taobao.com")
if err == nil {
close(done)
} else {
pt("client get error %s: %v\n", addr, err)
}
}()
select {
case <-time.After(time.Second * 8):
pt("client test timeout %s\n", addr)
case <-done:
pt("client %s ok\n", addr)
return true
}
pt("client %s not ok\n", addr)
return false
}