fix detect proxy
This commit is contained in:
@@ -219,3 +219,4 @@ __marimo__/
|
||||
# Streamlit
|
||||
.streamlit/secrets.toml
|
||||
data/tasks.json
|
||||
proxies.txt
|
||||
|
||||
+34
-21
@@ -104,43 +104,56 @@ class Proxy:
|
||||
return None
|
||||
|
||||
|
||||
def _check_proxy_sync(
|
||||
scheme: str,
|
||||
ip: str,
|
||||
port: str,
|
||||
login: Optional[str],
|
||||
password: Optional[str],
|
||||
timeout: float,
|
||||
test_url: str,
|
||||
) -> bool:
|
||||
"""Синхронная проверка прокси через requests + PySocks."""
|
||||
import requests
|
||||
if login and password:
|
||||
proxy_url = f"{scheme}://{login}:{password}@{ip}:{port}"
|
||||
else:
|
||||
proxy_url = f"{scheme}://{ip}:{port}"
|
||||
proxies = {"http": proxy_url, "https": proxy_url}
|
||||
try:
|
||||
r = requests.get(test_url, proxies=proxies, timeout=timeout)
|
||||
return r.status_code in (200, 204)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
async def detect_proxy_type(
|
||||
ip: str,
|
||||
port: str,
|
||||
login: Optional[str] = None,
|
||||
password: Optional[str] = None,
|
||||
timeout: float = 5.0,
|
||||
test_url: str = "http://api.ipify.org"
|
||||
test_url: str = "http://www.gstatic.com/generate_204"
|
||||
) -> Optional[ProxyType]:
|
||||
"""
|
||||
Определяет тип прокси подключением.
|
||||
Пробует SOCKS5 → SOCKS4 → HTTP. Возвращает первый рабочий или None.
|
||||
Учётные данные передаются в URL прокси (socks5://user:pass@ip:port).
|
||||
"""
|
||||
import aiohttp
|
||||
|
||||
candidates = [
|
||||
(ProxyType.SOCKS5, f"socks5://{ip}:{port}"),
|
||||
(ProxyType.SOCKS4, f"socks4://{ip}:{port}"),
|
||||
(ProxyType.HTTP, f"http://{ip}:{port}"),
|
||||
(ProxyType.SOCKS5, "socks5"),
|
||||
(ProxyType.SOCKS4, "socks4"),
|
||||
(ProxyType.HTTP, "http"),
|
||||
]
|
||||
|
||||
auth = aiohttp.BasicAuth(login, password) if login and password else None
|
||||
|
||||
for proxy_type, proxy_url in candidates:
|
||||
try:
|
||||
connector = aiohttp.TCPConnector(ssl=False)
|
||||
async with aiohttp.ClientSession(connector=connector) as session:
|
||||
async with session.get(
|
||||
test_url,
|
||||
proxy=proxy_url,
|
||||
proxy_auth=auth,
|
||||
timeout=aiohttp.ClientTimeout(total=timeout),
|
||||
) as resp:
|
||||
if resp.status == 200:
|
||||
loop = asyncio.get_event_loop()
|
||||
for proxy_type, scheme in candidates:
|
||||
ok = await loop.run_in_executor(
|
||||
None, _check_proxy_sync, scheme, ip, port, login, password, timeout, test_url
|
||||
)
|
||||
if ok:
|
||||
logger.info(f"Proxy {ip}:{port} detected as {proxy_type.value}")
|
||||
return proxy_type
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
logger.warning(f"Proxy {ip}:{port} — type not detected")
|
||||
return None
|
||||
|
||||
@@ -53,7 +53,7 @@ def _patch_camoufox():
|
||||
|
||||
def _patched_ip(proxy_string: str) -> str:
|
||||
global _socks5_real_ip
|
||||
if _socks5_real_ip and "127.0.0.1" in str(proxy_string):
|
||||
if _socks5_real_ip:
|
||||
return _socks5_real_ip
|
||||
return _orig_ip(proxy_string)
|
||||
|
||||
@@ -160,16 +160,7 @@ class BrowserService:
|
||||
reading_time
|
||||
)
|
||||
else:
|
||||
async with AsyncCamoufox(
|
||||
headless=True,
|
||||
geoip=False,
|
||||
humanize=True,
|
||||
locale="ru-RU",
|
||||
exclude_addons=[DefaultAddons.UBO]
|
||||
) as browser:
|
||||
result = await self._browse_page(
|
||||
browser, url, "direct", reading_time
|
||||
)
|
||||
return VisitResult(url=url, success=False, error="No proxy available")
|
||||
|
||||
if proxy:
|
||||
await self.proxy_manager.release_proxy(proxy, result.success)
|
||||
|
||||
Reference in New Issue
Block a user