feat: 整合官方 2.7.0 安全修复与本地文件分享

This commit is contained in:
orion committed 2026-09-18 10:08:42 +08:00
1 parent b10113e5ab
commit 9e9571b031
42 files changed
+2824 -268

No files matched your search

+36 -19
View File
@@ -7,6 +7,7 @@ from collections import Counter
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException
from apps.admin import services as admin_services
from apps.admin.services import FileService, ConfigService, LocalFileService
from apps.admin.dependencies import (
admin_required,
@@ -31,6 +32,7 @@ from apps.admin.schemas import (
LoginData,
UpdateFileData,
)
from apps.base.local_share import is_local_ref
from core.response import APIResponse
from apps.base.models import FileCodes, KeyValue, ShareCodeReservations, TransferStats
from apps.admin.dependencies import (
@@ -40,7 +42,8 @@ from apps.admin.dependencies import (
)
from core.settings import settings
from core.utils import get_now, verify_password
from core.utils import get_now, hash_password, password_needs_rehash, verify_password
from core.config import refresh_settings
from apps.base.utils import ip_limit
admin_api = APIRouter(
@@ -58,23 +61,34 @@ def _pick_query_text(*values: Optional[str]) -> Optional[str]:
@admin_api.post("/login")
async def login(data: LoginData, ip: str = Depends(ip_limit["login"])):
# 登录失败计入 IP 频率限制,超过 loginCount/loginMinute 后暂时锁定
if not verify_password(data.password, settings.admin_token):
ip_limit["login"].add_ip(ip)
raise HTTPException(status_code=401, detail="密码错误")
# Check and issue the token under the same lock used for password changes.
async with admin_services.keyvalue_write_lock:
await refresh_settings(force=True)
if not verify_password(data.password, settings.admin_token):
ip_limit["login"].add_ip(ip)
raise HTTPException(status_code=401, detail="密码错误")
expires_in = get_admin_session_expire_seconds()
token = create_token({"is_admin": True}, expires_in=expires_in)
return APIResponse(
detail={
"id": "admin",
"username": "admin",
"token": token,
"token_type": "Bearer",
"expires_at": verify_token(token)["exp"],
"expires_in": expires_in,
}
)
if password_needs_rehash(settings.admin_token):
record = await KeyValue.filter(key="settings").first()
if record:
stored = dict(record.value or {})
stored["admin_token"] = hash_password(data.password)
record.value = stored
await record.save()
await refresh_settings(force=True)
expires_in = get_admin_session_expire_seconds()
token = create_token({"is_admin": True}, expires_in=expires_in)
return APIResponse(
detail={
"id": "admin",
"username": "admin",
"token": token,
"token_type": "Bearer",
"expires_at": verify_token(token)["exp"],
"expires_in": expires_in,
}
)
@admin_api.get("/verify")
@@ -107,7 +121,7 @@ async def build_dashboard_recent_file(file_code: FileCodes) -> dict:
@admin_api.get("/dashboard")
async def dashboard(file_service: FileService = Depends(get_file_service)):
all_codes = await FileCodes.filter(is_trashed=False).all()
all_size = sum([code.size for code in all_codes])
all_size = sum(code.size for code in all_codes if not is_local_ref(code))
sys_start = await KeyValue.filter(key="sys_start").first()
now = await get_now()
today_start = now.replace(hour=0, minute=0, second=0, microsecond=0)
@@ -816,9 +830,12 @@ async def file_preview(
@admin_api.get("/local/lists")
async def get_local_lists(
path: Optional[str] = None,
local_file_service: LocalFileService = Depends(get_local_file_service),
):
files = await local_file_service.list_files()
listing = await local_file_service.list_files(path or "", legacy=path is None)
# 无 path 参数保留旧主题的数组响应;显式 path 支持目录导航。
files = listing if path is not None else [item for item in listing["items"] if item["type"] == "file"]
return APIResponse(detail=files)