From 0e97d7866da5bcfe6e46cadd8eea33be8a3e4920 Mon Sep 17 00:00:00 2001 From: pzy <2360718056@qq.com> Date: Mon, 13 Apr 2026 19:03:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(cf):=20=E8=B0=83=E6=8D=A2=20=5Fresolve=5Fpr?= =?UTF-8?q?ofile=20=E8=AF=BB=E5=8F=96=E4=BC=98=E5=85=88=E7=BA=A7=20?= =?UTF-8?q?=E2=80=94=20=E6=89=81=E5=B9=B3=20legacy=20=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E4=BC=98=E5=85=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题: 修复第一版里 _resolve_profile 优先读 proxy.clearance.user_agent(嵌套 v2 路径), fallback 才读 proxy.user_agent(扁平 v1 路径)。但 config.defaults.toml 里 proxy.clearance.user_agent 预设了占位 Chrome 136 UA(非空字符串),所以外部 cf-refresher sidecar 写到 proxy.user_agent 的新鲜 Firefox UA 永远被 defaults 盖掉。 结果 Cookie 里带的 cf_clearance 是用 Firefox UA 获取的,但实际请求 UA 变成了 Chrome 136。 Cloudflare 把 cf_clearance 绑定到首次获取时的 UA,UA 一变即 403。 修复: 扁平 legacy 路径(proxy.xxx)优先, 嵌套(proxy.clearance.xxx) 作为 fallback。 这样 cf-refresher 写扁平路径时能立即覆盖 defaults 的占位值。cf_clearance/cf_cookies 同样遵循此顺序以保持一致。 --- app/dataplane/proxy/adapters/headers.py | 29 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/app/dataplane/proxy/adapters/headers.py b/app/dataplane/proxy/adapters/headers.py index e175d115..20185385 100644 --- a/app/dataplane/proxy/adapters/headers.py +++ b/app/dataplane/proxy/adapters/headers.py @@ -167,23 +167,32 @@ def _resolve_profile(lease: ProxyLease | None) -> tuple[str, str, str]: empty string. This prevents bare requests with no User-Agent when running in direct mode or when FlareSolverr has not yet delivered a bundle. - Backward-compat: cf_clearance is also probed at the legacy ``proxy.cf_clearance`` - path so that configs migrated from v1.x (where clearance was refreshed by a - sidecar writing the flat key) keep working. The legacy ``proxy.cf_cookies`` - / ``proxy.user_agent`` paths are similarly honored as a last-resort fallback. + Priority order (flat legacy first, nested fallback second): + - ``proxy.cf_clearance`` / ``proxy.cf_cookies`` / ``proxy.user_agent`` + (flat v1.x-style keys, written by external cf-refresher sidecars) + - ``proxy.clearance.cf_clearance`` / ``proxy.clearance.cf_cookies`` / + ``proxy.clearance.user_agent`` (nested v2.x schema, populated by + ``config.defaults.toml`` and the internal clearance manager) + + Rationale: when an external sidecar refreshes cf_clearance and writes to + the flat keys, those values must *override* the stale defaults shipped in + ``config.defaults.toml`` under ``[proxy.clearance]``. Previously the order + was reversed, so a non-empty placeholder UA in defaults shadowed the fresh + UA supplied by the refresher, causing a UA/cf_clearance mismatch and a 403 + from Cloudflare (the cookie is bound to the UA of the acquiring browser). """ cfg = get_config() cfg_cookies = ( - cfg.get_str("proxy.clearance.cf_cookies", "") - or cfg.get_str("proxy.cf_cookies", "") + cfg.get_str("proxy.cf_cookies", "") + or cfg.get_str("proxy.clearance.cf_cookies", "") ) cfg_ua = ( - cfg.get_str("proxy.clearance.user_agent", "") - or cfg.get_str("proxy.user_agent", "") + cfg.get_str("proxy.user_agent", "") + or cfg.get_str("proxy.clearance.user_agent", "") ) cfg_clearance = ( - cfg.get_str("proxy.clearance.cf_clearance", "") - or cfg.get_str("proxy.cf_clearance", "") + cfg.get_str("proxy.cf_clearance", "") + or cfg.get_str("proxy.clearance.cf_clearance", "") ) if lease is not None: return (