-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxies.go
181 lines (151 loc) · 3.32 KB
/
proxies.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
// Package sslproxies is a scraper for finding proxies based on sslproxies.org
package sslproxies
import (
"errors"
"io/ioutil"
"net/http"
"github.com/tamboto2000/htmltojson"
)
// Proxy contains information about a proxy
type Proxy struct {
IP string `json:"ip"`
Port string `json:"port"`
Code string `json:"code"`
Country string `json:"country"`
Anonymity string `json:"anonymity"`
Google bool `json:"google"`
HTTPS bool `json:"https"`
}
// GetAll get all proxies
func GetAll() ([]Proxy, error) {
raw, err := request()
if err != nil {
return nil, err
}
return parse(raw)
}
// Get get proxies with filters.
// Set count to 0 to get all fetched proxies,
// set code to specify from which country this proxy originate,
// set anon to specify the level of anonymity
func Get(count int, code, anon string) ([]Proxy, error) {
proxs, err := GetAll()
if err != nil {
return nil, err
}
newProxs := make([]Proxy, 0)
for _, p := range proxs {
if code != "" {
if p.Code != code {
continue
}
}
if anon != "" {
if p.Anonymity != anon {
continue
}
}
newProxs = append(newProxs, p)
if count > 0 {
if len(newProxs) == count {
return newProxs, nil
}
}
}
return newProxs, nil
}
func request() ([]byte, error) {
req, err := http.NewRequest("GET", "https://www.sslproxies.org/", nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0")
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
req.Header.Set("Connection", "keep-alive")
cl := new(http.Client)
resp, err := cl.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode > 200 {
return nil, errors.New(string(body))
}
return body, nil
}
func parse(raw []byte) ([]Proxy, error) {
node, err := htmltojson.ParseBytes(raw)
if err != nil {
return nil, err
}
node = htmltojson.SearchNode(
htmltojson.Element,
"table",
"",
"class",
"table table-striped table-bordered",
node,
)
if node == nil {
return nil, errors.New("table is missing")
}
nodes := htmltojson.SearchAllNode(
htmltojson.Element,
"tr",
"",
"",
"",
node,
)
if nodes == nil {
return nil, errors.New("table is missing")
}
items := make([]Proxy, 0)
for i, node := range nodes {
if i == 0 {
continue
}
nodes := htmltojson.SearchAllNode(
htmltojson.Element,
"td",
"",
"",
"",
&node,
)
if nodes == nil {
return nil, errors.New("table is missing")
}
if node.Child[2].Child == nil {
continue
}
item := Proxy{
IP: node.Child[0].Child[0].Data,
Port: node.Child[1].Child[0].Data,
Code: node.Child[2].Child[0].Data,
Country: node.Child[3].Child[0].Data,
Anonymity: node.Child[4].Child[0].Data,
}
if node.Child[5].Child != nil {
if len(node.Child[5].Child) >= 1 {
if node.Child[5].Child[0].Data == "yes" {
item.Google = true
}
}
} else {
item.Google = false
}
if node.Child[6].Child[0].Data == "yes" {
item.HTTPS = true
} else {
item.HTTPS = false
}
items = append(items, item)
}
return items, nil
}