fix(huya): 为精英宝典兑换接入代理

This commit is contained in:
yml
2026-11-13 19:17:13 +08:00
parent 711be0e2b6
commit 563b32b7b7
5 changed files with 374 additions and 10 deletions
+73 -2
View File
@@ -12,7 +12,10 @@ from typing import Any
from loguru import logger
from sqlalchemy.orm import Session
from core.douyu.proxy_fetcher import ProxyFetcher
from ..models import HuyaTask
from ..models import ProxyConfig as ProxyConfigModel
from .huya_service import cookie_value
# 绑定跳转与充值下单共用(bind 与 recharge 两个域都引用了 sourceId)
@@ -47,6 +50,15 @@ class HuyaBatchRunnerCore:
self._counter_lock = threading.Lock()
self._started = 0
self._wss_sessions = threading.local()
self._proxy_cfg = None
self._proxy_fetcher = None
self._static_proxies = None
if self.task_type == "exchange_goods":
self._proxy_cfg = (
db.query(ProxyConfigModel).first() if db is not None else None
)
self._proxy_fetcher = self._create_proxy_fetcher()
self._static_proxies = self._resolve_static_proxies()
def stop(self):
self._stop.set()
@@ -130,11 +142,70 @@ class HuyaBatchRunnerCore:
)
return None
def _http_client(self):
def _create_proxy_fetcher(self) -> ProxyFetcher | None:
"""兑换任务使用现有全局代理 API,每个账号任务取一个新出口。"""
cfg = self._proxy_cfg
if not cfg or not cfg.enabled or not cfg.api_url:
return None
platform = getattr(cfg, "whitelist_platform", None) or "xiequ"
credentials = getattr(cfg, "whitelist_credentials", None)
if (
not credentials
and getattr(cfg, "whitelist_uid", "")
and getattr(cfg, "whitelist_ukey", "")
):
credentials = {
"uid": cfg.whitelist_uid,
"ukey": cfg.whitelist_ukey,
}
return ProxyFetcher(
api_url=cfg.api_url,
whitelist_platform=platform,
whitelist_credentials=(
credentials if getattr(cfg, "whitelist_enabled", False) else None
),
stop_event=self._stop,
)
def _resolve_static_proxies(self) -> dict[str, str] | None:
cfg = self._proxy_cfg
if not cfg or not cfg.enabled:
return None
http = str(getattr(cfg, "http", "") or "").strip()
https = str(getattr(cfg, "https", "") or "").strip()
if not http and not https:
return None
return {"http": http or https, "https": https or http}
def _exchange_proxies(self) -> tuple[dict[str, str] | None, str, str]:
"""返回兑换任务的代理、来源和错误;其他任务不会调用这里。"""
cfg = self._proxy_cfg
if not cfg or not cfg.enabled:
return None, "disabled", ""
if self._static_proxies:
return self._static_proxies, "static", ""
if self._proxy_fetcher:
try:
proxy_url = self._proxy_fetcher.fetch_new_proxy(max_attempts=3)
except Exception as exc: # noqa: BLE001 外部代理接口异常必须收敛到任务
return None, "error", f"获取代理失败: {exc}"
if proxy_url:
return (
{"http": proxy_url, "https": proxy_url},
"api",
"",
)
return None, "error", "代理 API 未返回可用代理"
return None, "error", "已启用代理,但未配置静态代理或代理 API"
def _http_client(self, proxies: dict[str, str] | None = None):
"""HTTP cdnws 通道客户端(无需网页设备态,方法签名与 WSS 会话保持一致)。"""
from core.huya.http_client import HuyaHttpClient
return HuyaHttpClient(logger=lambda message: self._push_log("info", message))
return HuyaHttpClient(
logger=lambda message: self._push_log("info", message),
proxies=proxies,
)
@staticmethod
def _web_wss_cookie_missing(cookie: str) -> tuple[str, ...]:
+19 -2
View File
@@ -40,7 +40,10 @@ class GoodsMixin:
def _push_log(self, level: str, message: str) -> None: ...
def _activity_client(self, uid: int, cookie: str): ...
def _shop_client(self, uid: int, cookie: str): ...
def _http_client(self): ...
def _http_client(self, proxies: dict[str, str] | None = None): ...
def _exchange_proxies(
self,
) -> tuple[dict[str, str] | None, str, str]: ...
def _probe_bind_status(
self, uid: int, cookie: str, b_act_id_int: int, outer_act_id: str = ""
): ...
@@ -444,7 +447,19 @@ class GoodsMixin:
pay_channel = self._normalize_pay_channel(
self.payload.get("pay_channel") or config_info.get("pay_channel")
)
client: Any = self._http_client()
proxies, proxy_source, proxy_error = self._exchange_proxies()
if proxy_error:
self._mark_task(
worker_db,
task,
"failed",
f"兑换代理不可用:{proxy_error}",
{"proxy_used": False, "proxy_source": "error"},
)
return
if proxies:
self._push_log("info", f"[{uid}] 兑换任务使用代理: {proxy_source}")
client: Any = self._http_client(proxies=proxies)
prepared = prepare_exchange(
client,
uid=uid,
@@ -464,6 +479,8 @@ class GoodsMixin:
"redeem_state": prepared.state.to_dict() if prepared.state else None,
"target": prepared.target,
"exchange_plan": prepared.plan,
"proxy_used": bool(proxies),
"proxy_source": proxy_source,
}
if prepared.status == EXCHANGE_STATUS_EXCHANGED:
result = {