fix proxy detect
This commit is contained in:
@@ -104,6 +104,48 @@ class Proxy:
|
||||
return None
|
||||
|
||||
|
||||
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"
|
||||
) -> Optional[ProxyType]:
|
||||
"""
|
||||
Определяет тип прокси подключением.
|
||||
Пробует SOCKS5 → SOCKS4 → HTTP. Возвращает первый рабочий или None.
|
||||
"""
|
||||
import aiohttp
|
||||
|
||||
candidates = [
|
||||
(ProxyType.SOCKS5, f"socks5://{ip}:{port}"),
|
||||
(ProxyType.SOCKS4, f"socks4://{ip}:{port}"),
|
||||
(ProxyType.HTTP, f"http://{ip}:{port}"),
|
||||
]
|
||||
|
||||
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:
|
||||
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
|
||||
|
||||
|
||||
class ProxyManager:
|
||||
"""
|
||||
Менеджер прокси с поддержкой SOCKS5 и HTTP.
|
||||
|
||||
Reference in New Issue
Block a user