feat: save registered accounts to postgres

This commit is contained in:
mmc
2026-03-19 14:32:17 +08:00
parent 6a250fe6a1
commit 2904e43b1f
11 changed files with 235 additions and 4 deletions

View File

@@ -155,6 +155,7 @@ class MailProvider(ABC):
class MailTmProvider(MailProvider):
def __init__(self, api_base: str = "https://api.mail.tm"):
self.api_base = api_base.rstrip("/")
self.last_mailbox_info: Dict[str, str] = {}
def _headers(self, token: str = "", use_json: bool = False) -> Dict[str, str]:
h: Dict[str, str] = {"Accept": "application/json"}
@@ -215,6 +216,11 @@ class MailTmProvider(MailProvider):
if token_resp.status_code == 200:
token = str(token_resp.json().get("token") or "").strip()
if token:
self.last_mailbox_info = {
"email": email,
"mail_password": password,
"mail_token": token,
}
return email, token
return "", ""
@@ -402,6 +408,7 @@ class DuckMailProvider(MailProvider):
self.api_base = api_base.rstrip("/")
self.bearer_token = bearer_token
self.domain = str(domain).strip()
self.last_mailbox_info: Dict[str, str] = {}
def _auth_headers(self, token: str = "") -> Dict[str, str]:
h: Dict[str, str] = {"Accept": "application/json"}
@@ -455,6 +462,11 @@ class DuckMailProvider(MailProvider):
if token_resp.status_code == 200:
mail_token = token_resp.json().get("token")
if mail_token:
self.last_mailbox_info = {
"email": email,
"mail_password": password,
"mail_token": str(mail_token),
}
return email, str(mail_token)
except Exception as exc:
logger.warning("DuckMail 创建邮箱失败: %s", exc)

View File

@@ -731,7 +731,14 @@ def _post_form(
) from exc
def _build_token_result(token_payload: Dict[str, Any], account_password: str = "") -> str:
def _build_token_result(
token_payload: Dict[str, Any],
account_password: str = "",
mail_password: str = "",
mail_token: str = "",
name: str = "",
birthdate: str = "",
) -> str:
access_token = str(token_payload.get("access_token") or "").strip()
refresh_token = str(token_payload.get("refresh_token") or "").strip()
id_token = str(token_payload.get("id_token") or "").strip()
@@ -773,6 +780,14 @@ def _build_token_result(token_payload: Dict[str, Any], account_password: str = "
}
if account_password:
config["account_password"] = account_password
if mail_password:
config["mail_password"] = mail_password
if mail_token:
config["mail_token"] = mail_token
if name:
config["name"] = name
if birthdate:
config["birthdate"] = birthdate
return json.dumps(config, ensure_ascii=False, separators=(",", ":"))
@@ -1321,6 +1336,8 @@ def run(
return None
# ------- 步骤2创建临时邮箱 -------
mailbox_password = ""
mailbox_token = ""
if mail_provider is not None:
emitter.info("正在创建临时邮箱...", step="create_email")
try:
@@ -1330,6 +1347,9 @@ def run(
)
except TypeError:
email, dev_token = mail_provider.create_mailbox(proxy="")
mailbox_info = getattr(mail_provider, "last_mailbox_info", {}) or {}
mailbox_password = str(mailbox_info.get("mail_password") or "").strip()
mailbox_token = str(mailbox_info.get("mail_token") or dev_token or "").strip()
else:
emitter.info("正在创建 Mail.tm 临时邮箱...", step="create_email")
email, dev_token = get_email_and_token(
@@ -1337,6 +1357,7 @@ def run(
emitter,
proxy_selector=None,
)
mailbox_token = str(dev_token or "").strip()
if not email or not dev_token:
emitter.error("临时邮箱创建失败", step="create_email")
return None
@@ -2012,7 +2033,14 @@ def run(
emitter.success("Token 获取成功!", step="get_token")
try: s.close()
except: pass
return _build_token_result(_token_json, account_password=account_password)
return _build_token_result(
_token_json,
account_password=account_password,
mail_password=mailbox_password,
mail_token=mailbox_token,
name=_rand_name,
birthdate=_rand_bday,
)
except Exception as e:
emitter.error(f"运行时发生错误: {e}", step="runtime")