config.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """持久化网页配置:账号数 / 接码 URL / 邮件助手 / CPA 等。"""
  2. from __future__ import annotations
  3. import json
  4. import os
  5. import threading
  6. from dataclasses import asdict, dataclass, field, fields
  7. CONFIG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.local.json")
  8. _LOCK = threading.Lock()
  9. @dataclass
  10. class AppConfig:
  11. # 注册控制
  12. account_count: int = 1
  13. headless: bool = False
  14. # 邮件
  15. mail_helper_url: str = "http://ali.ss5.xyz:17373"
  16. mail_domain: str = "edu.a4sky.com"
  17. mail_poll_interval_sec: int = 4
  18. mail_poll_max_attempts: int = 60 # 4s * 60 = 4min
  19. # PayPal / 接码
  20. phone_e164: str = "+15822201173"
  21. sms_api_url: str = "http://a.62-us.com/api/get_sms?key=a5d3262e05efaba982aba7cfae20b8bc"
  22. # 代理(两个字段独立配置):
  23. # proxy_url — 全局代理(ChatGPT 注册 / 长链 / PayPal 都走)。空 = 全程直连。
  24. # paypal_only_proxy — 仅 PayPal 阶段用的代理。空 = PayPal 沿用 proxy_url(或直连)。
  25. # 典型搭配:proxy_url 留空(注册直连,避免代理屏蔽 chatgpt.com),paypal_only_proxy 填代理。
  26. proxy_url: str = ""
  27. paypal_only_proxy: str = ""
  28. paypal_proxy: str = "" # 旧字段(保留向后兼容;如填了且 paypal_only_proxy/proxy_url 都为空,会迁移到 paypal_only_proxy)
  29. # CPA
  30. cpa_url: str = ""
  31. cpa_management_key: str = ""
  32. # 调试
  33. use_promo: bool = True
  34. # API / 外网访问
  35. api_host: str = "0.0.0.0" # 改成 "0.0.0.0" 即可外网访问
  36. api_port: int = 7791
  37. api_token: str = "" # 留空 = 不校验;非空时所有 /api/* 请求需 Authorization: Bearer <token>
  38. api_cors_origin: str = "*" # CORS Allow-Origin,可填具体域名或 *
  39. @classmethod
  40. def load(cls) -> "AppConfig":
  41. if not os.path.exists(CONFIG_PATH):
  42. return cls()
  43. try:
  44. with open(CONFIG_PATH, "r", encoding="utf-8") as f:
  45. raw = json.load(f)
  46. except Exception:
  47. return cls()
  48. valid = {f.name for f in fields(cls)}
  49. cleaned = {k: v for k, v in (raw or {}).items() if k in valid}
  50. return cls(**cleaned)
  51. def save(self):
  52. with _LOCK:
  53. with open(CONFIG_PATH, "w", encoding="utf-8") as f:
  54. json.dump(asdict(self), f, ensure_ascii=False, indent=2)
  55. def update(self, patch: dict) -> "AppConfig":
  56. valid = {f.name for f in fields(self)}
  57. for k, v in (patch or {}).items():
  58. if k not in valid:
  59. continue
  60. current = getattr(self, k)
  61. if isinstance(current, bool):
  62. setattr(self, k, bool(v) if not isinstance(v, str) else v.lower() in ("1", "true", "yes", "on"))
  63. elif isinstance(current, int):
  64. try:
  65. setattr(self, k, int(v))
  66. except Exception:
  67. pass
  68. else:
  69. setattr(self, k, "" if v is None else str(v))
  70. # 向后兼容:旧字段 paypal_proxy → 新字段 paypal_only_proxy(之前一段时间它被错误地等价于 proxy_url)
  71. if self.paypal_proxy and not self.paypal_only_proxy and not self.proxy_url:
  72. self.paypal_only_proxy = self.paypal_proxy
  73. self.save()
  74. return self
  75. @property
  76. def effective_global_proxy(self) -> str:
  77. """ChatGPT 注册 / 长链 / 默认浏览器 context 用的代理。空 = 直连。"""
  78. return (self.proxy_url or "").strip()
  79. @property
  80. def effective_paypal_proxy(self) -> str:
  81. """PayPal 阶段用的代理,独立字段优先;否则继承 proxy_url。空 = 直连。"""
  82. return (self.paypal_only_proxy or self.proxy_url or "").strip()
  83. # 兼容旧调用:先前 chatgpt_flow 用了 effective_proxy
  84. @property
  85. def effective_proxy(self) -> str:
  86. return self.effective_global_proxy