Spaces:
Running
Running
Upload 22 files
Browse files- router_messages.py +82 -6
router_messages.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
# router_messages.py
|
| 2 |
-
from fastapi import APIRouter, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import time
|
| 5 |
import uuid
|
|
|
|
|
|
|
| 6 |
import 数据库连接 as db
|
| 7 |
from notifications import add_notification
|
| 8 |
from models import PrivateMessage
|
|
|
|
| 9 |
|
| 10 |
router = APIRouter()
|
| 11 |
|
|
@@ -17,13 +20,13 @@ class SystemAnnouncement(BaseModel):
|
|
| 17 |
content: str
|
| 18 |
|
| 19 |
# ==========================================
|
| 20 |
-
# 新增:发布系统公告接口 (仅限管理员)
|
| 21 |
# ==========================================
|
| 22 |
@router.post("/api/system/announcement")
|
| 23 |
-
async def publish_announcement(ann: SystemAnnouncement):
|
| 24 |
-
#
|
| 25 |
-
if
|
| 26 |
-
raise HTTPException(status_code=403, detail="无权发布系统公告")
|
| 27 |
|
| 28 |
announcements_db = db.load_data("announcements.json", default_data=[])
|
| 29 |
|
|
@@ -41,6 +44,79 @@ async def publish_announcement(ann: SystemAnnouncement):
|
|
| 41 |
db.save_data("announcements.json", announcements_db)
|
| 42 |
return {"status": "success"}
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# ==========================================
|
| 45 |
# 原有功能:私信与聊天
|
| 46 |
# ==========================================
|
|
|
|
| 1 |
# router_messages.py
|
| 2 |
+
from fastapi import APIRouter, HTTPException, Depends
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import time
|
| 5 |
import uuid
|
| 6 |
+
import subprocess
|
| 7 |
+
import os
|
| 8 |
import 数据库连接 as db
|
| 9 |
from notifications import add_notification
|
| 10 |
from models import PrivateMessage
|
| 11 |
+
from 安全认证 import require_auth
|
| 12 |
|
| 13 |
router = APIRouter()
|
| 14 |
|
|
|
|
| 20 |
content: str
|
| 21 |
|
| 22 |
# ==========================================
|
| 23 |
+
# 新增:发布系统公告接口 (仅限管理员,使用JWT验证)
|
| 24 |
# ==========================================
|
| 25 |
@router.post("/api/system/announcement")
|
| 26 |
+
async def publish_announcement(ann: SystemAnnouncement, current_user: str = Depends(require_auth)):
|
| 27 |
+
# ✅ 安全检查:使用 JWT Token 解析出的真实用户账号,而不是请求体中的字段
|
| 28 |
+
if current_user != "123456":
|
| 29 |
+
raise HTTPException(status_code=403, detail="无权发布系统公告,仅管理员可操作")
|
| 30 |
|
| 31 |
announcements_db = db.load_data("announcements.json", default_data=[])
|
| 32 |
|
|
|
|
| 44 |
db.save_data("announcements.json", announcements_db)
|
| 45 |
return {"status": "success"}
|
| 46 |
|
| 47 |
+
|
| 48 |
+
# ==========================================
|
| 49 |
+
# 管理员调试:执行 Python 脚本
|
| 50 |
+
# ==========================================
|
| 51 |
+
class AdminScriptRequest(BaseModel):
|
| 52 |
+
admin_account: str
|
| 53 |
+
script_name: str
|
| 54 |
+
|
| 55 |
+
@router.post("/api/admin/run-script")
|
| 56 |
+
async def run_admin_script(req: AdminScriptRequest, current_user: str = Depends(require_auth)):
|
| 57 |
+
"""
|
| 58 |
+
管理员专属:执行指定的 Python 脚本
|
| 59 |
+
✅ 安全改造:使用 JWT Token 验证真实登录用户
|
| 60 |
+
"""
|
| 61 |
+
# ✅ 安全检查:使用 JWT Token 解析出的真实用户账号
|
| 62 |
+
if current_user != "123456":
|
| 63 |
+
raise HTTPException(status_code=403, detail="无权执行此操作,仅管理员可操作")
|
| 64 |
+
|
| 65 |
+
script_name = req.script_name.strip()
|
| 66 |
+
if not script_name:
|
| 67 |
+
raise HTTPException(status_code=400, detail="脚本名称不能为空")
|
| 68 |
+
|
| 69 |
+
# 安全检查:仅允许执行 .py 文件
|
| 70 |
+
if not script_name.endswith(".py"):
|
| 71 |
+
raise HTTPException(status_code=400, detail="仅支持执行 .py 文件")
|
| 72 |
+
|
| 73 |
+
# 获取当前工作目录
|
| 74 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 75 |
+
script_path = os.path.join(current_dir, script_name)
|
| 76 |
+
|
| 77 |
+
# 检查文件是否存在
|
| 78 |
+
if not os.path.exists(script_path):
|
| 79 |
+
return {
|
| 80 |
+
"status": "error",
|
| 81 |
+
"output": f"❌ 脚本文件不存在: {script_name}\n\n当前目录: {current_dir}\n\n可用脚本: {[f for f in os.listdir(current_dir) if f.endswith('.py')]}"
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
# 执行脚本,设置超时 60 秒
|
| 86 |
+
result = subprocess.run(
|
| 87 |
+
["python", script_path],
|
| 88 |
+
capture_output=True,
|
| 89 |
+
text=True,
|
| 90 |
+
timeout=60,
|
| 91 |
+
cwd=current_dir,
|
| 92 |
+
encoding="utf-8"
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
output = ""
|
| 96 |
+
if result.stdout:
|
| 97 |
+
output += f"📝 标准输出:\n{result.stdout}\n"
|
| 98 |
+
if result.stderr:
|
| 99 |
+
output += f"\n⚠️ 错误输出:\n{result.stderr}"
|
| 100 |
+
if not output:
|
| 101 |
+
output = "✅ 脚本执行完成,无输出"
|
| 102 |
+
|
| 103 |
+
return {
|
| 104 |
+
"status": "success" if result.returncode == 0 else "error",
|
| 105 |
+
"return_code": result.returncode,
|
| 106 |
+
"output": output
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
except subprocess.TimeoutExpired:
|
| 110 |
+
return {
|
| 111 |
+
"status": "error",
|
| 112 |
+
"output": "❌ 脚本执行超时 (60秒)"
|
| 113 |
+
}
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return {
|
| 116 |
+
"status": "error",
|
| 117 |
+
"output": f"❌ 执行异常: {str(e)}"
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
# ==========================================
|
| 121 |
# 原有功能:私信与聊天
|
| 122 |
# ==========================================
|