From f5a77867ee29964bfe04bf03c247b32c4e6ac180 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 28 Jun 2026 08:50:05 +0800 Subject: [PATCH] fix: require license gate before gui entry --- CHANGELOG.md | 11 ++ douyin/core/version.py | 4 +- gui/dashboard_app/app.py | 12 ++- gui/dashboard_app/widgets.py | 20 +++- gui/web_gui_pkg/license_gate.py | 157 +++++++++++++++++++++++++++++ gui/web_gui_pkg/routes.py | 14 +++ pyproject.toml | 2 +- tests/test_web_gui_routes_logic.py | 57 +++++++++++ 8 files changed, 268 insertions(+), 9 deletions(-) create mode 100644 gui/web_gui_pkg/license_gate.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 526a760..2773372 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,17 @@ ## [Unreleased] +## [V3.3.7] — 2026-06-28 + +### Changed + +- Dashboard 首次启动未授权时锁定在“授权”页,验证卡密成功后再进入数据看板。 +- WebGUI 未授权访问首页时先显示卡密验证门禁页,验证成功后再加载完整控制台。 + +### Fixed + +- 保留运行接口的授权拦截,避免未输入卡密时通过接口直接启动运行。 + ## [V3.3.6] — 2026-06-27 ## [V3.3.5] — 2026-06-27 ## [V3.3.4] — 2026-06-26 diff --git a/douyin/core/version.py b/douyin/core/version.py index 1602ba6..d121255 100644 --- a/douyin/core/version.py +++ b/douyin/core/version.py @@ -10,8 +10,8 @@ from typing import Optional APP_NAME = "douyin_guaji" -APP_VERSION = "V3.3.6" -APP_BUILD_DATE = "2026-06-27" +APP_VERSION = "V3.3.7" +APP_BUILD_DATE = "2026-06-28" # 兼容旧代码与测试:提供 VERSION / RELEASE_DATE 别名 # tests 期望: from version import VERSION, RELEASE_DATE, print_version_banner diff --git a/gui/dashboard_app/app.py b/gui/dashboard_app/app.py index 1480687..4879614 100644 --- a/gui/dashboard_app/app.py +++ b/gui/dashboard_app/app.py @@ -58,8 +58,8 @@ class DashboardApp: "yg_like_keywords_text": self.data.cfg.get("yanghao_like_keywords", []) or [], "yg_fav_keywords_text": self.data.cfg.get("yanghao_fav_keywords", []) or [], } - self.current_page = initial_page if initial_page in PAGES else "数据看板" self.license_locked = bool(license_locked) + self.current_page = "授权" if self.license_locked else (initial_page if initial_page in PAGES else "数据看板") self._license_monitor_started = False self.status_var = tk.StringVar(value="请先验证卡密" if self.license_locked else "就绪") self.adb_exe = find_adb_executable() @@ -244,6 +244,8 @@ class DashboardApp: def _sync_nav(self) -> None: for name, btn in self.nav_buttons.items(): active = name == self.current_page + if hasattr(btn, "set_enabled"): + btn.set_enabled((not self.license_locked) or name == "授权") if hasattr(btn, "set_active"): btn.set_active(active) @@ -500,13 +502,17 @@ class DashboardApp: self.show_page(self.current_page) def show_page(self, name: str) -> None: - self.current_page = name + target = name if name in PAGES else "数据看板" + if self.license_locked and target != "授权": + target = "授权" + self.status_var.set("请先验证卡密") + self.current_page = target self._sync_nav() self._capture_text_widgets() self._apply_text_cache_to_data() self._ensure_text_widgets() clear(self.content) - builder = PAGES.get(name, PAGES["数据看板"]) + builder = PAGES.get(target, PAGES["数据看板"]) builder(self.content, self.data, PageContext(self.config_vars, self.text_widgets, self._actions(), self.status_var, self.runner)) try: self.content_scroll.canvas.yview_moveto(0) diff --git a/gui/dashboard_app/widgets.py b/gui/dashboard_app/widgets.py index 738bf6b..0510b4d 100644 --- a/gui/dashboard_app/widgets.py +++ b/gui/dashboard_app/widgets.py @@ -222,21 +222,35 @@ class RoundedNavButton(tk.Canvas): self.text = _with_icon(text, NAV_ICONS.get(text)) self.command = command self.active = active + self.enabled = True super().__init__(parent, height=46, bg=theme.SURFACE, highlightthickness=0, bd=0, cursor="hand2") self.bind("", lambda _e: self._draw()) - self.bind("", lambda _e: self.command()) + self.bind("", lambda _e: self._invoke()) self.bind("", lambda _e: self._draw(hover=True)) self.bind("", lambda _e: self._draw()) + def _invoke(self) -> None: + if self.enabled: + self.command() + def set_active(self, active: bool) -> None: self.active = active self._draw() + def set_enabled(self, enabled: bool) -> None: + self.enabled = bool(enabled) + self.configure(cursor="hand2" if self.enabled else "arrow") + self._draw() + def _draw(self, hover: bool = False) -> None: self.delete("all") width = max(1, self.winfo_width()) - bg = theme.TEAL_SOFT if self.active else (theme.SURFACE_HOVER if hover else theme.SURFACE) - fg = theme.TEAL_DARK if self.active else theme.TEXT + if not self.enabled: + bg = theme.SURFACE + fg = theme.MUTED + else: + bg = theme.TEAL_SOFT if self.active else (theme.SURFACE_HOVER if hover else theme.SURFACE) + fg = theme.TEAL_DARK if self.active else theme.TEXT rounded_rect(self, 1, 4, width - 2, 42, theme.RADIUS_SM, fill=bg, outline=bg) if self.active: rounded_rect(self, 4, 12, 10, 34, 4, fill=theme.TEAL, outline=theme.TEAL) diff --git a/gui/web_gui_pkg/license_gate.py b/gui/web_gui_pkg/license_gate.py new file mode 100644 index 0000000..85c3c41 --- /dev/null +++ b/gui/web_gui_pkg/license_gate.py @@ -0,0 +1,157 @@ +from __future__ import annotations + + +def render_license_gate_html(csrf_token: str) -> str: + return f""" + + + + + 卡密验证 - 抖音自动化助手 + + + +
+
+
抖
+
+

卡密验证

+

首次启动需要先验证卡密,成功后进入控制台。

+
+
+
+ + + +
+
+
+ + +""" diff --git a/gui/web_gui_pkg/routes.py b/gui/web_gui_pkg/routes.py index 22345bf..82d5e7f 100644 --- a/gui/web_gui_pkg/routes.py +++ b/gui/web_gui_pkg/routes.py @@ -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) diff --git a/pyproject.toml b/pyproject.toml index 943504e..6ea23de 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ # 这里的 version 由 tools/dev/bump_version.py 与 version.py 同步维护。 # 注意:为兼容 PEP 440,这里去掉 "V" 前缀,仅保留数字部分(如 V3.3.3 → 3.3.3)。 name = "douyin_guaji" -version = "3.3.6" +version = "3.3.7" description = "基于 ADB + OCR + 像素检测 + 模板匹配的抖音直播间福袋自动挂机工具" readme = "README.md" requires-python = ">=3.10" diff --git a/tests/test_web_gui_routes_logic.py b/tests/test_web_gui_routes_logic.py index 493d701..cba30b6 100644 --- a/tests/test_web_gui_routes_logic.py +++ b/tests/test_web_gui_routes_logic.py @@ -2,6 +2,7 @@ from __future__ import annotations +import io from types import SimpleNamespace import pytest @@ -16,6 +17,9 @@ class _DummyHandler: self.calls = [] self.client_address = ("127.0.0.1", 12345) self.static_calls = [] + self.response_status = None + self.response_headers = [] + self.wfile = io.BytesIO() def _read_json(self): return self._body @@ -28,6 +32,15 @@ class _DummyHandler: self.calls.append((status, {"error": "send_error"})) return None + def send_response(self, status): + self.response_status = status + + def send_header(self, name, value): + self.response_headers.append((name, value)) + + def end_headers(self): + return None + def _serve_static(self, rel_path): self.static_calls.append(rel_path) return None @@ -101,6 +114,22 @@ def _get(path: str, monkeypatch, query: str = ""): return handler.calls[-1] +def _license_ok(monkeypatch): + class OkStatus: + ok = True + + monkeypatch.setattr(routes, "check_license", lambda: OkStatus()) + + +def _license_missing(monkeypatch): + class MissingStatus: + ok = False + reason = "missing" + message = "缺少授权文件" + + monkeypatch.setattr(routes, "check_license", lambda: MissingStatus()) + + @pytest.mark.parametrize( "raw,expected", [ @@ -154,6 +183,34 @@ def test_license_status_route_returns_public_payload(monkeypatch): assert obj == {"ok": True, "reason": "ok", "customer": "demo"} +def test_web_root_requires_license_gate_before_static_app(monkeypatch): + _license_missing(monkeypatch) + handler = _DummyHandler() + runner = _DummyRunner() + u = SimpleNamespace(path="/", query="") + + routes.handle_get(handler, u, runner, "token") + + html = handler.wfile.getvalue().decode("utf-8") + assert handler.response_status == 200 + assert "卡密验证" in html + assert "输入卡密" in html + assert "window.location.reload" in html + assert handler.static_calls == [] + + +def test_web_root_enters_static_app_when_license_exists(monkeypatch): + _license_ok(monkeypatch) + handler = _DummyHandler() + runner = _DummyRunner() + u = SimpleNamespace(path="/", query="") + + routes.handle_get(handler, u, runner, "token") + + assert handler.static_calls == ["/"] + assert handler.wfile.getvalue() == b"" + + def test_license_install_route_accepts_code(monkeypatch): calls = {}