refactor: 收口跨平台重复业务实现
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import asyncio
|
||||
import threading
|
||||
import uuid
|
||||
from dataclasses import dataclass
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -11,6 +12,7 @@ from core.douyu.proxy import (
|
||||
resolve_working_proxy,
|
||||
verify_proxy_url,
|
||||
)
|
||||
from core.douyu.proxy_fetcher import ProxyFetcher
|
||||
from core.douyu.proxy_platforms import create_adapter
|
||||
from core.douyu.proxy_platforms.base import _get_local_exit_ip
|
||||
|
||||
@@ -18,6 +20,52 @@ from ..models import AuditLog
|
||||
from ..models import ProxyConfig as ProxyConfigModel
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProxyRuntime:
|
||||
"""一次任务运行期间可复用的代理来源。"""
|
||||
|
||||
enabled: bool = False
|
||||
static_proxies: dict[str, str] | None = None
|
||||
api_fetcher: ProxyFetcher | None = None
|
||||
|
||||
|
||||
def build_proxy_runtime(
|
||||
cfg: ProxyConfigModel | None,
|
||||
*,
|
||||
enabled: bool = True,
|
||||
stop_event: threading.Event | None = None,
|
||||
) -> ProxyRuntime:
|
||||
"""把数据库代理配置规范成静态代理或 API 获取器。
|
||||
|
||||
调用方仍决定代理不可用时是失败还是降级直连;这里只负责唯一的配置映射,
|
||||
避免登录、CK 检测、注册和任务执行器各写一套白名单兼容逻辑。
|
||||
"""
|
||||
if not enabled or cfg is None or not cfg.enabled:
|
||||
return ProxyRuntime()
|
||||
|
||||
http = str(getattr(cfg, "http", "") or "").strip()
|
||||
https = str(getattr(cfg, "https", "") or "").strip()
|
||||
static_proxies = (
|
||||
{"http": http or https, "https": https or http} if http or https else None
|
||||
)
|
||||
|
||||
api_fetcher = None
|
||||
api_url = str(getattr(cfg, "api_url", "") or "").strip()
|
||||
if api_url:
|
||||
whitelist = _build_whitelist_params(cfg)
|
||||
api_fetcher = ProxyFetcher(
|
||||
api_url=api_url,
|
||||
whitelist_platform=whitelist["whitelist_platform"],
|
||||
whitelist_credentials=whitelist["whitelist_credentials"],
|
||||
stop_event=stop_event,
|
||||
)
|
||||
return ProxyRuntime(
|
||||
enabled=True,
|
||||
static_proxies=static_proxies,
|
||||
api_fetcher=api_fetcher,
|
||||
)
|
||||
|
||||
|
||||
def _build_whitelist_params(cfg: ProxyConfigModel) -> dict:
|
||||
"""从 ProxyConfig 构建白名单参数,自动处理新旧字段兼容。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user