forked from dperson/torproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_scraper.py
More file actions
461 lines (398 loc) · 15.8 KB
/
proxy_scraper.py
File metadata and controls
461 lines (398 loc) · 15.8 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
"""
Public SOCKS proxy scraper from multiple sources.
Fetches, deduplicates and filters proxies by country.
"""
import re
import time
import socket
import concurrent.futures
from dataclasses import dataclass, field
from typing import List, Optional, Dict
from urllib.parse import urlparse
import requests
import socks
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TaskProgressColumn
console = Console()
PROXY_CHECK_TIMEOUT = 8
MAX_CHECK_WORKERS = 30
@dataclass
class Proxy:
"""Represents a SOCKS proxy."""
host: str
port: int
proto: str = "socks5" # "socks4" or "socks5"
country: str = "" # ISO 2-letter country code (e.g. "FR", "US")
country_name: str = "" # Full country name
latency_ms: float = -1.0 # -1 = not tested
alive: bool = False
username: str = ""
password: str = ""
@property
def address(self) -> str:
return f"{self.host}:{self.port}"
@property
def url(self) -> str:
creds = f"{self.username}:{self.password}@" if self.username else ""
return f"{self.proto}://{creds}{self.host}:{self.port}"
def __hash__(self):
return hash((self.host, self.port))
def __eq__(self, other):
return isinstance(other, Proxy) and self.host == other.host and self.port == other.port
# ── Proxy sources ─────────────────────────────────────────────────────────────
def _fetch_proxyscrape_socks5() -> List[Proxy]:
"""proxyscrape.com — SOCKS5."""
proxies = []
urls = [
"https://api.proxyscrape.com/v3/free-proxy-list/get?request=displayproxies&proxy_type=socks5&timeout=10000&country=all&simplified=true",
"https://api.proxyscrape.com/v2/?request=getproxies&protocol=socks5&timeout=10000&country=all",
]
for url in urls:
try:
r = requests.get(url, timeout=15)
for line in r.text.strip().splitlines():
line = line.strip()
if ":" in line:
host, port_s = line.rsplit(":", 1)
try:
proxies.append(Proxy(host=host.strip(), port=int(port_s.strip()), proto="socks5"))
except ValueError:
pass
except Exception:
pass
return proxies
def _fetch_proxyscrape_socks4() -> List[Proxy]:
"""proxyscrape.com — SOCKS4."""
proxies = []
urls = [
"https://api.proxyscrape.com/v3/free-proxy-list/get?request=displayproxies&proxy_type=socks4&timeout=10000&country=all&simplified=true",
"https://api.proxyscrape.com/v2/?request=getproxies&protocol=socks4&timeout=10000&country=all",
]
for url in urls:
try:
r = requests.get(url, timeout=15)
for line in r.text.strip().splitlines():
line = line.strip()
if ":" in line:
host, port_s = line.rsplit(":", 1)
try:
proxies.append(Proxy(host=host.strip(), port=int(port_s.strip()), proto="socks4"))
except ValueError:
pass
except Exception:
pass
return proxies
def _fetch_github_hookzof() -> List[Proxy]:
"""hookzof/socks5_list on GitHub."""
proxies = []
url = "https://raw.githubusercontent.com/hookzof/socks5_list/master/proxy.txt"
try:
r = requests.get(url, timeout=15)
for line in r.text.strip().splitlines():
line = line.strip()
if ":" in line:
host, port_s = line.rsplit(":", 1)
try:
proxies.append(Proxy(host=host.strip(), port=int(port_s.strip()), proto="socks5"))
except ValueError:
pass
except Exception:
pass
return proxies
def _fetch_github_proxifly() -> List[Proxy]:
"""proxifly/proxy-list — SOCKS5 with country."""
proxies = []
url = "https://raw.githubusercontent.com/proxifly/free-proxy-list/main/proxies/protocols/socks5/data.txt"
try:
r = requests.get(url, timeout=15)
for line in r.text.strip().splitlines():
line = line.strip()
if ":" in line:
host, port_s = line.rsplit(":", 1)
try:
proxies.append(Proxy(host=host.strip(), port=int(port_s.strip()), proto="socks5"))
except ValueError:
pass
except Exception:
pass
return proxies
def _fetch_github_thespeedx() -> List[Proxy]:
"""TheSpeedX/PROXY-List — SOCKS5."""
proxies = []
url = "https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/socks5.txt"
try:
r = requests.get(url, timeout=15)
for line in r.text.strip().splitlines():
line = line.strip()
if ":" in line:
host, port_s = line.rsplit(":", 1)
try:
proxies.append(Proxy(host=host.strip(), port=int(port_s.strip()), proto="socks5"))
except ValueError:
pass
except Exception:
pass
return proxies
def _fetch_github_thespeedx_s4() -> List[Proxy]:
"""TheSpeedX/PROXY-List — SOCKS4."""
proxies = []
url = "https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/socks4.txt"
try:
r = requests.get(url, timeout=15)
for line in r.text.strip().splitlines():
line = line.strip()
if ":" in line:
host, port_s = line.rsplit(":", 1)
try:
proxies.append(Proxy(host=host.strip(), port=int(port_s.strip()), proto="socks4"))
except ValueError:
pass
except Exception:
pass
return proxies
def _fetch_github_monosans() -> List[Proxy]:
"""monosans/proxy-list — SOCKS5."""
proxies = []
urls = [
"https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/socks5.txt",
"https://raw.githubusercontent.com/monosans/proxy-list/main/proxies_anonymous/socks5.txt",
]
for url in urls:
try:
r = requests.get(url, timeout=15)
for line in r.text.strip().splitlines():
line = line.strip().split()[0] # strip trailing comments
if ":" in line:
host, port_s = line.rsplit(":", 1)
try:
proxies.append(Proxy(host=host.strip(), port=int(port_s.strip()), proto="socks5"))
except ValueError:
pass
except Exception:
pass
return proxies
def _fetch_proxylist_download() -> List[Proxy]:
"""proxy-list.download — SOCKS5 with country."""
proxies = []
try:
r = requests.get(
"https://www.proxy-list.download/api/v2/get?l=en&t=socks5",
timeout=15,
)
data = r.json()
for item in data.get("LISTA", []):
host = item.get("IP", "")
port_s = item.get("PORT", "")
country = item.get("COUNTRY", "")
if host and port_s:
try:
proxies.append(Proxy(
host=host,
port=int(port_s),
proto="socks5",
country=country.upper()[:2] if country else "",
))
except ValueError:
pass
except Exception:
pass
return proxies
def _fetch_openproxylist() -> List[Proxy]:
"""openproxylist.xyz — SOCKS5."""
proxies = []
try:
r = requests.get("https://openproxylist.xyz/socks5.txt", timeout=15)
for line in r.text.strip().splitlines():
line = line.strip()
if ":" in line:
host, port_s = line.rsplit(":", 1)
try:
proxies.append(Proxy(host=host.strip(), port=int(port_s.strip()), proto="socks5"))
except ValueError:
pass
except Exception:
pass
return proxies
# ── Country resolution via ip-api.com ─────────────────────────────────────────
def resolve_countries_batch(proxies: List[Proxy], via_tor_port: Optional[int] = None) -> List[Proxy]:
"""
Enrich proxies that have no country by querying ip-api.com in batches of 100.
Requests are routed through Tor if via_tor_port is set.
"""
to_resolve = [p for p in proxies if not p.country]
if not to_resolve:
return proxies
req_kwargs = {}
if via_tor_port:
req_kwargs["proxies"] = {
"http": f"socks5h://127.0.0.1:{via_tor_port}",
"https": f"socks5h://127.0.0.1:{via_tor_port}",
}
ip_map: Dict[str, tuple] = {}
batch_size = 100
batches = [to_resolve[i:i + batch_size] for i in range(0, len(to_resolve), batch_size)]
total_batches = len(batches)
try:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TaskProgressColumn(),
TextColumn("[dim]{task.fields[resolved]} resolved[/dim]"),
console=console,
transient=False,
) as progress:
task = progress.add_task(
"[cyan]Geolocating via ip-api.com...[/cyan]",
total=total_batches,
resolved=0,
)
for batch_idx, batch in enumerate(batches):
payload = [{"query": p.host} for p in batch]
try:
r = requests.post(
"http://ip-api.com/batch?fields=query,countryCode,country",
json=payload,
timeout=20,
**req_kwargs,
)
for entry in r.json():
query = entry.get("query", "")
cc = entry.get("countryCode", "")
cn = entry.get("country", "")
if query and cc:
ip_map[query] = (cc.upper(), cn)
except Exception:
pass
progress.update(
task,
advance=1,
resolved=len(ip_map),
description=(
f"[cyan]Geolocation "
f"[bold]{batch_idx + 1}/{total_batches}[/bold] batches...[/cyan]"
),
)
if batch_idx < total_batches - 1:
time.sleep(0.5) # ip-api rate limit: 45 req/min
except Exception:
pass
for p in proxies:
if p.host in ip_map:
p.country, p.country_name = ip_map[p.host]
return proxies
# ── Proxy liveness check ──────────────────────────────────────────────────────
def _check_proxy(proxy: Proxy, via_tor_port: Optional[int] = None) -> Proxy:
"""
Test whether a proxy is reachable.
If via_tor_port is set, the TCP connection goes through Tor first.
"""
start = time.monotonic()
try:
if via_tor_port:
import socks as socks_mod
s = socks_mod.socksocket()
s.set_proxy(socks_mod.SOCKS5, "127.0.0.1", via_tor_port)
s.settimeout(PROXY_CHECK_TIMEOUT)
s.connect((proxy.host, proxy.port))
s.close()
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(PROXY_CHECK_TIMEOUT)
s.connect((proxy.host, proxy.port))
s.close()
proxy.latency_ms = (time.monotonic() - start) * 1000
proxy.alive = True
except Exception:
proxy.alive = False
return proxy
def check_proxies(
proxies: List[Proxy],
via_tor_port: Optional[int] = None,
max_workers: int = MAX_CHECK_WORKERS,
show_progress: bool = True,
) -> List[Proxy]:
"""Check all proxies in parallel and return the alive ones sorted by latency."""
results = []
if show_progress:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TaskProgressColumn(),
console=console,
) as progress:
task = progress.add_task(
f"[cyan]Checking {len(proxies)} proxies...[/cyan]",
total=len(proxies),
)
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = {
executor.submit(_check_proxy, p, via_tor_port): p
for p in proxies
}
for future in concurrent.futures.as_completed(futures):
results.append(future.result())
progress.advance(task)
else:
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
results = list(executor.map(lambda p: _check_proxy(p, via_tor_port), proxies))
alive = [p for p in results if p.alive]
return sorted(alive, key=lambda p: p.latency_ms)
# ── Entry point ───────────────────────────────────────────────────────────────
SCRAPERS = [
("proxyscrape SOCKS5", _fetch_proxyscrape_socks5),
("proxyscrape SOCKS4", _fetch_proxyscrape_socks4),
("GitHub hookzof", _fetch_github_hookzof),
("GitHub proxifly", _fetch_github_proxifly),
("GitHub TheSpeedX S5", _fetch_github_thespeedx),
("GitHub TheSpeedX S4", _fetch_github_thespeedx_s4),
("GitHub monosans", _fetch_github_monosans),
("proxy-list.download", _fetch_proxylist_download),
("openproxylist.xyz", _fetch_openproxylist),
]
def fetch_all_proxies(verbose: bool = False) -> List[Proxy]:
"""Fetch proxies from all sources and deduplicate."""
all_proxies: List[Proxy] = []
seen = set()
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task(
"[cyan]Fetching public proxies...[/cyan]",
total=len(SCRAPERS),
)
for name, scraper in SCRAPERS:
progress.update(task, description=f"[cyan]Scraping: {name}[/cyan]")
try:
found = scraper()
new = 0
for p in found:
key = (p.host, p.port)
if key not in seen:
seen.add(key)
all_proxies.append(p)
new += 1
if verbose:
console.print(f" [dim]{name}: {new} proxies[/dim]")
except Exception as e:
if verbose:
console.print(f" [red]{name}: error — {e}[/red]")
progress.advance(task)
console.print(
f"[green]{len(all_proxies)} unique proxies collected from {len(SCRAPERS)} sources.[/green]"
)
return all_proxies
def get_countries_available(proxies: List[Proxy]) -> Dict[str, int]:
"""Return a {country_code: proxy_count} dict for proxies that have country info."""
countries: Dict[str, int] = {}
for p in proxies:
if p.country:
countries[p.country] = countries.get(p.country, 0) + 1
return dict(sorted(countries.items(), key=lambda x: -x[1]))
def filter_by_country(proxies: List[Proxy], country_code: str) -> List[Proxy]:
"""Filter proxies by ISO country code."""
cc = country_code.upper().strip()
return [p for p in proxies if p.country.upper() == cc]