-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathfreeproxy_fetch.go
More file actions
130 lines (111 loc) · 2.94 KB
/
Copy pathfreeproxy_fetch.go
File metadata and controls
130 lines (111 loc) · 2.94 KB
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
package main
import (
"encoding/base64"
"fmt"
"io"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"time"
)
var (
freeProxyIPPattern = regexp.MustCompile(`data-ip="([^"]+)"`)
freeProxyPortPattern = regexp.MustCompile(`data-port="([^"]+)"`)
)
func fetchFreeProxyToFile(sourceURL, outPath string, timeout time.Duration) (int, error) {
proxies, err := fetchFreeProxyList(sourceURL, timeout)
if err != nil {
return 0, err
}
if len(proxies) == 0 {
return 0, fmt.Errorf("no proxies found at %s", sourceURL)
}
if err := os.WriteFile(outPath, []byte(strings.Join(proxies, "\n")+"\n"), 0o644); err != nil {
return 0, err
}
return len(proxies), nil
}
func fetchFreeProxyList(sourceURL string, timeout time.Duration) ([]string, error) {
client := &http.Client{Timeout: timeout}
seen := make(map[string]struct{})
proxies := make([]string, 0, 256)
for page := 1; ; page++ {
pageURL, err := freeProxyPageURL(sourceURL, page)
if err != nil {
return nil, err
}
pageProxies, err := fetchFreeProxyPage(client, pageURL)
if err != nil {
return nil, fmt.Errorf("page %d: %w", page, err)
}
if len(pageProxies) == 0 {
break
}
for _, proxy := range pageProxies {
if _, ok := seen[proxy]; ok {
continue
}
seen[proxy] = struct{}{}
proxies = append(proxies, proxy)
}
}
return proxies, nil
}
func freeProxyPageURL(rawURL string, page int) (string, error) {
parsed, err := url.Parse(rawURL)
if err != nil {
return "", fmt.Errorf("parse url: %w", err)
}
query := parsed.Query()
if page <= 1 {
query.Del("page")
} else {
query.Set("page", fmt.Sprintf("%d", page))
}
parsed.RawQuery = query.Encode()
return parsed.String(), nil
}
func fetchFreeProxyPage(client *http.Client, pageURL string) ([]string, error) {
req, err := http.NewRequest(http.MethodGet, pageURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "opera-proxy freeproxy fetcher/1.0")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
ipMatches := freeProxyIPPattern.FindAllSubmatch(body, -1)
portMatches := freeProxyPortPattern.FindAllSubmatch(body, -1)
rowCount := min(len(ipMatches), len(portMatches))
proxies := make([]string, 0, rowCount)
for i := 0; i < rowCount; i++ {
ip, err := freeProxyDecodeBase64(string(ipMatches[i][1]))
if err != nil {
return nil, fmt.Errorf("decode ip on row %d: %w", i+1, err)
}
port, err := freeProxyDecodeBase64(string(portMatches[i][1]))
if err != nil {
return nil, fmt.Errorf("decode port on row %d: %w", i+1, err)
}
proxies = append(proxies, ip+":"+port)
}
return proxies, nil
}
func freeProxyDecodeBase64(value string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return "", err
}
return string(decoded), nil
}