feat: 为出站请求统一接入 HTTP 代理

This commit is contained in:
2026-08-14 16:08:39 +08:00
parent 3dc2aeb3a2
commit fabca9510d
23 changed files with 213 additions and 14 deletions
+1
View File
@@ -61,6 +61,7 @@ def build_container() -> TradingContainer:
client = GatewayClient(
settings.order_gateway_url,
settings.bearer_token,
settings=settings,
timeout=max(60.0, settings.lease_max_wait_seconds + 10),
)
local_db = LocalDB(settings.trading_db_path_resolved)
+2 -1
View File
@@ -37,6 +37,7 @@ from typing import Any
import httpx
from app.shared.config import Settings
from app.shared.proxy import httpx_client_options
from app.shared.errors import NotLoggedInError, UpstreamRequestError
from app.trading.core import auth_site
@@ -110,7 +111,7 @@ class AuthSession:
headers=profile.headers(),
timeout=self._settings.request_timeout_seconds,
follow_redirects=True,
proxy=self._settings.httpx_proxy,
**httpx_client_options(self._settings),
http2=True,
)
self._sites[name] = _SiteAuth(name=name, client=client)
+2 -1
View File
@@ -25,6 +25,7 @@ from typing import Any
import yaml
from app.shared.config import BASE_DIR, Settings
from app.shared.proxy import playwright_launch_proxy
from app.trading.core import auth_site
logger = logging.getLogger(__name__)
@@ -355,7 +356,7 @@ async def login_one(
user_data_dir=str(account.user_data_dir),
headless=False, # 自动登录仍需有头:撞验证码要人工接管
channel=settings.browser_channel or None,
proxy=settings.playwright_proxy,
proxy=playwright_launch_proxy(settings),
user_agent=profile.user_agent,
locale="ja-JP",
timezone_id="Asia/Tokyo",
+6 -1
View File
@@ -16,7 +16,9 @@ from typing import Any
import httpx
from app.shared.config import Settings
from app.shared.errors import AppError
from app.shared.proxy import httpx_client_options
from app.shared.task_state import OrderState, TaskStatus
from app.trading.worker.models import LeaseTask
@@ -29,13 +31,16 @@ class GatewayClient:
一份 AsyncClient 实例贯穿 worker 整个生命周期,连接池由 httpx 管理。
"""
def __init__(self, base_url: str, bearer_token: str, *, timeout: float = 60.0):
def __init__(
self, base_url: str, bearer_token: str, *, settings: Settings, timeout: float = 60.0
):
# 末尾去斜杠,避免 base + "/api/..." 拼出双斜杠
self._base_url = base_url.rstrip("/")
self._client = httpx.AsyncClient(
base_url=self._base_url,
headers={"Authorization": f"Bearer {bearer_token}"},
timeout=timeout,
**httpx_client_options(settings, target_url=self._base_url),
)
async def aclose(self) -> None:
+2
View File
@@ -97,6 +97,7 @@ from app.shared.purchase_contract import (
basket_domain_of,
inventory_flag_for,
)
from app.shared.proxy import playwright_launch_proxy
from app.shared.task_state import OrderState
from app.trading.core import auth_site
from app.trading.worker.models import LeaseTask
@@ -556,6 +557,7 @@ class SiteInteractor:
# 状态的操作(加购/结算/支付)都必须走非无头浏览器
headless=False,
channel=self._settings.browser_channel or None,
proxy=playwright_launch_proxy(self._settings),
args=["--no-first-run", "--disable-blink-features=AutomationControlled"],
)
self._context = await self._browser.new_context(