forked from akkuman/rotateproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
86 lines (81 loc) · 1.76 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
package rotateproxy
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
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 string) bool {
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("https://www.baidu.com/")
if err != nil {
return false
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false
}
if !strings.Contains(string(body), "www.baidu.com") {
return false
}
return true
}
func StartCheckProxyAlive() {
go func() {
ticker := time.NewTicker(120 * time.Second)
for {
select {
case <-crawlDone:
fmt.Println("Checking")
checkAlive()
fmt.Println("Check done")
case <-ticker.C:
checkAlive()
}
}
}()
}
func checkAlive() {
proxies, err := QueryProxyURL()
if err != nil {
fmt.Printf("[!] query db error: %v\n", err)
}
for i := range proxies {
proxy := proxies[i]
go func() {
if CheckProxyAlive(proxy.URL) {
fmt.Printf("%v 可用\n", proxy.URL)
SetProxyURLAvail(proxy.URL)
} else {
AddProxyURLRetry(proxy.URL)
}
}()
}
}