-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathverification.go
62 lines (50 loc) · 1.27 KB
/
verification.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
package freeproxy
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"github.com/sirupsen/logrus"
"github.com/soluchok/freeproxy/providers"
)
type checkIP struct {
IP string
}
func verifyProxy(proxy string) bool {
req, err := http.NewRequest("GET", "https://api.ipify.org/?format=json", nil)
if err != nil {
logrus.Errorf("cannot create new request for verify err:%s", err)
return false
}
proxyURL, err := url.Parse("http://" + proxy)
if err != nil {
logrus.Errorf("cannot parse proxy %q err:%s", proxy, err)
return false
}
client := providers.NewClient()
client.Transport.(*http.Transport).Proxy = http.ProxyURL(proxyURL)
resp, err := client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
logrus.Debugf("cannot verify proxy %q err:%s", proxy, err)
return false
}
var body bytes.Buffer
if _, err := io.Copy(&body, resp.Body); err != nil {
logrus.Errorf("cannot copy resp.Body err:%s", err)
return false
}
if resp.StatusCode != http.StatusOK {
return false
}
var check checkIP
if err := json.Unmarshal(body.Bytes(), &check); err != nil {
logrus.Errorf("%d cannot unmarshal %q to checkIP struct err:%s", resp.StatusCode, body.String(), err)
return false
}
return strings.HasPrefix(proxy, check.IP)
}