-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
339 lines (283 loc) · 7.69 KB
/
client.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package client
import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strings"
)
const adminEndpoint = "https://admin.instantproxies.com/"
const loginPhp = "login_do.php"
const mainPhp = "main.php"
const checkIPEndpoint = "https://checkip.instantproxies.com/"
const checkProxiesEndpoint = "https://instantproxies.com/proxytester/test.json.php"
// ------------------------------------
// API client
// ------------------------------------
type Client struct {
UserName string
Password string
Endpoint string
httpClient SimpleHTTPClient
initSuccessfully bool
}
func NewClient(username string, password string, endpoint string) *Client {
if endpoint == "" {
endpoint = adminEndpoint
}
if !strings.HasSuffix(endpoint, "/") {
endpoint += "/"
}
_, err := url.Parse(endpoint)
if err != nil {
return nil
}
client := &Client{
UserName: username,
Password: password,
Endpoint: endpoint,
}
jar, _ := cookiejar.New(nil)
client.httpClient = &http.Client{
Jar: jar,
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse // no follow redirect
},
}
return client
}
func (client *Client) Authenticate() error {
if client.initSuccessfully {
return nil
}
payload := url.Values{}
payload.Add("username", client.UserName)
payload.Add("password", client.Password)
payload.Add("button", "Sign In")
res, networkErr := client.httpClient.PostForm(client.loginEndpoint(), payload)
defer res.Body.Close()
if networkErr != nil {
return networkErr
}
bodyBytes, readErr := io.ReadAll(res.Body)
if readErr != nil {
return readErr
}
bodyStr := string(bodyBytes)
if strings.Contains(bodyStr, "Invalid username or password") {
return errors.New("invalid username or password")
}
if res.StatusCode != 302 {
return fmt.Errorf("expected 302 HTTP status for success login, got %d", res.StatusCode)
}
redirectLocation := res.Header.Get("Location")
if redirectLocation != mainPhp {
return fmt.Errorf("expect redirect location to be '%s', got '%s'", mainPhp, redirectLocation)
}
client.initSuccessfully = true
return nil
}
func (client *Client) loginEndpoint() string {
return client.Endpoint + loginPhp
}
func (client *Client) mainEndpoint() string {
return client.Endpoint + mainPhp
}
// ------------------------------------
// API client get data
// ------------------------------------
func (client *Client) getMainPhpText() (string, error) {
res, networkErr := client.httpClient.Get(client.mainEndpoint())
defer res.Body.Close()
if networkErr != nil {
return "", networkErr
}
bodyBytes, readErr := io.ReadAll(res.Body)
if readErr != nil {
return "", readErr
}
return string(bodyBytes), nil
}
func getTextAreaInnerText(html string, id string) ([]string, error) {
// todo consider an html parser?
regex, regexErr := regexp.Compile(fmt.Sprintf("(?s)<textarea id=\"%s\".*?>(.*?)</textarea>", id))
if regexErr != nil {
return nil, regexErr
}
match := regex.FindStringSubmatch(html)
if len(match) != 2 {
return nil, fmt.Errorf("cannot find element '%s'", id)
}
replacer := strings.NewReplacer("\t", "", "\r", "", "\n\n", "\n")
textAreaText := strings.Trim(replacer.Replace(match[1]), "\n")
lines := strings.Split(textAreaText, "\n")
return lines, nil
}
func (client *Client) GetProxies() ([]*Proxy, error) {
initError := client.Authenticate()
if initError != nil {
return nil, initError
}
html, reqErr := client.getMainPhpText()
if reqErr != nil {
return nil, reqErr
}
lines, err := getTextAreaInnerText(html, "proxies-textarea")
if err != nil {
return nil, err
}
return MakeProxies(lines)
}
func (client *Client) GetAuthorizedIPs() ([]net.IP, error) {
initError := client.Authenticate()
if initError != nil {
return nil, initError
}
html, reqErr := client.getMainPhpText()
if reqErr != nil {
return nil, reqErr
}
lines, err := getTextAreaInnerText(html, "authips-textarea")
if err != nil {
return nil, err
}
ips := make([]net.IP, len(lines))
for i, line := range lines {
ips[i] = net.ParseIP(line)
if ips[i] == nil {
return nil, fmt.Errorf("can not parse IP %s", line)
}
}
return ips, nil
}
// ------------------------------------
// API client set data
// ------------------------------------
func (client *Client) AddAuthorizedIP(ip net.IP) error {
return client.AddAuthorizedIPs([]net.IP{ip})
}
func (client *Client) AddAuthorizedIPs(ips []net.IP) error {
initError := client.Authenticate()
if initError != nil {
return initError
}
authorizedIPs, getIPErr := client.GetAuthorizedIPs()
if getIPErr != nil {
return getIPErr
}
// TODO make ip unique?
authorizedIPs = append(authorizedIPs, ips...)
return client.SetAuthorizedIPs(authorizedIPs)
}
func (client *Client) RemoveAuthorizedIP(ip net.IP) error {
return client.RemoveAuthorizedIPs([]net.IP{ip})
}
func (client *Client) RemoveAuthorizedIPs(ips []net.IP) error {
initError := client.Authenticate()
if initError != nil {
return initError
}
authorizedIPs, getIPErr := client.GetAuthorizedIPs()
if getIPErr != nil {
return getIPErr
}
for _, ip := range ips {
idx := -1
for i, aip := range authorizedIPs {
if aip.Equal(ip) {
idx = i
break
}
}
if idx != -1 {
authorizedIPs = append(authorizedIPs[:idx], authorizedIPs[idx+1:]...)
}
}
return client.SetAuthorizedIPs(authorizedIPs)
}
func (client *Client) SetAuthorizedIPs(ips []net.IP) error {
initError := client.Authenticate()
if initError != nil {
return initError
}
authorizedIPStrs := make([]string, len(ips))
for i, ip := range ips {
authorizedIPStrs[i] = ip.String()
}
payload := url.Values{}
payload.Add("cmd", "Submit Update")
payload.Add("authips", strings.Join(authorizedIPStrs, "\n"))
response, networkErr := client.httpClient.PostForm(client.mainEndpoint(), payload)
if networkErr != nil {
return networkErr
}
// todo check status & content of response
if response.StatusCode != 200 {
return fmt.Errorf("expected status 200, got %v", response.StatusCode)
}
return nil
}
// ------------------------------------
// API util
// ------------------------------------
func (client *Client) GetOwnedPublicIP() (net.IP, error) {
response, networkErr := client.httpClient.Get(checkIPEndpoint)
defer response.Body.Close()
if networkErr != nil {
return nil, networkErr
}
bodyRaw, readErr := io.ReadAll(response.Body)
if readErr != nil {
return nil, readErr
}
ipString := strings.Trim(string(bodyRaw)[3:], " \n\r\t")
ip := net.ParseIP(ipString)
if ip == nil {
return nil, fmt.Errorf("can not parse IP '%s'", ipString)
}
return ip, nil
}
func (client *Client) TestOwnedProxies() ([]bool, error) {
proxies, err := client.GetProxies()
if err != nil {
return nil, err
}
return client.TestProxies(proxies), nil
}
func (client *Client) TestProxies(proxies []*Proxy) []bool {
pool := NewWorkerPool[bool](4)
for _, proxy := range proxies {
pool.Submit(func() (bool, error) {
return client.testProxy(proxy)
})
}
pool.Close()
pool.Wait()
return pool.ResultValues()
}
func (client *Client) testProxy(proxy *Proxy) (bool, error) {
fmt.Printf("Testing %v\n", proxy)
query := url.Values{}
query.Add("proxy", proxy.String())
checkUrl := checkProxiesEndpoint + "?" + query.Encode()
res, networkErr := client.httpClient.Get(checkUrl)
defer res.Body.Close()
if networkErr != nil {
return false, networkErr
}
if res.StatusCode != 200 {
return false, fmt.Errorf("expected HTTP status 200, got %d", res.StatusCode)
}
bodyRaw, readErr := io.ReadAll(res.Body)
if readErr != nil {
return false, readErr
}
body := strings.Trim(string(bodyRaw), " \n\r\t")
passed := strings.HasSuffix(body, "200,PASSED")
fmt.Printf("Result testing %v: %v\n", proxy, passed)
return passed, nil
}