fix proxie
This commit is contained in:
@@ -133,7 +133,7 @@ async def detect_proxy_type(
|
||||
login: Optional[str] = None,
|
||||
password: Optional[str] = None,
|
||||
timeout: float = 5.0,
|
||||
test_url: str = "http://www.gstatic.com/generate_204"
|
||||
test_url: str = "https://www.google.com/generate_204"
|
||||
) -> Optional[ProxyType]:
|
||||
"""
|
||||
Определяет тип прокси подключением.
|
||||
|
||||
+28
-10
@@ -21,7 +21,16 @@ from services.socks5_to_http_proxy import Socks5ProxyPool
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
_socks5_real_ip: Optional[str] = None
|
||||
_current_proxy_ip: Optional[str] = None
|
||||
|
||||
|
||||
def _get_real_ip() -> Optional[str]:
|
||||
return _current_proxy_ip
|
||||
|
||||
|
||||
def _set_real_ip(ip: Optional[str]) -> None:
|
||||
global _current_proxy_ip
|
||||
_current_proxy_ip = ip
|
||||
|
||||
|
||||
def _patch_camoufox():
|
||||
@@ -54,9 +63,11 @@ def _patch_camoufox():
|
||||
_orig_ip = _ci.public_ip
|
||||
|
||||
def _patched_ip(proxy_string: str = None) -> str:
|
||||
global _socks5_real_ip
|
||||
if _socks5_real_ip:
|
||||
return _socks5_real_ip
|
||||
ip = _get_real_ip()
|
||||
if ip:
|
||||
logger.debug(f"public_ip() intercepted → returning {ip}")
|
||||
return ip
|
||||
logger.warning(f"public_ip() not set, proxy_string={proxy_string}")
|
||||
if proxy_string is not None:
|
||||
return _orig_ip(proxy_string)
|
||||
return _orig_ip()
|
||||
@@ -127,25 +138,25 @@ class BrowserService:
|
||||
if self.proxy_manager.has_proxies():
|
||||
proxy = await self.proxy_manager.acquire_proxy(timeout=30)
|
||||
|
||||
global _socks5_real_ip
|
||||
proxy_locale = None
|
||||
|
||||
if proxy and proxy.proxy_type in [ProxyType.SOCKS5, ProxyType.SOCKS4]:
|
||||
proxy_config = await self.socks5_pool.get_proxy_config(proxy)
|
||||
use_socks5 = True
|
||||
real_ip = proxy.id.split("://")[-1].split(":")[0]
|
||||
_socks5_real_ip = real_ip
|
||||
_set_real_ip(real_ip)
|
||||
proxy_locale = _locale_for_ip(real_ip)
|
||||
logger.info(f"Using SOCKS5 tunnel for: {proxy.id} locale={proxy_locale}")
|
||||
has_auth = bool(proxy.login and proxy.password)
|
||||
logger.info(f"Using SOCKS5 tunnel for: {proxy.id} auth={'yes' if has_auth else 'NO'} locale={proxy_locale}")
|
||||
elif proxy:
|
||||
proxy_config = proxy.proxy_config
|
||||
real_ip = proxy.id.split("://")[-1].split(":")[0]
|
||||
_socks5_real_ip = real_ip
|
||||
_set_real_ip(real_ip)
|
||||
proxy_locale = _locale_for_ip(real_ip)
|
||||
logger.info(f"Using HTTP proxy: {proxy.id} locale={proxy_locale}")
|
||||
else:
|
||||
proxy_config = None
|
||||
_socks5_real_ip = None
|
||||
_set_real_ip(None)
|
||||
logger.info("Direct connection")
|
||||
|
||||
if proxy_config:
|
||||
@@ -172,6 +183,13 @@ class BrowserService:
|
||||
if use_socks5:
|
||||
await self.socks5_pool.release(proxy)
|
||||
|
||||
# Если таймаут или соединение не прошло — помечаем прокси как нерабочий и меняем
|
||||
if not result.success and result.error and any(
|
||||
k in result.error for k in ("Timeout", "ERR_", "Connection", "SOCKS")
|
||||
):
|
||||
logger.warning(f"Visit attempt {attempt + 1} bad proxy, retrying with new")
|
||||
continue
|
||||
|
||||
return result
|
||||
|
||||
except (ValueError, InvalidIP) as e:
|
||||
@@ -208,7 +226,7 @@ class BrowserService:
|
||||
})
|
||||
|
||||
goto_task = asyncio.create_task(
|
||||
page.goto(url, wait_until="commit", timeout=30000)
|
||||
page.goto(url, wait_until="commit", timeout=60000)
|
||||
)
|
||||
await self._move_mouse(page, goto_task)
|
||||
await goto_task
|
||||
|
||||
@@ -182,13 +182,15 @@ class Socks5ToHttpProxy:
|
||||
# Создаем SOCKS5 соединение
|
||||
remote_socket = socks.socksocket()
|
||||
remote_socket.settimeout(15)
|
||||
|
||||
|
||||
if self.username:
|
||||
logger.debug(f"SOCKS5 connect {host}:{port} via {self.socks5_host} auth=yes user={self.username[:4]}***")
|
||||
remote_socket.set_proxy(
|
||||
socks.SOCKS5, self.socks5_host, self.socks5_port,
|
||||
username=self.username, password=self.password
|
||||
)
|
||||
else:
|
||||
logger.warning(f"SOCKS5 connect {host}:{port} via {self.socks5_host} auth=NO — credentials missing!")
|
||||
remote_socket.set_proxy(
|
||||
socks.SOCKS5, self.socks5_host, self.socks5_port
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user