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