fix: require license gate before gui entry

This commit is contained in:
Your Name committed 2026-06-28 08:50:05 +08:00
1 parent 407c3b37c2
commit f5a77867ee
8 files changed
+268 -9

No files matched your search

+157
View File
@@ -0,0 +1,157 @@
from __future__ import annotations
def render_license_gate_html(csrf_token: str) -> str:
return f"""<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>卡密验证 - 抖音自动化助手</title>
<style>
:root {{
color-scheme: light;
--bg: #f5f8fb;
--surface: #ffffff;
--text: #16202a;
--muted: #6f7f8f;
--border: #dce7ef;
--teal: #13998f;
--teal-dark: #08746d;
--teal-soft: #e6f7f5;
--danger: #c83a4b;
}}
* {{ box-sizing: border-box; }}
body {{
margin: 0;
min-height: 100vh;
display: grid;
place-items: center;
padding: 28px;
font: 16px/1.55 "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;
color: var(--text);
background:
radial-gradient(circle at 18% 12%, rgba(19, 153, 143, .12), transparent 30%),
linear-gradient(135deg, #fbfdff 0%, var(--bg) 100%);
}}
.gate {{
width: min(520px, 100%);
padding: 34px;
border: 1px solid var(--border);
border-radius: 22px;
background: rgba(255, 255, 255, .92);
box-shadow: 0 24px 70px rgba(22, 32, 42, .10);
}}
.brand {{ display: flex; gap: 16px; align-items: center; margin-bottom: 28px; }}
.mark {{
width: 48px;
height: 48px;
display: grid;
place-items: center;
border-radius: 14px;
color: #fff;
background: var(--teal);
font-size: 23px;
font-weight: 800;
}}
h1 {{ margin: 0; font-size: 26px; line-height: 1.2; }}
p {{ margin: 7px 0 0; color: var(--muted); }}
label {{ display: block; margin: 0 0 10px; font-weight: 700; }}
input {{
width: 100%;
height: 52px;
padding: 0 16px;
border: 1px solid var(--border);
border-radius: 14px;
outline: none;
font: inherit;
background: #fbfdff;
}}
input:focus {{
border-color: var(--teal);
box-shadow: 0 0 0 4px var(--teal-soft);
background: #fff;
}}
button {{
width: 100%;
height: 52px;
margin-top: 18px;
border: 0;
border-radius: 14px;
color: #fff;
background: var(--teal);
font: inherit;
font-weight: 800;
cursor: pointer;
}}
button:disabled {{ cursor: wait; opacity: .75; }}
.msg {{
min-height: 24px;
margin-top: 14px;
color: var(--muted);
font-size: 15px;
}}
.msg.error {{ color: var(--danger); }}
.msg.ok {{ color: var(--teal-dark); }}
</style>
</head>
<body>
<main class="gate" aria-labelledby="title">
<div class="brand">
<div class="mark">抖</div>
<div>
<h1 id="title">卡密验证</h1>
<p>首次启动需要先验证卡密,成功后进入控制台。</p>
</div>
</div>
<form id="license-form">
<label for="license-input">输入卡密</label>
<input id="license-input" name="license_text" autocomplete="off" autofocus>
<button id="submit-btn" type="submit">验证并进入</button>
<div id="message" class="msg" role="status"></div>
</form>
</main>
<script>
const csrfToken = {csrf_token!r};
const form = document.getElementById("license-form");
const input = document.getElementById("license-input");
const button = document.getElementById("submit-btn");
const message = document.getElementById("message");
form.addEventListener("submit", async (event) => {{
event.preventDefault();
const licenseText = input.value.trim();
message.className = "msg";
if (!licenseText) {{
message.textContent = "请先输入卡密。";
message.classList.add("error");
input.focus();
return;
}}
button.disabled = true;
message.textContent = "正在验证...";
try {{
const res = await fetch("/api/license", {{
method: "POST",
headers: {{
"Content-Type": "application/json",
"X-CSRF-Token": csrfToken
}},
body: JSON.stringify({{ license_text: licenseText }})
}});
const data = await res.json().catch(() => ({{}}));
if (!res.ok || !data.ok) {{
throw new Error(data.message || data.error || "卡密验证失败");
}}
message.textContent = "验证成功,正在进入...";
message.classList.add("ok");
window.location.reload();
}} catch (err) {{
message.textContent = err && err.message ? err.message : "卡密验证失败";
message.classList.add("error");
}} finally {{
button.disabled = false;
}}
}});
</script>
</body>
</html>"""
+14
View File
@@ -12,6 +12,7 @@ from douyin.license.license_manager import (
get_license_status_payload,
install_license_or_activate,
)
from gui.web_gui_pkg.license_gate import render_license_gate_html
from gui.web_gui_pkg import (
_wg_log,
find_adb_executable,
@@ -104,6 +105,16 @@ def _license_gate_payload() -> dict:
return payload
def _send_html(handler, html: str, status: int = 200) -> None:
data = html.encode("utf-8")
handler.send_response(status)
handler.send_header("Content-Type", "text/html; charset=utf-8")
handler.send_header("Content-Length", str(len(data)))
handler.send_header("Cache-Control", "no-store")
handler.end_headers()
handler.wfile.write(data)
def _is_invalid_ports_spec(spec) -> bool:
"""判断端口规格是否为明确非法输入。"""
if spec is None:
@@ -369,6 +380,9 @@ def handle_get(handler, u, runner, csrf_token: str) -> None:
handler.send_error(HTTPStatus.FORBIDDEN)
return
if u.path in {"/", "/index.html"} and not check_license().ok:
return _send_html(handler, render_license_gate_html(csrf_token))
handler._serve_static(u.path)