forked from pingc0y/go_proxy_pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.go
322 lines (300 loc) · 8.17 KB
/
verify.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main
import (
"context"
"crypto/tls"
"fmt"
"golang.org/x/net/proxy"
"io"
"log"
"net"
"net/http"
"net/url"
"regexp"
"strings"
"sync"
"time"
)
var wg3 sync.WaitGroup
var ch1 = make(chan int, 50)
var verifyIS = false
var PublicIp = "0.0.0.0"
func Verify(pi *ProxyIp, wg *sync.WaitGroup, ch chan int, first bool) {
defer func() {
wg.Done()
<-ch
}()
// 拼接IP和端口
proxyAddress := pi.IPAddress + ":" + pi.Port
// 通用协议验证逻辑
startTime := time.Now()
switch {
case (pi.Protocol == "HTTPS" || first) && VerifyProxy2(proxyAddress, "HTTPS"):
pi.Protocol = "HTTPS"
pi.IsWorking = true
case (pi.Protocol == "CONNECT" || first) && VerifyProxy2(proxyAddress, "CONNECT"):
pi.Protocol = "CONNECT"
pi.IsWorking = true
case (pi.Protocol == "HTTP" || first) && VerifyProxy2(proxyAddress, "HTTP"):
pi.Protocol = "HTTP"
pi.IsWorking = true
case (pi.Protocol == "SOCKET5" || first) && VerifyProxy2(proxyAddress, "SOCKET5"):
pi.Protocol = "SOCKET5"
pi.IsWorking = true
default:
pi.IsWorking = false
}
pi.ResponseTime = time.Since(startTime).Milliseconds()
if pi.IsWorking && first {
// 获取匿名级别、ISP等信息并保存
pi.Anonymity = Anonymity(pi)
if pi.Anonymity == "" {
pi.IsWorking = false
}
pi.Isp, pi.Country, pi.Province, pi.City = getIpAddressInfo(pi.IPAddress)
updateProxyRecord(pi)
} else if pi.Protocol != "" {
// 仅更新验证时间和评分及状态
updateProxyRecord(pi)
}
}
func VerifyProxy2(proxyAddress string, protocol string) bool {
// 根据代理协议构造代理URL
var verifyPrefix string
var tr *http.Transport
switch protocol {
case "SOCKET5":
verifyPrefix = "https://"
dialer, err := proxy.SOCKS5("tcp", proxyAddress, nil, proxy.Direct)
if err != nil {
log.Printf("SOCKET5 代理创建失败: %v", err)
return false
}
tr = &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr) // 使用 SOCKS5 的 Dial 方法
}, TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
case "HTTP":
verifyPrefix = "http://"
proxyUrl, proxyErr := url.Parse("http://" + proxyAddress)
if proxyErr != nil {
log.Printf("代理URL解析失败: %v", proxyErr)
return false
}
tr = &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
}
case "HTTPS":
verifyPrefix = "https://"
proxyUrl, proxyErr := url.Parse("https://" + proxyAddress)
if proxyErr != nil {
log.Printf("代理URL解析失败: %v", proxyErr)
return false
}
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyURL(proxyUrl),
}
case "CONNECT":
verifyPrefix = "https://"
proxyUrl, proxyErr := url.Parse("http://" + proxyAddress)
if proxyErr != nil {
log.Printf("代理URL解析失败: %v", proxyErr)
return false
}
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyURL(proxyUrl),
}
default:
log.Printf("不支持的协议: %s", protocol)
return false
}
// 创建HTTP客户端
client := &http.Client{
Timeout: 10 * time.Second,
Transport: tr,
}
// 检查 VerifyUrl 和 VerifyUrlWords 是否正确配置
if conf.Config.VerifyUrl == "" || conf.Config.VerifyUrlWords == "" {
log.Printf("配置中的 VerifyUrl 或 VerifyUrlWords 不正确")
return false
}
// 创建并发送请求
request, err := http.NewRequest("GET", verifyPrefix+conf.Config.VerifyUrl, nil)
if err != nil {
log.Printf("请求失败: %v", err)
return false
}
response, err := client.Do(request)
if err != nil {
//log.Printf("代理请求失败: %v", err)
return false
}
defer func() {
if err := response.Body.Close(); err != nil {
log.Printf("关闭 response.Body 时出错: %v", err)
}
}()
dataBytes, _ := io.ReadAll(response.Body)
result := string(dataBytes)
return strings.Contains(result, conf.Config.VerifyUrlWords)
}
func Anonymity(pr *ProxyIp) string {
host := "http://httpbin.org/get"
var proxyAddress string
// 根据代理协议构造代理URL
if pr.Protocol == "SOCKET5" {
proxyAddress = "socks5://" + pr.IPAddress + ":" + pr.Port
} else if pr.Protocol == "HTTP" || pr.Protocol == "CONNECT" {
proxyAddress = "http://" + pr.IPAddress + ":" + pr.Port
} else if pr.Protocol == "HTTPS" {
proxyAddress = "https://" + pr.IPAddress + ":" + pr.Port
}
// 解析代理URL
proxyUrl, proxyErr := url.Parse(proxyAddress)
if proxyErr != nil {
log.Printf("解析代理URL失败: %v", proxyErr)
pr.IsWorking = false
return ""
}
// 设置代理及请求
tr := http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyURL(proxyUrl), // 设置代理
}
client := http.Client{Timeout: 15 * time.Second, Transport: &tr}
request, err := http.NewRequest("GET", host, nil)
//处理返回结果
response, err := client.Do(request)
if err != nil {
//log.Printf("代理请求失败: %s %v", proxyAddress, err)
pr.IsWorking = false
return ""
}
defer func() {
if err := response.Body.Close(); err != nil {
log.Printf("关闭 response.Body 时出错: %v", err)
}
}()
// 读取响应体
dataBytes, _ := io.ReadAll(response.Body)
result := string(dataBytes)
// 判断代理匿名性
arr := regexp.MustCompile(`"origin": "(.*)",`).FindStringSubmatch(result)
if len(arr) == 0 {
//log.Printf("响应不符合预期: %s %s", proxyAddress, result)
pr.IsWorking = false
return ""
}
origin := arr[1]
if strings.Contains(origin, PublicIp) {
pr.IsWorking = true
return "透明" // 透明代理,暴露了真实IP
}
if strings.Contains(origin, pr.IPAddress) {
pr.IsWorking = true
return "普匿" // 普通匿名代理
}
// 如果请求成功但没有匹配到特定条件,视为高匿名
pr.IsWorking = true
return "高匿"
}
func getIpAddressInfo(IpAddres string) (string, string, string, string) {
var Isp, Country, Province, City = "", "", "", ""
tr := http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := http.Client{Timeout: 15 * time.Second, Transport: &tr}
//处理返回结果
response, err := client.Get("https://searchplugin.csdn.net/api/v1/ip/get?ip=" + IpAddres)
if err != nil {
response, err = client.Get("https://searchplugin.csdn.net/api/v1/ip/get?ip=" + IpAddres)
if err != nil {
return Isp, Country, Province, City
}
}
defer func() {
if err := response.Body.Close(); err != nil {
log.Printf("关闭 response.Body 时出错: %v", err)
}
}()
dataBytes, _ := io.ReadAll(response.Body)
result := string(dataBytes)
address := regexp.MustCompile("\"address\":\"(.+?)\",").FindAllStringSubmatch(result, -1)
if len(address) != 0 {
addresss := removeDuplication_map(strings.Split(address[0][1], " "))
le := len(addresss)
Isp = strings.Split(addresss[le-1], "/")[0]
for i := range addresss {
if i == le-1 {
break
}
switch i {
case 0:
Country = addresss[0]
case 1:
Province = addresss[1]
case 2:
City = addresss[2]
}
}
}
return Isp, Country, Province, City
}
// 获取公网IP
func getPublicIp() {
// 发起HTTP请求
response, err := http.Get("http://myexternalip.com/raw")
if err != nil {
fmt.Println("请求出错:", err)
return
}
defer func() {
if err := response.Body.Close(); err != nil {
log.Printf("关闭 response.Body 时出错: %v", err)
}
}()
body, err := io.ReadAll(response.Body)
if err != nil {
fmt.Println("读取响应内容出错:", err)
return
}
PublicIp = string(body)
// 打印获取到的外部IP
log.Printf("外部IP: %s", body)
}
func VerifyProxy() {
if run {
log.Println("代理抓取中, 无法进行代理验证")
return
}
verifyIS = true
log.Printf("开始验证代理存活情况")
getPublicIp()
ProxyPool := getAllProxy()
for i := range ProxyPool {
wg3.Add(1)
ch1 <- 1
go Verify(&ProxyPool[i], &wg3, ch1, false)
}
time.Sleep(15 * time.Second)
wg3.Wait()
log.Printf("代理验证结束, 当前可用IP数: %d\n", len(ProxyPool))
cleanInvalidProxies()
verifyIS = false
}
func removeDuplication_map(arr []string) []string {
set := make(map[string]struct{}, len(arr))
j := 0
for _, v := range arr {
_, ok := set[v]
if ok {
continue
}
set[v] = struct{}{}
arr[j] = v
j++
}
return arr[:j]
}