深度优化: 修复跨服务器登录、密码哈希、仪表盘UI、流量趋势等12项问题
### 严重修复 1. 跨服务器无法登录 - 新增SESSION_COOKIE_DOMAIN/SECURE/NAME配置 2. SOCKS5用户密码明文存储→bcrypt哈希(兼容自动迁移) 3. 仪表盘审计日志不显示→替换为空循环为真实API加载 4. 管理员登录防爆破未生效→auth.py集成check_fail2ban ### 中等问题修复 5. 流量趋势图表无数据→新增record_traffic_snapshot后台线程 6. 网络流量累计值跳变→新增基线差值机制 7. asyncio连接计数非线程安全→加锁保护 ### 优化改进 8. API密钥页面截断文本修复 9. 新增gunicorn/bcrypt依赖 10. 更新README环境变量文档和生产建议 11. 登录密码常量时间对比防时序攻击 12. 仪表盘图表添加animation:false防卡顿
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""SQLAlchemy 数据模型。"""
|
||||
from datetime import datetime, timezone
|
||||
from database import db
|
||||
import bcrypt
|
||||
|
||||
|
||||
# ── SOCKS5 实例 ──────────────────────────────────────────────────
|
||||
@@ -45,7 +46,7 @@ class User(db.Model):
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(64), nullable=False, unique=True, index=True)
|
||||
password = db.Column(db.String(128), nullable=True) # 明文或哈希;认证方式为 userpass 时必须
|
||||
password = db.Column(db.String(128), nullable=True) # 存储 bcrypt 哈希
|
||||
enabled = db.Column(db.Boolean, default=True)
|
||||
|
||||
# 流量限制
|
||||
@@ -75,6 +76,29 @@ class User(db.Model):
|
||||
updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc),
|
||||
onupdate=lambda: datetime.now(timezone.utc))
|
||||
|
||||
def set_password(self, password):
|
||||
"""用 bcrypt 哈希存储密码。"""
|
||||
self.password = bcrypt.hashpw(
|
||||
password.encode("utf-8"), bcrypt.gensalt()
|
||||
).decode("utf-8")
|
||||
|
||||
def check_password(self, password):
|
||||
"""验证密码。兼容旧版明文(自动迁移到哈希)。"""
|
||||
if not self.password:
|
||||
return False
|
||||
if self.password.startswith("$2b$") or self.password.startswith("$2a$"):
|
||||
try:
|
||||
return bcrypt.checkpw(
|
||||
password.encode("utf-8"), self.password.encode("utf-8")
|
||||
)
|
||||
except Exception:
|
||||
return False
|
||||
# 兼容旧版明文密码
|
||||
if self.password == password:
|
||||
self.set_password(password)
|
||||
return True
|
||||
return False
|
||||
|
||||
def is_active(self):
|
||||
"""用户是否可用(未过期、未封禁、已启用)"""
|
||||
if not self.enabled or self.banned:
|
||||
|
||||
Reference in New Issue
Block a user