完善自动注册代理和状态恢复
This commit is contained in:
@@ -7,12 +7,15 @@ import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional
|
||||
|
||||
from core.douyu.proxy_fetcher import ProxyFetcher
|
||||
from core.huya.auto_register import HuyaAutoRegisterResult, register_huya_with_sms_line
|
||||
from core.huya.cookie_utils import normalize_huya_cookie
|
||||
from core.sms_provider import SmsLine
|
||||
|
||||
from ..database import SessionLocal
|
||||
from ..models import ProxyConfig as ProxyConfigModel
|
||||
from .huya_service import upsert_huya_cookie
|
||||
|
||||
|
||||
@@ -88,6 +91,7 @@ class HuyaRegisterBatch:
|
||||
items: list[HuyaRegisterItemState]
|
||||
password_prefix: str = "hy"
|
||||
fixed_password: str = ""
|
||||
use_proxy: bool = False
|
||||
status: str = "pending"
|
||||
message: str = "等待开始"
|
||||
created_at: datetime = field(default_factory=_now)
|
||||
@@ -102,11 +106,39 @@ class HuyaRegisterRunner:
|
||||
self,
|
||||
batch: HuyaRegisterBatch,
|
||||
sms_lines: list[SmsLine],
|
||||
proxy_config: Optional[ProxyConfigModel] = None,
|
||||
):
|
||||
self.batch = batch
|
||||
self.sms_lines = sms_lines
|
||||
self.proxy_config = proxy_config
|
||||
self._lock = threading.Lock()
|
||||
self._stop = threading.Event()
|
||||
self._shared_proxy_fetcher = self._create_proxy_fetcher()
|
||||
|
||||
def _create_proxy_fetcher(self) -> ProxyFetcher | None:
|
||||
"""按需创建 API 代理获取器。"""
|
||||
if not self.batch.use_proxy or not self.proxy_config or not self.proxy_config.enabled:
|
||||
return None
|
||||
if not self.proxy_config.api_url:
|
||||
return None
|
||||
|
||||
wl_platform = "xiequ"
|
||||
wl_credentials = None
|
||||
if self.proxy_config.whitelist_enabled:
|
||||
wl_platform = getattr(self.proxy_config, "whitelist_platform", None) or "xiequ"
|
||||
wl_credentials = getattr(self.proxy_config, "whitelist_credentials", None)
|
||||
if not wl_credentials and self.proxy_config.whitelist_uid and self.proxy_config.whitelist_ukey:
|
||||
wl_credentials = {
|
||||
"uid": self.proxy_config.whitelist_uid,
|
||||
"ukey": self.proxy_config.whitelist_ukey,
|
||||
}
|
||||
|
||||
return ProxyFetcher(
|
||||
api_url=self.proxy_config.api_url,
|
||||
whitelist_platform=wl_platform,
|
||||
whitelist_credentials=wl_credentials,
|
||||
stop_event=self._stop,
|
||||
)
|
||||
|
||||
def stop(self):
|
||||
self._stop.set()
|
||||
@@ -131,6 +163,7 @@ class HuyaRegisterRunner:
|
||||
"wait_seconds": self.batch.wait_seconds,
|
||||
"poll_interval": self.batch.poll_interval,
|
||||
"password_prefix": self.batch.password_prefix,
|
||||
"use_proxy": self.batch.use_proxy,
|
||||
"total": total,
|
||||
"success_count": success,
|
||||
"failed_count": failed,
|
||||
@@ -166,11 +199,35 @@ class HuyaRegisterRunner:
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def _resolve_proxy(self) -> tuple[dict[str, str] | None, str]:
|
||||
"""为单个手机号解析代理;返回代理字典和错误消息。"""
|
||||
if not self.batch.use_proxy:
|
||||
return None, ""
|
||||
if not self.proxy_config or not self.proxy_config.enabled:
|
||||
return None, "已开启代理,但代理配置未启用"
|
||||
|
||||
if self.proxy_config.http or self.proxy_config.https:
|
||||
proxy_url = self.proxy_config.http or self.proxy_config.https
|
||||
return {"http": proxy_url, "https": proxy_url}, ""
|
||||
|
||||
if self._shared_proxy_fetcher:
|
||||
proxy_url = self._shared_proxy_fetcher.fetch_new_proxy(max_attempts=3)
|
||||
if proxy_url:
|
||||
return {"http": proxy_url, "https": proxy_url}, ""
|
||||
return None, "获取代理失败"
|
||||
|
||||
return None, "已开启代理,但未配置静态代理或代理 API"
|
||||
|
||||
def _run_one(self, index: int, item: SmsLine):
|
||||
if self._stop.is_set():
|
||||
self._set_item(index, status="stopped", message="已停止", finished_at=_now())
|
||||
return
|
||||
|
||||
proxies, proxy_error = self._resolve_proxy()
|
||||
if proxy_error:
|
||||
self._set_item(index, status="error", message=proxy_error, finished_at=_now())
|
||||
return
|
||||
|
||||
self._set_item(index, status="sending", message="注册并改密", started_at=_now(), finished_at=None)
|
||||
result = register_huya_with_sms_line(
|
||||
item,
|
||||
@@ -178,6 +235,7 @@ class HuyaRegisterRunner:
|
||||
poll_interval=self.batch.poll_interval,
|
||||
password_prefix=self.batch.password_prefix,
|
||||
fixed_password=self.batch.fixed_password,
|
||||
proxies=proxies,
|
||||
stop_event=self._stop,
|
||||
)
|
||||
|
||||
@@ -236,6 +294,12 @@ class HuyaRegisterRunner:
|
||||
self.batch.started_at = _now()
|
||||
|
||||
try:
|
||||
if self._shared_proxy_fetcher:
|
||||
ok, msg = self._shared_proxy_fetcher.warmup_whitelist()
|
||||
if not ok:
|
||||
with self._lock:
|
||||
self.batch.message = f"代理白名单预热失败: {msg}"
|
||||
|
||||
with ThreadPoolExecutor(max_workers=self.batch.concurrency) as executor:
|
||||
futures = []
|
||||
for index, item in enumerate(self.sms_lines):
|
||||
@@ -280,6 +344,8 @@ class HuyaRegisterRegistry:
|
||||
poll_interval: float,
|
||||
password_prefix: str = "hy",
|
||||
fixed_password: str = "",
|
||||
use_proxy: bool = False,
|
||||
proxy_config: Optional[ProxyConfigModel] = None,
|
||||
) -> HuyaRegisterRunner:
|
||||
batch_id = uuid.uuid4().hex[:12]
|
||||
batch = HuyaRegisterBatch(
|
||||
@@ -291,12 +357,13 @@ class HuyaRegisterRegistry:
|
||||
poll_interval=max(1.0, float(poll_interval or 5)),
|
||||
password_prefix=(password_prefix or "hy").strip()[:8] or "hy",
|
||||
fixed_password=(fixed_password or "").strip(),
|
||||
use_proxy=bool(use_proxy),
|
||||
items=[
|
||||
HuyaRegisterItemState(line=index + 1, phone=item.phone, provider=item.provider, sms_url=item.url)
|
||||
for index, item in enumerate(sms_lines)
|
||||
],
|
||||
)
|
||||
runner = HuyaRegisterRunner(batch=batch, sms_lines=sms_lines)
|
||||
runner = HuyaRegisterRunner(batch=batch, sms_lines=sms_lines, proxy_config=proxy_config)
|
||||
with self._lock:
|
||||
self._runners[batch_id] = runner
|
||||
return runner
|
||||
|
||||
Reference in New Issue
Block a user