standalone.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import os
  2. import time
  3. import random
  4. import threading
  5. from datetime import datetime
  6. from typing import Dict, Callable, Optional
  7. from vs_types import (
  8. BookerStandaloneConfig,
  9. QueryWaitMode,
  10. VSPlgConfig,
  11. VSQueryResult,
  12. VSBookResult,
  13. GroupStatus,
  14. PluginStatus,
  15. utc_now_str,
  16. BizException,
  17. RateLimiteddError,
  18. SessionExpiredOrInvalidError,
  19. PermissionDeniedError,
  20. )
  21. from vs_plg_factory import VSPlgFactory
  22. from utils.safe_redis_cli import SafeRedisClient
  23. from toolkit.proxy_manager import ProxyManager
  24. from toolkit.vs_cloud_api import VSCloudApi
  25. from toolkit.notifier import notify_date_changes, notify_book_result
  26. class StandaloneGCO:
  27. """
  28. Standalone GCO 实例管理器。
  29. 遵循 SentinelGCO / BookerGCO 的统一生命周期契约 (start / stop / update_config)。
  30. """
  31. TAG = "STANDALONE"
  32. def __init__(self, cfg: BookerStandaloneConfig, redis_conf: Dict, logger: Callable[[str], None] = None):
  33. self.m_cfg = cfg
  34. self.m_logger = logger
  35. self.m_factory = VSPlgFactory()
  36. self.m_instance = None
  37. self.m_lock = threading.RLock()
  38. self.m_stop_event = threading.Event()
  39. self.m_last_login_time = 0
  40. self.m_next_query_time = 0
  41. self.m_next_keepalive_time = 0
  42. self.m_last_wait_log_time = 0.0
  43. self.m_current_dates = {}
  44. self.redis_client = SafeRedisClient(redis_conf, self.m_logger)
  45. def _log(self, message: str):
  46. prefix = f"[-] [{self.TAG}] [{self.m_cfg.identifier}]"
  47. if self.m_logger:
  48. self.m_logger(f"{prefix} {message}")
  49. else:
  50. print(f"{prefix} {message}")
  51. def _get_wait_interval(self) -> float:
  52. """根据配置计算下一次查询的等待时间"""
  53. mode = self.m_cfg.query_wait.mode
  54. if mode == QueryWaitMode.Loop:
  55. return 1.0
  56. elif mode == QueryWaitMode.Fixed:
  57. return float(self.m_cfg.query_wait.fixed_wait)
  58. elif mode == QueryWaitMode.Random:
  59. return random.uniform(self.m_cfg.query_wait.random_min, self.m_cfg.query_wait.random_max)
  60. return 30.0
  61. def _is_within_active_hours(self) -> bool:
  62. """判断当前是否在允许创建实例和查询的时间段内"""
  63. start_str = self.m_cfg.active_time_start
  64. end_str = self.m_cfg.active_time_end
  65. current_bj_time = datetime.utcnow().time()
  66. start_time = datetime.strptime(start_str, "%H:%M").time()
  67. end_time = datetime.strptime(end_str, "%H:%M").time()
  68. return start_time <= current_bj_time <= end_time
  69. def update_config(self, new_cfg: BookerStandaloneConfig):
  70. """动态更新配置"""
  71. with self.m_lock:
  72. if self.m_cfg.enable and not new_cfg.enable:
  73. self._log("Config dynamically updated: Group DISABLED.")
  74. elif not self.m_cfg.enable and new_cfg.enable:
  75. self._log("Config dynamically updated: Group ENABLED.")
  76. else:
  77. self._log("Config dynamically updated: Parameters refreshed.")
  78. self.m_cfg = new_cfg
  79. def get_status(self) -> GroupStatus:
  80. with self.m_lock:
  81. instance = self.m_instance
  82. gid = self.m_cfg.identifier
  83. enable = self.m_cfg.enable
  84. plugin_name = self.m_cfg.plugin_config.plugin_name
  85. plugins_status = []
  86. healthy_count = 0
  87. if instance:
  88. st = instance.get_plugin_status()
  89. if st.is_healthy:
  90. healthy_count += 1
  91. plugins_status.append(st)
  92. return GroupStatus(
  93. group_id=gid,
  94. enable=enable,
  95. plugin_name=plugin_name,
  96. total_tasks=1 if instance else 0,
  97. healthy_tasks=healthy_count,
  98. plugins=plugins_status,
  99. updated_at=utc_now_str(),
  100. )
  101. def start(self):
  102. """非阻塞启动,注册插件并开启后台工作线程"""
  103. if not self.m_cfg.enable:
  104. return
  105. self._log("Starting Standalone GCO...")
  106. plugin_name = self.m_cfg.plugin_config.plugin_name
  107. class_name = "".join(part.title() for part in plugin_name.split('_'))
  108. plugin_path = os.path.join(self.m_cfg.plugin_config.lib_path, self.m_cfg.plugin_config.plugin_bin)
  109. self.m_factory.register_plugin(plugin_name, plugin_path, class_name)
  110. worker_name = f"Standalone-{self.m_cfg.identifier}"
  111. threading.Thread(target=self._worker_loop, daemon=True, name=worker_name).start()
  112. def stop(self):
  113. """优雅停止"""
  114. self._log("Stopping Standalone GCO...")
  115. self.m_stop_event.set()
  116. self._cleanup_instance("standalone stopped")
  117. def _cleanup_instance(self, reason: str = ""):
  118. with self.m_lock:
  119. if self.m_instance is not None:
  120. try:
  121. self._log(f"🧹 Cleaned up instance. Reason: {reason}")
  122. self.m_instance.cleanup()
  123. except Exception as e:
  124. self._log(f"Cleanup failed for instance. Reason: {reason}. Error: {e}")
  125. finally:
  126. self.m_instance = None
  127. def _init_instance(self):
  128. """初始化单实例并进行登录创建会话"""
  129. self._cleanup_instance("recreating new session")
  130. now = time.time()
  131. if now < self.m_last_login_time + self.m_cfg.login_interval:
  132. wait_time = self.m_last_login_time + self.m_cfg.login_interval - now
  133. self._log(f"Waiting {int(wait_time)}s for login interval cooldown...")
  134. if self.m_stop_event.wait(wait_time):
  135. return
  136. plugin_name = self.m_cfg.plugin_config.plugin_name
  137. plg_cfg = VSPlgConfig()
  138. plg_cfg.debug = self.m_cfg.debug
  139. plg_cfg.account = self.m_cfg.account
  140. proxy = ProxyManager.Instance().get_next_proxy(self.m_cfg.proxy_pool, proxy_cd=600)
  141. plg_cfg.proxy = type(plg_cfg.proxy)(**proxy)
  142. plg_cfg.free_config = self.m_cfg.free_config
  143. plg_cfg.session_max_life = self.m_cfg.session_max_life
  144. instance = self.m_factory.create(self.m_cfg.identifier, plugin_name)
  145. instance.set_log(self.m_logger)
  146. instance.set_config(plg_cfg)
  147. with self.m_lock:
  148. self.m_instance = instance
  149. self._log("Creating session...")
  150. instance.create_session()
  151. self.m_last_login_time = time.time()
  152. interval = self._get_wait_interval()
  153. self.m_next_query_time = time.time() + interval
  154. self.m_next_keepalive_time = time.time() + 10.0
  155. self._log(f"Session created successfully. Initial query scheduled in {interval:.1f}s (at {datetime.fromtimestamp(self.m_next_query_time).strftime('%H:%M:%S')}).")
  156. def _normalize_error(self, err: Exception) -> BizException:
  157. """
  158. 将任意异常规整化为领域异常 BizException。
  159. 保留对未结构化历史异常字符串特征的兜底兼容识别。
  160. """
  161. if isinstance(err, BizException):
  162. return err
  163. err_str = str(err)
  164. if "42901" in err_str or "Rate limited" in err_str:
  165. return RateLimiteddError(message=err_str)
  166. if "40101" in err_str or "Session expired" in err_str or "session invalid" in err_str.lower():
  167. return SessionExpiredOrInvalidError(message=err_str)
  168. if "40301" in err_str or "403" in err_str or "Cloudflare Access Denied" in err_str:
  169. return PermissionDeniedError(message=err_str)
  170. return BizException(
  171. code=50000,
  172. message=f"Unhandled ({type(err).__name__}): {err_str}",
  173. )
  174. def _dispatch_error_policy(self, action: str, err: Exception) -> None:
  175. """
  176. Standalone 错误调度与自愈策略引擎 (Policy Engine):
  177. 基于 (Action, 异常事实类型) 矩阵 + Standalone 单机模式 + 本组配置闭环执行自愈动作:
  178. - create_session/login 时 429:判定为账号级限流,锁定账号,执行 login_interval 退避等待;
  179. - query 时 429:会话已建立,判定为出口 IP 频率超限,不锁定账号,销毁实例触发下轮代理轮换;
  180. - session expired / 401:销毁当前实例,重置下一次查询时间为 5s 后以触发重新登录;
  181. - 403 Forbidden / WAF:判定为 IP 被封,清理实例换 IP 并退避;
  182. - 内部直接完成日志输出与定时器维护,调用方无需感知具体冷却数值。
  183. """
  184. biz_err = self._normalize_error(err)
  185. acc = self.m_cfg.account if (self.m_cfg.account and self.m_cfg.account.username not in ("", "Guest")) else None
  186. # 1. 目标站限流 / 429
  187. if isinstance(biz_err, RateLimiteddError):
  188. if action in ("create_session", "login", "spawn"):
  189. backoff = float(self.m_cfg.login_interval)
  190. self._log(f"🔒 [Account Rate Limited] 429 throttled during {action} (Account credential/frequency restricted).")
  191. if acc and getattr(acc, "id", 0):
  192. try:
  193. VSCloudApi.Instance().lock_account(acc.id, int(backoff))
  194. self._log(f"🔒 Account [{acc.username}] locked for {int(backoff)}s via Cloud API.")
  195. except Exception as lock_err:
  196. self._log(f"Failed to lock account via cloud API: {lock_err}")
  197. self.m_last_login_time = time.time()
  198. self._cleanup_instance(f"{action} account rate limited")
  199. elif action == "query":
  200. # 查询时 429:出口 IP 限流,绝不锁账号,换 IP 即可!
  201. self._log("🔄 [Proxy Rate Limited] Query hit 429 (IP throttled, Account preserved). Cleaning instance to rotate proxy...")
  202. self._cleanup_instance("query ip rate limited")
  203. self.m_next_query_time = time.time() + 10.0
  204. else:
  205. self._log(f"⚠️ [Rate Limited] 429 encountered during {action}.")
  206. self.m_next_query_time = time.time() + 10.0
  207. # 2. 会话过期 / 401
  208. elif isinstance(biz_err, SessionExpiredOrInvalidError):
  209. if action in ("create_session", "login", "spawn"):
  210. self._log(f"❌ [Auth Failed] Session creation failed (401 invalid credentials or challenge). Backing off for 10s...")
  211. self.m_last_login_time = time.time() - self.m_cfg.login_interval + 10.0
  212. self._cleanup_instance(f"{action} auth failed")
  213. else:
  214. self._log(f"🔄 [Session Expired] Session died during {action}. Cleaning up instance for re-login in 5s...")
  215. self._cleanup_instance(f"{action} session expired")
  216. self.m_next_query_time = time.time() + 5.0
  217. # 3. 权限被拒 / WAF 拦截 / 403
  218. elif isinstance(biz_err, PermissionDeniedError):
  219. self._log(f"🛡️ [WAF Blocked] 403 Forbidden detected during {action}. Cleaning instance to rotate proxy (60s cooldown)...")
  220. self._cleanup_instance(f"{action} waf blocked")
  221. self.m_next_query_time = time.time() + 60.0
  222. # 4. 网络或浏览器超时
  223. elif isinstance(biz_err, NetworkTimeoutError):
  224. self._log(f"⏱️ [Timeout] Network/browser timeout during {action}. Retrying in 5s...")
  225. self.m_next_query_time = time.time() + 5.0
  226. # 5. 兜底策略
  227. else:
  228. self._log(f"⚠️ [{action}] Unexpected error: {biz_err.message} (code={biz_err.code})")
  229. if action in ("create_session", "worker_loop"):
  230. self.m_last_login_time = time.time() - self.m_cfg.login_interval + 5.0
  231. else:
  232. self.m_next_query_time = time.time() + 10.0
  233. def _worker_loop(self):
  234. """
  235. 后台守护循环:心跳保活 -> 时间判定 -> 查询 -> 预定
  236. """
  237. self._log("Standalone worker loop started.")
  238. while not self.m_stop_event.is_set():
  239. try:
  240. if not self.m_cfg.enable:
  241. self.m_stop_event.wait(1.0)
  242. continue
  243. if not self._is_within_active_hours():
  244. self.m_stop_event.wait(5.0)
  245. continue
  246. # 1. 会话初始化与自愈
  247. if self.m_instance is None:
  248. try:
  249. self._init_instance()
  250. except Exception as e:
  251. self._dispatch_error_policy("create_session", e)
  252. continue
  253. # 2. 健康检查
  254. if self.m_instance and not self.m_instance.health_check():
  255. self._log("Health check failed. Session dead. Recreating session...")
  256. self._cleanup_instance("session health check failed")
  257. continue
  258. # 3. 查询等待与周期性保活
  259. now = time.time()
  260. if now < self.m_next_query_time:
  261. remaining = self.m_next_query_time - now
  262. next_query_str = datetime.fromtimestamp(self.m_next_query_time).strftime('%H:%M:%S')
  263. keep_alive_sec = self.m_cfg.keep_alive
  264. # 周期性保活
  265. if self.m_instance and now >= self.m_next_keepalive_time:
  266. self.m_instance.keep_alive()
  267. self.m_next_keepalive_time = now + keep_alive_sec
  268. self._log(f"Session keepalive sent. Next query at {next_query_str} (remaining: {remaining:.0f}s)...")
  269. self.m_last_wait_log_time = now
  270. # 周期性显示等待倒计时(每隔 15 秒提示一次)
  271. elif now - self.m_last_wait_log_time >= 15.0 and remaining > 3.0:
  272. self.m_last_wait_log_time = now
  273. self._log(f"Waiting for next query at {next_query_str} (remaining: {remaining:.0f}s)...")
  274. wait_sec = min(remaining, 1.0)
  275. self.m_stop_event.wait(wait_sec)
  276. continue
  277. # 4. 随机选取 routing_key 进行查询
  278. apt_types = self.m_cfg.appointment_types
  279. if not apt_types:
  280. self.m_stop_event.wait(2.0)
  281. continue
  282. weights = [max(0.0, float(t.weight)) for t in apt_types]
  283. if sum(weights) > 0:
  284. apt_type = random.choices(apt_types, weights=weights, k=1)[0]
  285. else:
  286. apt_type = random.choice(apt_types)
  287. self._log(f"Querying slots for {apt_type.routing_key}...")
  288. try:
  289. query_result = self.m_instance.query(apt_type)
  290. query_result.apt_type = apt_type
  291. notify_date_changes(query_result, cache=self.m_current_dates, logger=self._log)
  292. except Exception as e:
  293. self._dispatch_error_policy("query", e)
  294. continue
  295. # 5. 发现档期,执行自动预定
  296. if query_result.success:
  297. self._log("🔥 SLOT FOUND! Initiating AUTO-BOOKING...")
  298. try:
  299. book_result = self.m_instance.book(query_result, self.m_cfg.user_preferences)
  300. notify_book_result(book_result, logger=self._log)
  301. if book_result.success:
  302. self._log(f"🎉 BOOKING SUCCESSFUL for {apt_type.routing_key}!")
  303. self.m_stop_event.wait(5.0)
  304. except Exception as e:
  305. self._dispatch_error_policy("book", e)
  306. # 6. 在所有操作(包括抢单流程)执行完毕后,再统一计算并调度下一次查询时间与保活时间
  307. interval = self._get_wait_interval()
  308. now = time.time()
  309. self.m_next_query_time = now + interval
  310. keep_alive_sec = self.m_cfg.keep_alive
  311. self.m_next_keepalive_time = now + keep_alive_sec
  312. self.m_last_wait_log_time = now
  313. self._log(f"Next query scheduled in {interval:.1f}s (at {datetime.fromtimestamp(self.m_next_query_time).strftime('%H:%M:%S')})...")
  314. except Exception as e:
  315. self._dispatch_error_policy("worker_loop", e)