feat: 为出站请求统一接入 HTTP 代理
This commit is contained in:
@@ -48,6 +48,7 @@ async def _start_visible(site: SiteInteractor, settings) -> None:
|
||||
site._browser = await site._playwright.chromium.launch(
|
||||
headless=False,
|
||||
channel=settings.browser_channel or None,
|
||||
proxy=settings.playwright_proxy,
|
||||
args=["--no-first-run", "--disable-blink-features=AutomationControlled"],
|
||||
)
|
||||
site._context = await site._browser.new_context(
|
||||
|
||||
@@ -24,7 +24,8 @@ sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
import httpx
|
||||
|
||||
from app.shared.config import get_settings
|
||||
from app.shared.config import Settings, get_settings
|
||||
from app.shared.proxy import httpx_client_options
|
||||
from app.trading.core import auth_site
|
||||
|
||||
PROBE_DIR = Path(__file__).resolve().parent.parent / ".probe" / "checkout"
|
||||
@@ -42,12 +43,13 @@ def load_cookies(state_path: Path) -> list[dict]:
|
||||
return state.get("cookies", [])
|
||||
|
||||
|
||||
def build_client(cookies: list[dict]) -> httpx.AsyncClient:
|
||||
def build_client(cookies: list[dict], settings: Settings) -> httpx.AsyncClient:
|
||||
client = httpx.AsyncClient(
|
||||
headers=auth_site.PROFILES["rakuten"].headers(),
|
||||
timeout=30.0,
|
||||
follow_redirects=True,
|
||||
http2=True,
|
||||
**httpx_client_options(settings),
|
||||
)
|
||||
for cookie in cookies:
|
||||
name = cookie.get("name")
|
||||
@@ -210,7 +212,7 @@ async def main() -> int:
|
||||
cookies = load_cookies(state_path)
|
||||
print(f"加载 {len(cookies)} 条 cookie 自 {state_path}")
|
||||
|
||||
async with build_client(cookies) as client:
|
||||
async with build_client(cookies, settings) as client:
|
||||
await probe_readonly(client)
|
||||
if args.mutate:
|
||||
if not args.item_url:
|
||||
|
||||
@@ -56,6 +56,7 @@ async def run(item_url: str, *, headful: bool) -> int:
|
||||
browser = await pw.chromium.launch(
|
||||
headless=not headful,
|
||||
channel=settings.browser_channel or None,
|
||||
proxy=settings.playwright_proxy,
|
||||
args=["--no-first-run", "--disable-blink-features=AutomationControlled"],
|
||||
)
|
||||
context = await browser.new_context(
|
||||
|
||||
@@ -48,6 +48,7 @@ async def _start_visible(site: SiteInteractor, settings) -> None:
|
||||
site._browser = await site._playwright.chromium.launch(
|
||||
headless=False,
|
||||
channel=settings.browser_channel or None,
|
||||
proxy=settings.playwright_proxy,
|
||||
args=["--no-first-run", "--disable-blink-features=AutomationControlled"],
|
||||
)
|
||||
site._context = await site._browser.new_context(
|
||||
|
||||
@@ -48,6 +48,7 @@ async def run(*, headful: bool) -> int:
|
||||
browser = await pw.chromium.launch(
|
||||
headless=not headful,
|
||||
channel=settings.browser_channel or None,
|
||||
proxy=settings.playwright_proxy,
|
||||
args=["--no-first-run", "--disable-blink-features=AutomationControlled"],
|
||||
)
|
||||
context = await browser.new_context(
|
||||
|
||||
@@ -53,6 +53,7 @@ async def _start_visible(site: SiteInteractor, settings) -> None:
|
||||
site._browser = await site._playwright.chromium.launch(
|
||||
headless=False,
|
||||
channel=settings.browser_channel or None,
|
||||
proxy=settings.playwright_proxy,
|
||||
args=["--no-first-run", "--disable-blink-features=AutomationControlled"],
|
||||
)
|
||||
site._context = await site._browser.new_context(
|
||||
|
||||
@@ -164,7 +164,11 @@ async def main() -> int:
|
||||
log: list[str] = []
|
||||
|
||||
async with async_playwright() as pw:
|
||||
browser = await pw.chromium.launch(headless=True, args=["--no-first-run"])
|
||||
browser = await pw.chromium.launch(
|
||||
headless=True,
|
||||
proxy=settings.playwright_proxy,
|
||||
args=["--no-first-run"],
|
||||
)
|
||||
context = await browser.new_context(
|
||||
storage_state=state_path,
|
||||
user_agent=auth_site.RAKUTEN_USER_AGENT,
|
||||
|
||||
@@ -179,7 +179,11 @@ async def main() -> int:
|
||||
log: list[str] = []
|
||||
|
||||
async with async_playwright() as pw:
|
||||
browser = await pw.chromium.launch(headless=True, args=["--no-first-run"])
|
||||
browser = await pw.chromium.launch(
|
||||
headless=True,
|
||||
proxy=settings.playwright_proxy,
|
||||
args=["--no-first-run"],
|
||||
)
|
||||
context = await browser.new_context(
|
||||
storage_state=state_path,
|
||||
user_agent=auth_site.RAKUTEN_USER_AGENT,
|
||||
|
||||
@@ -121,6 +121,7 @@ async def main() -> int:
|
||||
browser = await pw.chromium.launch(
|
||||
headless=True,
|
||||
channel=settings.browser_channel or None,
|
||||
proxy=settings.playwright_proxy,
|
||||
args=["--no-first-run", "--disable-blink-features=AutomationControlled"],
|
||||
)
|
||||
context = await browser.new_context(
|
||||
|
||||
@@ -723,6 +723,7 @@ async def _start_visible(site: SiteInteractor, settings) -> None:
|
||||
site._browser = await site._playwright.chromium.launch( # noqa: SLF001
|
||||
headless=False,
|
||||
channel=settings.browser_channel or None,
|
||||
proxy=settings.playwright_proxy,
|
||||
args=["--no-first-run", "--disable-blink-features=AutomationControlled"],
|
||||
)
|
||||
site._context = await site._browser.new_context( # noqa: SLF001
|
||||
|
||||
Reference in New Issue
Block a user