-
Notifications
You must be signed in to change notification settings - Fork 6
/
ip2proxywebservice.go
167 lines (126 loc) · 3.42 KB
/
ip2proxywebservice.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
package ip2proxy
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
)
// The IP2ProxyResult struct stores all of the available
// proxy info found in the IP2Proxy Web Service.
type IP2ProxyResult struct {
Response string `json:"response"`
CountryCode string `json:"countryCode"`
CountryName string `json:"countryName"`
RegionName string `json:"regionName"`
CityName string `json:"cityName"`
ISP string `json:"isp"`
Domain string `json:"domain"`
UsageType string `json:"usageType"`
ASN string `json:"asn"`
AS string `json:"as"`
LastSeen string `json:"lastSeen"`
ProxyType string `json:"proxyType"`
Threat string `json:"threat"`
IsProxy string `json:"isProxy"`
Provider string `json:"provider"`
}
// The IP2ProxyCreditResult struct stores the
// credit balance for the IP2Proxy Web Service.
type IP2ProxyCreditResult struct {
Response string `json:"response"`
}
// The WS struct is the main object used to query the IP2Proxy Web Service.
type WS struct {
apiKey string
apiPackage string
useSSL bool
}
var regexAPIKey = regexp.MustCompile(`^[\dA-Z]{10}$`)
var regexAPIPackage = regexp.MustCompile(`^PX\d+$`)
const baseURL = "api.ip2proxy.com/"
const msgInvalidAPIKey = "Invalid API key."
const msgInvalidAPIPackage = "Invalid package name."
// OpenWS initializes with the web service API key, API package and whether to use SSL
func OpenWS(apikey string, apipackage string, usessl bool) (*WS, error) {
var ws = &WS{}
ws.apiKey = apikey
ws.apiPackage = apipackage
ws.useSSL = usessl
err := ws.checkParams()
if err != nil {
return nil, err
}
return ws, nil
}
func (w *WS) checkParams() error {
if !regexAPIKey.MatchString(w.apiKey) {
return errors.New(msgInvalidAPIKey)
}
if !regexAPIPackage.MatchString(w.apiPackage) {
return errors.New(msgInvalidAPIPackage)
}
return nil
}
// LookUp will return all proxy fields based on the queried IP address.
func (w *WS) LookUp(ipAddress string) (IP2ProxyResult, error) {
var res IP2ProxyResult
err := w.checkParams()
if err != nil {
return res, err
}
protocol := "https"
if !w.useSSL {
protocol = "http"
}
myUrl := protocol + "://" + baseURL + "?key=" + w.apiKey + "&package=" + w.apiPackage + "&ip=" + url.QueryEscape(ipAddress)
resp, err := http.Get(myUrl)
if err != nil {
return res, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return res, err
}
err = json.Unmarshal(bodyBytes, &res)
if err != nil {
return res, err
}
return res, nil
}
return res, errors.New("Error HTTP " + strconv.Itoa(int(resp.StatusCode)))
}
// GetCredit will return the web service credit balance.
func (w *WS) GetCredit() (IP2ProxyCreditResult, error) {
var res IP2ProxyCreditResult
err := w.checkParams()
if err != nil {
return res, err
}
protocol := "https"
if !w.useSSL {
protocol = "http"
}
myUrl := protocol + "://" + baseURL + "?key=" + w.apiKey + "&check=true"
resp, err := http.Get(myUrl)
if err != nil {
return res, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return res, err
}
err = json.Unmarshal(bodyBytes, &res)
if err != nil {
return res, err
}
return res, nil
}
return res, errors.New("Error HTTP " + strconv.Itoa(int(resp.StatusCode)))
}