fix: harden screenshot capture and diagnostics
This commit is contained in:
1 parent
ee3424cd9e
commit
f010853b97
19 files changed
+870
-70
No files matched your search
@@ -32,6 +32,7 @@ ADMIN_SESSION_COOKIE = "dygj_admin_session"
|
||||
ADMIN_SESSION_SECONDS = 12 * 60 * 60
|
||||
DEFAULT_MAX_INSTALLER_UPLOAD_BYTES = 700 * 1024 * 1024
|
||||
DEFAULT_MAX_DELTA_UPLOAD_BYTES = 250 * 1024 * 1024
|
||||
DEFAULT_MAX_DIAGNOSTIC_LOG_UPLOAD_BYTES = 10 * 1024 * 1024
|
||||
UPLOAD_CHUNK_BYTES = 1024 * 1024
|
||||
|
||||
|
||||
@@ -391,6 +392,15 @@ def _update_summary_html(manifest: UpdateManifest) -> str:
|
||||
)
|
||||
|
||||
|
||||
def _format_bytes(size: int) -> str:
|
||||
value = float(size or 0)
|
||||
for unit in ("B", "KB", "MB", "GB"):
|
||||
if value < 1024 or unit == "GB":
|
||||
return f"{value:.1f} {unit}" if unit != "B" else f"{int(value)} B"
|
||||
value /= 1024
|
||||
return f"{int(size)} B"
|
||||
|
||||
|
||||
def _admin_page(
|
||||
service: LicenseService,
|
||||
csrf_token: str,
|
||||
@@ -401,6 +411,7 @@ def _admin_page(
|
||||
stats = service.stats()
|
||||
card_rows = service.list_cards(limit=50)
|
||||
audit_rows = service.audit_logs(limit=30)
|
||||
diagnostic_rows = service.list_diagnostic_logs(limit=30)
|
||||
update_manifest = service.load_update_manifest()
|
||||
update_html = _update_summary_html(update_manifest)
|
||||
created_rows = "".join(
|
||||
@@ -429,6 +440,18 @@ def _admin_page(
|
||||
"</tr>"
|
||||
for row in audit_rows
|
||||
)
|
||||
diagnostic_html = "".join(
|
||||
"<tr>"
|
||||
f"<td>{escape(row.uploaded_at)}</td>"
|
||||
f"<td><code>{escape(row.code_hint)}</code></td>"
|
||||
f"<td><code>{escape(row.machine_fingerprint[:16])}</code></td>"
|
||||
f"<td>{escape(row.filename)}</td>"
|
||||
f"<td>{escape(_format_bytes(row.size))}</td>"
|
||||
f"<td><code>{escape(row.sha256[:12])}...</code></td>"
|
||||
f"<td><a href='/admin/logs/download/{escape(row.key)}'>下载</a></td>"
|
||||
"</tr>"
|
||||
for row in diagnostic_rows
|
||||
)
|
||||
message_html = f"<p class='msg'>{escape(message)}</p>" if message else ""
|
||||
html = f"""<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
@@ -544,6 +567,14 @@ def _admin_page(
|
||||
<tbody>{table_rows}</tbody>
|
||||
</table>
|
||||
</section>
|
||||
<section>
|
||||
<h2>客户端诊断日志</h2>
|
||||
<div class="help">同一卡密只保留最新一份日志包,新上传会覆盖旧包。</div>
|
||||
<table>
|
||||
<thead><tr><th>上传时间</th><th>卡密</th><th>机器</th><th>文件名</th><th>大小</th><th>SHA256</th><th>操作</th></tr></thead>
|
||||
<tbody>{diagnostic_html}</tbody>
|
||||
</table>
|
||||
</section>
|
||||
<section>
|
||||
<h2>最近审计日志</h2>
|
||||
<table>
|
||||
@@ -586,6 +617,10 @@ def create_app(
|
||||
"DYGJ_LICENSE_MAX_DELTA_UPLOAD_MB",
|
||||
DEFAULT_MAX_DELTA_UPLOAD_BYTES // (1024 * 1024),
|
||||
) * 1024 * 1024
|
||||
app.state.max_diagnostic_log_upload_bytes = _env_int(
|
||||
"DYGJ_LICENSE_MAX_DIAGNOSTIC_LOG_UPLOAD_MB",
|
||||
DEFAULT_MAX_DIAGNOSTIC_LOG_UPLOAD_BYTES // (1024 * 1024),
|
||||
) * 1024 * 1024
|
||||
app.state.rate_limit_buckets = {}
|
||||
app.state.admin_sessions = {}
|
||||
|
||||
@@ -626,6 +661,55 @@ def create_app(
|
||||
return result_response(LicenseActionResult(False, "rate_limited", "请求过于频繁,请稍后再试"))
|
||||
return result_response(get_service().verify_license_doc(req.license))
|
||||
|
||||
@app.post("/api/v1/logs/upload")
|
||||
async def upload_logs(
|
||||
request: Request,
|
||||
license: str = Form(""),
|
||||
log_file: UploadFile = File(...),
|
||||
) -> JSONResponse:
|
||||
if _rate_limited(request, "logs_upload", max_requests=10):
|
||||
return result_response(LicenseActionResult(False, "rate_limited", "请求过于频繁,请稍后再试"))
|
||||
try:
|
||||
license_doc = json.loads(str(license or ""))
|
||||
if not isinstance(license_doc, dict):
|
||||
raise ValueError("license must be object")
|
||||
except Exception:
|
||||
return JSONResponse({"ok": False, "reason": "invalid_license", "message": "授权数据格式无效"}, status_code=400)
|
||||
tmp_path: Path | None = None
|
||||
try:
|
||||
tmp_path = await _save_upload_to_temp(
|
||||
log_file,
|
||||
upload_dir=get_service().diagnostic_log_dir / ".uploads",
|
||||
max_bytes=app.state.max_diagnostic_log_upload_bytes,
|
||||
suffix=".zip.upload",
|
||||
)
|
||||
entry = get_service().save_diagnostic_log_file(
|
||||
license_doc=license_doc,
|
||||
filename=str(log_file.filename or "diagnostic-logs.zip"),
|
||||
file_path=tmp_path,
|
||||
)
|
||||
return JSONResponse(
|
||||
{
|
||||
"ok": True,
|
||||
"message": "日志上传成功",
|
||||
"filename": entry.filename,
|
||||
"size": entry.size,
|
||||
"uploaded_at": entry.uploaded_at,
|
||||
}
|
||||
)
|
||||
except ValueError as exc:
|
||||
reason = str(exc) or "upload_failed"
|
||||
status_code = 413 if "超过大小限制" in reason else 403
|
||||
return JSONResponse({"ok": False, "reason": reason, "message": reason}, status_code=status_code)
|
||||
except Exception as exc:
|
||||
return JSONResponse({"ok": False, "reason": "upload_failed", "message": str(exc)}, status_code=500)
|
||||
finally:
|
||||
if tmp_path is not None:
|
||||
try:
|
||||
tmp_path.unlink(missing_ok=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@app.post("/api/v1/redeem/external/create")
|
||||
def redeem_external_create(request: Request, req: CreateRedeemRequest) -> JSONResponse:
|
||||
if not _bearer_token_allowed(request, app.state.external_api_token):
|
||||
@@ -807,6 +891,19 @@ document.getElementById("claim").addEventListener("click", async () => {{
|
||||
)
|
||||
return response
|
||||
|
||||
@app.get("/admin/logs/download/{key}")
|
||||
def admin_log_download(request: Request, key: str) -> Response:
|
||||
if not _admin_allowed(request, app.state.admin_token):
|
||||
return RedirectResponse("/admin/login", status_code=303)
|
||||
target = get_service().diagnostic_log_path(key)
|
||||
if target is None:
|
||||
return JSONResponse({"ok": False, "reason": "not_found"}, status_code=404)
|
||||
return FileResponse(
|
||||
target,
|
||||
media_type="application/zip",
|
||||
filename=f"diagnostic-{key[:12]}.zip",
|
||||
)
|
||||
|
||||
@app.get("/admin/login")
|
||||
def admin_login_page() -> HTMLResponse:
|
||||
return _login_page()
|
||||
|
||||
Reference in new issue
Block a user