Selaa lähdekoodia

feat: implement BLS visa appointment plugin and configuration files

hujiarui 1 kuukausi sitten
vanhempi
sitoutus
2eea24ad5a
6 muutettua tiedostoa jossa 199 lisäystä ja 57 poistoa
  1. 7 10
      booker.py
  2. 9 1
      plugins/bls_plugin.py
  3. 158 30
      plugins/vfs_plugin.py
  4. 4 7
      sentinel.py
  5. 15 5
      toolkit/proxy_manager.py
  6. 6 4
      toolkit/thread_pool.py

+ 7 - 10
booker.py

@@ -359,15 +359,8 @@ class BaseBookerGCO:
                 self._on_maintain_ping(healthy_tasks, dead_tasks)
 
                 if dead_tasks:
-                    with self.m_lock:
-                        current_tasks = list(self.m_tasks)
-                        self.m_tasks = [t for t in self.m_tasks if t in healthy_tasks]
                     for t in dead_tasks:
-                        if t in current_tasks:
-                            self._cleanup_task(t, "unhealthy or keep-alive failed")
-                else:
-                    with self.m_lock:
-                        self.m_tasks = [t for t in self.m_tasks if t in healthy_tasks]
+                        self._remove_task(t, "unhealthy or keep-alive failed")
             except Exception as e:
                 self._log(f'Maintain loop exception: {e}')
 
@@ -730,7 +723,7 @@ class OrderBookerGCO(BaseBookerGCO):
             plg_cfg.account.username = user_inputs.get("username", "")
             plg_cfg.account.password = user_inputs.get("password", "")
             if not plg_cfg.account.username:
-                return
+                raise AccountIncompleteError(f"Task={task_id} missing username credentials in user_inputs")
             
             acceptable_keys = [target_routing_key]
             if self.m_cfg.need_proxy:
@@ -775,4 +768,8 @@ class OrderBookerGCO(BaseBookerGCO):
                 self._cleanup_instance(instance, "spawn failed")
             if not success and task_id:
                 with self.m_lock:
-                    self.m_task_data_cache.pop(str(task_id), None)
+                    self.m_task_data_cache.pop(str(task_id), None)
+                try:
+                    self.redis_client.zrem(self.m_tracker_key, task_id)
+                except Exception:
+                    pass

+ 9 - 1
plugins/bls_plugin.py

@@ -1061,7 +1061,15 @@ class BlsPlugin(IPlugin):
                 pass # 忽略已关闭的错误
             self.page = None
         
-        # 2. 删除文件
+        # 2. 关闭 HTTP 会话 (curl_cffi)
+        if self.session:
+            try:
+                self.session.close()
+            except Exception:
+                pass
+            self.session = None
+
+        # 3. 删除文件
         # 注意:Chrome 关闭后可能需要几百毫秒释放文件锁,稍微等待
         if os.path.exists(self.root_workspace):
             for _ in range(3):

+ 158 - 30
plugins/vfs_plugin.py

@@ -202,6 +202,92 @@ class VfsPlugin(IPlugin):
                 return False
         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):
         """进入极轻量 404 页面并注入独占 Turnstile 容器 (绕过复杂首页)"""
         if self.injected:
@@ -237,23 +323,30 @@ class VfsPlugin(IPlugin):
             self.page.get(login_url)
             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)
-            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()
 
+            # 再次确认等待室后是否被拦截
+            self._check_page_blocked("login portal after waiting room")
+
             # 处理 OneTrust Cookie 弹窗
             cookie_btn = self.page.ele('#onetrust-accept-btn-handler', timeout=3)
             if cookie_btn and cookie_btn.states.is_displayed:
                 cookie_btn.click()
                 self._log("Accepted OneTrust cookie consent.")
                 time.sleep(0.5)
+        except PermissionDeniedError:
+            raise
         except Exception as e:
             self._log(f"Handling login portal context warning: {e}")
 
+        # 再次确认是否被拦截
+        self._check_page_blocked("login portal final")
+
         # 强力清除任何可能阻挡交互的遮罩层
         try:
             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.page.get(login_page_url)
+            time.sleep(3)
+
+            # 打开 login 页面如果提示 403 / Block,立即抛出异常快速失败,无需继续
+            self._check_page_blocked("login page")
+
             self._log("Handling Cloudflare challenge...")
             
             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()
+            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)
 
@@ -1039,13 +1135,12 @@ class VfsPlugin(IPlugin):
         res.book_time = selected_slot_time_range
         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
 
@@ -1379,29 +1474,62 @@ class VfsPlugin(IPlugin):
         resp = self._perform_request("POST", url, headers=headers, json_data=data)
         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 = ""
+        pay_tab = None
         
         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.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:
             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
 

+ 4 - 7
sentinel.py

@@ -310,14 +310,11 @@ class SentinelGCO:
           
                 if dead_tasks:
                     with self.m_lock:
-                        current_tasks = list(self.m_tasks)
-                        self.m_tasks = [t for t in self.m_tasks if t in active_tasks]
+                        for t in dead_tasks:
+                            if t in self.m_tasks:
+                                self.m_tasks.remove(t)
                     for t in dead_tasks:
-                        if t in current_tasks:
-                            self._cleanup_task(t, "health check failed")
-                else:
-                    with self.m_lock:
-                        self.m_tasks = [t for t in self.m_tasks if t in active_tasks]
+                        self._cleanup_task(t, "health check failed")
                 
                 if not active_tasks:
                     continue

+ 15 - 5
toolkit/proxy_manager.py

@@ -11,6 +11,7 @@ from typing import List, Dict, Any, Optional
 import requests
 import configure
 from toolkit.cloud_api import CloudApi
+from toolkit.thread_pool import ThreadPool
 from core_types import BizLogicError
 from log_macros import VSC_INFO, VSC_WARN, VSC_ERROR, VSC_DEBUG
 
@@ -107,8 +108,11 @@ class ProxyManager:
                 with self._lock:
                     self._probing_ports.discard(port)
 
-        t = threading.Thread(target=_do_probe, daemon=True)
-        t.start()
+        try:
+            ThreadPool.getInstance().enqueue(_do_probe)
+        except Exception:
+            t = threading.Thread(target=_do_probe, daemon=True)
+            t.start()
 
     def _resolve_ip_identifier(self, node: Dict[str, Any], trigger_probe: bool = False) -> str:
         """Gets the IP identifier of the node (prefers true egress IP; falls back to port anchor if unparsed)."""
@@ -159,7 +163,10 @@ class ProxyManager:
                 with self._lock:
                     self._fetching_reputation.discard(plugin_name)
 
-        threading.Thread(target=_fetch_worker, daemon=True).start()
+        try:
+            ThreadPool.getInstance().enqueue(_fetch_worker)
+        except Exception:
+            threading.Thread(target=_fetch_worker, daemon=True).start()
         return (cache_entry.get("map", {}) if cache_entry else {})
 
     def _is_site_outage(self, plugin_name: str) -> bool:
@@ -417,6 +424,7 @@ class ProxyManager:
                     # Layer 2: Detailed error classification
                     is_waf_block = (
                         "cf-mitigated" in error_type.lower() or
+                        "403201" in error_type or
                         "403202" in error_type or
                         "waf blocked" in error_type.lower() or
                         (http_code == 403 and "mitigated" in error_type.lower())
@@ -449,5 +457,7 @@ class ProxyManager:
             except Exception:
                 pass
 
-        t = threading.Thread(target=_report_worker, daemon=True)
-        t.start()
+        try:
+            ThreadPool.getInstance().enqueue(_report_worker)
+        except Exception:
+            pass

+ 6 - 4
toolkit/thread_pool.py

@@ -44,7 +44,9 @@ class ThreadPool:
         @brief Shuts down the thread pool.
         @param wait If True, waits for all pending futures to complete.
         """
-        if self._executor:
-            VSC_INFO("thread_pool", "Shutting down ThreadPool (wait=%s)...", wait)
-            self._executor.shutdown(wait=wait)
-            self._executor = None
+        with ThreadPool._lock:
+            if self._executor:
+                VSC_INFO("thread_pool", "Shutting down ThreadPool (wait=%s)...", wait)
+                self._executor.shutdown(wait=wait)
+                self._executor = None
+            ThreadPool._instance = None