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:
@@ -0,0 +1,270 @@
|
||||
"""REST API v1 — 全部路由。"""
|
||||
import os
|
||||
from flask import Blueprint, request, jsonify, current_app
|
||||
from auth import login_required
|
||||
from database import db
|
||||
from models import Instance, User, AuditLog
|
||||
from services import user_service, stats_service, backup_service
|
||||
|
||||
bp = Blueprint("api", __name__)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
# 仪表盘
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
@bp.route("/dashboard")
|
||||
@login_required
|
||||
def dashboard():
|
||||
return jsonify(stats_service.get_dashboard_summary())
|
||||
|
||||
|
||||
@bp.route("/stats/system")
|
||||
@login_required
|
||||
def system_stats():
|
||||
return jsonify(stats_service.get_realtime_metrics())
|
||||
|
||||
|
||||
@bp.route("/stats/traffic-trend")
|
||||
@login_required
|
||||
def traffic_trend():
|
||||
days = request.args.get("days", 30, type=int)
|
||||
return jsonify(stats_service.get_traffic_trend(days))
|
||||
|
||||
|
||||
@bp.route("/stats/users")
|
||||
@login_required
|
||||
def user_ranking():
|
||||
return jsonify(stats_service.get_user_stats())
|
||||
|
||||
|
||||
@bp.route("/stats/instances")
|
||||
@login_required
|
||||
def instance_ranking():
|
||||
return jsonify(stats_service.get_instance_stats())
|
||||
|
||||
|
||||
@bp.route("/stats/connections")
|
||||
@login_required
|
||||
def active_connections():
|
||||
return jsonify(stats_service.get_active_connections())
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
# 实例管理
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
@bp.route("/instances", methods=["GET"])
|
||||
@login_required
|
||||
def list_instances():
|
||||
return jsonify(current_app.instance_manager.get_status())
|
||||
|
||||
|
||||
@bp.route("/instances", methods=["POST"])
|
||||
@login_required
|
||||
def create_instance():
|
||||
data = request.get_json()
|
||||
name = data.get("name")
|
||||
if not name:
|
||||
return {"error": "名称不能为空"}, 400
|
||||
if db.session.query(Instance).filter_by(name=name).first():
|
||||
return {"error": "名称已存在"}, 400
|
||||
|
||||
inst = Instance(
|
||||
name=name,
|
||||
listen_host=data.get("listen_host", "0.0.0.0"),
|
||||
listen_port=int(data.get("listen_port", 1080)),
|
||||
timeout=int(data.get("timeout", 30)),
|
||||
auth_method=data.get("auth_method", "none"),
|
||||
bandwidth_down=float(data.get("bandwidth_down", 0)),
|
||||
bandwidth_up=float(data.get("bandwidth_up", 0)),
|
||||
max_concurrent=int(data.get("max_concurrent", 10)),
|
||||
notes=data.get("notes", ""),
|
||||
)
|
||||
db.session.add(inst)
|
||||
db.session.commit()
|
||||
|
||||
if data.get("start", True):
|
||||
current_app.instance_manager.start_instance(inst.id)
|
||||
|
||||
return jsonify({"id": inst.id}), 201
|
||||
|
||||
|
||||
@bp.route("/instances/<int:iid>", methods=["PUT"])
|
||||
@login_required
|
||||
def update_instance(iid):
|
||||
inst = db.session.query(Instance).get(iid)
|
||||
if not inst:
|
||||
return {"error": "实例不存在"}, 404
|
||||
data = request.get_json()
|
||||
for k in ("listen_host", "listen_port", "timeout", "auth_method",
|
||||
"bandwidth_down", "bandwidth_up", "max_concurrent", "notes"):
|
||||
v = data.get(k)
|
||||
if v is not None:
|
||||
setattr(inst, k, float(v) if k in ("bandwidth_down", "bandwidth_up")
|
||||
else int(v) if k in ("listen_port", "timeout", "max_concurrent")
|
||||
else v)
|
||||
db.session.commit()
|
||||
|
||||
# 重启以应用配置
|
||||
if inst.running:
|
||||
current_app.instance_manager.restart_instance(iid)
|
||||
|
||||
return jsonify({"ok": True})
|
||||
|
||||
|
||||
@bp.route("/instances/<int:iid>", methods=["DELETE"])
|
||||
@login_required
|
||||
def delete_instance(iid):
|
||||
inst = db.session.query(Instance).get(iid)
|
||||
if not inst:
|
||||
return {"error": "实例不存在"}, 404
|
||||
current_app.instance_manager.stop_instance(iid)
|
||||
db.session.delete(inst)
|
||||
db.session.commit()
|
||||
return jsonify({"ok": True})
|
||||
|
||||
|
||||
@bp.route("/instances/<int:iid>/start", methods=["POST"])
|
||||
@login_required
|
||||
def start_instance(iid):
|
||||
if current_app.instance_manager.start_instance(iid):
|
||||
return jsonify({"ok": True})
|
||||
return {"error": "启动失败"}, 500
|
||||
|
||||
|
||||
@bp.route("/instances/<int:iid>/stop", methods=["POST"])
|
||||
@login_required
|
||||
def stop_instance(iid):
|
||||
if current_app.instance_manager.stop_instance(iid):
|
||||
return jsonify({"ok": True})
|
||||
return {"error": "停止失败"}, 500
|
||||
|
||||
|
||||
@bp.route("/instances/<int:iid>/restart", methods=["POST"])
|
||||
@login_required
|
||||
def restart_instance(iid):
|
||||
if current_app.instance_manager.restart_instance(iid):
|
||||
return jsonify({"ok": True})
|
||||
return {"error": "重启失败"}, 500
|
||||
|
||||
|
||||
@bp.route("/instances/sync", methods=["POST"])
|
||||
@login_required
|
||||
def sync_instances():
|
||||
current_app.instance_manager.sync_instances()
|
||||
return jsonify({"ok": True})
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
# 用户管理
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
@bp.route("/users", methods=["GET"])
|
||||
@login_required
|
||||
def list_users():
|
||||
page = request.args.get("page", 1, type=int)
|
||||
search = request.args.get("search", "")
|
||||
return jsonify(current_app.user_service.list_users(page=page, search=search))
|
||||
|
||||
|
||||
@bp.route("/users", methods=["POST"])
|
||||
@login_required
|
||||
def create_user():
|
||||
data = request.get_json()
|
||||
result = current_app.user_service.create_user(
|
||||
username=data.get("username", ""),
|
||||
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:
|
||||
return jsonify(result), 400
|
||||
return jsonify(result), 201
|
||||
|
||||
|
||||
@bp.route("/users/<int:uid>", methods=["PUT"])
|
||||
@login_required
|
||||
def update_user(uid):
|
||||
data = request.get_json()
|
||||
result = current_app.user_service.update_user(uid, **data)
|
||||
if "error" in result:
|
||||
return jsonify(result), 404
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@bp.route("/users/<int:uid>", methods=["DELETE"])
|
||||
@login_required
|
||||
def delete_user(uid):
|
||||
result = current_app.user_service.delete_user(uid)
|
||||
if "error" in result:
|
||||
return jsonify(result), 404
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@bp.route("/users/<int:uid>/toggle", methods=["POST"])
|
||||
@login_required
|
||||
def toggle_user(uid):
|
||||
enabled = request.json.get("enabled", False) if request.is_json else True
|
||||
return jsonify(current_app.user_service.toggle_user(uid, enabled))
|
||||
|
||||
|
||||
@bp.route("/users/<int:uid>/ban", methods=["POST"])
|
||||
@login_required
|
||||
def ban_user(uid):
|
||||
banned = request.json.get("banned", True) if request.is_json else True
|
||||
return jsonify(current_app.user_service.ban_user(uid, banned))
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
# 审计日志
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
@bp.route("/logs", methods=["GET"])
|
||||
@login_required
|
||||
def query_logs():
|
||||
page = request.args.get("page", 1, type=int)
|
||||
return jsonify(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", ""),
|
||||
))
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
# 备份
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
@bp.route("/backups", methods=["GET"])
|
||||
@login_required
|
||||
def list_backups():
|
||||
return jsonify(backup_service.list_backups())
|
||||
|
||||
|
||||
@bp.route("/backups", methods=["POST"])
|
||||
@login_required
|
||||
def create_backup():
|
||||
note = request.get_json().get("note", "") if request.is_json else ""
|
||||
result = backup_service.create_backup(note=note)
|
||||
if "error" in result:
|
||||
return jsonify(result), 500
|
||||
return jsonify(result), 201
|
||||
|
||||
|
||||
@bp.route("/backups/<int:bid>/restore", methods=["POST"])
|
||||
@login_required
|
||||
def restore_backup(bid):
|
||||
result = backup_service.restore_backup(bid)
|
||||
if "error" in result:
|
||||
return jsonify(result), 500
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@bp.route("/backups/cleanup", methods=["POST"])
|
||||
@login_required
|
||||
def cleanup_backups():
|
||||
keep = request.json.get("keep", 10) if request.is_json else 10
|
||||
backup_service.cleanup_old_backups(keep=int(keep))
|
||||
return jsonify({"ok": True})
|
||||
Reference in New Issue
Block a user