@@ -194,25 +194,20 @@ def _response_debug_detail(resp, limit: int = 800) -> str:
194194def _is_cloudflare_challenge (resp ) -> bool :
195195 if resp is None :
196196 return False
197- text = str (getattr (resp , "text" , "" ) or "" ).lower ()
198- headers = getattr (resp , "headers" , {}) or {}
199- server = str (headers .get ("server" ) or "" ).lower ()
200197 try :
201198 status_code = int (getattr (resp , "status_code" , 0 ) or 0 )
202199 except (TypeError , ValueError ):
203200 status_code = 0
204-
205- explicit_challenge_markers = (
206- "challenges.cloudflare.com" in text
207- or "<title>just a moment" in text
208- or "/cdn-cgi/challenge-platform/" in text
201+ if status_code not in (403 , 503 ):
202+ return False
203+ text = str (getattr (resp , "text" , "" ) or "" ).lower ()
204+ return (
205+ "<title>just a moment" in text
206+ or "<title>attention required! | cloudflare" in text
209207 or "cf-chl-" in text
210208 or "__cf_chl_" in text
209+ or "cf-browser-verification" in text
211210 )
212- if explicit_challenge_markers :
213- return True
214-
215- return "cloudflare" in server and status_code in (403 , 503 )
216211
217212
218213def _mail_config () -> dict :
@@ -300,10 +295,11 @@ def _headers_with_clearance(
300295 return normalized
301296
302297
303- def _cloudflare_block_message (resp , prefix : str = "被 Cloudflare 拦截" ) -> str :
298+ def _cloudflare_block_message (resp , prefix : str = "被 Cloudflare 拦截" , reason : str = "" ) -> str :
304299 status = getattr (resp , "status_code" , "unknown" )
305300 debug = _response_debug_detail (resp )
306- return f"{ prefix } ,clearance 刷新失败或重试后仍失败,请更换 IP/代理重试: status={ status } , { debug } "
301+ reason = reason or "clearance 刷新失败或重试后仍失败,请更换 IP/代理重试"
302+ return f"{ prefix } ,{ reason } : status={ status } , { debug } "
307303
308304
309305def request_with_local_retry (session : requests .Session , method : str , url : str , retry_attempts : int = 3 , ** kwargs ):
@@ -386,6 +382,7 @@ def __init__(self, proxy: str = "") -> None:
386382 self .proxy = str (proxy or "" ).strip ()
387383 self .session = create_session (self .proxy )
388384 self .clearance_user_agent = ""
385+ self .clearance_failure_reason = ""
389386 self .device_id = str (uuid .uuid4 ())
390387 self .code_verifier = ""
391388 self .platform_auth_code = ""
@@ -407,6 +404,14 @@ def _json_headers(self, referer: str) -> dict[str, str]:
407404 return headers
408405
409406 def _refresh_cloudflare_clearance (self , target_url : str , index : int ) -> ClearanceBundle | None :
407+ self .clearance_failure_reason = ""
408+ profile = proxy_settings .get_profile (proxy = self .proxy , upstream = True )
409+ if not profile .clearance_enabled :
410+ self .clearance_failure_reason = (
411+ "可尝试使用 FlareSolverr 清障方式,注意需要 Docker 部署 flaresolverr、privoxy、warp-proxy 等相关容器"
412+ )
413+ step (index , f"检测到 Cloudflare 拦截,{ self .clearance_failure_reason } " , "yellow" )
414+ return None
410415 step (index , "检测到 Cloudflare 拦截,尝试刷新 clearance" , "yellow" )
411416 bundle = proxy_settings .refresh_clearance (
412417 target_url = target_url ,
@@ -418,6 +423,9 @@ def _refresh_cloudflare_clearance(self, target_url: str, index: int) -> Clearanc
418423 _apply_clearance_to_session (self .session , bundle )
419424 self .clearance_user_agent = bundle .user_agent or self .clearance_user_agent
420425 step (index , "Cloudflare clearance 刷新完成,重试当前请求" , "yellow" )
426+ else :
427+ self .clearance_failure_reason = "clearance 刷新未返回可用 Cookie,请检查 FlareSolverr URL、代理和出口 IP"
428+ step (index , f"Cloudflare clearance 刷新失败:{ self .clearance_failure_reason } " , "yellow" )
421429 return bundle
422430
423431 def _platform_authorize (self , email : str , index : int ) -> None :
@@ -453,7 +461,7 @@ def _platform_authorize(self, email: str, index: int) -> None:
453461 if _is_cloudflare_challenge (resp ):
454462 bundle = self ._refresh_cloudflare_clearance (auth_base , index )
455463 if bundle is None :
456- raise RuntimeError (_cloudflare_block_message (resp ))
464+ raise RuntimeError (_cloudflare_block_message (resp , reason = self . clearance_failure_reason ))
457465 retry_headers = _headers_with_clearance (self ._navigate_headers (f"{ platform_base } /" ), target_url , self .proxy , self .clearance_user_agent )
458466 resp , error = request_with_local_retry (self .session , "get" , target_url , headers = retry_headers , allow_redirects = True , verify = False )
459467 if _is_cloudflare_challenge (resp ):
@@ -479,7 +487,7 @@ def _register_user(self, email: str, password: str, index: int) -> None:
479487 if _is_cloudflare_challenge (resp ):
480488 bundle = self ._refresh_cloudflare_clearance (auth_base , index )
481489 if bundle is None :
482- raise RuntimeError (_cloudflare_block_message (resp ))
490+ raise RuntimeError (_cloudflare_block_message (resp , reason = self . clearance_failure_reason ))
483491 headers = self ._json_headers (f"{ auth_base } /create-account/password" )
484492 headers ["openai-sentinel-token" ] = build_sentinel_token (self .session , self .device_id , "username_password_create" )
485493 headers = _headers_with_clearance (headers , url , self .proxy , self .clearance_user_agent )
@@ -502,7 +510,7 @@ def _send_otp(self, index: int) -> None:
502510 if _is_cloudflare_challenge (resp ):
503511 bundle = self ._refresh_cloudflare_clearance (auth_base , index )
504512 if bundle is None :
505- raise RuntimeError (_cloudflare_block_message (resp ))
513+ raise RuntimeError (_cloudflare_block_message (resp , reason = self . clearance_failure_reason ))
506514 headers = _headers_with_clearance (self ._navigate_headers (f"{ auth_base } /create-account/password" ), url , self .proxy , self .clearance_user_agent )
507515 resp , error = request_with_local_retry (self .session , "get" , url , headers = headers , allow_redirects = True , verify = False )
508516 if _is_cloudflare_challenge (resp ):
@@ -533,7 +541,7 @@ def _create_account(self, name: str, birthdate: str, index: int) -> None:
533541 if _is_cloudflare_challenge (resp ):
534542 bundle = self ._refresh_cloudflare_clearance (auth_base , index )
535543 if bundle is None :
536- raise RuntimeError (_cloudflare_block_message (resp ))
544+ raise RuntimeError (_cloudflare_block_message (resp , reason = self . clearance_failure_reason ))
537545 headers = self ._json_headers (f"{ auth_base } /about-you" )
538546 headers ["openai-sentinel-token" ] = build_sentinel_token (self .session , self .device_id , "oauth_create_account" )
539547 headers = _headers_with_clearance (headers , url , self .proxy , self .clearance_user_agent )
0 commit comments