diff --git a/CHANGELOG.md b/CHANGELOG.md index 2773372..61d0d26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,17 @@ ## [Unreleased] +## [V3.3.8] — 2026-06-28 + +### Fixed + +- 修复部分荣耀/华为设备上 `adb exec-out screencap -p` 返回 `4294967295` 或损坏 PNG 后,截图链路未继续尝试 shell stdout 兜底的问题。 +- 打包时收集 `paddlex` / `paddleocr` / Paddle / OpenCV / NumPy / PIL / pyclipper / shapely 运行依赖,避免新电脑上 OCR 初始化缺少 `_internal\paddlex\.version` 或相关二进制组件。 + +### Added + +- Dashboard 新增“上传诊断日志”,客户端只打包有限大小的日志片段;服务端按卡密覆盖保存一份,便于远程排查问题。 + ## [V3.3.7] — 2026-06-28 ### Changed diff --git a/douyin/core/underlying_operations_pkg/screenshot_direct.py b/douyin/core/underlying_operations_pkg/screenshot_direct.py index e7b2253..3705c90 100644 --- a/douyin/core/underlying_operations_pkg/screenshot_direct.py +++ b/douyin/core/underlying_operations_pkg/screenshot_direct.py @@ -28,10 +28,18 @@ if TYPE_CHECKING: from douyin.core.underlying_operations import underlying_operations -def _uo_log(msg: str) -> None: +def _uo_log(msg: str, level: str = "INFO") -> None: """与 underlying_operations._uo_log 一致:默认 INFO 级别。""" try: - logging.info(msg) + lv = str(level or "INFO").upper() + if lv == "WARNING": + logging.warning(msg) + elif lv == "ERROR": + logging.error(msg) + elif lv == "DEBUG": + logging.debug(msg) + else: + logging.info(msg) except Exception: pass @@ -89,6 +97,45 @@ def _valid_png(path0: str) -> bool: return False +def _adb_shell_png_variants(data: bytes) -> list[bytes]: + """Return safe candidates for ``adb shell screencap -p`` stdout. + + Some Android/Windows ADB combinations route shell stdout through a text-ish + path and duplicate carriage returns in PNG line endings. Try the raw bytes + first, then the common CRLF repairs without changing the primary exec-out + path. + """ + if not data: + return [] + variants: list[bytes] = [] + + def _add(candidate: bytes) -> None: + if candidate and candidate not in variants: + variants.append(candidate) + + marker = b"\x89PNG" + idx = data.find(marker) + if idx > 0: + data = data[idx:] + + _add(data) + _add(data.replace(b"\r\r\n", b"\r\n")) + _add(data.replace(b"\r\n", b"\n")) + return variants + + +def _write_valid_png_bytes(tmp_file: str, data: bytes) -> bool: + for candidate in _adb_shell_png_variants(data): + try: + with open(tmp_file, "wb", buffering=262144) as f: + f.write(candidate) + if _valid_png(tmp_file): + return True + except Exception: + pass + return False + + def capture_screenshot_direct( self: "underlying_operations", device_id, path: str = "pic" ) -> bool: @@ -128,6 +175,52 @@ def capture_screenshot_direct( adb = self._adb_path() + def _commit_valid_tmp(success_kind: str, start_ts: float) -> bool: + if not _valid_png(tmp_file): + return False + if not _safe_replace_file(tmp_file, out_file): + return False + _mirror_screenshot_to_external(out_file) + if not _valid_png(out_file): + return False + overall_dur = (time_module.time() - overall_start) * 1000.0 + part_dur = (time_module.time() - start_ts) * 1000.0 + logging.info( + "[截图] ✓ 获取屏幕截图成功 | 总耗时(ms)=%.1f (%s成功, 回退部分耗时=%.1fms)", + overall_dur, + success_kind, + part_dur, + ) + return True + + def _try_shell_stdout_fallback() -> bool: + stdout_fallback_start = time_module.time() + try: + result = subprocess.run( + [adb, "-s", device_id, "shell", "screencap", "-p"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + check=True, + timeout=8, + ) + if _write_valid_png_bytes(tmp_file, result.stdout or b""): + return _commit_valid_tmp("shell stdout回退", stdout_fallback_start) + try: + size = len(result.stdout or b"") + _uo_log(f"[截图] shell stdout回退输出无效:bytes={size}") + except Exception: + pass + return False + except Exception as stdout_error: + try: + _uo_log( + f"[截图] shell stdout回退失败:{type(stdout_error).__name__}: {stdout_error}", + "WARNING", + ) + except Exception: + pass + return False + # 优先走 exec-out try: execout_timeout_sec = float(os.environ.get("ADB_EXECOUT_TIMEOUT_SEC", "10")) @@ -466,70 +559,56 @@ def capture_screenshot_direct( except Exception: _uo_log(f"[截图] 文件替换失败,但截图已保存到临时文件 {tmp_file}") return True - return False - except Exception: try: - stdout_fallback_start = time_module.time() - result = subprocess.run( - [adb, "-s", device_id, "shell", "screencap", "-p"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - check=True, - timeout=8, - ) - with open(tmp_file, "wb", buffering=262144) as f: - f.write(result.stdout or b"") - if _valid_png(tmp_file) and _safe_replace_file(tmp_file, out_file): - _mirror_screenshot_to_external(out_file) - if _valid_png(out_file): - overall_dur = (time_module.time() - overall_start) * 1000.0 - fallback_total_dur = ( - time_module.time() - stdout_fallback_start - ) * 1000.0 - logging.info( - "[截图] ✓ 获取屏幕截图成功 | 总耗时(ms)=%.1f (shell stdout回退成功, 回退部分耗时=%.1fms)", - overall_dur, - fallback_total_dur, - ) - return True - except Exception as stdout_error: - try: + if os.path.exists(tmp_file): + st = os.stat(tmp_file) _uo_log( - f"[截图] shell stdout回退失败:{type(stdout_error).__name__}: {stdout_error}", - "WARNING", + f"[截图] 回退方案截图无效,继续尝试 shell stdout 回退: size={st.st_size}" ) - except Exception: - pass - # 再次回退:尝试用 screenshot1.png 覆盖 screenshot.png - try: - subprocess.run( - [ - adb, - "-s", - device_id, - "shell", - "screencap", - "-p", - remote_shot_alt, - ], - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - check=True, - ) - subprocess.run( - [ - adb, - "-s", - device_id, - "shell", - "mv", - remote_shot_alt, - remote_shot, - ], - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - check=True, - ) - return self.get_screenshot(device_id, path) except Exception: - return False + pass + except Exception as fallback_error: + try: + _uo_log(f"[截图] 回退方案失败,继续尝试 shell stdout 回退:{fallback_error}") + except Exception: + pass + + if _try_shell_stdout_fallback(): + return True + + # 再次回退:尝试用 screenshot1.png 覆盖 screenshot.png + try: + if "remote_shot" not in locals(): + remote_shot = path_config.get_android_screenshot_path() + remote_shot_alt = path_config.get_android_screenshot_alt_path() + subprocess.run( + [ + adb, + "-s", + device_id, + "shell", + "screencap", + "-p", + remote_shot_alt, + ], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + check=True, + ) + subprocess.run( + [ + adb, + "-s", + device_id, + "shell", + "mv", + remote_shot_alt, + remote_shot, + ], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + check=True, + ) + return self.get_screenshot(device_id, path) + except Exception: + return False diff --git a/douyin/core/version.py b/douyin/core/version.py index d121255..d881be4 100644 --- a/douyin/core/version.py +++ b/douyin/core/version.py @@ -10,7 +10,7 @@ from typing import Optional APP_NAME = "douyin_guaji" -APP_VERSION = "V3.3.7" +APP_VERSION = "V3.3.8" APP_BUILD_DATE = "2026-06-28" # 兼容旧代码与测试:提供 VERSION / RELEASE_DATE 别名 diff --git a/douyin/diagnostics/__init__.py b/douyin/diagnostics/__init__.py new file mode 100644 index 0000000..e2f0c7d --- /dev/null +++ b/douyin/diagnostics/__init__.py @@ -0,0 +1 @@ +"""Diagnostic helpers for packaged clients.""" diff --git a/douyin/diagnostics/log_upload.py b/douyin/diagnostics/log_upload.py new file mode 100644 index 0000000..be31f10 --- /dev/null +++ b/douyin/diagnostics/log_upload.py @@ -0,0 +1,184 @@ +from __future__ import annotations + +import json +import os +import time +import zipfile +from dataclasses import dataclass +from pathlib import Path +from typing import Any +from urllib import request + +from douyin.core import path_config +from douyin.license.license_manager import get_default_license_path, get_default_verify_url + +DEFAULT_MAX_ARCHIVE_BYTES = 8 * 1024 * 1024 +DEFAULT_MAX_SOURCE_BYTES = 2 * 1024 * 1024 +DEFAULT_MAX_FILES = 12 + + +@dataclass(frozen=True) +class LogUploadResult: + ok: bool + message: str + reason: str = "" + archive_path: str = "" + size: int = 0 + + +def get_default_log_upload_url() -> str: + raw = str(os.environ.get("DOUYIN_LOG_UPLOAD_URL") or "").strip() + if raw: + return raw + verify_url = get_default_verify_url() + if verify_url.endswith("/verify_license"): + return verify_url[: -len("/verify_license")] + "/logs/upload" + return verify_url.rstrip("/") + "/logs/upload" + + +def _candidate_log_files(logs_dir: Path) -> list[Path]: + suffixes = {".log", ".jsonl", ".txt"} + if not logs_dir.exists(): + return [] + files = [ + p + for p in logs_dir.rglob("*") + if p.is_file() and p.suffix.lower() in suffixes and p.name != ".keep" + ] + return sorted(files, key=lambda p: p.stat().st_mtime, reverse=True) + + +def _tail_bytes(path: Path, limit: int) -> bytes: + size = path.stat().st_size + with path.open("rb") as f: + if size > limit: + f.seek(size - limit) + return f.read(limit) + + +def build_diagnostic_log_archive( + *, + output_dir: str | os.PathLike[str] | None = None, + max_archive_bytes: int = DEFAULT_MAX_ARCHIVE_BYTES, + max_source_bytes: int = DEFAULT_MAX_SOURCE_BYTES, + max_files: int = DEFAULT_MAX_FILES, +) -> Path: + logs_dir = Path(path_config.get_logs_dir()) + out_dir = Path(output_dir) if output_dir is not None else logs_dir + out_dir.mkdir(parents=True, exist_ok=True) + archive = out_dir / f"diagnostic_logs_{int(time.time())}.zip" + files = _candidate_log_files(logs_dir)[: max(1, int(max_files))] + if not files: + raise FileNotFoundError("没有找到可上传的日志文件") + + with zipfile.ZipFile(archive, "w", compression=zipfile.ZIP_DEFLATED, compresslevel=6) as zf: + manifest: dict[str, Any] = { + "created_at": int(time.time()), + "source": "douyin_guaji", + "truncated_each_file_bytes": int(max_source_bytes), + "files": [], + } + for file in files: + rel = file.relative_to(logs_dir).as_posix() + data = _tail_bytes(file, max_source_bytes) + zf.writestr(f"logs/{rel}", data) + manifest["files"].append( + { + "path": rel, + "original_size": file.stat().st_size, + "included_size": len(data), + "mtime": int(file.stat().st_mtime), + } + ) + zf.writestr("manifest.json", json.dumps(manifest, ensure_ascii=False, indent=2)) + + if archive.stat().st_size > max_archive_bytes: + try: + archive.unlink() + finally: + raise ValueError(f"诊断日志包超过大小限制(最大 {max_archive_bytes // (1024 * 1024)} MB)") + return archive + + +def upload_diagnostic_logs( + *, + upload_url: str = "", + timeout_seconds: int = 20, + http_post_file=None, +) -> LogUploadResult: + license_path = get_default_license_path() + if not license_path.exists(): + return LogUploadResult(False, "缺少授权文件,无法上传日志", "missing_license") + try: + license_doc = json.loads(license_path.read_text(encoding="utf-8")) + except Exception as exc: + return LogUploadResult(False, f"授权文件读取失败: {exc}", "invalid_license") + + try: + archive = build_diagnostic_log_archive() + except Exception as exc: + return LogUploadResult(False, str(exc), "archive_failed") + + url = str(upload_url or "").strip() or get_default_log_upload_url() + try: + post = http_post_file or _default_http_post_file + data = post( + url, + license_doc=license_doc, + file_path=archive, + timeout_seconds=timeout_seconds, + ) + except Exception as exc: + return LogUploadResult(False, f"上传失败: {exc}", "upload_failed", str(archive), archive.stat().st_size) + if not bool(data.get("ok")): + return LogUploadResult( + False, + str(data.get("message") or data.get("error") or "上传失败"), + str(data.get("reason") or "rejected"), + str(archive), + archive.stat().st_size, + ) + return LogUploadResult(True, "日志上传成功", "ok", str(archive), archive.stat().st_size) + + +def _default_http_post_file( + url: str, + *, + license_doc: dict[str, Any], + file_path: Path, + timeout_seconds: int = 20, +) -> dict[str, Any]: + boundary = f"----DYGJ{int(time.time() * 1000)}" + payload = json.dumps(license_doc, ensure_ascii=False).encode("utf-8") + file_data = file_path.read_bytes() + body = bytearray() + + def add_field(name: str, value: bytes, content_type: str = "text/plain") -> None: + body.extend(f"--{boundary}\r\n".encode("ascii")) + body.extend(f'Content-Disposition: form-data; name="{name}"\r\n'.encode("ascii")) + body.extend(f"Content-Type: {content_type}\r\n\r\n".encode("ascii")) + body.extend(value) + body.extend(b"\r\n") + + add_field("license", payload, "application/json") + body.extend(f"--{boundary}\r\n".encode("ascii")) + body.extend( + f'Content-Disposition: form-data; name="log_file"; filename="{file_path.name}"\r\n'.encode("ascii") + ) + body.extend(b"Content-Type: application/zip\r\n\r\n") + body.extend(file_data) + body.extend(b"\r\n") + body.extend(f"--{boundary}--\r\n".encode("ascii")) + + req = request.Request( + url, + data=bytes(body), + headers={"Content-Type": f"multipart/form-data; boundary={boundary}"}, + method="POST", + ) + with request.urlopen(req, timeout=max(1, int(timeout_seconds))) as resp: + raw = resp.read().decode("utf-8") + obj = json.loads(raw) + if not isinstance(obj, dict): + raise ValueError("server response is not an object") + return obj diff --git a/gui/dashboard_app/app.py b/gui/dashboard_app/app.py index 4879614..7d1c96e 100644 --- a/gui/dashboard_app/app.py +++ b/gui/dashboard_app/app.py @@ -5,6 +5,7 @@ import tkinter as tk from tkinter import messagebox, ttk from douyin.core.version import print_version_banner +from douyin.diagnostics.log_upload import upload_diagnostic_logs from douyin.license.license_manager import ( check_license, get_default_activation_url, @@ -274,6 +275,7 @@ class DashboardApp: "scan_wifi": self.scan_wifi, "wifi_adb": self.open_wifi_adb, "verify_license": self.verify_license, + "upload_logs": self.upload_logs, "show_run_log": lambda: self.show_page("运行日志"), "show_actions_log": lambda: self.show_page("行为日志"), } @@ -496,6 +498,27 @@ class DashboardApp: parent=self.root, ) + def upload_logs(self) -> None: + if not self._ensure_license_ready(): + return + self.status_var.set("正在上传诊断日志") + self.root.update_idletasks() + result = upload_diagnostic_logs() + if result.ok: + self.status_var.set("诊断日志上传成功") + messagebox.showinfo( + "上传诊断日志", + f"上传成功。\n大小:{result.size // 1024} KB", + parent=self.root, + ) + else: + self.status_var.set(f"日志上传失败:{result.message}") + messagebox.showerror( + "上传诊断日志失败", + f"{result.message}\n\n原因:{result.reason}", + parent=self.root, + ) + def refresh_data(self) -> None: self.data = load_dashboard_data() self.status_var.set("已刷新") diff --git a/gui/dashboard_app/page_config.py b/gui/dashboard_app/page_config.py index 3db7560..3ba1acf 100644 --- a/gui/dashboard_app/page_config.py +++ b/gui/dashboard_app/page_config.py @@ -198,3 +198,4 @@ def build_license(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> Non row = tk.Frame(box, bg=theme.SURFACE) row.pack(fill="x", padx=20, pady=(0, 16)) button(row, "验证卡密", ctx.action("verify_license"), kind="primary").pack(side="left", padx=(0, 8)) + button(row, "上传诊断日志", ctx.action("upload_logs"), kind="soft").pack(side="left", padx=(0, 8)) diff --git a/gui/dashboard_app/widgets.py b/gui/dashboard_app/widgets.py index 0510b4d..de6567c 100644 --- a/gui/dashboard_app/widgets.py +++ b/gui/dashboard_app/widgets.py @@ -27,6 +27,7 @@ BUTTON_ICONS = { "打开pic目录": "□", "打开目录": "□", "验证卡密": "✓", + "上传诊断日志": "⇧", "刷新状态": "⟳", "保存日志配置": "✓", "查看大图": "▢", diff --git a/license_server/api.py b/license_server/api.py index 628d15a..9608a6b 100644 --- a/license_server/api.py +++ b/license_server/api.py @@ -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( "" for row in audit_rows ) + diagnostic_html = "".join( + "" + f"{escape(row.uploaded_at)}" + f"{escape(row.code_hint)}" + f"{escape(row.machine_fingerprint[:16])}" + f"{escape(row.filename)}" + f"{escape(_format_bytes(row.size))}" + f"{escape(row.sha256[:12])}..." + f"下载" + "" + for row in diagnostic_rows + ) message_html = f"

{escape(message)}

" if message else "" html = f""" @@ -544,6 +567,14 @@ def _admin_page( {table_rows} +
+

客户端诊断日志

+
同一卡密只保留最新一份日志包,新上传会覆盖旧包。
+ + + {diagnostic_html} +
上传时间卡密机器文件名大小SHA256操作
+

最近审计日志

@@ -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() diff --git a/license_server/core.py b/license_server/core.py index babd4b6..ea744c9 100644 --- a/license_server/core.py +++ b/license_server/core.py @@ -97,6 +97,17 @@ class AuditLogEntry: created_at: str +@dataclass(frozen=True) +class DiagnosticLogEntry: + key: str + code_hint: str + machine_fingerprint: str + filename: str + size: int + sha256: str + uploaded_at: str + + @dataclass(frozen=True) class UpdateManifest: enabled: bool @@ -183,6 +194,14 @@ def sanitize_delta_filename(filename: str, *, from_version: str = "", to_version return cleaned or "DouyinGuaji-delta.zip" +def sanitize_diagnostic_log_filename(filename: str) -> str: + raw = Path(str(filename or "")).name.strip() + if not raw.lower().endswith(".zip"): + raw = "diagnostic-logs.zip" + cleaned = re.sub(r'[<>:"/\\|?*\x00-\x1f]+', "_", raw).strip(" .") + return cleaned or "diagnostic-logs.zip" + + def sha256_bytes(data: bytes) -> str: return hashlib.sha256(data).hexdigest() @@ -226,6 +245,12 @@ class LicenseService: def update_manifest_path(self) -> Path: return self.db_path.parent / "update_manifest.json" + @property + def diagnostic_log_dir(self) -> Path: + path = self.db_path.parent / "diagnostic_logs" + path.mkdir(parents=True, exist_ok=True) + return path + def _connect(self) -> sqlite3.Connection: conn = sqlite3.connect(self.db_path, timeout=10) conn.row_factory = sqlite3.Row @@ -729,6 +754,79 @@ class LicenseService: ) return manifest + def save_diagnostic_log_file( + self, + *, + license_doc: dict[str, Any], + filename: str, + file_path: str | Path, + ) -> DiagnosticLogEntry: + source = Path(file_path) + if not source.is_file() or source.stat().st_size <= 0: + raise ValueError("diagnostic log file is empty") + payload = self._validated_license_payload_for_upload(license_doc) + card_hash = str(payload.get("card_hash") or "").strip().lower() + raw_machine = str(payload.get("machine_fingerprint") or "").strip() + safe_original = sanitize_diagnostic_log_filename(filename) + key = hashlib.sha256(card_hash.encode("ascii")).hexdigest() + target = self.diagnostic_log_dir / f"{key}.zip" + shutil.copyfile(source, target) + uploaded_at = iso_utc(utcnow()) + meta = { + "key": key, + "code_hint": f"{card_hash[:8]}...{card_hash[-4:]}", + "machine_fingerprint": raw_machine, + "filename": safe_original, + "stored_filename": target.name, + "size": target.stat().st_size, + "sha256": sha256_file(target), + "uploaded_at": uploaded_at, + } + target.with_suffix(".json").write_text( + json.dumps(meta, ensure_ascii=False, indent=2), + encoding="utf-8", + ) + return DiagnosticLogEntry( + key=key, + code_hint=str(meta["code_hint"]), + machine_fingerprint=raw_machine, + filename=safe_original, + size=int(meta["size"]), + sha256=str(meta["sha256"]), + uploaded_at=uploaded_at, + ) + + def list_diagnostic_logs(self, *, limit: int = 30) -> list[DiagnosticLogEntry]: + rows: list[DiagnosticLogEntry] = [] + for meta_path in sorted( + self.diagnostic_log_dir.glob("*.json"), + key=lambda p: p.stat().st_mtime, + reverse=True, + )[: max(1, int(limit))]: + try: + meta = json.loads(meta_path.read_text(encoding="utf-8")) + rows.append( + DiagnosticLogEntry( + key=str(meta.get("key") or meta_path.stem), + code_hint=str(meta.get("code_hint") or ""), + machine_fingerprint=str(meta.get("machine_fingerprint") or ""), + filename=str(meta.get("filename") or ""), + size=int(meta.get("size") or 0), + sha256=str(meta.get("sha256") or ""), + uploaded_at=str(meta.get("uploaded_at") or ""), + ) + ) + except Exception: + continue + return rows + + def diagnostic_log_path(self, key: str) -> Path | None: + raw = str(key or "").strip() + if not re.fullmatch(r"[0-9a-f]{64}", raw): + return None + target = self.diagnostic_log_dir / f"{raw}.zip" + return target if target.is_file() else None + def disable_update(self) -> UpdateManifest: existing = self.load_update_manifest() manifest = UpdateManifest( @@ -859,6 +957,21 @@ class LicenseService: touch_last_seen=False, ) + def _validated_license_payload_for_upload(self, license_doc: dict[str, Any]) -> dict[str, Any]: + result = self.verify_license_doc(license_doc) + if not result.ok: + raise ValueError(result.reason) + payload = license_doc.get("payload") if isinstance(license_doc, dict) else None + if not isinstance(payload, dict): + raise ValueError("invalid_format") + card_hash = str(payload.get("card_hash") or "").strip().lower() + raw_machine = str(payload.get("machine_fingerprint") or "").strip() + if len(card_hash) != 64 or any(ch not in "0123456789abcdef" for ch in card_hash): + raise ValueError("invalid_card_hash") + if not raw_machine: + raise ValueError("missing_machine") + return payload + def _verify_card_row( self, conn: sqlite3.Connection, diff --git a/operations/screenshot_operations.py b/operations/screenshot_operations.py index 1c41ad5..df2c35f 100644 --- a/operations/screenshot_operations.py +++ b/operations/screenshot_operations.py @@ -45,6 +45,26 @@ def _ops_log(msg: str, level: str = "INFO") -> None: pass +def _adb_shell_png_variants(data: bytes) -> list[bytes]: + if not data: + return [] + variants: list[bytes] = [] + + def _add(candidate: bytes) -> None: + if candidate and candidate not in variants: + variants.append(candidate) + + marker = b"\x89PNG" + idx = data.find(marker) + if idx > 0: + data = data[idx:] + + _add(data) + _add(data.replace(b"\r\r\n", b"\r\n")) + _add(data.replace(b"\r\n", b"\n")) + return variants + + class ScreenshotOperations: @@ -169,6 +189,41 @@ class ScreenshotOperations: except Exception: return False + def _write_valid_png_bytes(data: bytes) -> bool: + for candidate in _adb_shell_png_variants(data): + try: + with open(tmp_file, "wb") as f: + f.write(candidate) + if _valid_png(tmp_file): + return True + except Exception: + pass + return False + + def _try_shell_stdout_fallback() -> bool: + try: + result = subprocess.run( + [adb, "-s", device_id, "shell", "screencap", "-p"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + check=True, + timeout=8, + ) + if _write_valid_png_bytes(result.stdout or b"") and _safe_replace_file(tmp_file, out_file): + if _valid_png(out_file): + _mirror_screenshot_to_external(out_file) + elapsed = (time.time() - direct_start) * 1000 + _ops_log(f"[{timetag}] [截图] [OK] 获取屏幕截图成功 (shell stdout回退,耗时: {elapsed:.0f}ms)") + return True + _ops_log(f"[截图] shell stdout回退输出无效: bytes={len(result.stdout or b'')}") + except subprocess.CalledProcessError as e: + _ops_log(f"[截图] shell stdout回退失败:ADB命令错误 (返回码: {e.returncode})") + except subprocess.TimeoutExpired: + _ops_log(f"[截图] shell stdout回退失败:操作超时 (设备ID: {device_id})") + except Exception as e: + _ops_log(f"[截图] shell stdout回退失败:{e}") + return False + # 优先走 exec-out:只需 1 次 adb 调用;不在手机端落盘,减少 I/O 与卡顿 # 为避免被并发读取到"半写入文件",写入 tmp 后再原子替换。 for _ in range(2): @@ -314,7 +369,7 @@ class ScreenshotOperations: return False else: _ops_log("[截图] 回退方案截图无效") - return False + return _try_shell_stdout_fallback() except subprocess.CalledProcessError as e: # 特殊处理adb错误 if e.returncode == 4294967295: @@ -325,6 +380,8 @@ class ScreenshotOperations: _ops_log(f"[截图] 回退方案失败:操作超时 (设备ID: {device_id})") except Exception as e: _ops_log(f"[截图] 回退方案失败:{e}") + if _try_shell_stdout_fallback(): + return True return False def _find_pic_path(self, picname: str, for_reading: bool = True) -> Optional[str]: diff --git a/packaging/pyinstaller/build_windows.ps1 b/packaging/pyinstaller/build_windows.ps1 index 3a97397..786181f 100644 --- a/packaging/pyinstaller/build_windows.ps1 +++ b/packaging/pyinstaller/build_windows.ps1 @@ -71,7 +71,23 @@ $CommonArgs = @( "--hidden-import", "PIL._tkinter_finder", "--hidden-import", "ttkbootstrap", "--hidden-import", "logging.handlers", - "--hidden-import", "cryptography.hazmat.primitives.serialization" + "--hidden-import", "cryptography.hazmat.primitives.serialization", + "--collect-submodules", "cv2", + "--collect-submodules", "numpy", + "--collect-submodules", "PIL", + "--collect-submodules", "paddle", + "--collect-submodules", "paddleocr", + "--collect-submodules", "paddlex", + "--collect-submodules", "pyclipper", + "--collect-submodules", "shapely", + "--collect-binaries", "cv2", + "--collect-binaries", "numpy", + "--collect-binaries", "paddle", + "--collect-binaries", "pyclipper", + "--collect-binaries", "shapely", + "--collect-data", "PIL", + "--collect-data", "paddlex", + "--collect-data", "paddleocr" ) if (-not $ReuseCache) { $CommonArgs = @("--clean") + $CommonArgs diff --git a/pyproject.toml b/pyproject.toml index 6ea23de..575d47d 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.7" +version = "3.3.8" description = "基于 ADB + OCR + 像素检测 + 模板匹配的抖音直播间福袋自动挂机工具" readme = "README.md" requires-python = ">=3.10" diff --git a/requirements.txt b/requirements.txt index 9aace85..abc5b09 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ opencv-python>=4.5.0 # OCR识别 - PaddleOCR(主要OCR引擎) paddleocr>=3.0.0 +paddlex>=3.0.0 # 深度学习框架(PaddleOCR的依赖) # 注意:PaddleOCR 3.0 需要 PaddlePaddle 3.0+ diff --git a/tests/test_distribution_manifest.py b/tests/test_distribution_manifest.py index 0f706f8..aaf7a6a 100644 --- a/tests/test_distribution_manifest.py +++ b/tests/test_distribution_manifest.py @@ -56,6 +56,8 @@ def test_distribution_manifest_blocks_project_native_binaries_outside_core_allow "run_cli.exe", "_internal/base_library.zip", "_internal/python312.dll", + "_internal/paddle/libs/paddle.dll", + "_internal/paddlex/.version", "_internal/tests/evil.pyd", "_internal/tools/helper.dll", "_internal/douyin/fudai/core/compiled.cp312-win_amd64.pyd", @@ -89,6 +91,8 @@ def test_distribution_manifest_allows_expected_runtime_shape(): "run_cli.exe", "_internal/base_library.zip", "_internal/python312.dll", + "_internal/paddle/libs/paddle.dll", + "_internal/paddlex/.version", "assets/fudai.png", "assets/desktop.png", "assets/fudai_task_count_v2_one_task.png", @@ -138,6 +142,8 @@ def test_distribution_manifest_can_allow_license_for_local_test_package(): "run_cli.exe", "_internal/base_library.zip", "_internal/python312.dll", + "_internal/paddle/libs/paddle.dll", + "_internal/paddlex/.version", "assets/fudai.png", "webui/index.html", "platform-tools/adb.exe", @@ -191,6 +197,8 @@ def test_distribution_manifest_requires_bundled_android_runtime(): "run_cli.exe", "_internal/base_library.zip", "_internal/python312.dll", + "_internal/paddle/libs/paddle.dll", + "_internal/paddlex/.version", "assets/fudai.png", "webui/index.html", "config/README.md", @@ -208,6 +216,32 @@ def test_distribution_manifest_requires_bundled_android_runtime(): assert "android-tools/ADBKeyboard.apk" in issues +def test_distribution_manifest_requires_bundled_ocr_runtime_data(): + from tools.distribution_manifest import find_distribution_issues + + entries = [ + "DouyinGuajiGUI.exe", + "web_gui.exe", + "run_cli.exe", + "_internal/base_library.zip", + "_internal/python312.dll", + "platform-tools/adb.exe", + "platform-tools/AdbWinApi.dll", + "platform-tools/AdbWinUsbApi.dll", + "android-tools/ADBKeyboard.apk", + "config/README.md", + "config/config.json", + "logs/.keep", + "pic/.keep", + "state/.keep", + ] + + issues = find_distribution_issues(entries) + + assert "_internal/paddlex/.version" in issues + assert "_internal/paddle/*" in issues + + def test_distribution_manifest_cli_passes_clean_tree(tmp_path): dist = tmp_path / "dist" for rel in [ @@ -216,6 +250,8 @@ def test_distribution_manifest_cli_passes_clean_tree(tmp_path): "run_cli.exe", "_internal/base_library.zip", "_internal/python312.dll", + "_internal/paddle/libs/paddle.dll", + "_internal/paddlex/.version", "assets/fudai.png", "assets/fudai_task_status_dacheng_shape.png", "webui/css/base.css", diff --git a/tests/test_log_upload.py b/tests/test_log_upload.py new file mode 100644 index 0000000..cab59e4 --- /dev/null +++ b/tests/test_log_upload.py @@ -0,0 +1,103 @@ +from __future__ import annotations + +import json +import zipfile +from datetime import datetime, timedelta, timezone + +from tests.license_test_keys import TEST_PRIVATE_KEY_PEM, TEST_PUBLIC_KEY_PEM + + +def test_build_diagnostic_log_archive_tails_logs_only(tmp_path, monkeypatch): + from douyin.core import path_config + from douyin.diagnostics import log_upload + + logs_dir = tmp_path / "logs" + logs_dir.mkdir() + (logs_dir / "unified.log").write_text("a" * 40, encoding="utf-8") + (logs_dir / "actions.log").write_text("action\n", encoding="utf-8") + (tmp_path / "license.dat").write_text("secret", encoding="utf-8") + monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(logs_dir)) + + archive = log_upload.build_diagnostic_log_archive( + output_dir=tmp_path, + max_source_bytes=10, + max_archive_bytes=1024 * 1024, + ) + + with zipfile.ZipFile(archive) as zf: + names = set(zf.namelist()) + assert "logs/unified.log" in names + assert "logs/actions.log" in names + assert "license.dat" not in names + assert zf.read("logs/unified.log") == b"a" * 10 + + +def test_license_server_log_upload_overwrites_same_card(tmp_path): + from fastapi.testclient import TestClient + + from license_server import api + from license_server.core import LicenseService + + service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM) + card = service.create_cards(days=5, count=1, customer="log-user")[0] + activated = service.activate(card.code, machine_fingerprint="machine-api") + assert activated.license_doc is not None + client = TestClient(api.create_app(service=service)) + + first = client.post( + "/api/v1/logs/upload", + data={"license": json.dumps(activated.license_doc, ensure_ascii=False)}, + files={"log_file": ("one.zip", b"first", "application/zip")}, + ) + second = client.post( + "/api/v1/logs/upload", + data={"license": json.dumps(activated.license_doc, ensure_ascii=False)}, + files={"log_file": ("two.zip", b"second", "application/zip")}, + ) + entries = service.list_diagnostic_logs() + stored = service.diagnostic_log_path(entries[0].key) + + assert first.status_code == 200 + assert second.status_code == 200 + assert len(entries) == 1 + assert entries[0].filename == "two.zip" + assert stored is not None + assert stored.read_bytes() == b"second" + + +def test_upload_diagnostic_logs_posts_signed_license_and_file(tmp_path, monkeypatch): + from tools.license_dev import create_dev_license + from douyin.core import path_config + from douyin.license import license_manager + from douyin.diagnostics import log_upload + + logs_dir = tmp_path / "logs" + logs_dir.mkdir() + (logs_dir / "unified.log").write_text("hello\n", encoding="utf-8") + license_path = tmp_path / "license.dat" + monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM) + monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a") + monkeypatch.setattr(log_upload, "get_default_license_path", lambda: license_path) + monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(logs_dir)) + create_dev_license( + output_path=license_path, + machine_fingerprint="machine-a", + expires_at=datetime.now(timezone.utc) + timedelta(days=3), + private_key_pem=TEST_PRIVATE_KEY_PEM, + ) + calls = {} + + def fake_post(url, *, license_doc, file_path, timeout_seconds=20): + calls["url"] = url + calls["license_doc"] = license_doc + calls["file_path"] = file_path + return {"ok": True, "message": "ok"} + + result = log_upload.upload_diagnostic_logs( + upload_url="https://license.example.test/api/v1/logs/upload", + http_post_file=fake_post, + ) + + assert result.ok is True + assert calls["license_doc"]["payload"]["machine_fingerprint"] == "machine-a" + assert calls["file_path"].exists() diff --git a/tests/test_obfuscation_staging.py b/tests/test_obfuscation_staging.py index e0cfc3b..1cd40f1 100644 --- a/tests/test_obfuscation_staging.py +++ b/tests/test_obfuscation_staging.py @@ -253,6 +253,22 @@ def test_windows_packaging_script_protects_core_modules_by_default(): assert '"--collect-submodules", "operations"' in cli_block assert '"--hidden-import", "logging.handlers"' in script assert '"--hidden-import", "cryptography.hazmat.primitives.serialization"' in script + assert '"--collect-submodules", "cv2"' in script + assert '"--collect-submodules", "numpy"' in script + assert '"--collect-submodules", "PIL"' in script + assert '"--collect-submodules", "paddle"' in script + assert '"--collect-submodules", "paddleocr"' in script + assert '"--collect-submodules", "paddlex"' in script + assert '"--collect-submodules", "pyclipper"' in script + assert '"--collect-submodules", "shapely"' in script + assert '"--collect-binaries", "cv2"' in script + assert '"--collect-binaries", "numpy"' in script + assert '"--collect-binaries", "paddle"' in script + assert '"--collect-binaries", "pyclipper"' in script + assert '"--collect-binaries", "shapely"' in script + assert '"--collect-data", "PIL"' in script + assert '"--collect-data", "paddlex"' in script + assert '"--collect-data", "paddleocr"' in script assert "tools.exe_packing $DistRoot --smoke-only" in script assert "Executable smoke check failed" in script assert "Post-smoke distribution manifest check failed" in script diff --git a/tests/test_screenshot_direct.py b/tests/test_screenshot_direct.py index 058aa1d..4ee31ec 100644 --- a/tests/test_screenshot_direct.py +++ b/tests/test_screenshot_direct.py @@ -4,6 +4,9 @@ from types import SimpleNamespace from unittest.mock import patch +PNG_BYTES = b"\x89PNG\r\n\x1a\n" + (b"x" * 2048) + b"IEND" + + def test_capture_screenshot_direct_invalid_fallback_png_returns_false(tmp_path): from douyin.core.underlying_operations_pkg.screenshot_direct import ( capture_screenshot_direct, @@ -44,7 +47,7 @@ def test_capture_screenshot_direct_uses_shell_stdout_fallback_when_execout_and_p pic_dir = tmp_path / "pic" pic_dir.mkdir() - png_bytes = b"\x89PNG\r\n\x1a\n" + (b"x" * 2048) + b"IEND" + png_bytes = PNG_BYTES calls: list[list[str]] = [] class FakePopen: @@ -77,3 +80,55 @@ def test_capture_screenshot_direct_uses_shell_stdout_fallback_when_execout_and_p assert result is True assert (pic_dir / "screenshot.png").exists() assert any(cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6 for cmd in calls) + + +def test_capture_screenshot_direct_tries_stdout_after_invalid_pull_file(tmp_path): + from douyin.core.underlying_operations_pkg.screenshot_direct import ( + capture_screenshot_direct, + ) + + fake_self = SimpleNamespace( + _adb_path=lambda: "adb", + get_screenshot=lambda *_a, **_k: False, + ) + + pic_dir = tmp_path / "pic" + pic_dir.mkdir() + calls: list[list[str]] = [] + + class FakePopen: + returncode = 4294967295 + + def __init__(self, cmd, **_kwargs): + calls.append(list(cmd)) + + def communicate(self, timeout=None): # noqa: ARG002 + return b"broken", None + + def fake_run(cmd, **kwargs): + calls.append(list(cmd)) + if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 7: + return SimpleNamespace(returncode=0, stdout=b"", stderr=b"") + if cmd[:4] == ["adb", "-s", "MOCK", "pull"]: + with open(cmd[4], "wb") as f: + f.write(b"not a png") + return SimpleNamespace(returncode=0, stdout=b"", stderr=b"") + if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6: + return SimpleNamespace(returncode=0, stdout=PNG_BYTES.replace(b"\r\n", b"\r\r\n"), stderr=b"") + raise RuntimeError(f"unexpected command: {cmd}") + + with patch( + "douyin.core.underlying_operations_pkg.screenshot_direct.path_config.get_pic_dir", + return_value=str(pic_dir), + ), patch( + "douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.Popen", + FakePopen, + ), patch( + "douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.run", + side_effect=fake_run, + ): + result = capture_screenshot_direct(fake_self, "MOCK", "pic") + + assert result is True + assert (pic_dir / "screenshot.png").read_bytes().startswith(b"\x89PNG") + assert any(cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6 for cmd in calls) diff --git a/tools/distribution_manifest.py b/tools/distribution_manifest.py index 7386a33..3e755b6 100644 --- a/tools/distribution_manifest.py +++ b/tools/distribution_manifest.py @@ -214,6 +214,12 @@ def find_missing_required_runtime_entries(entries: list[str]) -> list[str]: missing.append("_internal/python*.dll") if "_internal/base_library.zip" not in normalized: missing.append("_internal/base_library.zip") + for required in ("_internal/paddlex/.version",): + if required.lower() not in normalized: + missing.append(required) + for prefix in ("_internal/paddle/",): + if not any(entry.startswith(prefix) for entry in normalized): + missing.append(prefix + "*") return missing