fix: hide /license after activation; keep gate before activate.

EOF

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-17 18:38:05 +08:00
parent d497d99a88
commit bc00671382
3 changed files with 42 additions and 15 deletions
+20 -7
View File
@@ -19,7 +19,8 @@ from lib.license.license_lib import (
_TEMPLATE_PATH = Path(__file__).resolve().parent / "templates" / "license.html"
def _allowed_path(path: str) -> bool:
def _license_public_path(path: str) -> bool:
"""未授权时仍可访问的路径(授权页 / 接口 / 静态资源)。"""
if path in (
"/license",
"/api/license/status",
@@ -35,6 +36,17 @@ def _allowed_path(path: str) -> bool:
return False
def _license_manage_requested(request: Request) -> bool:
"""已授权时默认禁止进入 /license;续费/换机用 ?renew=1。"""
q = request.query_params
return (q.get("renew") or q.get("manage") or "").strip().lower() in (
"1",
"true",
"yes",
"on",
)
def install_license_middleware(app: FastAPI) -> None:
@app.get("/health")
async def _license_health():
@@ -63,6 +75,10 @@ def install_license_middleware(app: FastAPI) -> None:
@app.api_route("/license", methods=["GET", "POST"])
async def _license_page(request: Request):
# 已授权:默认不可再进授权页(续费/换机:/license?renew=1
if is_license_valid() and request.method == "GET" and not _license_manage_requested(request):
return RedirectResponse(url="/", status_code=302)
msg = ""
err = ""
if request.method == "POST":
@@ -71,12 +87,10 @@ def install_license_middleware(app: FastAPI) -> None:
ckey = str(form.get("client_api_key") or "").strip()
result = redeem_code(code, client_api_key=ckey or None)
if result.get("ok"):
msg = result.get("message") or "激活成功"
else:
err = result.get("message") or "激活失败"
return RedirectResponse(url="/", status_code=302)
err = result.get("message") or "激活失败"
status = get_license_status()
html = _TEMPLATE_PATH.read_text(encoding="utf-8")
# 简单替换,避免 Jinja 依赖差异
filled = (
html.replace("{{ device_id }}", get_device_id())
.replace("{{ api_url }}", str(status.get("api_url") or ""))
@@ -88,7 +102,6 @@ def install_license_middleware(app: FastAPI) -> None:
.replace("{{ plan }}", str(status.get("plan") or ""))
.replace("{{ valid_text }}", "已授权" if status.get("valid") else "未授权")
)
# Flask 模板用 JinjaFastAPI 路径用占位符版本
if "{%" in filled or "{{" in filled:
from jinja2 import Template
@@ -107,7 +120,7 @@ def install_license_middleware(app: FastAPI) -> None:
if os.getenv("LICENSE_DISABLED", "").strip().lower() in ("1", "true", "yes", "on"):
return await call_next(request)
path = request.url.path or "/"
if _allowed_path(path):
if _license_public_path(path):
return await call_next(request)
if is_license_valid():
return await call_next(request)