Fetch dynamic TDC script and collect browser fingerprint payload.
| 37 | tokenid: str |
| 38 | info: dict[str, Any] |
| 39 | tdc_path: str |
| 40 | tdc_url: str |
| 41 | |
| 42 | def to_payload(self) -> dict[str, Any]: |
| 43 | return asdict(self) |
| 44 | |
| 45 | |
| 46 | class TencentTdcService: |
| 47 | """Fetch dynamic TDC script and collect browser fingerprint payload.""" |
| 48 | |
| 49 | def __init__( |
| 50 | self, |
| 51 | *, |
| 52 | settings: Settings | None = None, |
| 53 | http_client: FingerprintHttpClient | None = None, |
| 54 | ) -> None: |
| 55 | self.settings = settings or get_settings() |
| 56 | self.http_client = http_client or FingerprintHttpClient(self.settings) |
| 57 | self.cache_dir = self.settings.data_dir / "tdc_cache" |
| 58 | self.runner_path = PROJECT_ROOT / "app" / "services" / "tdc_vm_runner.cjs" |
| 59 | |
| 60 | def status_payload(self) -> dict[str, Any]: |
| 61 | return { |
| 62 | "node": self.settings.tencent_captcha_node, |
| 63 | "runner": str(self.runner_path), |
| 64 | "cache_dir": str(self.cache_dir), |
| 65 | } |
| 66 | |
| 67 | def collect_for_challenge( |
| 68 | self, |
| 69 | account: AccountRecord, |
| 70 | raw_challenge: dict[str, Any], |
| 71 | ) -> TdcCollectResult: |
| 72 | tdc_path = self.extract_tdc_path(raw_challenge) |
| 73 | if not tdc_path: |
| 74 | raise BadRequestError("challenge 里没有 tdc_path,没法生成 collect/eks") |
| 75 | |
| 76 | user_agent = resolve_user_agent(account.user_agent, account.browser_impersonate) |
| 77 | tdc_url = self._build_tdc_url(tdc_path) |
| 78 | tdc_code = self._fetch_script( |
| 79 | tdc_url, |
| 80 | account=account, |
| 81 | user_agent=user_agent, |
| 82 | ) |
| 83 | ft_code = self._fetch_ft_script(account=account, user_agent=user_agent) |
| 84 | result = self._run_vm( |
| 85 | tdc_code=tdc_code, |
| 86 | ft_code=ft_code, |
| 87 | account=account, |
| 88 | user_agent=user_agent, |
| 89 | ) |
| 90 | |
| 91 | return TdcCollectResult( |
| 92 | collect_raw=str(result.get("collectRaw") or ""), |
| 93 | collect=str(result.get("collect") or ""), |
| 94 | tlg=int(result.get("tlg") or 0), |
| 95 | eks=str(result.get("eks") or ""), |
| 96 | tokenid=str(result.get("tokenid") or ""), |