新增 FastAPI 文件服务器核心模块(配置、鉴权、存储、心跳上报),新增 HTTP 路由接口(健康检查、文件上传/下载/删除、管理登录/密码修改/统计),新增 Vue3 管理页面源码;新增 MySQL 元数据存储支持,新增 PyMySQL 依赖;新增 .gitignore 忽略 IDE 与备份文件;新增文件服务器工具脚本;删除旧的 build/ 脚本与 exe 构建产物,优化 Electron 平台登录逻辑为纯接口模式,新增平台账号环境变量注入,优化试用期异常检查逻辑,拆分服务岗转正流程为独立脚本。
54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""管理接口:登录 / 修改密码 / 统计。"""
|
|
from __future__ import annotations
|
|
|
|
import hmac
|
|
import time
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
from fileserver.core import auth, config, store
|
|
|
|
router = APIRouter(tags=["admin"])
|
|
|
|
|
|
class _LoginIn(BaseModel):
|
|
username: str = ""
|
|
password: str = ""
|
|
|
|
|
|
class _ChangePwdIn(BaseModel):
|
|
old_password: str = ""
|
|
new_password: str = ""
|
|
|
|
|
|
@router.post("/api/admin/login")
|
|
def api_login(d: _LoginIn) -> dict:
|
|
"""管理页面登录:校验账号密码,签发管理会话 token(HMAC 签名 + 有效期)。"""
|
|
if not auth.verify_login(d.username, d.password):
|
|
time.sleep(0.5) # 失败延迟,降低爆破速度
|
|
raise HTTPException(401, "用户名或密码错误")
|
|
return {"ok": True, "token": auth.sign_admin_token(d.username),
|
|
"expires_in": config.ADMIN_TOKEN_TTL, "user": d.username}
|
|
|
|
|
|
@router.post("/api/admin/change-password")
|
|
def api_change_password(d: _ChangePwdIn,
|
|
_: str = Depends(auth.check_admin)) -> dict:
|
|
"""修改管理密码:需旧密码正确;成功后旧会话全部失效(重新登录)。"""
|
|
if not config.ADMIN_PASSWORD:
|
|
raise HTTPException(400, "当前未配置管理密码")
|
|
if not hmac.compare_digest(d.old_password, config.ADMIN_PASSWORD):
|
|
raise HTTPException(400, "旧密码不正确")
|
|
if len(d.new_password) < 6:
|
|
raise HTTPException(400, "新密码至少 6 位")
|
|
config.update_admin_password(d.new_password)
|
|
return {"ok": True, "message": "密码已修改,请重新登录"}
|
|
|
|
|
|
@router.get("/api/admin/stats")
|
|
def api_stats(_: str = Depends(auth.check_admin)) -> dict:
|
|
"""汇总统计:文件数 / 总大小 / 总下载次数。"""
|
|
return {"ok": True, **store.stats()}
|