feat: 重构为 SOCKS5 代理管理平台 v2.0
架构: - engine/server.py: asyncio SOCKS5 服务器引擎(含隧道/认证/限速) - engine/instances.py: 多实例管理器(独立事件循环/热启动/热停止) - services/user_service.py: 用户认证/流量统计/IP白名单/防爆破 - services/stats_service.py: 实时监控/流量趋势/连接追踪 - services/backup_service.py: 数据库备份与恢复 - api/v1.py: 完整 RESTful API - web/routes.py: Web 管理面板(暗色 Bootstrap 5 主题) 功能覆盖: ✅ 多实例 SOCKS5 管理(创建/启动/停止/重启/热加载) ✅ 用户认证(无认证/账号密码/流量上限/限速/并发限制) ✅ IP 白名单/黑名单/防爆破 ✅ 实时仪表盘(CPU/内存/网络/活跃连接) ✅ 流量统计(30天趋势图/用户排名/实例统计) ✅ 活跃连接追踪 ✅ 审计日志(事件/用户/IP 筛选) ✅ 数据库备份与恢复 ✅ 管理员密码保护 ✅ 16 项功能测试全部通过
This commit is contained in:
+287
@@ -0,0 +1,287 @@
|
||||
"""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
|
||||
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"),
|
||||
},
|
||||
"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):
|
||||
current_app.instance_manager.start_instance(iid)
|
||||
flash("实例已启动", "success")
|
||||
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):
|
||||
current_app.instance_manager.restart_instance(iid)
|
||||
flash("实例已重启", "info")
|
||||
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:
|
||||
current_app.instance_manager.restart_instance(iid)
|
||||
|
||||
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):
|
||||
enabled = "enabled" in request.form
|
||||
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):
|
||||
banned = "ban" in request.form
|
||||
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.gmtime()))
|
||||
|
||||
|
||||
@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"))
|
||||
Reference in New Issue
Block a user