@@ -3,7 +3,7 @@ package rotateproxy
33import (
44 "crypto/tls"
55 "fmt"
6- "io/ioutil "
6+ "io"
77 "net/http"
88 "net/url"
99 "strings"
@@ -38,23 +38,26 @@ func CheckProxyAlive(proxyURL string) (respBody string, timeout int64, avail boo
3838 Timeout : 20 * time .Second ,
3939 }
4040 startTime := time .Now ()
41- resp , err := httpclient .Get ("http://cip.cc/" )
41+
42+ // http://cip.cc isn't stable enough for proxies alive test.
43+ resp , err := httpclient .Get ("https://www.baidu.com/robots.txt" )
44+
4245 if err != nil {
4346 return "" , 0 , false
4447 }
4548 defer resp .Body .Close ()
4649 timeout = int64 (time .Since (startTime ))
47- body , err := ioutil .ReadAll (resp .Body )
50+ body , err := io .ReadAll (resp .Body )
4851 if err != nil {
4952 return "" , 0 , false
5053 }
51- if ! strings .Contains (string (body ), "地址 " ) {
54+ if ! strings .Contains (string (body ), "Baiduspider-image " ) {
5255 return "" , 0 , false
5356 }
5457 return string (body ), timeout , true
5558}
5659
57- func CheckProxyWithCheckURL (proxyURL string , checkURL string ) (timeout int64 , avail bool ) {
60+ func CheckProxyWithCheckURL (proxyURL string , checkURL string , checkURLwords string ) (timeout int64 , avail bool ) {
5861 fmt .Printf ("check %s: %s\n " , proxyURL , checkURL )
5962 proxy , _ := url .Parse (proxyURL )
6063 httpclient := & http.Client {
@@ -72,32 +75,41 @@ func CheckProxyWithCheckURL(proxyURL string, checkURL string) (timeout int64, av
7275 }
7376 defer resp .Body .Close ()
7477 timeout = int64 (time .Since (startTime ))
78+ body , err := io .ReadAll (resp .Body )
79+
80+ if err != nil {
81+ return 0 , false
82+ }
7583
7684 // TODO: support regex
7785 if resp .StatusCode != 200 {
7886 return 0 , false
7987 }
8088
89+ if ! strings .Contains (string (body ), checkURLwords ) {
90+ return 0 , false
91+ }
92+
8193 return timeout , true
8294}
8395
84- func StartCheckProxyAlive (checkURL string ) {
96+ func StartCheckProxyAlive (checkURL string , checkURLwords string ) {
8597 go func () {
8698 ticker := time .NewTicker (120 * time .Second )
8799 for {
88100 select {
89101 case <- crawlDone :
90102 fmt .Println ("Checking" )
91- checkAlive (checkURL )
103+ checkAlive (checkURL , checkURLwords )
92104 fmt .Println ("Check done" )
93105 case <- ticker .C :
94- checkAlive (checkURL )
106+ checkAlive (checkURL , checkURLwords )
95107 }
96108 }
97109 }()
98110}
99111
100- func checkAlive (checkURL string ) {
112+ func checkAlive (checkURL string , checkURLwords string ) {
101113 proxies , err := QueryProxyURL ()
102114 if err != nil {
103115 fmt .Printf ("[!] query db error: %v\n " , err )
@@ -108,7 +120,7 @@ func checkAlive(checkURL string) {
108120 respBody , timeout , avail := CheckProxyAlive (proxy .URL )
109121 if avail {
110122 if checkURL != "" {
111- timeout , avail = CheckProxyWithCheckURL (proxy .URL , checkURL )
123+ timeout , avail = CheckProxyWithCheckURL (proxy .URL , checkURL , checkURLwords )
112124 }
113125 if avail {
114126 fmt .Printf ("%v 可用\n " , proxy .URL )
0 commit comments