fix: 修复暗色主题UI对比度、端口占用死循环及多项Web表单bug

后端修复:
- engine/instances.py: Socks5Server.start() 端口占用时死循环, 增加超时和异常透传
- engine/instances.py: import time 移至顶层
- engine/instances.py: sync_instances/start_instance 容忍单实例启动失败
- run.py: 启动时 sync_instances 失败不退出进程
- web/routes.py: instances start/restart/edit 路由捕获 OSError, flash 友好提示
- api/v1.py: instances start/restart/update API 返回 JSON 错误而非 500 崩溃
- web/routes.py: toggle_user/ban_user 支持 hidden 字段语义, 缺字段时切换状态
- web/routes.py: inject_nav 增加独立 nav.dashboard 和 page_name, 修复双 active bug
- auth.py: login 路由添加 logging import, 修 NameError

前端重构:
- templates/base.html: 全局对比度提升(--text #f1f5f9, --accent #a5b4fc), 标题色显式声明, alert/modal/progress/table 完整暗色样式
- templates/base.html: main-content padding-top 修复 navbar 遮挡内容
- templates/base.html: 侧边栏 active 高亮 (白字 + elevated bg + 左边框)
- templates/base.html: 顶栏动态显示当前页面名
- templates/*.html: 统一 page-header 类, 所有图标按钮添加 title tooltip
- templates/users.html: toggle/ban 表单添加 hidden enabled/ban 字段
- templates/connections.html: fmtBytes 改为 Jinja macro, 时间格式化
- templates/dashboard.html: 时间格式化为 YYYY-MM-DD HH:MM:SS, chart canvas 高度调整
- templates/logs.html: 时间格式化为可读格式
- templates/stats.html: 提升 chart 颜色对比度
- templates/system.html: 使用 time.localtime() 显示本地时间
This commit is contained in:
Your Name
2026-07-16 23:01:39 +08:00
parent 6504726431
commit 0bd9bcac12
13 changed files with 268 additions and 103 deletions
+63 -14
View File
@@ -12,15 +12,33 @@ bp = Blueprint("web", __name__)
@bp.app_context_processor
def inject_nav():
from flask import session as flask_session
p = request.path
if p == "/" or p.startswith("/dashboard"):
page_name = "仪表盘"
elif p.startswith("/instances"):
page_name = "代理实例"
elif p.startswith("/users"):
page_name = "用户管理"
elif p == "/stats":
page_name = "流量统计"
elif p.startswith("/stats/connections"):
page_name = "活跃连接"
elif p.startswith("/logs"):
page_name = "审计日志"
elif p.startswith("/system"):
page_name = "系统管理"
else:
page_name = ""
return {
"nav": {
"instances": request.path.startswith("/instances") or
request.path == "/" or request.path == "/dashboard",
"users": request.path.startswith("/users"),
"stats": request.path.startswith("/stats"),
"logs": request.path.startswith("/logs"),
"system": request.path.startswith("/system"),
"dashboard": p == "/" or p.startswith("/dashboard"),
"instances": p.startswith("/instances"),
"users": p.startswith("/users"),
"stats": p.startswith("/stats"),
"logs": p.startswith("/logs"),
"system": p.startswith("/system"),
},
"page_name": page_name,
"logged_in": flask_session.get("logged_in", False),
}
@@ -45,8 +63,11 @@ def instances():
@bp.route("/instances/<int:iid>/start", methods=["POST"])
@login_required
def start_instance(iid):
current_app.instance_manager.start_instance(iid)
flash("实例已启动", "success")
try:
current_app.instance_manager.start_instance(iid)
flash("实例已启动", "success")
except OSError as e:
flash(f"启动失败(端口可能被占用): {e}", "danger")
return redirect(url_for("web.instances"))
@@ -61,8 +82,11 @@ def stop_instance(iid):
@bp.route("/instances/<int:iid>/restart", methods=["POST"])
@login_required
def restart_instance(iid):
current_app.instance_manager.restart_instance(iid)
flash("实例已重启", "info")
try:
current_app.instance_manager.restart_instance(iid)
flash("实例已重启", "info")
except OSError as e:
flash(f"重启失败(端口可能被占用): {e}", "danger")
return redirect(url_for("web.instances"))
@@ -116,7 +140,10 @@ def edit_instance(iid):
db.session.commit()
if inst.running:
current_app.instance_manager.restart_instance(iid)
try:
current_app.instance_manager.restart_instance(iid)
except OSError as e:
flash(f"重启失败(端口可能被占用): {e}", "warning")
flash("配置已更新", "success")
return redirect(url_for("web.instances"))
@@ -205,7 +232,19 @@ def delete_user(uid):
@bp.route("/users/<int:uid>/toggle", methods=["POST"])
@login_required
def toggle_user(uid):
enabled = "enabled" in request.form
# 支持两种调用方式:
# 1) 隐藏字段 enabled=on/off (web form)
# 2) 不带字段时切换当前状态 (传统行为, 防止表单无字段导致总是禁用)
if "enabled" in request.form:
enabled = request.form.get("enabled") in ("on", "true", "1", "yes")
else:
from models import User
from database import db
u = db.session.query(User).get(uid)
if not u:
flash("用户不存在", "danger")
return redirect(url_for("web.users"))
enabled = not u.enabled
result = current_app.user_service.toggle_user(uid, enabled)
flash("" + ("启用" if enabled else "禁用") + "该用户", "success")
return redirect(url_for("web.users"))
@@ -214,7 +253,17 @@ def toggle_user(uid):
@bp.route("/users/<int:uid>/ban", methods=["POST"])
@login_required
def ban_user(uid):
banned = "ban" in request.form
# 同 toggle_user 的两种调用方式
if "ban" in request.form:
banned = request.form.get("ban") in ("on", "true", "1", "yes")
else:
from models import User
from database import db
u = db.session.query(User).get(uid)
if not u:
flash("用户不存在", "danger")
return redirect(url_for("web.users"))
banned = not u.banned
result = current_app.user_service.ban_user(uid, banned)
flash("" + ("封禁" if banned else "解封") + "该用户", "success")
return redirect(url_for("web.users"))
@@ -265,7 +314,7 @@ def system():
sys_version=platform.python_version(),
sys_platform=platform.platform(),
sys_hostname=platform.node(),
uptime=time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
uptime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
@bp.route("/system/backup", methods=["POST"])