|
@@ -202,6 +202,92 @@ class VfsPlugin(IPlugin):
|
|
|
return False
|
|
return False
|
|
|
return True
|
|
return True
|
|
|
|
|
|
|
|
|
|
+ def _check_page_blocked(self, stage: str = "login page"):
|
|
|
|
|
+ """
|
|
|
|
|
+ 检查当前页面是否出现 403 / Cloudflare WAF / VFS 403201 等 IP 拦截。
|
|
|
|
|
+ 如果页面直接返回 {"code": "403201"} 或提示 403/Block,立即抛出 PermissionDeniedError 快速失败,触发上层销毁实例并轮换代理。
|
|
|
|
|
+ """
|
|
|
|
|
+ if not self.page:
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ # 1. 检查页面是否直接返回了 JSON 拦截对象 (如 {"code": "403201", "description": ""})
|
|
|
|
|
+ page_json = getattr(self.page, "json", None)
|
|
|
|
|
+ if isinstance(page_json, dict):
|
|
|
|
|
+ code = str(page_json.get("code", "")).strip()
|
|
|
|
|
+ if code in ("403201", "403202") or "40320" in code:
|
|
|
|
|
+ self._log(f"🛡️ [IP Blocked] VFS {stage} returned JSON block code '{code}': {page_json}")
|
|
|
|
|
+ raise PermissionDeniedError(f"IP Blocked (VFS {code}): {page_json}")
|
|
|
|
|
+
|
|
|
|
|
+ # 2. 检查页面正文是否为 JSON 格式的拦截响应
|
|
|
|
|
+ body_text = ""
|
|
|
|
|
+ try:
|
|
|
|
|
+ body_el = self.page.ele("tag:body")
|
|
|
|
|
+ if body_el:
|
|
|
|
|
+ body_text = (body_el.text or "").strip()
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+ if body_text and body_text.startswith("{") and body_text.endswith("}"):
|
|
|
|
|
+ try:
|
|
|
|
|
+ data = json.loads(body_text)
|
|
|
|
|
+ if isinstance(data, dict):
|
|
|
|
|
+ code = str(data.get("code", "")).strip()
|
|
|
|
|
+ if code in ("403201", "403202") or "40320" in code:
|
|
|
|
|
+ self._log(f"🛡️ [IP Blocked] VFS {stage} body JSON block '{code}': {body_text}")
|
|
|
|
|
+ raise PermissionDeniedError(f"IP Blocked (VFS {code}): {body_text}")
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+ # 3. 检查响应状态码(若支持)
|
|
|
|
|
+ resp = getattr(self.page, "response", None)
|
|
|
|
|
+ if resp and getattr(resp, "status_code", None) == 403:
|
|
|
|
|
+ self._log(f"🛡️ [IP Blocked] HTTP 403 response detected on {stage}.")
|
|
|
|
|
+ raise PermissionDeniedError(f"HTTP 403 Forbidden: Blocked on {stage}")
|
|
|
|
|
+
|
|
|
|
|
+ # 4. 检查页面 Title
|
|
|
|
|
+ title = (self.page.title or "").strip()
|
|
|
|
|
+ title_lower = title.lower()
|
|
|
|
|
+ if "403" in title_lower or "forbidden" in title_lower or "access denied" in title_lower:
|
|
|
|
|
+ self._log(f"🛡️ [IP Blocked] 403 / Access Denied detected in title '{title}' on {stage}.")
|
|
|
|
|
+ raise PermissionDeniedError(f"403 Forbidden detected in page title: '{title}'")
|
|
|
|
|
+
|
|
|
|
|
+ # 5. 检查页面 HTML / 文本特征
|
|
|
|
|
+ html = (getattr(self.page, "html", "") or "").lower()
|
|
|
|
|
+ raw_html = (getattr(self.page, "raw_html", "") or "").lower()
|
|
|
|
|
+ combined_html = f"{html} {raw_html}"
|
|
|
|
|
+
|
|
|
|
|
+ block_signatures = [
|
|
|
|
|
+ "403201",
|
|
|
|
|
+ "403202",
|
|
|
|
|
+ "\"code\": \"403201\"",
|
|
|
|
|
+ "\"code\":\"403201\"",
|
|
|
|
|
+ "\"code\": \"403202\"",
|
|
|
|
|
+ "\"code\":\"403202\"",
|
|
|
|
|
+ "sorry, you have been blocked",
|
|
|
|
|
+ "403 forbidden",
|
|
|
|
|
+ "you are unable to access vfsglobal.com",
|
|
|
|
|
+ "has banned your ip address",
|
|
|
|
|
+ "the owner of this website has banned your ip address",
|
|
|
|
|
+ "error code 1020",
|
|
|
|
|
+ "error code 1015",
|
|
|
|
|
+ "error 403",
|
|
|
|
|
+ ]
|
|
|
|
|
+ for sig in block_signatures:
|
|
|
|
|
+ if sig in combined_html or (body_text and sig in body_text.lower()):
|
|
|
|
|
+ self._log(f"🛡️ [IP Blocked] Block signature '{sig}' detected on {stage}.")
|
|
|
|
|
+ raise PermissionDeniedError(f"IP Blocked: VFS returned '{sig}' on {stage}")
|
|
|
|
|
+
|
|
|
|
|
+ # 6. 检查 Cloudflare 拦截综合特征
|
|
|
|
|
+ if ("attention required" in title_lower or "cloudflare" in title_lower) and ("blocked" in combined_html or "access denied" in combined_html):
|
|
|
|
|
+ self._log(f"🛡️ [IP Blocked] Cloudflare attention/block page detected on {stage}.")
|
|
|
|
|
+ raise PermissionDeniedError(f"403 Forbidden: Blocked by Cloudflare WAF on {stage}")
|
|
|
|
|
+
|
|
|
|
|
+ except PermissionDeniedError:
|
|
|
|
|
+ raise
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
def _prepare_clean_page(self):
|
|
def _prepare_clean_page(self):
|
|
|
"""进入极轻量 404 页面并注入独占 Turnstile 容器 (绕过复杂首页)"""
|
|
"""进入极轻量 404 页面并注入独占 Turnstile 容器 (绕过复杂首页)"""
|
|
|
if self.injected:
|
|
if self.injected:
|
|
@@ -237,23 +323,30 @@ class VfsPlugin(IPlugin):
|
|
|
self.page.get(login_url)
|
|
self.page.get(login_url)
|
|
|
time.sleep(3)
|
|
time.sleep(3)
|
|
|
|
|
|
|
|
- # 处理 Cloudflare 拦截或等待室
|
|
|
|
|
|
|
+ # 打开 login 页面如果提示 403 / Block,立即抛出异常快速失败,无需继续
|
|
|
|
|
+ self._check_page_blocked("login portal")
|
|
|
|
|
+
|
|
|
|
|
+ # 处理 Cloudflare 等待室
|
|
|
cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug if self.config else True)
|
|
cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug if self.config else True)
|
|
|
- if 'Attention Required! | Cloudflare' in self.page.title and 'Sorry, you have been blocked' in self.page.html:
|
|
|
|
|
- self._log('Block by cloudflare, try refresh...')
|
|
|
|
|
- self.page.refresh()
|
|
|
|
|
- self.page.wait.doc_loaded()
|
|
|
|
|
cf_bypasser.handle_waiting_room()
|
|
cf_bypasser.handle_waiting_room()
|
|
|
|
|
|
|
|
|
|
+ # 再次确认等待室后是否被拦截
|
|
|
|
|
+ self._check_page_blocked("login portal after waiting room")
|
|
|
|
|
+
|
|
|
# 处理 OneTrust Cookie 弹窗
|
|
# 处理 OneTrust Cookie 弹窗
|
|
|
cookie_btn = self.page.ele('#onetrust-accept-btn-handler', timeout=3)
|
|
cookie_btn = self.page.ele('#onetrust-accept-btn-handler', timeout=3)
|
|
|
if cookie_btn and cookie_btn.states.is_displayed:
|
|
if cookie_btn and cookie_btn.states.is_displayed:
|
|
|
cookie_btn.click()
|
|
cookie_btn.click()
|
|
|
self._log("Accepted OneTrust cookie consent.")
|
|
self._log("Accepted OneTrust cookie consent.")
|
|
|
time.sleep(0.5)
|
|
time.sleep(0.5)
|
|
|
|
|
+ except PermissionDeniedError:
|
|
|
|
|
+ raise
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
self._log(f"Handling login portal context warning: {e}")
|
|
self._log(f"Handling login portal context warning: {e}")
|
|
|
|
|
|
|
|
|
|
+ # 再次确认是否被拦截
|
|
|
|
|
+ self._check_page_blocked("login portal final")
|
|
|
|
|
+
|
|
|
# 强力清除任何可能阻挡交互的遮罩层
|
|
# 强力清除任何可能阻挡交互的遮罩层
|
|
|
try:
|
|
try:
|
|
|
self.page.run_js("""
|
|
self.page.run_js("""
|
|
@@ -424,16 +517,19 @@ class VfsPlugin(IPlugin):
|
|
|
self._log(f"Using traditional landing page mode. Navigating to {login_page_url}...")
|
|
self._log(f"Using traditional landing page mode. Navigating to {login_page_url}...")
|
|
|
|
|
|
|
|
self.page.get(login_page_url)
|
|
self.page.get(login_page_url)
|
|
|
|
|
+ time.sleep(3)
|
|
|
|
|
+
|
|
|
|
|
+ # 打开 login 页面如果提示 403 / Block,立即抛出异常快速失败,无需继续
|
|
|
|
|
+ self._check_page_blocked("login page")
|
|
|
|
|
+
|
|
|
self._log("Handling Cloudflare challenge...")
|
|
self._log("Handling Cloudflare challenge...")
|
|
|
|
|
|
|
|
cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug if self.config else True)
|
|
cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug if self.config else True)
|
|
|
-
|
|
|
|
|
- if 'Attention Required! | Cloudflare' in self.page.title and 'Sorry, you have been blocked' in self.page.html:
|
|
|
|
|
- self._log('Block by cloudflare, try refresh...')
|
|
|
|
|
- self.page.refresh()
|
|
|
|
|
- self.page.wait.doc_loaded()
|
|
|
|
|
- time.sleep(random.uniform(3, 5))
|
|
|
|
|
cf_bypasser.handle_waiting_room()
|
|
cf_bypasser.handle_waiting_room()
|
|
|
|
|
+ time.sleep(random.uniform(2, 3))
|
|
|
|
|
+
|
|
|
|
|
+ # 再次确认挑战后是否被拦截
|
|
|
|
|
+ self._check_page_blocked("login page after challenge")
|
|
|
|
|
|
|
|
cf_token = self._extract_turnstile_token_from_landing_page(timeout_sec=40, reset_first=False)
|
|
cf_token = self._extract_turnstile_token_from_landing_page(timeout_sec=40, reset_first=False)
|
|
|
|
|
|
|
@@ -1039,13 +1135,12 @@ class VfsPlugin(IPlugin):
|
|
|
res.book_time = selected_slot_time_range
|
|
res.book_time = selected_slot_time_range
|
|
|
res.urn = final_urn
|
|
res.urn = final_urn
|
|
|
|
|
|
|
|
- if schedule_res.get("IsPaymentRequired", False):
|
|
|
|
|
- payload = schedule_res.get("payLoad", "")
|
|
|
|
|
- if payload:
|
|
|
|
|
- self._log("Processing payment link...")
|
|
|
|
|
- payment_url = self._pay_request(payload)
|
|
|
|
|
- if payment_url:
|
|
|
|
|
- res.payment_link = payment_url
|
|
|
|
|
|
|
+ lowered_schedule = {str(k).lower(): v for k, v in schedule_res.items()} if isinstance(schedule_res, dict) else {}
|
|
|
|
|
+ if lowered_schedule.get("ispaymentrequired", False):
|
|
|
|
|
+ self._log("Processing payment link...")
|
|
|
|
|
+ payment_url = self._pay_request(schedule_res)
|
|
|
|
|
+ if payment_url:
|
|
|
|
|
+ res.payment_link = payment_url
|
|
|
|
|
|
|
|
return res
|
|
return res
|
|
|
|
|
|
|
@@ -1379,29 +1474,62 @@ class VfsPlugin(IPlugin):
|
|
|
resp = self._perform_request("POST", url, headers=headers, json_data=data)
|
|
resp = self._perform_request("POST", url, headers=headers, json_data=data)
|
|
|
return resp.json()
|
|
return resp.json()
|
|
|
|
|
|
|
|
- def _pay_request(self, payload) -> str:
|
|
|
|
|
|
|
+ def _get_payment_start_url(self, schedule_res: Dict[str, Any]) -> str:
|
|
|
"""
|
|
"""
|
|
|
- 解析支付重定向 URL (DrissionPage 新标签页版)
|
|
|
|
|
|
|
+ 根据 VFS schedule 响应构建初始支付请求 URL (对齐 main.js getPaymentUrl 逻辑)
|
|
|
"""
|
|
"""
|
|
|
- start_url = f"https://online.vfsglobal.com/PG-Component/Payment/PayRequest?payLoad={payload}"
|
|
|
|
|
|
|
+ lowered = {str(k).lower(): v for k, v in schedule_res.items()}
|
|
|
|
|
+ base_url = lowered.get("url") or "https://online.vfsglobal.com/PG-Component/Payment/PayRequest"
|
|
|
|
|
+ is_pg2 = lowered.get("ispg2enabled", False)
|
|
|
|
|
+ payload = lowered.get("payload", "")
|
|
|
|
|
+
|
|
|
|
|
+ # PG2 开启时直接使用 url
|
|
|
|
|
+ if is_pg2 or not payload:
|
|
|
|
|
+ return base_url
|
|
|
|
|
+
|
|
|
|
|
+ # 对 payload 进行严格 URL 编码(安全处理 '|'、'=' 等特殊字符)
|
|
|
|
|
+ encoded_payload = urllib.parse.quote(str(payload), safe='')
|
|
|
|
|
+ sep = "&" if "?" in base_url else "?"
|
|
|
|
|
+ return f"{base_url}{sep}payLoad={encoded_payload}"
|
|
|
|
|
+
|
|
|
|
|
+ def _pay_request(self, schedule_res_or_payload: Any) -> str:
|
|
|
|
|
+ """
|
|
|
|
|
+ 解析支付重定向 URL (DrissionPage 新标签页版,修复 URL 编码与重定向等待逻辑)
|
|
|
|
|
+ """
|
|
|
|
|
+ if isinstance(schedule_res_or_payload, dict):
|
|
|
|
|
+ start_url = self._get_payment_start_url(schedule_res_or_payload)
|
|
|
|
|
+ else:
|
|
|
|
|
+ encoded = urllib.parse.quote(str(schedule_res_or_payload), safe='')
|
|
|
|
|
+ start_url = f"https://online.vfsglobal.com/PG-Component/Payment/PayRequest?payLoad={encoded}"
|
|
|
|
|
+
|
|
|
final_url = ""
|
|
final_url = ""
|
|
|
|
|
+ pay_tab = None
|
|
|
|
|
|
|
|
try:
|
|
try:
|
|
|
- self._log(f"Resolving payment redirect...<{start_url}>")
|
|
|
|
|
|
|
+ self._log(f"Resolving payment redirect...<{start_url[:120]}...>")
|
|
|
pay_tab = self.page.new_tab(start_url)
|
|
pay_tab = self.page.new_tab(start_url)
|
|
|
- pay_tab.wait.url_change(start_url, timeout=15)
|
|
|
|
|
|
|
|
|
|
- final_url = pay_tab.url
|
|
|
|
|
- self._log(f"Payment URL resolved: {final_url}")
|
|
|
|
|
|
|
+ # 等待重定向离开 PayRequest (最多 15 秒)
|
|
|
|
|
+ for _ in range(30):
|
|
|
|
|
+ time.sleep(0.5)
|
|
|
|
|
+ cur_url = pay_tab.url
|
|
|
|
|
+ if cur_url and "PayRequest" not in cur_url and cur_url != "about:blank":
|
|
|
|
|
+ final_url = cur_url
|
|
|
|
|
+ break
|
|
|
|
|
|
|
|
- pay_tab.close()
|
|
|
|
|
|
|
+ if not final_url and pay_tab:
|
|
|
|
|
+ final_url = pay_tab.url
|
|
|
|
|
+
|
|
|
|
|
+ self._log(f"Payment URL resolved: {final_url}")
|
|
|
|
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
self._log(f"[WARN] Failed to resolve payment URL: {e}")
|
|
self._log(f"[WARN] Failed to resolve payment URL: {e}")
|
|
|
- try:
|
|
|
|
|
- pay_tab.close()
|
|
|
|
|
- except:
|
|
|
|
|
- pass
|
|
|
|
|
|
|
+ finally:
|
|
|
|
|
+ if pay_tab:
|
|
|
|
|
+ try:
|
|
|
|
|
+ pay_tab.close()
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
|
|
|
return final_url
|
|
return final_url
|
|
|
|
|
|