72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
"""生产服务器一键建号 / 重置密码并开通会员。
|
||
|
||
用法(在服务器项目 server/ 目录下,用运行后端的同一个虚拟环境执行):
|
||
.venv/bin/python scripts/bootstrap_prod_account.py [手机号] [密码]
|
||
|
||
默认:13538859781 / 1159025a(开通 10 年,等价永久)
|
||
数据库路径与后端一致:优先环境变量 XL_DB_PATH,其次 data/xuanlong.db
|
||
"""
|
||
import os
|
||
import sqlite3
|
||
import sys
|
||
from datetime import datetime, timedelta
|
||
|
||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
sys.path.insert(0, ROOT)
|
||
|
||
from app.security import hash_password # noqa: E402
|
||
|
||
|
||
def resolve_db() -> str:
|
||
if os.environ.get("XL_DB_PATH"):
|
||
return os.environ["XL_DB_PATH"]
|
||
# .env 里可能配置了 XL_DB_PATH
|
||
env_file = os.path.join(ROOT, ".env")
|
||
if os.path.exists(env_file):
|
||
with open(env_file, encoding="utf-8") as f:
|
||
for line in f:
|
||
line = line.strip()
|
||
if line.startswith("XL_DB_PATH="):
|
||
return line.split("=", 1)[1].strip().strip('"').strip("'")
|
||
return os.path.join(ROOT, "data", "xuanlong.db")
|
||
|
||
|
||
def main() -> None:
|
||
phone = sys.argv[1] if len(sys.argv) > 1 else "13538859781"
|
||
pwd = sys.argv[2] if len(sys.argv) > 2 else "1159025a"
|
||
if len(phone) != 11 or not phone.startswith("1") or not phone.isdigit():
|
||
raise SystemExit("手机号格式不对")
|
||
if not (8 <= len(pwd) <= 20):
|
||
raise SystemExit("密码需 8-20 位")
|
||
|
||
db = resolve_db()
|
||
if not os.path.exists(db):
|
||
raise SystemExit(f"找不到数据库:{db}")
|
||
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||
until = (datetime.now() + timedelta(days=3650)).strftime("%Y-%m-%d %H:%M:%S")
|
||
conn = sqlite3.connect(db)
|
||
try:
|
||
row = conn.execute("SELECT id FROM users WHERE phone=?", (phone,)).fetchone()
|
||
if row:
|
||
conn.execute(
|
||
"UPDATE users SET password_hash=?, is_active=1, active_until=?, "
|
||
"is_banned=0, failed_attempts=0, locked_until=NULL WHERE phone=?",
|
||
(hash_password(pwd), until, phone),
|
||
)
|
||
print(f"✓ 已重置密码并开通会员:{phone}(uid={row[0]},至 {until})")
|
||
else:
|
||
conn.execute(
|
||
"INSERT INTO users(phone,password_hash,is_active,active_until,created_at) "
|
||
"VALUES(?,?,1,?,?)",
|
||
(phone, hash_password(pwd), until, now),
|
||
)
|
||
print(f"✓ 已创建账号并开通会员:{phone}(至 {until})")
|
||
conn.commit()
|
||
finally:
|
||
conn.close()
|
||
print("完成。如后端正在运行,无需重启。")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|