0bd9bcac12
后端修复: - 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() 显示本地时间
337 lines
12 KiB
Python
337 lines
12 KiB
Python
"""Web 管理面板路由。"""
|
|
from flask import Blueprint, render_template, request, redirect, url_for, flash, current_app
|
|
from auth import login_required
|
|
from database import db
|
|
from models import Instance, User
|
|
from services import stats_service, backup_service
|
|
|
|
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": {
|
|
"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),
|
|
}
|
|
|
|
|
|
# ── 仪表盘 ─────────────────────────────────────────────────────
|
|
@bp.route("/")
|
|
@bp.route("/dashboard")
|
|
@login_required
|
|
def index():
|
|
summary = stats_service.get_dashboard_summary()
|
|
return render_template("dashboard.html", s=summary)
|
|
|
|
|
|
# ── 实例管理 ───────────────────────────────────────────────────
|
|
@bp.route("/instances")
|
|
@login_required
|
|
def instances():
|
|
inst_list = current_app.instance_manager.get_status()
|
|
return render_template("instances.html", instances=inst_list)
|
|
|
|
|
|
@bp.route("/instances/<int:iid>/start", methods=["POST"])
|
|
@login_required
|
|
def start_instance(iid):
|
|
try:
|
|
current_app.instance_manager.start_instance(iid)
|
|
flash("实例已启动", "success")
|
|
except OSError as e:
|
|
flash(f"启动失败(端口可能被占用): {e}", "danger")
|
|
return redirect(url_for("web.instances"))
|
|
|
|
|
|
@bp.route("/instances/<int:iid>/stop", methods=["POST"])
|
|
@login_required
|
|
def stop_instance(iid):
|
|
current_app.instance_manager.stop_instance(iid)
|
|
flash("实例已停止", "warning")
|
|
return redirect(url_for("web.instances"))
|
|
|
|
|
|
@bp.route("/instances/<int:iid>/restart", methods=["POST"])
|
|
@login_required
|
|
def restart_instance(iid):
|
|
try:
|
|
current_app.instance_manager.restart_instance(iid)
|
|
flash("实例已重启", "info")
|
|
except OSError as e:
|
|
flash(f"重启失败(端口可能被占用): {e}", "danger")
|
|
return redirect(url_for("web.instances"))
|
|
|
|
|
|
@bp.route("/instances/add", methods=["POST"])
|
|
@login_required
|
|
def add_instance():
|
|
name = request.form.get("name", "").strip()
|
|
if not name:
|
|
flash("名称不能为空", "danger")
|
|
return redirect(url_for("web.instances"))
|
|
if db.session.query(Instance).filter_by(name=name).first():
|
|
flash("名称已存在", "danger")
|
|
return redirect(url_for("web.instances"))
|
|
|
|
inst = Instance(
|
|
name=name,
|
|
listen_host=request.form.get("listen_host", "0.0.0.0"),
|
|
listen_port=int(request.form.get("listen_port", 1080)),
|
|
timeout=int(request.form.get("timeout", 30)),
|
|
auth_method=request.form.get("auth_method", "none"),
|
|
bandwidth_down=float(request.form.get("bandwidth_down", 0)),
|
|
bandwidth_up=float(request.form.get("bandwidth_up", 0)),
|
|
max_concurrent=int(request.form.get("max_concurrent", 10)),
|
|
notes=request.form.get("notes", ""),
|
|
)
|
|
db.session.add(inst)
|
|
db.session.commit()
|
|
|
|
if request.form.get("start") == "on":
|
|
current_app.instance_manager.start_instance(inst.id)
|
|
|
|
flash(f"实例 {name} 已创建", "success")
|
|
return redirect(url_for("web.instances"))
|
|
|
|
|
|
@bp.route("/instances/<int:iid>/edit", methods=["POST"])
|
|
@login_required
|
|
def edit_instance(iid):
|
|
inst = db.session.query(Instance).get(iid)
|
|
if not inst:
|
|
return "实例不存在", 404
|
|
data = request.form
|
|
inst.listen_host = data.get("listen_host", inst.listen_host)
|
|
inst.listen_port = int(data.get("listen_port", inst.listen_port))
|
|
inst.timeout = int(data.get("timeout", inst.timeout))
|
|
inst.auth_method = data.get("auth_method", inst.auth_method)
|
|
inst.bandwidth_down = float(data.get("bandwidth_down", inst.bandwidth_down))
|
|
inst.bandwidth_up = float(data.get("bandwidth_up", inst.bandwidth_up))
|
|
inst.max_concurrent = int(data.get("max_concurrent", inst.max_concurrent))
|
|
inst.notes = data.get("notes", inst.notes)
|
|
db.session.commit()
|
|
|
|
if inst.running:
|
|
try:
|
|
current_app.instance_manager.restart_instance(iid)
|
|
except OSError as e:
|
|
flash(f"重启失败(端口可能被占用): {e}", "warning")
|
|
|
|
flash("配置已更新", "success")
|
|
return redirect(url_for("web.instances"))
|
|
|
|
|
|
@bp.route("/instances/<int:iid>/delete", methods=["POST"])
|
|
@login_required
|
|
def delete_instance(iid):
|
|
inst = db.session.query(Instance).get(iid)
|
|
if not inst:
|
|
return "实例不存在", 404
|
|
current_app.instance_manager.stop_instance(iid)
|
|
db.session.delete(inst)
|
|
db.session.commit()
|
|
flash("实例已删除", "warning")
|
|
return redirect(url_for("web.instances"))
|
|
|
|
|
|
# ── 用户管理 ───────────────────────────────────────────────────
|
|
@bp.route("/users")
|
|
@login_required
|
|
def users():
|
|
page = int(request.args.get("page", 1))
|
|
search = request.args.get("search", "")
|
|
data = current_app.user_service.list_users(page=page, search=search)
|
|
return render_template("users.html", **data, search=search)
|
|
|
|
|
|
@bp.route("/users/add", methods=["POST"])
|
|
@login_required
|
|
def add_user():
|
|
data = request.form
|
|
result = current_app.user_service.create_user(
|
|
username=data.get("username", "").strip(),
|
|
password=data.get("password", ""),
|
|
bandwidth_down=float(data.get("bandwidth_down", 0)),
|
|
bandwidth_up=float(data.get("bandwidth_up", 0)),
|
|
max_concurrent=int(data.get("max_concurrent", 10)),
|
|
total_traffic_mb=float(data.get("total_traffic_mb", 0)),
|
|
monthly_traffic_mb=float(data.get("monthly_traffic_mb", 0)),
|
|
ip_whitelist=data.get("ip_whitelist", ""),
|
|
ip_blacklist=data.get("ip_blacklist", ""),
|
|
expire_at=data.get("expire_at"),
|
|
)
|
|
if "error" in result:
|
|
flash(result["error"], "danger")
|
|
else:
|
|
flash("用户已创建", "success")
|
|
return redirect(url_for("web.users"))
|
|
|
|
|
|
@bp.route("/users/<int:uid>/edit", methods=["POST"])
|
|
@login_required
|
|
def edit_user(uid):
|
|
data = request.form
|
|
kwargs = {
|
|
"password": data.get("password", ""),
|
|
"bandwidth_down": float(data.get("bandwidth_down", 0)),
|
|
"bandwidth_up": float(data.get("bandwidth_up", 0)),
|
|
"max_concurrent": int(data.get("max_concurrent", 10)),
|
|
"total_traffic_mb": float(data.get("total_traffic_mb", 0)),
|
|
"monthly_traffic_mb": float(data.get("monthly_traffic_mb", 0)),
|
|
"ip_whitelist": data.get("ip_whitelist", ""),
|
|
"ip_blacklist": data.get("ip_blacklist", ""),
|
|
"expire_at": data.get("expire_at"),
|
|
}
|
|
result = current_app.user_service.update_user(uid, **kwargs)
|
|
if "error" in result:
|
|
flash(result["error"], "danger")
|
|
else:
|
|
flash("用户已更新", "success")
|
|
return redirect(url_for("web.users"))
|
|
|
|
|
|
@bp.route("/users/<int:uid>/delete", methods=["POST"])
|
|
@login_required
|
|
def delete_user(uid):
|
|
result = current_app.user_service.delete_user(uid)
|
|
if "error" in result:
|
|
flash(result["error"], "danger")
|
|
else:
|
|
flash("用户已删除", "warning")
|
|
return redirect(url_for("web.users"))
|
|
|
|
|
|
@bp.route("/users/<int:uid>/toggle", methods=["POST"])
|
|
@login_required
|
|
def toggle_user(uid):
|
|
# 支持两种调用方式:
|
|
# 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"))
|
|
|
|
|
|
@bp.route("/users/<int:uid>/ban", methods=["POST"])
|
|
@login_required
|
|
def ban_user(uid):
|
|
# 同 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"))
|
|
|
|
|
|
# ── 统计 ───────────────────────────────────────────────────────
|
|
@bp.route("/stats")
|
|
@login_required
|
|
def stats():
|
|
return render_template("stats.html",
|
|
trend=stats_service.get_traffic_trend(30),
|
|
users=stats_service.get_user_stats(),
|
|
instances=stats_service.get_instance_stats())
|
|
|
|
|
|
# ── 活跃连接 ───────────────────────────────────────────────────
|
|
@bp.route("/stats/connections")
|
|
@login_required
|
|
def connections():
|
|
return render_template("connections.html",
|
|
conns=stats_service.get_active_connections())
|
|
|
|
|
|
# ── 审计日志 ───────────────────────────────────────────────────
|
|
@bp.route("/logs")
|
|
@login_required
|
|
def logs():
|
|
page = int(request.args.get("page", 1))
|
|
data = current_app.user_service.query_logs(
|
|
page=page,
|
|
event=request.args.get("event", ""),
|
|
user=request.args.get("user", ""),
|
|
src_ip=request.args.get("src_ip", ""),
|
|
)
|
|
return render_template("logs.html", **data,
|
|
event_filter=request.args.get("event", ""),
|
|
user_filter=request.args.get("user", ""),
|
|
ip_filter=request.args.get("src_ip", ""))
|
|
|
|
|
|
# ── 系统管理 ───────────────────────────────────────────────────
|
|
@bp.route("/system")
|
|
@login_required
|
|
def system():
|
|
import platform, time
|
|
backups = backup_service.list_backups()
|
|
return render_template("system.html", backups=backups,
|
|
sys_version=platform.python_version(),
|
|
sys_platform=platform.platform(),
|
|
sys_hostname=platform.node(),
|
|
uptime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
|
|
|
|
|
|
@bp.route("/system/backup", methods=["POST"])
|
|
@login_required
|
|
def create_backup():
|
|
backup_service.create_backup()
|
|
flash("备份已完成", "success")
|
|
return redirect(url_for("web.system"))
|
|
|
|
|
|
@bp.route("/system/restore/<int:bid>", methods=["POST"])
|
|
@login_required
|
|
def restore_backup(bid):
|
|
result = backup_service.restore_backup(bid)
|
|
if "error" in result:
|
|
flash(result["error"], "danger")
|
|
else:
|
|
flash("恢复完成,请刷新页面", "success")
|
|
return redirect(url_for("web.system"))
|