-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcheck.go
104 lines (97 loc) · 2.3 KB
/
check.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
package rotateproxy
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
type IPResponse struct {
ORIGIN string `json:"origin"`
}
type IPInfo struct {
Status string `json:"status"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Region string `json:"region"`
RegionName string `json:"regionName"`
City string `json:"city"`
Zip string `json:"zip"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Timezone string `json:"timezone"`
Isp string `json:"isp"`
Org string `json:"org"`
As string `json:"as"`
Query string `json:"query"`
}
func CheckProxyAlive(proxyURL,ip string,timeout int) (respBody string, avail bool,time_config int) {
startTime := time.Now().UnixNano()
proxy, _ := url.Parse(proxyURL)
httpclient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxy),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 20 * time.Second,
}
resp, err := httpclient.Get("http://httpbin.org/ip")
if err != nil {
return "", false,10000
}
defer resp.Body.Close()
var res IPResponse
err = json.NewDecoder(resp.Body).Decode(&res)
if len(res.ORIGIN)<2{
return "", false,10000
}
if err != nil {
return "", false,10000
}
endTime := time.Now().UnixNano()
Milliseconds:= int((endTime - startTime) / 1e6)// 毫秒
fmt.Println("IP : ",ip," 用时毫秒 ",Milliseconds," 可用")
if Milliseconds>timeout{
return "", false,10000
}
return res.ORIGIN, true,Milliseconds
}
func StartCheckProxyAlive(timeout int) {
go func() {
ticker := time.NewTicker(120 * time.Second)
for {
select {
case <-crawlDone:
fmt.Println("Checking")
checkAlive(timeout)
fmt.Println("Check done")
case <-ticker.C:
checkAlive(timeout)
}
}
}()
}
func checkAlive(timeout int) {
proxies, err := QueryProxyURL()
if err != nil {
fmt.Printf("[!] query db error: %v\n", err)
}
for i := range proxies {
proxy := proxies[i]
if proxy.Available{
continue
}
if proxy.Retry >5{
continue
}
go func() {
_, _,time_conig := CheckProxyAlive(proxy.URL,proxy.IP,timeout)
if time_conig !=10000 {
SetProxytime(proxy.URL, time_conig)
}else{
AddProxyURLRetry(proxy.URL)
}
}()
}
}