File size: 7,500 Bytes
47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 47fb306 f4c2091 | 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 | import re
import json
import time
import logging
import ipaddress
from bs4 import BeautifulSoup
from curl_cffi import requests as curl_requests
import config
logger = logging.getLogger('scraper')
# Universal IP:PORT matcher
_IPPORT_RE = re.compile(
r'\b(?:\d{1,3}\.){3}\d{1,3}:\d{2,5}\b'
)
# Dynamic extraction keywords
_IP_KEYS = {
'ip', 'host', 'address', 'addr',
'proxy_address', 'proxy_host',
'hostname'
}
_PORT_KEYS = {
'port', 'proxy_port',
'port_number'
}
_PROTO_KEYS = {
'protocol', 'protocols',
'type', 'proxy_type',
'schema', 'kind',
'proxy_scheme'
}
# Protocol aliases
_PROTOCOL_ALIASES = {
'socks5': 'socks5',
'socks5h': 'socks5',
's5': 'socks5',
}
_HEADERS = {
'User-Agent': (
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/124.0.0.0 Safari/537.36'
),
'Accept': '*/*',
}
def _valid_ip(ip: str) -> bool:
try:
ipaddress.ip_address(ip)
return True
except Exception:
return False
def _valid_port(port) -> bool:
try:
port = int(port)
return 1 <= port <= 65535
except Exception:
return False
def _normalize_protocol(value) -> str | None:
if value is None:
return None
if isinstance(value, list):
for item in value:
proto = _normalize_protocol(item)
if proto == 'socks5':
return 'socks5'
return None
value = str(value).strip().lower()
for alias, normalized in _PROTOCOL_ALIASES.items():
if alias in value:
return normalized
return None
def _extract_ip_port(text: str) -> list[str]:
found = []
for match in _IPPORT_RE.findall(text):
try:
ip, port = match.split(':', 1)
if not _valid_ip(ip):
continue
if not _valid_port(port):
continue
found.append(match)
except Exception:
continue
return found
def _extract_from_dict(
item: dict,
fallback_protocol: str,
found: dict
) -> None:
ip = None
port = None
protocol = None
for key, value in item.items():
lowered = str(key).lower()
if lowered in _IP_KEYS:
ip = value
elif lowered in _PORT_KEYS:
port = value
elif lowered in _PROTO_KEYS:
protocol = _normalize_protocol(value)
if ip and port:
ip = str(ip).strip()
port = str(port).strip()
if _valid_ip(ip) and _valid_port(port):
proxy = f'{ip}:{port}'
found[proxy] = protocol or fallback_protocol
def _walk(data, fallback_protocol: str, found: dict) -> None:
"""
Universal recursive parser.
Handles:
- nested JSON
- lists
- dicts
- strings
- embedded JSON strings
"""
if isinstance(data, dict):
_extract_from_dict(data, fallback_protocol, found)
for value in data.values():
_walk(value, fallback_protocol, found)
elif isinstance(data, list):
for item in data:
_walk(item, fallback_protocol, found)
elif isinstance(data, tuple):
for item in data:
_walk(item, fallback_protocol, found)
elif isinstance(data, str):
for proxy in _extract_ip_port(data):
found[proxy] = fallback_protocol
# Embedded JSON support
stripped = data.strip()
if (
stripped.startswith('{') or
stripped.startswith('[')
):
try:
parsed = json.loads(stripped)
_walk(parsed, fallback_protocol, found)
except Exception:
pass
def _extract_from_html(
html: str,
fallback_protocol: str
) -> dict:
"""
Universal HTML parser.
Supports:
- tables
- div layouts
- inline text
- script tags
"""
found = {}
soup = BeautifulSoup(html, 'html.parser')
# Global page text scan
page_text = soup.get_text(' ', strip=True)
for proxy in _extract_ip_port(page_text):
found[proxy] = fallback_protocol
# Script extraction
for script in soup.find_all('script'):
content = script.get_text(' ', strip=True)
if not content:
continue
for proxy in _extract_ip_port(content):
found[proxy] = fallback_protocol
try:
parsed = json.loads(content)
_walk(parsed, fallback_protocol, found)
except Exception:
pass
return found
def add_candidate(
proxy_str: str,
source: str,
protocol: str
) -> bool:
with config.store_lock:
if proxy_str in config.store['blacklist']:
return False
if proxy_str in config.store['active_proxies']:
return False
if proxy_str in config.store['candidate_proxies']:
return False
config.store['candidate_proxies'][proxy_str] = {
'proxy': proxy_str,
'source': source,
'protocol': protocol,
'found_at': time.time(),
}
return True
def scraper_loop() -> None:
logger.info(
'[scraper] started with %d source(s)',
len(config.PROXY_SOURCES)
)
while True:
total_seen = 0
total_added = 0
for url, fallback_protocol in config.PROXY_SOURCES:
try:
response = curl_requests.get(
url,
timeout=20,
headers=_HEADERS,
impersonate='chrome124',
allow_redirects=True,
)
text = response.text
found = {}
# Fast universal scan
for proxy in _extract_ip_port(text):
found[proxy] = fallback_protocol
# Deep parsing only when needed
if len(found) < 10:
# JSON parsing
try:
parsed = json.loads(text)
_walk(parsed, fallback_protocol, found)
except Exception:
pass
# HTML parsing
if '<html' in text.lower():
found.update(
_extract_from_html(
text,
fallback_protocol
)
)
source = (
url.split('//')[-1]
.split('/')[0]
.split('?')[0]
)
for proxy, protocol in found.items():
# SOCKS5 ONLY
if protocol != 'socks5':
continue
total_seen += 1
if add_candidate(
proxy,
source,
protocol
):
total_added += 1
except Exception as e:
logger.error(
'[scraper] failed [%s]: %s',
url,
e
)
logger.info(
'[scraper] cycle complete | seen=%d | added=%d',
total_seen,
total_added
)
time.sleep(600)
|