This commit is contained in:
ddrwode
2026-02-27 17:36:31 +08:00
parent 19855500ab
commit db0fa81b2e
2 changed files with 58 additions and 21 deletions

View File

@@ -123,6 +123,13 @@ class FlowService:
def _choose_mobile_ua() -> str:
return random.choice(MOBILE_UA_POOL)
@staticmethod
def _infer_fingerprint_os(mobile_ua: str) -> str:
ua = (mobile_ua or "").lower()
if "iphone" in ua or "ipad" in ua or "cpu iphone os" in ua:
return "iOS"
return "Android"
@staticmethod
def _connect_page_from_start_data(start_data: dict[str, Any]) -> ChromiumPage:
port = start_data.get("port")
@@ -143,30 +150,37 @@ class FlowService:
proxy_port: int,
mobile_ua: str,
) -> dict[str, Any]:
fingerprint_os = FlowService._infer_fingerprint_os(mobile_ua)
proxy_candidates = [
{"protocol": "http", "host": proxy_host, "port": proxy_port},
{"protocol": "http", "host": proxy_host, "port": str(proxy_port)},
{"protocol": "http", "server": f"{proxy_host}:{proxy_port}"},
]
fingerprint_candidates = [
{"os": fingerprint_os, "userAgent": mobile_ua},
{"os": fingerprint_os},
{"os": "Android"},
]
last_error: Optional[Exception] = None
errors: list[str] = []
for proxy_conf in proxy_candidates:
try:
return client.create_browser(
browser_name=browser_name,
start_page_url=start_page_url,
proxy=proxy_conf,
fingerprint={
"os": "Android",
"platformVersion": 12,
"userAgent": mobile_ua,
},
)
except Exception as exc:
last_error = exc
continue
for fingerprint_conf in fingerprint_candidates:
try:
return client.create_browser(
browser_name=browser_name,
start_page_url=start_page_url,
proxy=proxy_conf,
fingerprint=fingerprint_conf,
)
except Exception as exc:
last_error = exc
errors.append(
f"proxy={proxy_conf}, fingerprint={fingerprint_conf}, error={exc}"
)
continue
raise RuntimeError(f"创建浏览器失败(代理配置尝试均失败): {last_error}")
detail = "; ".join(errors[-4:]) if errors else str(last_error)
raise RuntimeError(f"创建浏览器失败(代理/指纹配置尝试均失败): {detail}")
@staticmethod
def _apply_mobile_ua(page: ChromiumPage, ua: str) -> None: