Initial commit

This commit is contained in:
Alex committed 2026-11-02 22:54:14 +08:00
commit 57fc3525eb
7311 files changed
+1291398

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
"""
app/utils/__init__.py - 工具模块
"""
from app.utils.paste import (
safe_universal_paste,
clear_and_paste,
UNIVERSAL_INSERT_JS,
)
__all__ = [
'safe_universal_paste',
'clear_and_paste',
'UNIVERSAL_INSERT_JS',
]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+403
View File
@@ -0,0 +1,403 @@
"""Protocol-neutral input attachments. No browser interaction or implicit conversion.
Canonical wire parts remain image_url / file, so existing image clients keep working.
File IDs and local paths are deliberately not supported without a file registry.
"""
from __future__ import annotations
import ast
import base64
import binascii
import hashlib
import json
import mimetypes
import re
import shutil
import tempfile
import threading
import time
import uuid
from dataclasses import dataclass
from functools import wraps
from pathlib import Path
from urllib.parse import unquote, urlsplit
from app.utils.remote_resource import get_public_remote_resource
ROOT = Path(__file__).resolve().parents[2] / "temp" / "attachment_inputs"
MIB = 1024 * 1024
DEFAULTS = {
"enabled": True,
"max_count": 8,
"max_file_mb": 20,
"max_total_mb": 50,
"allowed_types": [],
"error_selectors": [],
"transport_order": ["file_input", "cdp_drop", "js_drop", "file_clipboard"],
"ready_timeout": 30,
}
TRANSPORTS = frozenset((*DEFAULTS["transport_order"], "image_clipboard"))
ATTACHMENT_TYPES = frozenset({"image_url", "image", "input_image", "file", "input_file", "document", "input_audio", "audio_url", "input_video", "video_url"})
_active: set[Path] = set()
_lock = threading.RLock()
class AttachmentError(ValueError):
def __init__(self, code: str, message: str, *, message_index=None, part_index=None):
super().__init__(message)
self.code, self.message = code, message
self.message_index, self.part_index = message_index, part_index
def detail(self):
return {"code": self.code, "message": self.message,
"message_index": self.message_index, "part_index": self.part_index}
def attachment_config(raw=None):
"""Safe effective config; old presets without this section use defaults."""
raw = raw if isinstance(raw, dict) else {}
result = {**DEFAULTS, "allowed_types": [], "transport_order": list(DEFAULTS["transport_order"])}
if isinstance(raw.get("enabled"), bool):
result["enabled"] = raw["enabled"]
for key, ceiling in (("max_count", 32), ("max_file_mb", 100), ("max_total_mb", 200), ("ready_timeout", 180)):
try:
result[key] = max(1, min(ceiling, int(raw.get(key, result[key]))))
except (TypeError, ValueError, OverflowError):
pass
values = raw.get("allowed_types")
if isinstance(values, list):
# Invalid nonempty allowlists must never silently turn into allow-all.
result["allowed_types"] = list(dict.fromkeys(str(v).strip().lower() for v in values if str(v).strip()))[:100]
if isinstance(raw.get("error_selectors"), list):
result["error_selectors"] = [str(v).strip() for v in raw["error_selectors"] if str(v).strip()][:30]
else:
result["error_selectors"] = []
values = raw.get("transport_order")
if isinstance(values, list):
result["transport_order"] = list(dict.fromkeys(v for v in values if isinstance(v, str) and v in TRANSPORTS))
return result
def content_parts(content):
if isinstance(content, dict):
return [content]
if isinstance(content, (list, tuple)):
return list(content)
if isinstance(content, str) and content.strip().startswith("["):
for parse in (json.loads, ast.literal_eval):
try:
value = parse(content)
if isinstance(value, list) and value and all(isinstance(v, dict) and "type" in v for v in value):
return value
except (ValueError, SyntaxError, TypeError):
pass
return content
def safe_filename(value, default="attachment.bin"):
name = unquote(str(value or "")).replace("\\", "/").rsplit("/", 1)[-1]
name = re.sub(r'[\x00-\x1f\x7f<>:"|?*]', "_", name).strip(" .")
if not name:
name = default
# Keep suffix on long names; storage directory is always server-generated.
suffix = Path(name).suffix[:20]
name = name[:160 - len(suffix)] + suffix if len(name) > 160 else name
if name.split(".")[0].upper() in {"CON", "PRN", "AUX", "NUL", *[f"COM{i}" for i in range(1, 10)], *[f"LPT{i}" for i in range(1, 10)]}:
name = "_" + name
return name
def normalize_attachment_part(part):
"""Return canonical attachment or None for non-attachment blocks; never stringify bytes."""
if not isinstance(part, dict):
return None
kind = str(part.get("type") or "").lower().strip()
if kind not in ATTACHMENT_TYPES:
source = part.get("source")
if any(key in part for key in ("file_data", "file_id")) or (isinstance(source, dict) and source.get("type") == "base64"):
raise AttachmentError("attachment_block_unsupported", "不支持的附件内容块类型,未将二进制内容转换为提示词")
return None
image = kind in {"image", "image_url", "input_image"}
value = part.get("file") if isinstance(part.get("file"), dict) else part
source = part.get("source") if isinstance(part.get("source"), dict) else {}
if value.get("file_id") or part.get("file_id") or source.get("file_id"):
raise AttachmentError("attachment_file_id_unsupported", "暂不支持 file_id,请传文件 URL 或完整 base64 数据")
mime = str(source.get("media_type") or value.get("mime_type") or part.get("mime_type") or "").split(";", 1)[0].strip().lower()
filename = value.get("filename") or part.get("filename") or part.get("title")
ref = None
if image:
ref = part.get("image_url") or part.get("url")
if isinstance(ref, dict):
ref = ref.get("url")
elif kind == "input_audio":
audio = part.get("input_audio")
if not isinstance(audio, dict):
raise AttachmentError("attachment_invalid_source", "input_audio 必须包含 data 和 format")
audio_mimes = {"wav": "audio/wav", "mp3": "audio/mpeg", "ogg": "audio/ogg", "flac": "audio/flac", "webm": "audio/webm", "mp4": "audio/mp4", "m4a": "audio/mp4", "aac": "audio/aac"}
fmt = str(audio.get("format") or "").lower()
if fmt not in audio_mimes:
raise AttachmentError("attachment_audio_format_unsupported", "不支持的 input_audio.format")
mime = audio_mimes[fmt]
data = audio.get("data")
ref = f"data:{mime};base64,{data}" if isinstance(data, str) and data else None
filename = filename or f"audio.{fmt}"
else:
ref = value.get("file_data") or value.get("file_url") or part.get("audio_url") or part.get("video_url") or part.get("url")
if isinstance(ref, dict):
ref = ref.get("url")
if source:
source_type = source.get("type")
if source_type == "url":
ref = source.get("url")
elif source_type == "base64":
data = source.get("data")
ref = f"data:{mime or ('image/png' if image else 'application/octet-stream')};base64,{data}" if isinstance(data, str) and data else None
elif source_type == "text" and kind == "document":
text = source.get("data")
if not isinstance(text, str):
raise AttachmentError("attachment_invalid_source", "document text source 必须为字符串")
ref = "data:text/plain;base64," + base64.b64encode(text.encode("utf-8")).decode("ascii")
filename, mime = filename or "document.txt", "text/plain"
else:
raise AttachmentError("attachment_source_unsupported", "不支持的附件 source.type")
if not isinstance(ref, str) or not ref.strip():
raise AttachmentError("attachment_missing_data", "附件缺少 URL 或完整 base64 数据")
ref = ref.strip()
if not ref.startswith(("https://", "http://", "data:")):
raise AttachmentError("attachment_source_unsupported", "附件仅支持 HTTP(S) URL 或 base64 data URI,不允许本地路径")
if ref.startswith("data:") and (";base64," not in ref or not ref.split(";base64,", 1)[1].strip()):
raise AttachmentError("attachment_invalid_base64", "附件 data URI 必须包含完整 base64 数据")
if image:
payload = {"url": ref}
detail = (part.get("image_url") or {}).get("detail") if isinstance(part.get("image_url"), dict) else part.get("detail")
if detail:
payload["detail"] = detail
return {"type": "image_url", "image_url": payload}
return {"type": "file", "file": {"file_data" if ref.startswith("data:") else "file_url": ref,
**({"filename": safe_filename(filename)} if filename else {}), **({"mime_type": mime} if mime else {})}}
def normalize_messages(messages):
result = []
for mi, message in enumerate(messages or []):
if not isinstance(message, dict):
result.append(message)
continue
content = content_parts(message.get("content"))
if isinstance(content, list):
normalized = []
for pi, part in enumerate(content):
try:
normalized.append(normalize_attachment_part(part) or part)
except AttachmentError as exc:
exc.message_index, exc.part_index = mi, pi
raise
content = normalized
result.append({**message, "content": content})
return result
def has_attachments(content):
parts = content_parts(content)
return isinstance(parts, list) and any(isinstance(p, dict) and p.get("type") in ATTACHMENT_TYPES for p in parts)
def type_allowed(filename, mime, rules):
if not rules:
return True
suffix = Path(filename).suffix.lower()
mime = str(mime or "").lower().split(";", 1)[0]
return any(rule == "*/*" or (rule.startswith(".") and suffix == rule) or rule == mime or
(rule.endswith("/*") and mime.startswith(rule[:-1])) for rule in rules)
@dataclass(frozen=True)
class PreparedAttachment:
id: str
path: Path
filename: str
mime: str
byte_size: int
sha256: str
message_index: int
part_index: int
role: str
is_image: bool = False
def cleanup_attachment_inputs(now=None):
now = time.time() if now is None else now
if not ROOT.exists():
return
with _lock:
for directory in ROOT.iterdir():
if directory in _active or not directory.is_dir():
continue
try:
marker = directory / ".expires"
if marker.exists() and float(marker.read_text()) < now:
shutil.rmtree(directory)
elif not marker.exists() and now - directory.stat().st_mtime > 86400:
shutil.rmtree(directory) # crash orphan; never an active lease
except (OSError, ValueError):
continue
class AttachmentBatch:
"""Request-owned lease. Potentially dispatched files get a grace period, not per-file timers."""
def __init__(self):
self.directory = None
self.items = []
self.may_be_reading = False
def adopt_generated(self, filepath):
"""Transfer an internally generated temp file to this request's lease."""
if self.directory is None:
cleanup_attachment_inputs()
ROOT.mkdir(parents=True, exist_ok=True)
with _lock:
self.directory = Path(tempfile.mkdtemp(prefix="request_", dir=ROOT))
_active.add(self.directory)
source = Path(filepath)
directory = self.directory / uuid.uuid4().hex
directory.mkdir()
target = directory / safe_filename(source.name)
source.replace(target)
return target
def prepare(self, messages, config=None, cancelled=lambda: False):
config = attachment_config(config)
normalized = normalize_messages(messages)
specs = [(mi, pi, m.get("role", ""), p) for mi, m in enumerate(normalized) if isinstance(m, dict)
for pi, p in enumerate(m.get("content") if isinstance(m.get("content"), list) else [])
if isinstance(p, dict) and p.get("type") in {"image_url", "file"}]
if not specs:
return []
if not config["enabled"]:
raise AttachmentError("attachments_disabled", "当前预设禁止上传附件")
if len(specs) > config["max_count"]:
raise AttachmentError("attachment_count_exceeded", f"附件数量超过限制 {config['max_count']}")
cleanup_attachment_inputs()
ROOT.mkdir(parents=True, exist_ok=True)
with _lock:
self.directory = Path(tempfile.mkdtemp(prefix="request_", dir=ROOT))
_active.add(self.directory)
total = 0
try:
for mi, pi, role, part in specs:
if cancelled():
raise AttachmentError("attachment_cancelled", "附件准备已取消")
budget = min(config["max_file_mb"] * MIB, config["max_total_mb"] * MIB - total)
item = self._materialize(part, mi, pi, role, budget, cancelled)
if not type_allowed(item.filename, item.mime, config["allowed_types"]):
raise AttachmentError("attachment_type_unsupported", f"当前预设不接受附件类型:{item.mime}", message_index=mi, part_index=pi)
total += item.byte_size
self.items.append(item)
return list(self.items)
except BaseException:
self.close()
raise
def _materialize(self, part, mi, pi, role, budget, cancelled):
image = part["type"] == "image_url"
payload = part["image_url"] if image else part["file"]
ref = payload.get("url") or payload.get("file_data") or payload.get("file_url")
aid = uuid.uuid4().hex
directory = self.directory / aid
directory.mkdir()
path = directory / "payload"
response = None
mime = payload.get("mime_type") or ""
filename = payload.get("filename")
try:
if ref.startswith("data:"):
header, encoded = ref.split(";base64,", 1)
mime = header[5:].split(";", 1)[0].lower() or "application/octet-stream"
if len(encoded) > ((max(0, budget) + 2) // 3) * 4 + 65536:
raise AttachmentError("attachment_size_exceeded", "附件编码内容超过大小限制")
compact = re.sub(r"\s+", "", encoded)
if len(compact) > ((max(0, budget) + 2) // 3) * 4:
raise AttachmentError("attachment_size_exceeded", "附件超过单文件或请求总大小限制")
try:
data = base64.b64decode(compact + "=" * (-len(compact) % 4), validate=True)
except (ValueError, binascii.Error):
raise AttachmentError("attachment_invalid_base64", "附件 base64 无效") from None
chunks = (data,)
else:
response = get_public_remote_resource(ref, timeout=(8, 30), stream=True)
response.raise_for_status()
mime = mime or str(response.headers.get("Content-Type") or "").split(";", 1)[0].lower().strip()
filename = filename or urlsplit(ref).path.rsplit("/", 1)[-1]
if str(response.headers.get("Content-Length", "")).isdigit() and int(response.headers["Content-Length"]) > budget:
raise AttachmentError("attachment_size_exceeded", "附件超过单文件或请求总大小限制")
chunks = response.iter_content(chunk_size=65536)
size, digest = 0, hashlib.sha256()
with path.open("wb") as stream:
for chunk in chunks:
if cancelled():
raise AttachmentError("attachment_cancelled", "附件准备已取消")
if not chunk:
continue
size += len(chunk)
if size > budget:
raise AttachmentError("attachment_size_exceeded", "附件超过单文件或请求总大小限制")
stream.write(chunk)
digest.update(chunk)
if not size:
raise AttachmentError("attachment_empty", "附件内容为空")
# Image validation is also applied to images sent as generic files.
guessed = mimetypes.guess_type(str(filename or ""))[0] or ""
if image or mime.startswith("image/") or guessed.startswith("image/"):
from app.utils.image_handler import _validate_image_bytes
extension = _validate_image_bytes(path.read_bytes())
if not extension:
raise AttachmentError("attachment_invalid_image", "图片格式、尺寸或内容无效")
mime = mimetypes.guess_type("image" + extension)[0] or "image/png"
filename = str(Path(safe_filename(filename, "image")).stem) + extension
image = True
else:
# Conservative sniffing: no parsing, unpacking or conversion of arbitrary files.
with path.open("rb") as stream:
head = stream.read(16)
if head.startswith(b"%PDF-"):
mime = "application/pdf"
elif head.startswith(b"RIFF") and head[8:12] == b"WAVE":
mime = "audio/wav"
mime = mime or guessed or "application/octet-stream"
filename = safe_filename(filename, "attachment" + (mimetypes.guess_extension(mime) or ".bin"))
target = directory / filename
path.replace(target)
return PreparedAttachment(aid, target, filename, mime, size, digest.hexdigest(), mi, pi, role, image)
except AttachmentError as exc:
exc.message_index, exc.part_index = mi, pi
raise
except Exception:
# Never include signed URLs / response bodies in client errors or logs.
raise AttachmentError("attachment_fetch_failed", "附件下载或读取失败,请检查 URL、权限及网络", message_index=mi, part_index=pi) from None
finally:
if response is not None:
response.close()
def close(self):
if self.directory is None:
return
with _lock:
try:
if self.may_be_reading:
(self.directory / ".expires").write_text(str(time.time() + 900))
else:
shutil.rmtree(self.directory, ignore_errors=True)
finally:
_active.discard(self.directory)
self.directory = None
def attachment_scope(fn):
@wraps(fn)
def wrapped(*args, **kwargs):
batch = AttachmentBatch()
try:
yield from fn(*args, **kwargs, _attachment_batch=batch)
finally:
batch.close()
return wrapped
+195
View File
@@ -0,0 +1,195 @@
"""Resolve the Chrome profile display name that owns a specific tab."""
from __future__ import annotations
import json
import threading
import time
from pathlib import Path
from typing import Any, Dict
from app.core.driver import browser_driver, driver_for_tab
_CACHE: Dict[str, Dict[str, str]] = {}
_CACHE_LOCK = threading.RLock()
_GENERIC_PROFILE_NAMES = {
"default",
"person 1",
"profile 1",
"your chrome",
"您的 chrome",
}
def _profile_display_name(profile_path: Path) -> Dict[str, str]:
profile_directory = profile_path.name
user_data_dir = profile_path.parent
info: Dict[str, Any] = {}
try:
with (user_data_dir / "Local State").open("r", encoding="utf-8-sig") as handle:
payload = json.load(handle)
info_cache = ((payload or {}).get("profile") or {}).get("info_cache") or {}
candidate = info_cache.get(profile_directory)
if isinstance(candidate, dict):
info = candidate
except (OSError, ValueError, TypeError):
info = {}
configured_name = str(info.get("name") or "").strip()
candidates = []
if configured_name and configured_name.casefold() not in _GENERIC_PROFILE_NAMES:
candidates.append(configured_name)
candidates.extend([
str(info.get("shortcut_name") or "").strip(),
str(info.get("gaia_given_name") or "").strip(),
str(info.get("gaia_name") or "").strip(),
configured_name,
profile_directory,
])
display_name = next((item for item in candidates if item), "profile")
return {
"name": display_name,
"profile_directory": profile_directory,
"profile_path": str(profile_path),
"user_data_dir": str(user_data_dir),
"source": "chrome_version",
}
def _target_infos(browser: Any) -> list[Dict[str, Any]]:
result = browser_driver(browser).run_cdp("Target.getTargets") or {}
items = result.get("targetInfos") if isinstance(result, dict) else []
return [item for item in (items or []) if isinstance(item, dict)]
def _resolve_via_profile_page(tab: Any, timeout: float = 3.0) -> Dict[str, str]:
browser = getattr(tab, "browser", None)
source_id = str(getattr(tab, "tab_id", "") or "").strip()
if browser is None or not source_id or not hasattr(browser, "_run_cdp"):
return {}
source_info = driver_for_tab(tab).run_cdp("Target.getTargetInfo") or {}
target_info = source_info.get("targetInfo") if isinstance(source_info, dict) else {}
context_id = str((target_info or {}).get("browserContextId") or "").strip()
cache_key = context_id or source_id
with _CACHE_LOCK:
cached = _CACHE.get(cache_key)
if cached:
return dict(cached)
temp_target_id = ""
popup_token = ""
try:
deadline = time.time() + max(0.5, float(timeout or 3.0))
# Creating the probe through CDP gives us its exact target id. The old
# window.open() flow had to rediscover the popup via Target.getTargets;
# if that lookup failed or raced, the id stayed empty and an orphaned
# about:blank tab was left behind.
create_args: Dict[str, Any] = {"url": "about:blank"}
if context_id:
create_args["browserContextId"] = context_id
try:
created = browser_driver(browser).run_cdp("Target.createTarget", **create_args) or {}
if isinstance(created, dict):
temp_target_id = str(created.get("targetId") or "").strip()
except Exception:
temp_target_id = ""
# Some older Chromium/DrissionPage combinations cannot create a target
# in a supplied browser context. Retain the popup fallback, but keep a
# WindowProxy so it can still be closed when target discovery fails.
if not temp_target_id:
existing_ids = {str(item.get("targetId") or "") for item in _target_infos(browser)}
popup_token = f"profile-probe-{threading.get_ident()}-{time.time_ns()}"
opened = driver_for_tab(tab).run_js(
"""
const token = arguments[0];
const child = window.open('about:blank', token);
window.__profileProbeWindows = window.__profileProbeWindows || {};
window.__profileProbeWindows[token] = child;
return child !== null;
""",
popup_token,
)
if opened is False:
return {}
while time.time() < deadline and not temp_target_id:
for item in _target_infos(browser):
target_id = str(item.get("targetId") or "").strip()
if (
target_id
and target_id not in existing_ids
and str(item.get("type") or "").lower() == "page"
and str(item.get("openerId") or "") == source_id
):
temp_target_id = target_id
break
if not temp_target_id:
time.sleep(0.05)
if not temp_target_id:
return {}
temp_tab = None
while time.time() < deadline and temp_tab is None:
try:
temp_tab = browser.get_tab(temp_target_id)
except Exception:
time.sleep(0.05)
if temp_tab is None:
return {}
driver_for_tab(temp_tab).run_cdp("Page.navigate", url="chrome://version")
profile_path_text = ""
while time.time() < deadline and not profile_path_text:
try:
profile_path_text = str(
driver_for_tab(temp_tab).run_js(
"return document.querySelector('#profile_path')?.textContent || ''"
)
or ""
).strip()
except Exception:
profile_path_text = ""
if not profile_path_text:
time.sleep(0.05)
if not profile_path_text:
return {}
identity = _profile_display_name(Path(profile_path_text))
identity["browser_context_id"] = context_id
identity["source_tab_id"] = source_id
with _CACHE_LOCK:
_CACHE[cache_key] = dict(identity)
return identity
except Exception:
return {}
finally:
if temp_target_id:
try:
browser_driver(browser).run_cdp("Target.closeTarget", targetId=temp_target_id)
except Exception:
pass
if popup_token:
try:
driver_for_tab(tab).run_js(
"""
const token = arguments[0];
const store = window.__profileProbeWindows || {};
const child = store[token];
if (child && !child.closed) child.close();
delete store[token];
return true;
""",
popup_token,
)
except Exception:
pass
def resolve_tab_browser_profile(tab: Any) -> Dict[str, str]:
"""Resolve only the browser profile that owns ``tab``."""
for _ in range(2):
identity = _resolve_via_profile_page(tab)
if identity.get("name"):
return identity
return {}
+41
View File
@@ -0,0 +1,41 @@
"""S7:公开健康/引导接口的「详细信息」访问判定。
设计取舍(用户要求:不能把所有接口都改成必须带令牌,否则大量第三方工具会坏):
- ``/health`` 永远可匿名访问,匿名远程调用者只拿到最小响应(服务存活、浏览器是否已连接、
是否需要登录),HTTP 状态码语义不变(浏览器未连接 → 503),监控/探活脚本照常可用;
- 详细诊断(版本、端口、标签池、请求队列、站点数量……)以及会主动连接浏览器的
``browser.health_check()`` 只对「特权调用者」开放;
- 特权调用者 = 严格本机直连(回环地址且无任何反代/隧道转发头,同 S4/S6)
或携带有效的服务令牌 ``AUTH_TOKEN`` / 面板令牌 ``DASHBOARD_AUTH_TOKEN``
(``Authorization: Bearer`` 或 ``X-API-Key``)。
"""
from __future__ import annotations
import os
from typing import Any, Mapping, Optional
from app.core.http_security import is_trusted_local_request
from app.utils.media_access import _candidates, _token_matches
def is_privileged_diagnostics_request(
client_host: Optional[str],
headers: Any,
env: Optional[Mapping[str, str]] = None,
) -> bool:
env = os.environ if env is None else env
candidates = _candidates(headers)
if candidates and (
_token_matches(env.get("AUTH_TOKEN", ""), candidates)
or _token_matches(env.get("DASHBOARD_AUTH_TOKEN", ""), candidates)
):
return True
return is_trusted_local_request(client_host, headers, env)
def request_is_privileged(request: Any, env: Optional[Mapping[str, str]] = None) -> bool:
client = getattr(request, "client", None)
client_host = getattr(client, "host", None) if client else None
return is_privileged_diagnostics_request(client_host, getattr(request, "headers", None), env)
+421
View File
@@ -0,0 +1,421 @@
"""
app/utils/file_paste.py - 文件粘贴工具
职责:
- 创建临时 txt/pdf 文件(存放超长文本)
- 通过 Win32 CF_HDROP 格式将文件复制到系统剪贴板
- 管理 temp 目录的生命周期(启动时清理、退出时清理)
"""
import os
import tempfile
import shutil
import time
from app.core.config import get_logger
from pathlib import Path
from typing import Optional
from app.utils.system_clipboard import (
ClipboardDependencyError,
ClipboardUnsupportedError,
copy_file_to_native_clipboard,
)
logger = get_logger("FPASTE")
# ================= 临时目录管理 =================
# 项目根目录下的 temp 文件夹
_PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
TEMP_DIR = _PROJECT_ROOT / "temp"
TRANSCODED_CACHE_DIR = _PROJECT_ROOT / "download_images" / "_transcoded"
SUPPORTED_TEMP_FILE_TYPES = ("txt", "pdf")
DEFAULT_TEMP_FILE_TYPE = "txt"
def _get_temp_cleanup_min_age_seconds() -> float:
try:
return max(0.0, float(os.getenv("TEMP_CLEANUP_MIN_AGE_SECONDS", "3600")))
except Exception:
return 3600.0
def _get_paste_temp_cleanup_min_age_seconds() -> float:
try:
return max(0.0, float(os.getenv("PASTE_TEMP_CLEANUP_MIN_AGE_SECONDS", "900")))
except Exception:
return 900.0
TEMP_CLEANUP_MIN_AGE_SECONDS = _get_temp_cleanup_min_age_seconds()
PASTE_TEMP_CLEANUP_MIN_AGE_SECONDS = _get_paste_temp_cleanup_min_age_seconds()
def _get_cleanup_min_age_seconds(path: Path) -> float:
try:
if (
path.is_file()
and path.name.startswith("paste_")
and path.suffix.lower() in {".txt", ".pdf"}
):
return PASTE_TEMP_CLEANUP_MIN_AGE_SECONDS
except Exception:
pass
return TEMP_CLEANUP_MIN_AGE_SECONDS
def _is_path_old_enough_for_cleanup(path: Path, now: float, min_age_seconds: float) -> bool:
try:
if now - path.stat().st_mtime < min_age_seconds:
return False
if path.is_dir():
for child in path.rglob("*"):
try:
if now - child.stat().st_mtime < min_age_seconds:
return False
except Exception:
return False
return True
except Exception:
return False
def _temp_log_label(filepath: str) -> str:
"""Return a short temp-file label for logs without exposing absolute paths."""
name = Path(str(filepath or "")).name
return f"temp/{name}" if name else "temp/<unknown>"
def ensure_temp_dir() -> Path:
"""确保 temp 目录存在"""
TEMP_DIR.mkdir(exist_ok=True)
return TEMP_DIR
def normalize_temp_file_type(file_type: str) -> str:
"""Normalize the configured temporary attachment type."""
normalized = str(file_type or DEFAULT_TEMP_FILE_TYPE).strip().lower().lstrip(".")
if normalized in SUPPORTED_TEMP_FILE_TYPES:
return normalized
return DEFAULT_TEMP_FILE_TYPE
def cleanup_temp_dir():
"""
清理 temp 目录中的过期内容
调用时机:
- 程序启动时
- 程序退出时
"""
try:
count = 0
now = time.time()
for target_dir, label_prefix in (
(TEMP_DIR, "temp"),
(TRANSCODED_CACHE_DIR, "download_images/_transcoded"),
):
if not target_dir.exists():
continue
for item in target_dir.iterdir():
if item.name == "attachment_inputs" and target_dir == TEMP_DIR:
from app.utils.attachments import cleanup_attachment_inputs
cleanup_attachment_inputs()
continue
try:
min_age_seconds = _get_cleanup_min_age_seconds(item)
if not _is_path_old_enough_for_cleanup(item, now, min_age_seconds):
continue
if item.is_file():
item.unlink()
count += 1
elif item.is_dir():
shutil.rmtree(item)
count += 1
except Exception as e:
short_name = item.name or "<unknown>"
logger.debug(f"清理临时文件失败: {label_prefix}/{short_name} - {e}")
if count > 0:
logger.info(f"已清理 {count} 个临时文件")
except Exception as e:
logger.warning(f"清理临时目录失败: {e}")
# ================= 临时文件创建 =================
def create_temp_txt(text: str, prefix: str = "paste_") -> Optional[str]:
"""
将文本写入临时 txt 文件
Args:
text: 要写入的文本内容
prefix: 文件名前缀
Returns:
文件的绝对路径,失败返回 None
"""
fd: Optional[int] = None
filepath: Optional[str] = None
try:
ensure_temp_dir()
# 使用 tempfile 创建唯一文件名,放在项目的 temp 目录下
fd, filepath = tempfile.mkstemp(
suffix=".txt",
prefix=prefix,
dir=str(TEMP_DIR)
)
# 写入内容(UTF-8 编码)
with os.fdopen(fd, 'w', encoding='utf-8') as f:
fd = None
f.write(text)
logger.debug(f"临时文件已创建: {_temp_log_label(filepath)} ({len(text)} 字符)")
return filepath
except Exception as e:
if fd is not None:
try:
os.close(fd)
except Exception:
pass
if filepath:
try:
os.unlink(filepath)
logger.debug(f"已清理创建失败的临时文件: {_temp_log_label(filepath)}")
except Exception:
pass
logger.error(f"创建临时文件失败: {e}")
return None
def _pdf_font_candidates() -> list[str]:
"""Return likely local fonts that can render Chinese and Latin text."""
env_path = os.getenv("FILE_PASTE_PDF_FONT_PATH", "").strip()
candidates = []
if env_path:
candidates.append(env_path)
candidates.extend([
r"C:\Windows\Fonts\msyh.ttc",
r"C:\Windows\Fonts\msyh.ttf",
r"C:\Windows\Fonts\simsun.ttc",
r"C:\Windows\Fonts\simhei.ttf",
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
"/Library/Fonts/Arial Unicode.ttf",
"/System/Library/Fonts/PingFang.ttc",
])
return candidates
def _find_pdf_font_path(*, prefer_single_face: bool = False) -> Optional[str]:
"""Find a local font for generated PDFs."""
candidates = _pdf_font_candidates()
if prefer_single_face:
candidates = sorted(
candidates,
key=lambda path: 0 if Path(str(path)).suffix.lower() in {".ttf", ".otf"} else 1,
)
for path in candidates:
try:
if path and os.path.exists(path):
return path
except Exception:
continue
return None
def _write_pdf_with_reportlab(text: str, filepath: str) -> None:
"""Write a text-layer PDF when reportlab is available."""
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
page_width, page_height = A4
margin_x, margin_y = 12, 12
font_size = 4.0
line_height = 4.8
font_name = "Helvetica"
font_path = _find_pdf_font_path(prefer_single_face=True)
if font_path:
font_name = "FilePasteFont"
pdfmetrics.registerFont(TTFont(font_name, font_path))
max_width = page_width - (margin_x * 2)
def string_width(value: str) -> float:
return pdfmetrics.stringWidth(value, font_name, font_size)
def wrap_paragraph(paragraph: str) -> list[str]:
if paragraph == "":
return [""]
lines = []
current = ""
for char in paragraph:
candidate = current + char
if current and string_width(candidate) > max_width:
lines.append(current)
current = char
else:
current = candidate
lines.append(current)
return lines
pdf = canvas.Canvas(filepath, pagesize=A4)
pdf.setTitle("Temporary context")
pdf.setAuthor("Universal Web-to-API")
y = page_height - margin_y
normalized_text = str(text or "").replace("\r\n", "\n").replace("\r", "\n")
for paragraph in normalized_text.split("\n") or [""]:
for line in wrap_paragraph(paragraph):
if y < margin_y:
pdf.showPage()
y = page_height - margin_y
pdf.setFont(font_name, font_size)
pdf.drawString(margin_x, y, line)
y -= line_height
pdf.save()
def _write_temp_pdf(text: str, filepath: str) -> None:
"""Generate text-layer PDF only; never silently degrade context to raster pages."""
_write_pdf_with_reportlab(text, filepath)
def create_temp_pdf(text: str, prefix: str = "paste_") -> Optional[str]:
"""
将文本写入临时 pdf 文件
Args:
text: 要写入的文本内容
prefix: 文件名前缀
Returns:
文件的绝对路径,失败返回 None
"""
fd: Optional[int] = None
filepath: Optional[str] = None
try:
ensure_temp_dir()
fd, filepath = tempfile.mkstemp(
suffix=".pdf",
prefix=prefix,
dir=str(TEMP_DIR)
)
os.close(fd)
fd = None
_write_temp_pdf(text, filepath)
logger.debug(f"PDF 临时文件已创建: {_temp_log_label(filepath)} ({len(text)} 字符)")
return filepath
except Exception as e:
if fd is not None:
try:
os.close(fd)
except Exception:
pass
if filepath:
try:
os.unlink(filepath)
logger.debug(f"已清理创建失败的临时文件: {_temp_log_label(filepath)}")
except Exception:
pass
logger.error(f"创建 PDF 临时文件失败: {e}")
return None
def create_temp_file(text: str, prefix: str = "paste_", file_type: str = DEFAULT_TEMP_FILE_TYPE) -> Optional[str]:
"""Create a temporary attachment for large text using the configured file type."""
normalized_type = normalize_temp_file_type(file_type)
if normalized_type == "pdf":
return create_temp_pdf(text, prefix=prefix)
return create_temp_txt(text, prefix=prefix)
# ================= Win32 剪贴板文件复制 =================
def copy_file_to_clipboard(filepath: str) -> bool:
"""
将文件以 CF_HDROP 格式复制到系统剪贴板
这模拟了用户在文件管理器中「复制」文件的操作。
之后在网页输入框中 Ctrl+V 即可粘贴文件。
Args:
filepath: 文件的绝对路径
Returns:
是否成功
"""
try:
abs_path = os.path.abspath(filepath)
if not os.path.exists(abs_path):
logger.error(f"文件不存在: {_temp_log_label(abs_path)}")
return False
copy_file_to_native_clipboard(abs_path)
logger.debug(f"文件已复制到剪贴板: {_temp_log_label(abs_path)}")
return True
except ClipboardUnsupportedError:
logger.info("当前平台不支持原生文件剪贴板,将依赖 file input / drop_zone 上传")
return False
except ClipboardDependencyError:
logger.error("缺少 Windows 原生文件剪贴板依赖,请执行: pip install pywin32")
return False
except Exception as e:
logger.error(f"复制文件到剪贴板失败: {e}")
return False
# ================= 组合操作 =================
def prepare_file_paste(text: str, file_type: str = DEFAULT_TEMP_FILE_TYPE) -> Optional[str]:
"""
完整的文件粘贴准备流程:
1. 创建临时 txt/pdf 文件
2. 将文件复制到剪贴板
Args:
text: 要粘贴的文本内容
Returns:
临时文件路径(成功时),失败返回 None
"""
filepath = create_temp_file(text, file_type=file_type)
if not filepath:
return None
if not copy_file_to_clipboard(filepath):
# 清理失败的临时文件
try:
os.unlink(filepath)
except Exception:
pass
return None
return filepath
__all__ = [
'TEMP_DIR',
'SUPPORTED_TEMP_FILE_TYPES',
'DEFAULT_TEMP_FILE_TYPE',
'ensure_temp_dir',
'cleanup_temp_dir',
'normalize_temp_file_type',
'create_temp_file',
'create_temp_txt',
'create_temp_pdf',
'copy_file_to_clipboard',
'prepare_file_paste',
]
+605
View File
@@ -0,0 +1,605 @@
"""
app/utils/human_mouse.py - 人类化鼠标行为模拟
职责:
- 平滑鼠标移动(多中间点 + 噪声 + 弧度)
- 空闲微漂移(等待期间的无意识手部抖动)
- 自然化滚轮滚动(多步小增量)
- 精确 CDP 点击(含 pressure 修复 + 按压微移)
所有函数通过 CDP Input.dispatchMouseEvent 直接操作,
不注入任何 JS,不触发页面脚本。
v5.10 改进:
- 移除 _dispatch_mouse_move 中的 tab.actions 降级(触发 CF)
- cdp_precise_click 按压期间增加微移事件(消灭事件沙漠)
- human_scroll 改用 CDP Input.dispatchMouseEvent(mouseWheel)
- idle_drift 频率更不规则(偶尔静止、偶尔连续微动)
"""
import time
import math
import random
from typing import Tuple, Optional, Callable
from app.core.config import logger
from app.core.driver import driver_for_tab
# ================= CDP 鼠标事件派发 =================
def _dispatch_mouse_move(tab, x: int, y: int, buttons: int = 0) -> bool:
"""
通过 CDP 派发 mouseMoved 事件(绝对视口坐标)
不注入 JS,不触发页面脚本的 Runtime.evaluate。
不降级到 tab.actions(会触发 CF 检测)。
Args:
tab: DrissionPage 标签页
x, y: 视口坐标
buttons: 按钮位掩码(0=无按钮,1=左键按住)
Returns:
是否成功
"""
try:
driver_for_tab(tab).run_cdp(
'Input.dispatchMouseEvent',
_timeout=0,
type='mouseMoved',
x=int(x),
y=int(y),
button='none',
buttons=buttons,
modifiers=0,
pointerType='mouse'
)
return True
except Exception as e:
# 🔴 不降级到 tab.actions.move_to()(测试7证明 actions 系列触发 CF)
logger.warning(f"[MOUSE_CDP] mouseMoved 派发失败: {e}")
return False
# ================= 平滑鼠标移动 =================
def smooth_move_mouse(
tab,
from_pos: Tuple[int, int],
to_pos: Tuple[int, int],
duration: float = None,
noise_scale: float = None,
check_cancelled: Callable[[], bool] = None
) -> Tuple[int, int]:
"""
人类化平滑鼠标移动
使用二次贝塞尔插值 + 高斯噪声 + 非对称 ease-out 缓动,
并叠加轻微高频 tremor,模拟“快速接近 + 末端瞄准”。
Args:
tab: DrissionPage 标签页
from_pos: 起始坐标 (x, y)
to_pos: 目标坐标 (x, y)
duration: 移动总时长(秒),None 则自动计算
noise_scale: 噪声幅度(像素),None 则按距离自动
check_cancelled: 取消检查函数
Returns:
最终鼠标坐标 (x, y)
"""
x0, y0 = from_pos
x1, y1 = to_pos
dx = x1 - x0
dy = y1 - y0
dist = math.hypot(dx, dy)
# Very small corrections can still be a single event; 5-15px gets a tiny
# no-delay glide so short movements do not look like coordinate snaps.
if dist < 5:
_dispatch_mouse_move(tab, x1, y1)
return (x1, y1)
if dist < 15:
glide_steps = random.randint(1, 2)
for i in range(1, glide_steps + 1):
if check_cancelled and check_cancelled():
return (x0, y0)
raw_t = i / (glide_steps + 1)
t = 1 - (1 - raw_t) ** 2
jitter = math.sin(raw_t * math.pi)
fx = int(round(x0 + dx * t + random.gauss(0, 0.35) * jitter))
fy = int(round(y0 + dy * t + random.gauss(0, 0.35) * jitter))
_dispatch_mouse_move(tab, fx, fy)
_dispatch_mouse_move(tab, x1, y1)
return (x1, y1)
# 自动计算移动时长:保持连贯,不额外拖慢
if duration is None:
duration = 0.09 + 0.09 * math.log2(1 + dist / 60)
duration *= random.uniform(0.9, 1.08)
duration = max(0.08, min(duration, 0.5))
# 步数:短时高采样,让移动更快但仍平滑
sample_rate = random.uniform(24, 36)
steps = max(6, min(28, int(duration * sample_rate)))
step_interval = duration / steps
# 噪声幅度:与距离成正比,上限 12px
if noise_scale is None:
noise_scale = min(dist * 0.03, 12.0)
# 贝塞尔控制点:在路径中垂线方向偏移,产生弧度
perp_x = -dy / dist if dist > 0 else 0
perp_y = dx / dist if dist > 0 else 0
arc_offset = random.gauss(0, dist * 0.06)
ctrl_x = (x0 + x1) / 2 + perp_x * arc_offset
ctrl_y = (y0 + y1) / 2 + perp_y * arc_offset
start_time = time.perf_counter()
tremor_hz = random.uniform(8.0, 12.0)
tremor_amp_x = random.uniform(0.2, 0.8)
tremor_amp_y = random.uniform(0.2, 0.8)
tremor_phase = random.uniform(0, math.pi * 2)
tremor_phase2 = random.uniform(0, math.pi * 2)
# 逐步移动
for i in range(1, steps + 1):
if check_cancelled and check_cancelled():
return (x0, y0)
# 非对称缓动:前段更快接近,后段更长时间微调
raw_t = i / steps
t = 1 - (1 - raw_t) ** 3
# 二次贝塞尔插值
bx = (1 - t) ** 2 * x0 + 2 * (1 - t) * t * ctrl_x + t ** 2 * x1
by = (1 - t) ** 2 * y0 + 2 * (1 - t) * t * ctrl_y + t ** 2 * y1
# 高斯噪声(中段最大,首尾趋零)
envelope = math.sin(raw_t * math.pi)
nx = random.gauss(0, noise_scale * envelope)
ny = random.gauss(0, noise_scale * envelope)
# 高频低幅手部颤抖(tremor)
tremor_theta = tremor_phase + raw_t * duration * tremor_hz * 2 * math.pi
tremor_x = (
math.sin(tremor_theta)
+ 0.35 * math.sin(2.3 * tremor_theta + tremor_phase2)
) * tremor_amp_x * 0.75
tremor_y = (
math.cos(tremor_theta)
+ 0.35 * math.sin(1.9 * tremor_theta + tremor_phase2 + 0.8)
) * tremor_amp_y * 0.75
fx = int(round(bx + nx + tremor_x))
fy = int(round(by + ny + tremor_y))
_dispatch_mouse_move(tab, fx, fy)
# 步间延迟(带微随机)
target_time = start_time + (step_interval * i)
remaining = target_time - time.perf_counter()
if remaining > 0:
time.sleep(remaining * random.uniform(0.7, 1.0))
# 最终精确到达目标
_dispatch_mouse_move(tab, x1, y1)
# Slightly vary the overshoot chance so long-run samples do not sit on one
# sharp probability edge.
if dist > 100 and random.random() < random.uniform(0.12, 0.28):
angle = math.atan2(dy, dx)
overshoot_dist = random.uniform(3, 8)
ox = int(x1 + math.cos(angle) * overshoot_dist)
oy = int(y1 + math.sin(angle) * overshoot_dist)
_dispatch_mouse_move(tab, ox, oy)
time.sleep(random.uniform(0.02, 0.06))
_dispatch_mouse_move(tab, x1, y1)
return (x1, y1)
# ================= 空闲微漂移 =================
def idle_drift(
tab,
duration: float,
center_pos: Tuple[int, int],
check_cancelled: Callable[[], bool] = None,
drift_radius: float = 3.0,
freq_hz: float = 1.5
) -> Tuple[int, int]:
"""
空闲时的无意识手部微抖动
模拟人类在"等待/阅读/思考"时鼠标的微小漂移。
使用偏向中心的布朗运动,不会越漂越远。
v5.10:频率更不规则,偶尔静止较长时间,偶尔连续快速微动。
Args:
tab: DrissionPage 标签页
duration: 漂移持续时间(秒)
center_pos: 中心坐标
check_cancelled: 取消检查函数
drift_radius: 最大漂移半径(像素)
freq_hz: 基准移动频率(Hz),实际频率会大幅波动
Returns:
最终鼠标坐标
"""
cx, cy = center_pos
cur_x, cur_y = float(cx), float(cy)
base_interval = 1.0 / freq_hz
deadline = time.perf_counter() + max(0.0, float(duration or 0.0))
while time.perf_counter() < deadline:
if check_cancelled and check_cancelled():
break
# 不规则间隔:30% 概率短间隔(连续微动),70% 概率长间隔(静止)
if random.random() < 0.3:
# 连续微动模式:快速连续 2-4 次小移动
burst_count = random.randint(2, 4)
for _ in range(burst_count):
if time.perf_counter() >= deadline:
break
if check_cancelled and check_cancelled():
break
burst_sleep = random.uniform(0.08, 0.25)
burst_sleep = min(burst_sleep, max(0.0, deadline - time.perf_counter()))
if burst_sleep <= 0:
break
time.sleep(burst_sleep)
# 微小移动(0.3-1.5px)
angle = random.uniform(0, 2 * math.pi)
step = random.uniform(0.3, 1.5)
cur_x += math.cos(angle) * step
cur_y += math.sin(angle) * step
# 回弹力
dist_from_center = math.hypot(cur_x - cx, cur_y - cy)
if dist_from_center > drift_radius * 0.6:
pull = 0.3
cur_x += (cx - cur_x) * pull
cur_y += (cy - cur_y) * pull
_dispatch_mouse_move(tab, int(cur_x), int(cur_y))
else:
# 静止模式:较长时间不动
sleep_time = base_interval * random.uniform(0.8, 2.5)
sleep_time = min(sleep_time, max(0.0, deadline - time.perf_counter()))
if sleep_time <= 0:
break
time.sleep(sleep_time)
# 单次微移(0.5-2px)
angle = random.uniform(0, 2 * math.pi)
step = random.uniform(0.5, 2.0)
cur_x += math.cos(angle) * step
cur_y += math.sin(angle) * step
# 回弹力
dist_from_center = math.hypot(cur_x - cx, cur_y - cy)
if dist_from_center > drift_radius * 0.6:
pull = 0.3
cur_x += (cx - cur_x) * pull
cur_y += (cy - cur_y) * pull
_dispatch_mouse_move(tab, int(cur_x), int(cur_y))
return (int(cur_x), int(cur_y))
# ================= 自然化滚轮滚动 =================
def human_scroll(
tab,
total_dy: int,
mouse_x: int = None,
mouse_y: int = None,
check_cancelled: Callable[[], bool] = None
):
"""
模拟人类滚轮滚动(多步小增量,CDP 直接派发)
v5.10:改用 CDP Input.dispatchMouseEvent(mouseWheel) 替代 tab.actions.scroll(),
避免 actions 系列的参数组装差异。
真实鼠标滚轮每格约 100-120px(标准 deltaY=100),间隔 20-80ms。
Args:
tab: DrissionPage 标签页
total_dy: 总滚动量(像素,正=向下,负=向上)
mouse_x: 鼠标 X 坐标(滚动时鼠标位置),None 则默认视口中心
mouse_y: 鼠标 Y 坐标,None 则默认视口中心
check_cancelled: 取消检查函数
"""
if total_dy == 0:
return
# 默认鼠标在视口中心
if mouse_x is None:
mouse_x = 600
if mouse_y is None:
mouse_y = 400
direction = 1 if total_dy > 0 else -1
remaining = abs(total_dy)
while remaining > 0:
if check_cancelled and check_cancelled():
return
# 每步 80-120px(匹配标准鼠标滚轮一格)
step = min(remaining, random.randint(80, 120))
try:
driver_for_tab(tab).run_cdp(
'Input.dispatchMouseEvent',
_timeout=0,
type='mouseWheel',
x=int(mouse_x),
y=int(mouse_y),
deltaX=0,
deltaY=step * direction,
button='none',
buttons=0,
pointerType='mouse'
)
except Exception as e:
logger.debug(f"[SCROLL_CDP] mouseWheel 派发失败: {e}")
# 降级到 actions(滚动触发 CF 的风险低于点击)
try:
driver_for_tab(tab).actions.scroll(0, step * direction)
except Exception:
break
remaining -= step
if remaining > 0:
time.sleep(random.uniform(0.02, 0.08))
def human_scroll_path(
tab,
from_pos: Tuple[int, int],
to_pos: Tuple[int, int],
check_cancelled: Callable[[], bool] = None
) -> Tuple[int, int]:
"""
沿坐标路径执行人类化滚轮滚动。
轨迹本身使用平滑鼠标移动,滚动量取 from/to 的坐标差,
以多段 wheel 事件逐步逼近目标,适合隐身模式下的坐标滑动。
"""
x0, y0 = int(from_pos[0]), int(from_pos[1])
x1, y1 = int(to_pos[0]), int(to_pos[1])
total_dx = x1 - x0
total_dy = y1 - y0
if total_dx == 0 and total_dy == 0:
_dispatch_mouse_move(tab, x0, y0)
return (x0, y0)
travel = math.hypot(total_dx, total_dy)
steps = max(4, min(14, int(travel / 55) + random.randint(1, 3)))
prev_scroll_x = 0
prev_scroll_y = 0
_dispatch_mouse_move(tab, x0, y0)
start_time = time.perf_counter()
duration = max(0.12, min(0.6, 0.14 + travel / 1200.0 + random.uniform(0.01, 0.06)))
step_interval = duration / steps
for i in range(1, steps + 1):
if check_cancelled and check_cancelled():
return (x0, y0)
raw_t = i / steps
t = 0.5 - 0.5 * math.cos(raw_t * math.pi)
cur_x = int(round(x0 + total_dx * t + random.gauss(0, 1.4) * math.sin(raw_t * math.pi)))
cur_y = int(round(y0 + total_dy * t + random.gauss(0, 1.8) * math.sin(raw_t * math.pi)))
_dispatch_mouse_move(tab, cur_x, cur_y)
target_scroll_x = int(round(total_dx * t))
target_scroll_y = int(round(total_dy * t))
delta_x = target_scroll_x - prev_scroll_x
delta_y = target_scroll_y - prev_scroll_y
if delta_x or delta_y:
try:
driver_for_tab(tab).run_cdp(
'Input.dispatchMouseEvent',
_timeout=0,
type='mouseWheel',
x=cur_x,
y=cur_y,
deltaX=delta_x,
deltaY=delta_y,
button='none',
buttons=0,
pointerType='mouse'
)
prev_scroll_x += delta_x
prev_scroll_y += delta_y
except Exception as e:
logger.debug(f"[SCROLL_CDP] path mouseWheel 派发失败: {e}")
break
target_time = start_time + step_interval * i
remaining = target_time - time.perf_counter()
if remaining > 0:
time.sleep(remaining * random.uniform(0.82, 1.0))
# 收尾,确保滚动量累计完整
rest_dx = total_dx - prev_scroll_x
rest_dy = total_dy - prev_scroll_y
if rest_dx or rest_dy:
try:
driver_for_tab(tab).run_cdp(
'Input.dispatchMouseEvent',
_timeout=0,
type='mouseWheel',
x=x1,
y=y1,
deltaX=rest_dx,
deltaY=rest_dy,
button='none',
buttons=0,
pointerType='mouse'
)
except Exception as e:
logger.debug(f"[SCROLL_CDP] path 收尾滚动失败: {e}")
_dispatch_mouse_move(tab, x1, y1)
return (x1, y1)
# ================= 精确 CDP 点击 =================
def cdp_precise_click(
tab,
x: int,
y: int,
hold_duration: float = None,
check_cancelled: Callable[[], bool] = None
) -> bool:
"""
通过 CDP 派发完整的鼠标点击事件序列
v5.10 改进:
- mousePressed 设置 force=0.5(PointerEvent.pressure=0.5,匹配真实鼠标)
- mouseReleased 设置 force=0(释放时 pressure 归零)
- 按压期间插入 1-2 个 mouseMoved(buttons=1) 微移事件(消灭事件沙漠)
- 点击前派发一次 mouseMoved 到精确坐标(匹配真实硬件行为)
完整事件序列(匹配真实硬件):
1. mouseMoved(buttons=0) — 鼠标到达位置
2. mousePressed(force=0.5) — 按下
3. mouseMoved(buttons=1) × 1-2 — 按压期间手指微移
4. mouseReleased(force=0) — 释放
Args:
tab: DrissionPage 标签页
x, y: 视口坐标
hold_duration: 按压时长(秒),None 则随机 60-140ms
check_cancelled: 取消检查函数
Returns:
是否成功
"""
if check_cancelled and check_cancelled():
return False
x, y = int(x), int(y)
if hold_duration is None:
hold_duration = random.uniform(0.04, 0.09)
try:
# 1. 点击前确认鼠标位置(真实硬件在按下前一帧必有 mouseMoved)
_dispatch_mouse_move(tab, x, y, buttons=0)
time.sleep(random.uniform(0.005, 0.015)) # 1 帧间隔
if check_cancelled and check_cancelled():
return False
# 2. mousePressed(force=0.5 → pressure=0.5)
driver_for_tab(tab).run_cdp(
'Input.dispatchMouseEvent',
_timeout=0,
type='mousePressed',
x=x,
y=y,
button='left',
buttons=1,
clickCount=1,
force=0.5,
pointerType='mouse'
)
# 3. 按压期间微移(模拟手指按压导致鼠标轻微位移)
# 真实硬件在 pressed→released 之间通常有 1-3 个 mouseMoved(buttons=1)
micro_move_count = random.randint(1, 2)
micro_interval = hold_duration / (micro_move_count + 1)
press_x, press_y = x, y
for _ in range(micro_move_count):
if check_cancelled and check_cancelled():
# 取消时仍需释放按钮
_release_mouse(tab, press_x, press_y)
return False
time.sleep(micro_interval * random.uniform(0.7, 1.3))
# 微小位移:1-2px(手指按压力导致)
press_x = x + random.randint(-2, 2)
press_y = y + random.randint(-1, 1)
_dispatch_mouse_move(tab, press_x, press_y, buttons=1)
# 剩余按压时间
remaining_hold = hold_duration - micro_interval * micro_move_count
if remaining_hold > 0:
time.sleep(max(0.01, remaining_hold * random.uniform(0.6, 1.0)))
if check_cancelled and check_cancelled():
_release_mouse(tab, press_x, press_y)
return False
# 4. mouseReleased(在最终微移位置释放,而非精确回到原点)
# 释放坐标允许和按下坐标有 1-2px 偏差(真实行为)
release_x = x + random.randint(-1, 1)
release_y = y + random.randint(-1, 1)
driver_for_tab(tab).run_cdp(
'Input.dispatchMouseEvent',
_timeout=0,
type='mouseReleased',
x=release_x,
y=release_y,
button='left',
buttons=0,
clickCount=1,
force=0,
pointerType='mouse'
)
return True
except Exception as e:
logger.warning(f"[CDP_CLICK] 精确点击失败: {e}")
return False
def _release_mouse(tab, x: int, y: int):
"""安全释放鼠标按钮(防止状态泄漏)"""
try:
driver_for_tab(tab).run_cdp(
'Input.dispatchMouseEvent',
_timeout=0,
type='mouseReleased',
x=int(x),
y=int(y),
button='left',
buttons=0,
clickCount=1,
force=0,
pointerType='mouse'
)
except Exception:
pass
__all__ = ['smooth_move_mouse', 'idle_drift', 'human_scroll', 'cdp_precise_click']
+424
View File
@@ -0,0 +1,424 @@
"""
app/utils/image_handler.py - 图片处理工具
职责:
- 解析多模态消息中的图片
- 下载网络图片/解码 Base64
- 保存到本地 image/ 目录
- 复制图片到剪贴板(Windows)
"""
import os
import re
import hashlib
import time
import warnings
from app.core.config import get_logger
import requests
import base64
import json
import io
from pathlib import Path
from typing import List, Dict, Optional, Tuple
from PIL import Image
from app.utils.system_clipboard import (
ClipboardDependencyError,
ClipboardUnsupportedError,
copy_image_to_native_clipboard,
)
from app.utils.remote_resource import get_public_remote_resource
logger = get_logger("IMG_HDL")
# ================= 配置常量 =================
IMAGE_DIR = Path("temp") / "image_inputs"
MAX_IMAGE_SIZE_MB = 20 # 单张图片最大 20MB
MAX_IMAGES_PER_REQUEST = 8
MAX_IMAGES_TOTAL_MB = 50
MAX_IMAGE_PIXELS = 40_000_000
MAX_IMAGE_DIMENSION = 16_384
MAX_IMAGE_FRAMES = 100
IMAGE_INPUT_TTL_SECONDS = 60 * 60
DOWNLOAD_TIMEOUT = 30 # 下载超时 30 秒
SUPPORTED_FORMATS = {'.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp'}
_PIL_FORMAT_EXTENSIONS = {
'JPEG': '.jpg',
'PNG': '.png',
'GIF': '.gif',
'WEBP': '.webp',
'BMP': '.bmp',
}
# ================= 图片提取 =================
def extract_images_from_messages(messages: List[Dict]) -> List[str]:
"""
从消息列表中提取所有图片并保存到本地
Args:
messages: OpenAI 格式的消息列表
Returns:
本地图片路径列表 ["image/1234567890_abc.png", ...]
"""
if not messages:
return []
# 确保 image 目录存在
IMAGE_DIR.mkdir(parents=True, exist_ok=True)
_cleanup_expired_image_inputs()
image_paths = []
total_bytes = 0
for msg in messages:
if not isinstance(msg, dict):
continue
content = msg.get('content')
if not content:
continue
# 🆕 情况1:字符串格式的多模态消息(需要解析)
if isinstance(content, str):
# 检测是否是列表的字符串形式
stripped = content.strip()
if stripped.startswith('[') and stripped.endswith(']'):
parsed = None
# 尝试 JSON 解析
try:
parsed = json.loads(stripped)
logger.debug("[IMAGE] JSON 解析成功")
except (json.JSONDecodeError, TypeError):
pass
# 尝试 Python literal_eval
if parsed is None:
try:
import ast
parsed = ast.literal_eval(stripped)
logger.debug("[IMAGE] Python literal_eval 解析成功")
except (ValueError, SyntaxError):
pass
# 解析成功,更新 content
if parsed and isinstance(parsed, list):
content = parsed
else:
continue # 纯文本,无图片
else:
continue # 纯文本,无图片
# 情况2:列表格式(多模态)
if isinstance(content, (list, tuple)):
for item in content:
if not isinstance(item, dict):
continue
if item.get("type") == "image_url":
image_url_obj = item.get("image_url", {})
if isinstance(image_url_obj, dict):
url = image_url_obj.get("url", "")
else:
url = str(image_url_obj)
if url:
if len(image_paths) >= MAX_IMAGES_PER_REQUEST:
logger.warning(f"[IMAGE] 单次请求图片数量超过限制: {MAX_IMAGES_PER_REQUEST}")
return image_paths
local_path = _process_single_image(url)
if local_path:
try:
image_size = Path(local_path).stat().st_size
except OSError:
image_size = 0
if total_bytes + image_size > MAX_IMAGES_TOTAL_MB * 1024 * 1024:
try:
Path(local_path).unlink(missing_ok=True)
except OSError:
pass
logger.warning(f"[IMAGE] 单次请求图片累计大小超过限制: {MAX_IMAGES_TOTAL_MB}MB")
return image_paths
total_bytes += image_size
image_paths.append(local_path)
if image_paths:
logger.debug(f"[IMAGE] 成功处理 {len(image_paths)} 张图片")
return image_paths
def _process_single_image(url: str) -> Optional[str]:
"""
处理单张图片:下载或解码,保存到本地
Args:
url: 图片 URL(可以是 https:// 或 data:image/... 格式)
Returns:
本地文件路径,失败返回 None
"""
try:
# 情况1:Base64 Data URI
if url.startswith("data:image"):
return _save_base64_image(url)
# 情况2:网络 URL
elif url.startswith(("http://", "https://")):
return _download_image(url)
else:
logger.warning(f"[IMAGE] 不支持的图片格式: {url[:100]}")
return None
except Exception as e:
logger.error(f"[IMAGE] 处理失败: {e}")
return None
def _validate_image_bytes(image_bytes: bytes) -> Optional[str]:
try:
with warnings.catch_warnings():
warnings.simplefilter("error", Image.DecompressionBombWarning)
with io.BytesIO(image_bytes) as image_buffer:
with Image.open(image_buffer) as img:
width, height = img.size
frames = int(getattr(img, "n_frames", 1) or 1)
detected_ext = _PIL_FORMAT_EXTENSIONS.get(str(img.format or '').upper())
if not detected_ext:
raise ValueError(f"unsupported_image_format:{img.format}")
if width <= 0 or height <= 0:
raise ValueError("invalid_image_dimensions")
if width > MAX_IMAGE_DIMENSION or height > MAX_IMAGE_DIMENSION:
raise ValueError(f"image_dimensions_too_large:{width}x{height}")
if width * height > MAX_IMAGE_PIXELS:
raise ValueError(f"image_pixels_too_large:{width * height}")
if frames > MAX_IMAGE_FRAMES:
raise ValueError(f"image_frames_too_many:{frames}")
img.verify()
return detected_ext
except Exception as exc:
logger.warning(f"[IMAGE] 图片格式或尺寸验证失败: {str(exc)[:120]}")
return None
def _cleanup_expired_image_inputs(now: Optional[float] = None) -> None:
cutoff = float(time.time() if now is None else now) - IMAGE_INPUT_TTL_SECONDS
try:
for path in IMAGE_DIR.iterdir():
if not path.is_file():
continue
try:
if path.stat().st_mtime < cutoff:
path.unlink(missing_ok=True)
except OSError:
continue
except OSError:
return
def _save_base64_image(data_uri: str) -> Optional[str]:
"""
解码并保存 Base64 图片
格式: data:image/png;base64,iVBORw0KGgo...
"""
try:
# 提取 MIME 类型和 Base64 数据
match = re.match(r'data:image/(\w+);base64,(.+)', data_uri)
if not match:
logger.warning("[IMAGE] Base64 格式无效")
return None
image_format = match.group(1).lower()
claimed_ext = f".{image_format}"
if claimed_ext not in SUPPORTED_FORMATS:
logger.warning(f"[IMAGE] 不支持的 Base64 图片格式: {image_format}")
return None
base64_data = match.group(2)
# 🆕 空数据直接拒绝(AstrBot 常见:只给前缀 data:image/...;base64,)
if not base64_data or not base64_data.strip():
logger.warning("[IMAGE] Base64 数据为空(仅有 data:image/...;base64, 前缀)")
return None
compact_base64 = re.sub(r"\s+", "", base64_data)
padding = compact_base64.count("=")
estimated_size = max(0, (len(compact_base64) * 3) // 4 - padding)
max_bytes = int(MAX_IMAGE_SIZE_MB * 1024 * 1024)
if estimated_size > max_bytes:
logger.warning(f"[IMAGE] Base64 图片过大: {estimated_size / (1024 * 1024):.2f}MB")
return None
# 解码
padded_base64 = compact_base64 + ("=" * ((4 - len(compact_base64) % 4) % 4))
image_bytes = base64.b64decode(padded_base64, validate=True)
# 大小检查
size_mb = len(image_bytes) / (1024 * 1024)
if size_mb > MAX_IMAGE_SIZE_MB:
logger.warning(f"[IMAGE] Base64 图片过大: {size_mb:.2f}MB")
return None
detected_ext = _validate_image_bytes(image_bytes)
if not detected_ext:
return None
# 生成文件名
timestamp = int(time.time() * 1000)
file_hash = hashlib.sha256(image_bytes).hexdigest()[:12]
filename = f"{timestamp}_{file_hash}{detected_ext}"
filepath = IMAGE_DIR / filename
# 保存文件
with open(filepath, 'wb') as f:
f.write(image_bytes)
logger.debug(f"[IMAGE] Base64 已保存: {filepath} ({size_mb:.2f}MB)")
return str(filepath)
except Exception as e:
logger.error(f"[IMAGE] Base64 解码失败: {e}")
return None
def _download_image(url: str) -> Optional[str]:
"""
下载网络图片
"""
response = None
try:
logger.debug(f"[IMAGE] 开始下载: {url[:100]}")
# 下载
response = get_public_remote_resource(
url,
timeout=(8, DOWNLOAD_TIMEOUT),
headers={'User-Agent': 'Mozilla/5.0'},
stream=True
)
response.raise_for_status()
# 获取内容类型
content_type = response.headers.get('Content-Type', '')
max_bytes = int(MAX_IMAGE_SIZE_MB * 1024 * 1024)
content_length = response.headers.get('Content-Length')
if content_length:
try:
if int(content_length) > max_bytes:
logger.warning(f"[IMAGE] 下载图片过大: {int(content_length) / (1024 * 1024):.2f}MB")
return None
except (TypeError, ValueError):
pass
# 流式读取内容,避免超大响应在尺寸检查前完整进入内存
chunks = []
total_bytes = 0
for chunk in response.iter_content(chunk_size=64 * 1024):
if not chunk:
continue
total_bytes += len(chunk)
if total_bytes > max_bytes:
logger.warning(f"[IMAGE] 下载图片过大: {total_bytes / (1024 * 1024):.2f}MB")
return None
chunks.append(chunk)
image_bytes = b''.join(chunks)
# 大小检查
size_mb = len(image_bytes) / (1024 * 1024)
if size_mb > MAX_IMAGE_SIZE_MB:
logger.warning(f"[IMAGE] 下载图片过大: {size_mb:.2f}MB")
return None
# 尝试从 URL 或 Content-Type 推断格式
ext = _guess_extension(url, content_type)
detected_ext = _validate_image_bytes(image_bytes)
if not detected_ext:
return None
ext = detected_ext
# 生成文件名
timestamp = int(time.time() * 1000)
file_hash = hashlib.sha256(image_bytes).hexdigest()[:12]
filename = f"{timestamp}_{file_hash}{ext}"
filepath = IMAGE_DIR / filename
# 保存文件
with open(filepath, 'wb') as f:
f.write(image_bytes)
logger.info(f"[IMAGE] 下载成功: {filepath} ({size_mb:.2f}MB)")
return str(filepath)
except requests.RequestException as e:
logger.error(f"[IMAGE] 下载失败: {e}")
return None
except Exception as e:
logger.error(f"[IMAGE] 保存失败: {e}")
return None
finally:
if response is not None:
try:
response.close()
except Exception:
pass
def _guess_extension(url: str, content_type: str) -> str:
"""
从 URL 或 Content-Type 推断文件扩展名
"""
# 从 URL 提取
url_lower = url.lower()
for ext in SUPPORTED_FORMATS:
if url_lower.endswith(ext):
return ext
# 从 Content-Type 提取
if 'image/' in content_type:
format_name = content_type.split('/')[-1].split(';')[0].strip()
ext = f".{format_name}"
if ext in SUPPORTED_FORMATS:
return ext
# 默认 PNG
return '.png'
# ================= 剪贴板操作 =================
def copy_image_to_clipboard(image_path: str) -> bool:
"""
复制图片到 Windows 剪贴板
Args:
image_path: 本地图片路径
Returns:
是否成功
"""
try:
copy_image_to_native_clipboard(image_path)
logger.debug(f"[CLIPBOARD] 图片已复制: {image_path}")
return True
except ClipboardUnsupportedError:
logger.info("[CLIPBOARD] 当前平台不支持原生图片剪贴板,将依赖网页原生上传入口")
return False
except ClipboardDependencyError:
logger.error("[CLIPBOARD] 缺少 Windows 原生图片剪贴板依赖,请执行: pip install pywin32")
return False
except Exception as e:
logger.error(f"[CLIPBOARD] 复制失败: {e}")
return False
__all__ = [
'extract_images_from_messages',
'copy_image_to_clipboard',
]
+452
View File
@@ -0,0 +1,452 @@
"""
app/utils/image_validation.py - 通用图片比对、特征签名与参考图过滤模块
职责:
1. 提取图片指纹特征(SHA256、像素 SHA256、尺寸、dHash 感知哈希)。
2. 精确比对与感知相似度比对,判断两张图片是否一致。
3. 从本地路径、Base64 data URI、HTTP(s) 或浏览器会话中读取图片字节。
4. 在多模态生成完成后,过滤掉与用户上传参考图一致的图片,防止误将原图当下发。
5. 提供通用的生成图片校验接口,供 Arena 等适配器复用。
"""
from __future__ import annotations
import base64
import hashlib
from io import BytesIO
from pathlib import Path
from typing import Any, Callable, Dict, Iterable, List, Optional
from urllib.parse import urlsplit
from PIL import Image, ImageOps
from app.core.config import logger
from app.utils.remote_resource import UnsafeRemoteResourceError, get_public_remote_resource
from app.core.driver import driver_for_tab
# S10:比对/C2PA 路径读取第三方页面图片时的预算(与 image_handler 的上传图片上限保持同一量级)。
MAX_VALIDATION_IMAGE_BYTES = 20 * 1024 * 1024
MAX_VALIDATION_IMAGE_PIXELS = 40_000_000
MAX_VALIDATION_IMAGE_DIMENSION = 16_384
def _decode_data_uri_bytes(source: str) -> bytes:
"""解码 data URI;先按 base64 长度预估解码大小,超预算直接放弃(不做完整解码)。"""
try:
encoded = source.split(",", 1)[1]
except IndexError:
return b""
if (len(encoded) * 3) // 4 > MAX_VALIDATION_IMAGE_BYTES + 3:
logger.warning("[IMAGE_VALIDATION] data URI 超过字节预算,跳过比对")
return b""
try:
payload = base64.b64decode(encoded)
except Exception:
return b""
return payload if len(payload) <= MAX_VALIDATION_IMAGE_BYTES else b""
def _read_remote_image_limited(source: str, headers: Dict[str, str], timeout: Any) -> bytes:
"""经受限抓取器读取远程图片:逐跳私网/重定向校验 + DNS pinning + 流式字节上限。"""
response = get_public_remote_resource(
source,
headers=headers,
credential_origin_url=source,
timeout=timeout,
stream=True,
)
try:
if int(getattr(response, "status_code", 0) or 0) >= 400:
return b""
declared = str((getattr(response, "headers", None) or {}).get("Content-Length") or "").strip()
if declared.isdigit() and int(declared) > MAX_VALIDATION_IMAGE_BYTES:
logger.warning("[IMAGE_VALIDATION] 远程图片 Content-Length 超过预算,跳过比对")
return b""
chunks: List[bytes] = []
total = 0
for chunk in response.iter_content(chunk_size=65536):
if not chunk:
continue
total += len(chunk)
if total > MAX_VALIDATION_IMAGE_BYTES:
logger.warning("[IMAGE_VALIDATION] 远程图片超过字节预算,已中止读取")
return b""
chunks.append(chunk)
return b"".join(chunks)
finally:
close = getattr(response, "close", None)
if callable(close):
close()
def get_current_page_url(tab: Any) -> str:
"""获取当前标签页 URL"""
if tab is None:
return ""
try:
return str(driver_for_tab(tab).run_js("return location.href") or "").strip()
except Exception:
return str(getattr(tab, "url", "") or "").strip()
def read_image_bytes(
tab: Any,
url: str,
timeout: Any = (2.0, 5.0),
) -> bytes:
"""通过 HTTP 请求或浏览器上下文安全读取图片二进制数据。"""
source = str(url or "").strip()
if not source:
return b""
if source.startswith("data:"):
return _decode_data_uri_bytes(source)
# 规范化超时时间为 (connect_timeout, read_timeout)
if isinstance(timeout, (int, float)):
eff_timeout: Any = (
(min(2.0, float(timeout)), float(timeout))
if float(timeout) > 5.0
else float(timeout)
)
elif isinstance(timeout, (tuple, list)) and len(timeout) >= 2:
try:
c_to = float(timeout[0]) if timeout[0] is not None else None
r_to = float(timeout[1]) if timeout[1] is not None else None
eff_timeout = (c_to, r_to)
except (TypeError, ValueError):
eff_timeout = (2.0, 5.0)
else:
eff_timeout = (2.0, 5.0)
if source.startswith(("http://", "https://")):
try:
referer = get_current_page_url(tab)
headers = {"User-Agent": "Mozilla/5.0"}
if referer:
headers["Referer"] = referer
payload = _read_remote_image_limited(source, headers, eff_timeout)
if payload:
return payload
except UnsafeRemoteResourceError as exc:
# S10:私网/非法目标不再退回浏览器 fetch(那会绕过同一道校验)
logger.warning(f"[IMAGE_VALIDATION] 拒绝读取非公网图片地址: {exc}")
return b""
except Exception:
pass
if tab is None:
return b""
try:
result = driver_for_tab(tab).run_js(
r"""
return (async function(url, maxBytes) {
let timer = null;
try {
const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
timer = controller ? setTimeout(() => controller.abort(), 6000) : null;
const response = await fetch(url, {
signal: controller ? controller.signal : undefined
});
if (!response.ok) return '';
const declared = Number(response.headers.get('content-length') || 0);
if (declared && declared > maxBytes) return '';
const buffer = await response.arrayBuffer();
if (buffer.byteLength > maxBytes) return '';
const bytes = new Uint8Array(buffer);
let binary = '';
const step = 0x8000;
for (let index = 0; index < bytes.length; index += step) {
binary += String.fromCharCode(...bytes.subarray(index, index + step));
}
return btoa(binary);
} catch (e) {
return '';
} finally {
if (timer) clearTimeout(timer);
}
})(arguments[0], arguments[1]);
""",
source,
MAX_VALIDATION_IMAGE_BYTES,
)
encoded = str(result or "")
if (len(encoded) * 3) // 4 > MAX_VALIDATION_IMAGE_BYTES + 3:
return b""
return base64.b64decode(encoded)
except Exception:
return b""
def read_local_image_bytes(path_value: Any) -> bytes:
"""读取本地文件图片字节"""
try:
path = Path(str(path_value or "")).expanduser()
return path.read_bytes() if path.is_file() else b""
except Exception:
return b""
def read_uploaded_image_bytes(
value: Any,
*,
local_reader: Optional[Callable[[Any], bytes]] = None,
) -> bytes:
"""读取上传的参考图片字节(支持 bytes、data URI、本地路径)"""
if isinstance(value, (bytes, bytearray)):
return bytes(value)
source = str(value or "").strip()
if source.startswith("data:"):
return _decode_data_uri_bytes(source)
reader = local_reader or read_local_image_bytes
return reader(source)
def extract_media_item_bytes(
tab: Any,
item: Dict[str, Any],
*,
local_reader: Optional[Callable[[Any], bytes]] = None,
) -> bytes:
"""从媒体描述字典中提取图片二进制数据"""
if not isinstance(item, dict):
return b""
reader = local_reader or read_local_image_bytes
# 优先读本地落盘路径
for key in ("local_path", "path", "file_path"):
payload = reader(item.get(key))
if payload:
return payload
# 其次从 data_uri / url / src 中读取
for key in ("data_uri", "url", "src"):
value = str(item.get(key) or "").strip()
if value:
payload = read_image_bytes(tab, value)
if payload:
return payload
return b""
def image_signatures(payload: bytes) -> Dict[str, Any]:
"""计算图片指纹(字节哈希、像素哈希、尺寸与差异哈希)"""
data = bytes(payload or b"")
signatures: Dict[str, Any] = {
"sha256": hashlib.sha256(data).hexdigest() if data else None,
"pixel_sha256": None,
"size": None,
"dhash": None,
}
if not data:
return signatures
try:
with Image.open(BytesIO(data)) as source:
# S10:解码前按头部尺寸做像素预算,防解压炸弹(只算 sha256,不做像素级比对)
width, height = source.size
if (
width <= 0
or height <= 0
or width > MAX_VALIDATION_IMAGE_DIMENSION
or height > MAX_VALIDATION_IMAGE_DIMENSION
or width * height > MAX_VALIDATION_IMAGE_PIXELS
):
logger.warning(f"[IMAGE_VALIDATION] 图片尺寸 {width}x{height} 超过像素预算,跳过像素签名")
return signatures
image = ImageOps.exif_transpose(source).convert("RGBA")
signatures["size"] = image.size
signatures["pixel_sha256"] = hashlib.sha256(image.tobytes()).hexdigest()
grayscale = image.convert("L").resize((17, 16), Image.Resampling.LANCZOS)
flattened_data = getattr(grayscale, "get_flattened_data", grayscale.getdata)
pixels = list(flattened_data())
bits = 0
for row in range(16):
offset = row * 17
for column in range(16):
bits = (bits << 1) | int(pixels[offset + column] > pixels[offset + column + 1])
signatures["dhash"] = bits
except Exception:
pass
return signatures
def same_image(
candidate: Dict[str, Any],
reference: Optional[Dict[str, Any]],
*,
max_dhash_distance: Optional[int] = 4,
) -> bool:
"""比对两张图片是否一致。优先严格匹配,可选感知哈希。"""
if not reference:
return False
# 1. 字节完全一致
if candidate.get("sha256") and candidate.get("sha256") == reference.get("sha256"):
return True
# 2. 解码像素与尺寸完全一致(防御服务端无损重新封装/元数据变动)
if (
candidate.get("size") == reference.get("size")
and candidate.get("pixel_sha256")
and candidate.get("pixel_sha256") == reference.get("pixel_sha256")
):
return True
# 3. 感知哈希汉明距离比对
candidate_dhash = candidate.get("dhash")
reference_dhash = reference.get("dhash")
return bool(
max_dhash_distance is not None
and isinstance(candidate_dhash, int)
and isinstance(reference_dhash, int)
and (candidate_dhash ^ reference_dhash).bit_count() <= max_dhash_distance
)
def compute_reference_signatures(
uploaded_images: Iterable[Any],
*,
local_reader: Optional[Callable[[Any], bytes]] = None,
) -> List[Dict[str, Any]]:
"""批量计算用户上传参考图的签名列表"""
signatures: List[Dict[str, Any]] = []
for raw in uploaded_images or []:
payload = read_uploaded_image_bytes(raw, local_reader=local_reader)
if payload:
sig = image_signatures(payload)
if sig.get("sha256") or sig.get("pixel_sha256"):
signatures.append(sig)
return signatures
def is_candidate_matching_reference(
candidate_signatures: Dict[str, Any],
reference_signatures: List[Dict[str, Any]],
*,
max_dhash_distance: Optional[int] = None,
) -> bool:
"""检查候选图签名是否命中任一参考图"""
if not candidate_signatures or not reference_signatures:
return False
return any(
same_image(candidate_signatures, ref, max_dhash_distance=max_dhash_distance)
for ref in reference_signatures
)
def filter_reference_images(
media_items: Iterable[Dict[str, Any]],
uploaded_images: Iterable[Any],
*,
tab: Any = None,
logger_context: str = "",
max_dhash_distance: Optional[int] = None,
local_reader: Optional[Callable[[Any], bytes]] = None,
) -> List[Dict[str, Any]]:
"""
通用参考图过滤器:
遍历媒体项列表,识别并剔除与用户上传参考图一致的图片项。
保留非图片媒体(如音频、视频)以及真正由 AI 生成的新图片。
"""
if not media_items:
return []
items = [item for item in media_items if isinstance(item, dict)]
if not items:
return []
reference_sigs = compute_reference_signatures(uploaded_images, local_reader=local_reader)
if not reference_sigs:
return items
tag = f"[{logger_context}] " if logger_context else ""
filtered_items: List[Dict[str, Any]] = []
dropped_count = 0
for item in items:
media_type = str(item.get("media_type") or item.get("type") or "image").strip().lower()
if media_type not in {"image", "image_url", "input_image"}:
filtered_items.append(item)
continue
payload = extract_media_item_bytes(tab, item, local_reader=local_reader)
if not payload:
# 无法读取字节时,保留该项(避免误杀)
filtered_items.append(item)
continue
candidate_sig = image_signatures(payload)
if is_candidate_matching_reference(
candidate_sig,
reference_sigs,
max_dhash_distance=max_dhash_distance,
):
dropped_count += 1
src_info = item.get("url") or item.get("local_path") or item.get("src") or "data_uri"
logger.info(
f"{tag}检测到提取结果与上传参考图一致,已自动过滤该图片项: "
f"source={str(src_info)[:100]}"
)
else:
filtered_items.append(item)
if dropped_count > 0:
logger.debug(
f"{tag}参考图过滤完成: 原始 {len(items)} 项, 过滤参考图 {dropped_count} 张, 剩余 {len(filtered_items)} 项"
)
return filtered_items
def validate_generated_images(
uploaded_images: Iterable[Any],
generated_media_items: Iterable[Dict[str, Any]],
*,
tab: Any = None,
max_dhash_distance: Optional[int] = None,
error_builder: Optional[Callable[[str], Exception]] = None,
local_reader: Optional[Callable[[Any], bytes]] = None,
) -> None:
"""
校验生成图片列表中是否存在与上传参考图完全相同的图片。
若存在,则调用 error_builder 抛出异常;若未提供 error_builder,则抛出 ValueError。
"""
reference_sigs = compute_reference_signatures(uploaded_images, local_reader=local_reader)
if not reference_sigs:
return
for item in generated_media_items or []:
if not isinstance(item, dict):
continue
media_type = str(item.get("media_type") or item.get("type") or "image").strip().lower()
if media_type not in {"image", "image_url", "input_image"}:
continue
payload = extract_media_item_bytes(tab, item, local_reader=local_reader)
if not payload:
continue
candidate = image_signatures(payload)
if is_candidate_matching_reference(
candidate,
reference_sigs,
max_dhash_distance=max_dhash_distance,
):
msg = "生成结果与上传图片一致"
if error_builder:
raise error_builder(msg)
raise ValueError(msg)
__all__ = [
"compute_reference_signatures",
"extract_media_item_bytes",
"filter_reference_images",
"get_current_page_url",
"image_signatures",
"is_candidate_matching_reference",
"read_image_bytes",
"read_local_image_bytes",
"read_uploaded_image_bytes",
"same_image",
"validate_generated_images",
]
+172
View File
@@ -0,0 +1,172 @@
"""S6:本地媒体出口(/media、/download_images)的访问控制与转码资源预算。
设计取舍
--------
生成的媒体 URL 会原样写进 OpenAI 兼容响应,被第三方聊天前端直接用 ``<img src>`` /
``<audio src>`` 渲染——这类前端不会带 Bearer 头。因此默认不强制鉴权(保持兼容),
提供 ``MEDIA_REQUIRE_AUTH=true`` 显式开启:开启后接受
- 服务 API 令牌(``Authorization: Bearer`` / ``X-API-Key``,与 /v1/* 相同);
- 控制面板令牌(``Authorization: Bearer``);
- 直接来自本机回环、没有任何反代/隧道转发头的请求(本机控制面板直接预览)。
``MEDIA_REQUIRE_AUTH`` 的非法取值按安全开关 fail-closed(视为开启)。
转码(ffmpeg,最长 60s):全局并发上限 ``MEDIA_TRANSCODE_MAX_CONCURRENCY``(默认 2),
等待超过 ``MEDIA_TRANSCODE_QUEUE_TIMEOUT_SEC``(默认 10s)返回 503;同一输出文件的并发
请求在同一把锁上合并(后到者等待先到者完成后直接命中缓存);源文件超过
``MEDIA_TRANSCODE_MAX_SOURCE_MB``(默认 200)拒绝转码。
"""
from __future__ import annotations
import hmac
import os
import threading
from contextlib import contextmanager
from typing import Any, Dict, Iterator, Mapping, Optional
from app.core.http_security import is_trusted_local_request
MEDIA_PATH_PREFIXES = ("/media/", "/download_images/")
def _env_bool_secure(env: Mapping[str, str], name: str, default: bool = False) -> bool:
value = env.get(name)
if value is None or str(value).strip() == "":
return default
lowered = str(value).strip().lower()
if lowered in ("true", "1", "yes", "on"):
return True
if lowered in ("false", "0", "no", "off"):
return False
return True # fail-closed
def _env_positive_float(env: Mapping[str, str], name: str, default: float) -> float:
try:
value = float(str(env.get(name, "") or "").strip())
except ValueError:
return default
return value if value > 0 and value == value else default
def media_auth_required(env: Optional[Mapping[str, str]] = None) -> bool:
env = os.environ if env is None else env
return _env_bool_secure(env, "MEDIA_REQUIRE_AUTH", False)
def is_media_path(path: str) -> bool:
value = str(path or "")
return any(value.startswith(prefix) for prefix in MEDIA_PATH_PREFIXES)
def _candidates(headers: Any, query_token: Optional[str] = None) -> list:
out = []
if headers is not None:
auth = str(headers.get("authorization") or "").strip()
if auth.lower().startswith("bearer "):
auth = auth[7:].strip()
if auth:
out.append(auth)
api_key = str(headers.get("x-api-key") or "").strip()
if api_key:
out.append(api_key)
token_param = str(query_token or "").strip()
if token_param:
out.append(token_param)
return out
def _token_matches(expected: str, candidates: list) -> bool:
expected = str(expected or "").strip()
if not expected:
return False
expected_bytes = expected.encode("utf-8")
return any(hmac.compare_digest(expected_bytes, c.encode("utf-8")) for c in candidates)
def media_request_authorized(
client_host: Optional[str],
headers: Any,
env: Optional[Mapping[str, str]] = None,
query_token: Optional[str] = None,
) -> bool:
"""MEDIA_REQUIRE_AUTH 开启时的判定;未开启时恒为 True。"""
env = os.environ if env is None else env
if not media_auth_required(env):
return True
candidates = _candidates(headers, query_token=query_token)
if candidates:
if _token_matches(env.get("AUTH_TOKEN", ""), candidates):
return True
if _token_matches(env.get("DASHBOARD_AUTH_TOKEN", ""), candidates):
return True
return is_trusted_local_request(client_host, headers, env)
class TranscodeBusyError(RuntimeError):
"""全局转码并发已满且等待超时。"""
class TranscodeGate:
"""转码的全局并发上限 + 同键合并。"""
def __init__(self, max_concurrency: int = 2, queue_timeout: float = 10.0):
self.max_concurrency = max(1, int(max_concurrency))
self.queue_timeout = max(0.0, float(queue_timeout))
self._slots = threading.BoundedSemaphore(self.max_concurrency)
self._keys_lock = threading.Lock()
self._key_locks: Dict[str, list] = {} # key -> [lock, refcount]
@classmethod
def from_env(cls, env: Optional[Mapping[str, str]] = None) -> "TranscodeGate":
env = os.environ if env is None else env
return cls(
max_concurrency=int(_env_positive_float(env, "MEDIA_TRANSCODE_MAX_CONCURRENCY", 2)),
queue_timeout=_env_positive_float(env, "MEDIA_TRANSCODE_QUEUE_TIMEOUT_SEC", 10.0),
)
@contextmanager
def key(self, key: str) -> Iterator[None]:
"""同一输出只允许一个转码在跑;后到者等待(期间不占全局名额)。"""
with self._keys_lock:
entry = self._key_locks.setdefault(key, [threading.Lock(), 0])
entry[1] += 1
try:
if not entry[0].acquire(timeout=self.queue_timeout + 90.0):
raise TranscodeBusyError("media_transcode_busy")
try:
yield
finally:
entry[0].release()
finally:
with self._keys_lock:
entry[1] -= 1
if entry[1] <= 0 and self._key_locks.get(key) is entry:
self._key_locks.pop(key, None)
@contextmanager
def slot(self) -> Iterator[None]:
if not self._slots.acquire(timeout=self.queue_timeout):
raise TranscodeBusyError("media_transcode_busy")
try:
yield
finally:
self._slots.release()
def transcode_max_source_bytes(env: Optional[Mapping[str, str]] = None) -> int:
env = os.environ if env is None else env
return int(_env_positive_float(env, "MEDIA_TRANSCODE_MAX_SOURCE_MB", 200) * 1024 * 1024)
__all__ = [
"MEDIA_PATH_PREFIXES",
"TranscodeBusyError",
"TranscodeGate",
"is_media_path",
"media_auth_required",
"media_request_authorized",
"transcode_max_source_bytes",
]
+213
View File
@@ -0,0 +1,213 @@
"""
app/utils/media_safety.py - 媒体落盘与同源提供的安全策略(审查条目 S8 / S9)
背景:下载的图片/音视频保存在 ``download_images/`` 并通过应用**同源**的
``/download_images/*``、``/media/*`` 提供。若把上游返回的 SVG、HTML 等「活动文档」
原样落盘,受害者直接打开该链接时脚本会在控制面板同源执行。
策略(单一事实源):
1. 写入端:扩展名只能来自白名单;Content-Type 与 URL 后缀都不能把 ``.svg``/``.html``
之类带进来;再对文件头做嗅探,标记语言(``<svg``、``<html``、``<?xml`` …)一律拒绝。
2. 出口端:非白名单扩展名(含历史遗留的 .svg/.html)一律 ``application/octet-stream``
+ ``attachment``;所有媒体响应附带 ``nosniff`` 与 ``sandbox`` CSP,即使内容被误判也不能执行脚本。
"""
from __future__ import annotations
from pathlib import Path
from typing import Dict, Optional, Tuple
from urllib.parse import urlparse
IMAGE_EXT_BY_MIME: Dict[str, str] = {
"image/jpeg": ".jpg",
"image/jpg": ".jpg",
"image/pjpeg": ".jpg",
"image/png": ".png",
"image/webp": ".webp",
"image/gif": ".gif",
"image/bmp": ".bmp",
"image/avif": ".avif",
}
AUDIO_EXT_BY_MIME: Dict[str, str] = {
"audio/aac": ".aac",
"audio/mpeg": ".mp3",
"audio/mp3": ".mp3",
"audio/wav": ".wav",
"audio/x-wav": ".wav",
"audio/wave": ".wav",
"audio/ogg": ".ogg",
"audio/opus": ".opus",
"audio/webm": ".webm",
"audio/mp4": ".m4a",
"audio/x-m4a": ".m4a",
"audio/flac": ".flac",
"audio/x-flac": ".flac",
}
VIDEO_EXT_BY_MIME: Dict[str, str] = {
"video/mp4": ".mp4",
"video/webm": ".webm",
"video/ogg": ".ogv",
"video/quicktime": ".mov",
}
SAFE_IMAGE_EXTENSIONS = frozenset({".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp", ".avif"})
SAFE_AUDIO_EXTENSIONS = frozenset({".aac", ".mp3", ".wav", ".ogg", ".oga", ".opus", ".webm", ".m4a", ".flac"})
SAFE_VIDEO_EXTENSIONS = frozenset({".mp4", ".m4v", ".webm", ".ogv", ".mov"})
SAFE_MEDIA_EXTENSIONS = SAFE_IMAGE_EXTENSIONS | SAFE_AUDIO_EXTENSIONS | SAFE_VIDEO_EXTENSIONS
_SAFE_EXTS_BY_KIND = {
"image": SAFE_IMAGE_EXTENSIONS,
"audio": SAFE_AUDIO_EXTENSIONS,
"video": SAFE_VIDEO_EXTENSIONS,
}
_MIME_MAP_BY_KIND = {
"image": IMAGE_EXT_BY_MIME,
"audio": AUDIO_EXT_BY_MIME,
"video": VIDEO_EXT_BY_MIME,
}
_DEFAULT_EXT_BY_KIND = {"image": ".png", "audio": ".mp3", "video": ".mp4"}
# 出口端 MIME:只为白名单扩展名给出可内联的媒体类型
_SERVE_MIME_BY_EXT: Dict[str, str] = {
".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png", ".webp": "image/webp",
".gif": "image/gif", ".bmp": "image/bmp", ".avif": "image/avif",
".aac": "audio/aac", ".mp3": "audio/mpeg", ".wav": "audio/wav", ".ogg": "audio/ogg",
".oga": "audio/ogg", ".opus": "audio/ogg", ".m4a": "audio/mp4", ".flac": "audio/flac",
".mp4": "video/mp4", ".m4v": "video/mp4", ".webm": "video/webm", ".ogv": "video/ogg",
".mov": "video/quicktime",
}
# sandbox(不含 allow-scripts)禁止脚本执行;media-src/img-src 'self' 保证浏览器
# 直接打开图片/音视频时的内置查看器仍能加载本身。
MEDIA_CSP = "sandbox; default-src 'none'; img-src 'self' data:; media-src 'self'; style-src 'unsafe-inline'"
def normalize_mime(content_type: Optional[str]) -> str:
return str(content_type or "").split(";", 1)[0].strip().lower()
def is_active_content_mime(content_type: Optional[str]) -> bool:
mime = normalize_mime(content_type)
if not mime:
return False
if mime in {"text/html", "application/xhtml+xml", "image/svg+xml", "text/xml",
"application/xml", "text/javascript", "application/javascript",
"application/x-javascript", "text/xsl", "application/pdf", "text/plain"}:
return True
return mime.endswith("+xml")
def choose_media_extension(kind: str, content_type: Optional[str], url: Optional[str] = None) -> Optional[str]:
"""返回安全的落盘扩展名;``None`` 表示拒绝落盘(保留远程链接/走其它回退)。
顺序:活动内容 MIME → 拒绝;MIME 与 kind 匹配且在白名单 → 用映射;
否则仅当 URL 后缀在同类白名单时采用;再否则用类别默认扩展名。
URL 后缀是 .html/.svg 等时**绝不**采用(S9 根因)。
"""
kind = str(kind or "").strip().lower()
safe_exts = _SAFE_EXTS_BY_KIND.get(kind)
if safe_exts is None:
return None
mime = normalize_mime(content_type)
if is_active_content_mime(mime):
return None
mapped = _MIME_MAP_BY_KIND[kind].get(mime)
if mapped:
return mapped
# MIME 属于别的媒体大类(如 image/png 走 video 分支)→ 不信任
if mime and "/" in mime and mime.split("/", 1)[0] in _SAFE_EXTS_BY_KIND and mime.split("/", 1)[0] != kind:
return None
if url:
try:
suffix = Path(urlparse(str(url)).path).suffix.lower()
except Exception:
suffix = ""
if suffix in safe_exts:
return ".jpg" if suffix == ".jpeg" else suffix
return _DEFAULT_EXT_BY_KIND[kind]
_MARKUP_PREFIXES = (b"<!doctype", b"<html", b"<svg", b"<?xml", b"<script", b"<head", b"<body",
b"<iframe", b"<!--", b"<xsl", b"<meta", b"<a ", b"<div", b"<img", b"<object",
b"<embed", b"<style", b"<link", b"<form", b"<title", b"<p>", b"<?php")
def looks_like_active_content(head: bytes) -> bool:
"""嗅探文件头:以 HTML/SVG/XML 标记开头(允许 BOM 与前导空白)即判为活动内容。"""
data = bytes(head or b"")[:1024]
if data.startswith(b"\xef\xbb\xbf"):
data = data[3:]
elif data.startswith((b"\xff\xfe", b"\xfe\xff")):
# UTF-16 文本:真实媒体文件不会以 UTF-16 BOM 开头
return True
stripped = data.lstrip(b" \t\r\n\x0c").lower()
if stripped.startswith(b"<"):
if stripped.startswith(_MARKUP_PREFIXES):
return True
# 其它以 < 开头的也不是任何受支持的二进制媒体格式
return True
return False
def detect_image_extension(head: bytes) -> Optional[str]:
data = bytes(head or b"")
if data.startswith(b"\x89PNG\r\n\x1a\n"):
return ".png"
if data.startswith(b"\xff\xd8\xff"):
return ".jpg"
if data.startswith((b"GIF87a", b"GIF89a")):
return ".gif"
if data.startswith(b"BM"):
return ".bmp"
if len(data) >= 12 and data.startswith(b"RIFF") and data[8:12] == b"WEBP":
return ".webp"
if len(data) >= 16 and data[4:8] == b"ftyp" and data[8:12] in {b"avif", b"avis"}:
return ".avif"
return None
def resolve_media_delivery(path: Path, preferred_mime: Optional[str] = None) -> Tuple[str, str]:
"""出口端:返回 (media_type, content_disposition_type)。
白名单扩展名 → 对应媒体 MIME + inline;其余(含历史遗留 .svg/.html)→ octet-stream + attachment。
``preferred_mime`` 仅在它本身是安全媒体类型时采用(例如转码输出)。
"""
suffix = Path(path).suffix.lower()
if suffix not in SAFE_MEDIA_EXTENSIONS:
return "application/octet-stream", "attachment"
mime = normalize_mime(preferred_mime)
if mime and not is_active_content_mime(mime) and mime.split("/", 1)[0] in ("image", "audio", "video"):
return mime, "inline"
return _SERVE_MIME_BY_EXT.get(suffix, "application/octet-stream"), "inline"
def safe_media_response_headers() -> Dict[str, str]:
# 注意:不设置 Cross-Origin-Resource-Policy —— 聊天客户端(如运行在 localhost:8000 的前端)
# 需要跨站 <img>/<audio> 引用这些链接,CORP 会把它们一并拦掉。
return {
"X-Content-Type-Options": "nosniff",
"Content-Security-Policy": MEDIA_CSP,
"Referrer-Policy": "no-referrer",
}
__all__ = [
"AUDIO_EXT_BY_MIME",
"IMAGE_EXT_BY_MIME",
"MEDIA_CSP",
"SAFE_MEDIA_EXTENSIONS",
"VIDEO_EXT_BY_MIME",
"choose_media_extension",
"detect_image_extension",
"is_active_content_mime",
"looks_like_active_content",
"normalize_mime",
"resolve_media_delivery",
"safe_media_response_headers",
]
+284
View File
@@ -0,0 +1,284 @@
"""
model_routing.py - Resolve OpenAI-style model ids to route-domain targets.
"""
from __future__ import annotations
from typing import Any, Dict, List, Optional
from app.utils.site_rules import derive_site_card_id
from app.utils.site_url import build_route_domain_aliases, get_preferred_route_domain
_GENERIC_MODEL_IDS = {
"",
"any",
"auto",
"default",
"web-browser",
"未知",
"unknown",
"gpt-3.5-turbo",
"gpt-4",
"gpt-4o",
"gpt-4.1",
"gpt-4.1-mini",
"gpt-4o-mini",
}
_MODEL_ALIAS_DELIMITERS = ("-", "_", "/", ":", ".")
def _normalize_model_id(value: Any) -> str:
return str(value or "").strip().lower()
def _clean_model_id(value: Any) -> str:
return str(value or "").strip()
def _build_model_id_candidates(route_domain: str) -> List[str]:
"""
Build user-facing model ids for a routed site.
Examples:
- chat.deepseek.com -> chat.deepseek.com, deepseek
- gemini.google.com -> gemini.google.com, gemini.com, gemini
"""
normalized_route = _normalize_model_id(route_domain)
if not normalized_route:
return []
result: List[str] = []
seen = set()
def _add(value: Any):
candidate = _normalize_model_id(value)
if candidate and candidate not in seen:
seen.add(candidate)
result.append(candidate)
route_aliases = build_route_domain_aliases(normalized_route)
_add(normalized_route)
for alias in route_aliases:
_add(alias)
for alias in [normalized_route, *route_aliases]:
_add(derive_site_card_id(alias))
return result
def _matches_model_alias(model_id: str, alias_id: str) -> bool:
if model_id == alias_id:
return True
if not alias_id or len(model_id) <= len(alias_id):
return False
if not model_id.startswith(alias_id):
return False
return model_id[len(alias_id)] in _MODEL_ALIAS_DELIMITERS
def _resolve_route_domain(tab: Dict[str, Any]) -> str:
return str(
tab.get("route_domain")
or get_preferred_route_domain(tab.get("current_domain") or "")
or ""
).strip().lower()
def _default_model_id_for_tab(tab: Dict[str, Any]) -> str:
return (
_clean_model_id(tab.get("route_domain"))
or _clean_model_id(get_preferred_route_domain(tab.get("current_domain") or ""))
or _clean_model_id(tab.get("current_domain"))
)
def _is_model_name_overridden(tab: Dict[str, Any]) -> bool:
return bool(_clean_model_id(tab.get("model_name_override_source")))
def _get_exposed_model_id(tab: Dict[str, Any]) -> str:
return _clean_model_id(tab.get("exposed_model_name")) or _default_model_id_for_tab(tab)
def collect_route_domain_models(tabs: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Build a deduplicated model list from active tabs.
The exported model id is the tab's exposed model name. Tabs without a
custom name keep the historical route-domain id and aliases.
"""
model_map: Dict[str, Dict[str, Any]] = {}
alias_map: Dict[str, str] = {}
ambiguous_aliases = set()
def _ensure_model(
model_id: str,
route_domain: str = "",
*,
allow_prefix: bool = True,
is_route_alias: bool = False,
) -> None:
normalized_model = _normalize_model_id(model_id)
if not normalized_model:
return
if normalized_model not in model_map:
model_map[normalized_model] = {
"id": _clean_model_id(model_id),
"display_name": _clean_model_id(model_id),
"model_name": _clean_model_id(model_id),
"route_type": "model_name",
"route_domains": set(),
"allow_prefix": bool(allow_prefix),
"is_route_alias": bool(is_route_alias),
}
if route_domain:
model_map[normalized_model]["route_domains"].add(route_domain)
if not allow_prefix:
model_map[normalized_model]["allow_prefix"] = False
if not is_route_alias:
model_map[normalized_model]["is_route_alias"] = False
for tab in tabs or []:
route_domain = _resolve_route_domain(tab)
exposed_model_id = _get_exposed_model_id(tab)
if not exposed_model_id:
continue
overridden = _is_model_name_overridden(tab)
_ensure_model(
exposed_model_id,
route_domain,
allow_prefix=not overridden,
is_route_alias=not overridden,
)
if overridden or not route_domain:
continue
exposed_key = _normalize_model_id(exposed_model_id)
for alias_id in _build_model_id_candidates(route_domain):
alias_key = _normalize_model_id(alias_id)
if not alias_key or alias_key == exposed_key:
continue
existing = alias_map.get(alias_key)
if existing and existing != exposed_key:
ambiguous_aliases.add(alias_key)
continue
alias_map[alias_key] = exposed_key
for alias_key in sorted(alias_map):
if alias_key in ambiguous_aliases or alias_key in model_map:
continue
target_key = alias_map[alias_key]
target = model_map.get(target_key)
if not target:
continue
model_map[alias_key] = {
"id": alias_key,
"display_name": alias_key,
"model_name": target["model_name"],
"route_type": "model_name",
"route_domains": set(target.get("route_domains") or set()),
"allow_prefix": True,
"is_route_alias": True,
}
result: List[Dict[str, Any]] = []
for model_key in sorted(model_map):
item = model_map[model_key]
route_domains = sorted(item.get("route_domains") or [])
result.append({
"id": item["id"],
"display_name": item.get("display_name") or item["id"],
"model_name": item.get("model_name") or item["id"],
"route_type": item.get("route_type") or "model_name",
"route_domain": route_domains[0] if len(route_domains) == 1 else "",
"route_domains": route_domains,
"allow_prefix": bool(item.get("allow_prefix", True)),
"is_route_alias": bool(item.get("is_route_alias", False)),
})
return result
def inspect_model_route(
model: Any,
tabs: List[Dict[str, Any]],
*,
allow_prefix: bool = True,
allow_generic: bool = True,
models: Optional[List[Dict[str, Any]]] = None,
) -> Dict[str, Any]:
"""
Return detailed model-route resolution information for logging/debugging.
"""
normalized_model = _normalize_model_id(model)
if models is None:
models = collect_route_domain_models(tabs)
available_model_ids = [str(item.get("id") or "") for item in models]
info: Dict[str, Any] = {
"normalized_model": normalized_model,
"route_domain": "",
"route_type": "",
"model_name": "",
"matched_id": "",
"match_type": "none",
"available_model_ids": available_model_ids,
}
# 优先级 1:先做标签页完全精确匹配并直接返回,避免暴露名恰为通用 id(如 gpt-4o)的
# 标签页被 generic 判定抢先、永远无法被精确路由
for item in models:
if normalized_model == _normalize_model_id(item["id"]):
info["route_domain"] = str(item.get("route_domain") or "")
info["route_type"] = str(item.get("route_type") or "model_name")
info["model_name"] = str(item.get("model_name") or item.get("id") or "")
info["matched_id"] = str(item.get("id") or "")
info["match_type"] = "exact"
return info
# 优先级 2:通用模型 id 落回 generic 默认分配流程(若启用 allow_generic)
if allow_generic and normalized_model in _GENERIC_MODEL_IDS:
info["match_type"] = "generic"
return info
if not allow_prefix:
return info
# 优先级 3:标签页前缀模糊匹配(按最长前缀优先匹配,避免短前缀遮蔽更具体的长前缀别名)
prefix_matches = [
item
for item in models
if item.get("allow_prefix") and _matches_model_alias(normalized_model, _normalize_model_id(item["id"]))
]
if prefix_matches:
best_match = max(prefix_matches, key=lambda x: len(str(x.get("id") or "")))
info["route_domain"] = str(best_match.get("route_domain") or "")
info["route_type"] = str(best_match.get("route_type") or "model_name")
info["model_name"] = str(best_match.get("model_name") or best_match.get("id") or "")
info["matched_id"] = str(best_match.get("id") or "")
info["match_type"] = "prefix"
return info
return info
def resolve_model_route_domain(model: Any, tabs: List[Dict[str, Any]]) -> str:
"""
Resolve a client-supplied `model` to a route-domain target.
Returns an empty string when the model should continue using the default
generic tab allocation flow.
"""
return str(inspect_model_route(model, tabs).get("route_domain") or "")
__all__ = [
"collect_route_domain_models",
"inspect_model_route",
"resolve_model_route_domain",
]
+206
View File
@@ -0,0 +1,206 @@
"""
app/utils/paste.py - 通用粘贴工具
解决问题:
- React/Vue 受控输入框直接改 value 不触发状态更新
- contenteditable 编辑器需要模拟用户输入
- 超长文本需要分块避免卡顿/被截断
"""
import time
import random
import json
from typing import Optional
from app.core.config import get_logger
from app.core.driver import driver_for_tab
logger = get_logger("PASTE")
# 通用 JS 函数(挂到 window 上避免作用域问题)
UNIVERSAL_INSERT_JS = r"""
window.universalAppend = function (element, text) {
if (!element) return false;
// 1) 聚焦(尽量不滚动)
try { element.focus({preventScroll: true}); } catch(e) { element.focus(); }
const isInput = (element.tagName === 'TEXTAREA' || element.tagName === 'INPUT');
// 2) input/textarea:用 setRangeText 最稳
if (isInput) {
const len = element.value.length;
element.setSelectionRange(len, len);
// setRangeText 会更新 value,并能更像"真实编辑"
if (typeof element.setRangeText === 'function') {
element.setRangeText(text, len, len, 'end');
} else {
element.value += text;
}
// 触发事件(React 受控输入常吃这个)
try {
element.dispatchEvent(new InputEvent('input', {bubbles: true, data: text, inputType: 'insertText'}));
} catch(e) {
element.dispatchEvent(new Event('input', {bubbles: true}));
}
return true;
}
// 3) contenteditable:先把光标折到末尾
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(element);
range.collapse(false);
selection.removeAllRanges();
selection.addRange(range);
// 4) 优先 execCommand(很多富文本编辑器对它兼容最好)
let ok = false;
try { ok = document.execCommand('insertText', false, text); } catch(e) {}
// 5) 兜底:直接插入 TextNode + 触发 input
if (!ok) {
range.insertNode(document.createTextNode(text));
range.collapse(false);
selection.removeAllRanges();
selection.addRange(range);
try {
element.dispatchEvent(new InputEvent('input', {bubbles: true, data: text, inputType: 'insertText'}));
} catch(e) {
element.dispatchEvent(new Event('input', {bubbles: true}));
}
}
return true;
};
window.getElementTextLength = function(element) {
if (!element) return 0;
if (element.tagName === 'TEXTAREA' || element.tagName === 'INPUT') {
return (element.value || '').length;
}
return (element.innerText || element.textContent || '').length;
};
"""
def safe_universal_paste(page, selector: str, text_content: str,
chunk_size: int = 2000,
min_delay: float = 0.3,
max_delay: float = 0.8,
max_retries: int = 2) -> bool:
"""
通用粘贴函数:无论目标是 textarea 还是 contenteditable div,都能正确追加文本
:param page: DrissionPage 对象
:param selector: 输入框的选择器
:param text_content: 要发送的完整文本
:param chunk_size: 分块大小(字符数)
:param min_delay: 每块之间的最小延时(秒)
:param max_delay: 每块之间的最大延时(秒)
:param max_retries: 每块写入失败时的最大重试次数
:return: 是否成功
"""
if not text_content:
return True
# 注入通用 JS 函数
try:
driver_for_tab(page).run_js(UNIVERSAL_INSERT_JS)
except Exception as e:
logger.error(f"注入 JS 失败: {e}")
return False
# 获取元素对象
ele = driver_for_tab(page).find(selector)
if not ele:
logger.warning(f"找不到元素: {selector}")
return False
# 分块逻辑
total_length = len(text_content)
chunks = [text_content[i:i+chunk_size] for i in range(0, total_length, chunk_size)]
logger.debug(f"开始粘贴,共 {len(chunks)} 块,总长度 {total_length}")
total_sent = 0
for i, chunk in enumerate(chunks):
success = False
for retry in range(max_retries + 1):
try:
# 获取写入前的长度
before_len = driver_for_tab(page).run_js("return window.getElementTextLength(arguments[0])", ele)
before_len = before_len or 0
# 安全转义文本,防止 JS 报错
safe_chunk = json.dumps(chunk)
# 调用 JS 写入
result = driver_for_tab(page).run_js(f"return window.universalAppend(arguments[0], {safe_chunk})", ele)
# 短暂等待让页面更新
time.sleep(0.05)
# 获取写入后的长度
after_len = driver_for_tab(page).run_js("return window.getElementTextLength(arguments[0])", ele)
after_len = after_len or 0
# 验证是否成功写入
expected_len = before_len + len(chunk)
actual_increase = after_len - before_len
# 允许小幅误差(可能有换行符转换等)
if actual_increase >= len(chunk) * 0.95:
success = True
total_sent += len(chunk)
logger.debug(f"第 {i+1}/{len(chunks)} 块成功,+{actual_increase} 字符")
break
else:
logger.warning(f"第 {i+1} 块校验失败 (预期+{len(chunk)},实际+{actual_increase}),重试 {retry+1}/{max_retries}")
except Exception as e:
logger.warning(f"第 {i+1} 块写入异常: {e},重试 {retry+1}/{max_retries}")
if not success:
logger.error(f"第 {i+1} 块最终失败,已写入 {total_sent}/{total_length} 字符")
return False
# 随机延时:模拟人类分段复制粘贴的思考时间
if i < len(chunks) - 1: # 最后一块不需要延时
time.sleep(random.uniform(min_delay, max_delay))
logger.debug(f"粘贴完成,共 {total_sent} 字符")
return True
def clear_and_paste(page, selector: str, text_content: str, **kwargs) -> bool:
"""
清空输入框后粘贴(用于替换内容而非追加)
"""
ele = driver_for_tab(page).find(selector)
if not ele:
logger.warning(f"找不到元素: {selector}")
return False
# 清空内容
try:
driver_for_tab(page).run_js("""
const ele = arguments[0];
if (ele.tagName === 'TEXTAREA' || ele.tagName === 'INPUT') {
ele.value = '';
} else {
ele.innerHTML = '';
}
ele.dispatchEvent(new Event('input', {bubbles: true}));
""", ele)
time.sleep(0.1)
except Exception as e:
logger.warning(f"清空失败: {e}")
return safe_universal_paste(page, selector, text_content, **kwargs)
__all__ = ['safe_universal_paste', 'clear_and_paste', 'UNIVERSAL_INSERT_JS']
+41
View File
@@ -0,0 +1,41 @@
"""
app/utils/platform.py - 轻量平台工具
职责:
- 统一判断当前运行平台
- 提供跨平台快捷键抽象
"""
from __future__ import annotations
import sys
def is_windows() -> bool:
return sys.platform.startswith("win")
def is_macos() -> bool:
return sys.platform == "darwin"
def is_linux() -> bool:
return sys.platform.startswith("linux")
def get_primary_modifier_key() -> str:
"""
返回系统主修饰键。
- Windows / Linux: Control
- macOS: Meta(对应 Command)
"""
return "Meta" if is_macos() else "Control"
__all__ = [
"get_primary_modifier_key",
"is_linux",
"is_macos",
"is_windows",
]
+343
View File
@@ -0,0 +1,343 @@
"""Security helpers for fetching user-controlled remote resources."""
from __future__ import annotations
import contextlib
import ipaddress
import socket
import threading
from typing import Any, Dict, Iterable, Iterator, Optional, Tuple
from urllib.parse import urljoin, urlsplit, urlunsplit
import requests
ALLOWED_REMOTE_SCHEMES = frozenset({"http", "https"})
MAX_REMOTE_REDIRECTS = 4
_PROXY_FAKE_IP_NETWORKS = (
ipaddress.ip_network("198.18.0.0/15"), # IPv4 Fake-IP (RFC 2544 benchmark, Clash/Mihomo/Sing-box)
ipaddress.ip_network("2001:2::/48"), # IPv6 Fake-IP (RFC 5180 benchmark, Clash/Mihomo/Sing-box standard)
ipaddress.ip_network("fc00::/7"), # IPv6 ULA (RFC 4193, legacy/custom Fake-IP e.g. fdfe::/64)
)
_PROXY_FAKE_IP_HTTPS_SUFFIXES = (
# Google & Gemini CDN / Media
"googleusercontent.com",
"usercontent.google.com",
"gstatic.com",
"googleapis.com",
"google.com",
# Cloudflare R2 & CDN
"r2.cloudflarestorage.com",
"cloudflare.com",
"cloudflareinsights.com",
# GitHub CDN
"githubusercontent.com",
"raw.githubusercontent.com",
"github.com",
# OpenAI CDN
"oaistatic.com",
"oaiusercontent.com",
"openai.com",
# Anthropic & Claude CDN
"anthropic.com",
"claude.ai",
# Microsoft & Bing CDN
"bing.com",
"msn.com",
"microsoft.com",
"azureedge.net",
"windows.net",
# Other mainstream AI & media hosting CDNs
"deepseek.com",
"grok.com",
"x.ai",
"together.ai",
"together.xyz",
"replicate.delivery",
)
class UnsafeRemoteResourceError(ValueError):
"""Raised when a remote URL can reach a non-public network address."""
def normalize_remote_http_url(value: Any) -> str:
raw = str(value or "").strip()
if not raw:
return ""
try:
parsed = urlsplit(raw)
except Exception:
return ""
scheme = str(parsed.scheme or "").lower()
hostname = str(parsed.hostname or "").strip().lower().rstrip(".")
if scheme not in ALLOWED_REMOTE_SCHEMES or not hostname:
return ""
if parsed.username is not None or parsed.password is not None:
return ""
try:
port = parsed.port
except ValueError:
return ""
host_for_netloc = f"[{hostname}]" if ":" in hostname else hostname
netloc = f"{host_for_netloc}:{port}" if port is not None else host_for_netloc
return urlunsplit((scheme, netloc, parsed.path or "/", parsed.query, ""))
def remote_url_origin(value: Any) -> str:
normalized = normalize_remote_http_url(value)
if not normalized:
return ""
parsed = urlsplit(normalized)
default_port = 80 if parsed.scheme == "http" else 443
port = parsed.port or default_port
host = str(parsed.hostname or "").lower().rstrip(".")
host_for_netloc = f"[{host}]" if ":" in host else host
return f"{parsed.scheme}://{host_for_netloc}:{port}"
def is_same_remote_origin(left: Any, right: Any) -> bool:
left_origin = remote_url_origin(left)
return bool(left_origin and left_origin == remote_url_origin(right))
def _is_public_ip(address: str) -> bool:
try:
ip = ipaddress.ip_address(str(address or "").split("%", 1)[0])
except ValueError:
return False
return bool(ip.is_global)
def _resolve_addresses(hostname: str) -> tuple[str, ...]:
try:
results = socket.getaddrinfo(hostname, None, type=socket.SOCK_STREAM)
except OSError as exc:
raise UnsafeRemoteResourceError(f"remote_dns_failed:{hostname}") from exc
addresses = tuple(sorted({str(item[4][0]) for item in results if item and item[4]}))
if not addresses:
raise UnsafeRemoteResourceError(f"remote_dns_empty:{hostname}")
return addresses
def _is_proxy_fake_ip(address: str) -> bool:
try:
ip = ipaddress.ip_address(str(address or "").split("%", 1)[0])
except ValueError:
return False
return any(ip in network for network in _PROXY_FAKE_IP_NETWORKS)
def _allows_proxy_fake_ip(hostname: str, scheme: str, addresses: Iterable[str]) -> bool:
host = str(hostname or "").strip().lower().rstrip(".")
if scheme != "https" or not any(
host == suffix or host.endswith(f".{suffix}")
for suffix in _PROXY_FAKE_IP_HTTPS_SUFFIXES
):
return False
resolved = tuple(addresses)
return bool(resolved) and all(
_is_public_ip(address) or _is_proxy_fake_ip(address)
for address in resolved
)
def resolve_public_addresses(hostname: str) -> tuple[str, ...]:
host = str(hostname or "").strip().lower().rstrip(".")
if not host:
raise UnsafeRemoteResourceError("remote_host_missing")
try:
literal = ipaddress.ip_address(host.split("%", 1)[0])
except ValueError:
literal = None
if literal is not None:
if not literal.is_global:
raise UnsafeRemoteResourceError(f"remote_address_not_public:{literal}")
return (str(literal),)
addresses = _resolve_addresses(host)
for address in addresses:
if not _is_public_ip(address):
raise UnsafeRemoteResourceError(f"remote_address_not_public:{address}")
return addresses
def validate_public_remote_url(value: Any) -> str:
normalized = normalize_remote_http_url(value)
if not normalized:
raise UnsafeRemoteResourceError("remote_url_invalid")
parsed = urlsplit(normalized)
resolve_public_addresses(str(parsed.hostname or ""))
return normalized
def _validate_fetch_target(value: Any) -> Tuple[str, Tuple[str, ...]]:
"""Validate a fetch target and return (normalized_url, checked_addresses)."""
normalized = normalize_remote_http_url(value)
if not normalized:
raise UnsafeRemoteResourceError("remote_url_invalid")
parsed = urlsplit(normalized)
host = str(parsed.hostname or "")
try:
return normalized, resolve_public_addresses(host)
except UnsafeRemoteResourceError:
addresses = _resolve_addresses(host)
if _allows_proxy_fake_ip(host, parsed.scheme, addresses):
return normalized, tuple(addresses)
blocked = next(
(address for address in addresses if not _is_public_ip(address)),
addresses[0],
)
raise UnsafeRemoteResourceError(f"remote_address_not_public:{blocked}")
def _validate_fetch_remote_url(value: Any) -> str:
"""Validate a fetch target, accounting for HTTPS CDN names behind TUN fake DNS."""
return _validate_fetch_target(value)[0]
# ---------------------------------------------------------------------------
# S11:DNS 固定(pinning)
#
# 校验阶段解析出的地址必须就是实际连接的地址,否则攻击者控制的 DNS 可以在「校验」与
# 「连接」之间把答案换成内网地址(DNS rebinding)。做法:在 urllib3 建立 TCP 连接的
# 唯一入口 `urllib3.util.connection.create_connection` 外面包一层,只在
# get_public_remote_resource 调用期间、且只对本线程登记过的主机名生效——连接直接打到
# 已校验的 IP,而 HTTPConnection.host 保持原主机名,所以 TLS SNI / 证书校验 / Host 头都不变。
# 未登记的主机名(包括走 HTTP(S)_PROXY 时的代理主机)原样放行:代理场景由可信出网代理解析目标。
# ---------------------------------------------------------------------------
_pin_state = threading.local()
_pin_install_lock = threading.Lock()
_pin_installed = False
def _current_pins() -> Dict[str, Tuple[str, ...]]:
pins = getattr(_pin_state, "pins", None)
if pins is None:
pins = {}
_pin_state.pins = pins
return pins
def _install_pinned_create_connection() -> None:
global _pin_installed
if _pin_installed:
return
with _pin_install_lock:
if _pin_installed:
return
try:
from urllib3.util import connection as urllib3_connection
except Exception: # pragma: no cover - urllib3 is a hard dependency of requests
return
original = urllib3_connection.create_connection
def pinned_create_connection(address, *args, **kwargs):
host, port = address[0], address[1]
pinned = _current_pins().get(str(host or "").strip().lower().rstrip("."))
if not pinned:
return original(address, *args, **kwargs)
last_error: Optional[BaseException] = None
for ip in pinned:
try:
return original((ip, port), *args, **kwargs)
except OSError as exc:
last_error = exc
raise last_error or OSError(f"remote_pinned_connect_failed:{host}")
pinned_create_connection.__uwapi_pinned__ = True # type: ignore[attr-defined]
urllib3_connection.create_connection = pinned_create_connection
_pin_installed = True
@contextlib.contextmanager
def _pinned_dns(url: str, addresses: Iterable[str]) -> Iterator[None]:
host = str(urlsplit(url).hostname or "").strip().lower().rstrip(".")
resolved = tuple(str(item) for item in addresses if str(item or "").strip())
if not host or not resolved:
yield
return
_install_pinned_create_connection()
pins = _current_pins()
previous = pins.get(host)
pins[host] = resolved
try:
yield
finally:
if previous is None:
pins.pop(host, None)
else:
pins[host] = previous
def _headers_for_target(
headers: Optional[Dict[str, str]],
*,
target_url: str,
credential_origin: str,
) -> Dict[str, str]:
result = dict(headers or {})
if not credential_origin or remote_url_origin(target_url) != credential_origin:
sensitive = {"authorization", "cookie", "referer"}
result = {k: v for k, v in result.items() if str(k).strip().lower() not in sensitive}
return result
def get_public_remote_resource(
url: Any,
*,
headers: Optional[Dict[str, str]] = None,
cookies: Any = None,
credential_origin_url: Any = None,
timeout: Any = (8, 20),
stream: bool = True,
max_redirects: int = MAX_REMOTE_REDIRECTS,
):
"""GET a public URL, validating every redirect and scoping credentials."""
current, addresses = _validate_fetch_target(url)
credential_origin = remote_url_origin(credential_origin_url)
redirects_left = max(0, int(max_redirects))
while True:
same_origin = bool(credential_origin and remote_url_origin(current) == credential_origin)
# S11:连接固定到刚校验过的地址(连接在 requests.get 返回前已建立,stream 读取复用同一 socket)
with _pinned_dns(current, addresses):
response = requests.get(
current,
headers=_headers_for_target(
headers,
target_url=current,
credential_origin=credential_origin,
),
cookies=cookies if same_origin else None,
timeout=timeout,
allow_redirects=False,
stream=stream,
)
if response.status_code not in {301, 302, 303, 307, 308}:
return response
location = str(response.headers.get("Location") or "").strip()
response.close()
if not location:
raise UnsafeRemoteResourceError("remote_redirect_missing_location")
if redirects_left <= 0:
raise UnsafeRemoteResourceError("remote_redirect_limit")
redirects_left -= 1
current, addresses = _validate_fetch_target(urljoin(current, location))
__all__ = [
"UnsafeRemoteResourceError",
"get_public_remote_resource",
"is_same_remote_origin",
"normalize_remote_http_url",
"remote_url_origin",
"resolve_public_addresses",
"validate_public_remote_url",
]
+143
View File
@@ -0,0 +1,143 @@
"""
app/utils/similarity.py - 文本相似度计算工具
用于验证提取器提取结果的准确性
"""
import re
from difflib import SequenceMatcher
from typing import Tuple
def normalize_text(text: str) -> str:
"""
标准化文本(用于比对)
处理:
- 统一换行符
- 压缩连续空白
- 去除首尾空白
"""
if not text:
return ""
# 统一换行符
text = text.replace('\r\n', '\n').replace('\r', '\n')
# 压缩连续空白(保留单个换行)
text = re.sub(r'[ \t]+', ' ', text)
text = re.sub(r'\n{3,}', '\n\n', text)
# 去除首尾空白
return text.strip()
def calculate_similarity(text1: str, text2: str, normalize: bool = True) -> float:
"""
计算两段文本的相似度
Args:
text1: 第一段文本
text2: 第二段文本
normalize: 是否先标准化文本
Returns:
相似度 (0.0 - 1.0)
"""
if normalize:
text1 = normalize_text(text1)
text2 = normalize_text(text2)
if not text1 and not text2:
return 1.0
if not text1 or not text2:
return 0.0
# 使用 SequenceMatcher 计算相似度
matcher = SequenceMatcher(None, text1, text2)
return matcher.ratio()
def verify_extraction(extracted: str, expected: str, threshold: float = 0.95) -> Tuple[bool, float, str]:
"""
验证提取结果
Args:
extracted: 提取的文本
expected: 预期的文本
threshold: 通过阈值(默认 0.95)
Returns:
(是否通过, 相似度, 消息)
"""
similarity = calculate_similarity(extracted, expected)
passed = similarity >= threshold
if passed:
message = f"验证通过 (相似度: {similarity:.1%})"
else:
# 计算差异信息
extracted_len = len(normalize_text(extracted))
expected_len = len(normalize_text(expected))
len_diff = abs(extracted_len - expected_len)
message = (
f"验证未通过 (相似度: {similarity:.1%}, "
f"提取长度: {extracted_len}, 预期长度: {expected_len}, "
f"差异: {len_diff} 字符)"
)
return passed, similarity, message
def get_diff_summary(text1: str, text2: str, context_chars: int = 50) -> str:
"""
获取差异摘要
Args:
text1: 第一段文本
text2: 第二段文本
context_chars: 上下文字符数
Returns:
差异摘要字符串
"""
t1 = normalize_text(text1)
t2 = normalize_text(text2)
if t1 == t2:
return "完全相同"
# 找到第一个不同的位置
min_len = min(len(t1), len(t2))
first_diff = -1
for i in range(min_len):
if t1[i] != t2[i]:
first_diff = i
break
if first_diff == -1:
first_diff = min_len
# 获取差异上下文
start = max(0, first_diff - context_chars)
context1 = t1[start:first_diff + context_chars] if first_diff < len(t1) else t1[start:]
context2 = t2[start:first_diff + context_chars] if first_diff < len(t2) else t2[start:]
return (
f"首个差异位置: {first_diff}\n"
f"文本1 ({len(t1)} 字符): ...{repr(context1)}...\n"
f"文本2 ({len(t2)} 字符): ...{repr(context2)}..."
)
__all__ = [
'normalize_text',
'calculate_similarity',
'verify_extraction',
'get_diff_summary'
]
+72
View File
@@ -0,0 +1,72 @@
"""Conservative automatic site admission; explicit user configs are not restricted."""
from __future__ import annotations
import re
from typing import Any
from app.utils.site_rules import get_site_rule
from app.utils.site_url import extract_remote_site_domain, normalize_route_domain
# B1:搜索引擎的各国主域(google.de、google.com.br …)数量太多,不逐个写进 site_rules.json。
# 只做**整串精确匹配**,gemini.google.com / aistudio.google.com 等子域不受影响;
# google.com.attacker.org 这类后缀伪装也不会命中。site_rules.json 中显式的
# auto_discovery 值(含 true)优先于内置规则。
_BUILTIN_NON_CHAT_HOST_PATTERNS = (
re.compile(r'^google\.(?:com?\.)?[a-z]{2,3}$'),
re.compile(r'^(?:[a-z]{2}\.)?bing\.com$'),
re.compile(r'^yandex\.(?:com?\.)?[a-z]{2,3}$'),
)
def _builtin_non_chat_host(host: str) -> bool:
return any(pattern.fullmatch(host) for pattern in _BUILTIN_NON_CHAT_HOST_PATTERNS)
def automatic_discovery_allowed(domain: str) -> bool:
host = normalize_route_domain(domain)
if not host or extract_remote_site_domain('https://' + host) is None:
return False
# www is an alias, not a reason to broaden a deny rule to all subdomains.
# google.com is a search page; gemini.google.com is a separate application.
rule = get_site_rule(host)
lookup_host = host
if 'auto_discovery' not in rule and host.startswith('www.'):
rule = get_site_rule(host[4:])
lookup_host = host[4:]
if 'auto_discovery' in rule:
return rule['auto_discovery'] is not False
return not _builtin_non_chat_host(lookup_host)
def specific_chat_selectors(value: Any) -> bool:
if not isinstance(value, dict):
return False
if any(v is not None and not isinstance(v, str) for v in value.values()):
return False
for key in ('input_box', 'result_container'):
if not str(value.get(key) or '').strip():
return False
# Never promote fallback containers (body/div/*) to evidence of an AI reply.
for part in value['result_container'].lower().removeprefix('css:').split(','):
if re.fullmatch(r'(?:html|body|main|div|section|article|p|span|\*|#root|#app)', part.strip()):
return False
return True
def admitted_selectors(analysis: Any) -> dict | None:
"""An explicit boolean classification AND usable selectors are required."""
if not isinstance(analysis, dict) or analysis.get('is_chat_site') is not True:
return None
selectors = {k: v for k, v in analysis.items() if k != 'is_chat_site'}
return selectors if specific_chat_selectors(selectors) else None
def selectors_match_chat_html(selectors: Any, html: str) -> bool:
"""Both core selectors must actually match the analyzed snapshot, not be invented."""
if not specific_chat_selectors(selectors):
return False
from bs4 import BeautifulSoup
try:
document = BeautifulSoup(html, 'html.parser')
return all(document.select_one(selectors[key]) is not None
for key in ('input_box', 'result_container'))
except Exception:
return False
+184
View File
@@ -0,0 +1,184 @@
"""
Site-level rule loading shared by routing, startup pages, and defaults.
"""
from __future__ import annotations
import copy
import json
import os
from pathlib import Path
from typing import Any, Dict, List, Tuple
_PROJECT_ROOT = Path(__file__).resolve().parents[2]
_SITE_RULES_FILE = Path(
os.getenv("SITE_RULES_FILE", str(_PROJECT_ROOT / "config" / "site_rules.json"))
)
_SITES_LOCAL_FILE = Path(
os.getenv("SITES_LOCAL_FILE", str(_PROJECT_ROOT / "config" / "sites.local.json"))
)
_IGNORED_SITE_ID_PARTS = {
"",
"www",
"chat",
"web",
"app",
"api",
"beta",
"console",
"new",
}
_SITE_RULES_CACHE: Dict[str, Any] = {
"signature": None,
"rules": {},
}
def _normalize_domain(value: Any) -> str:
return str(value or "").strip().lower().strip(".")
def _normalize_rule(rule: Any) -> Dict[str, Any]:
if not isinstance(rule, dict):
return {}
normalized: Dict[str, Any] = {}
display_name = str(rule.get("display_name") or "").strip()
if display_name:
normalized["display_name"] = display_name
startup_url = str(rule.get("startup_url") or "").strip()
if startup_url:
normalized["startup_url"] = startup_url
card_id = str(rule.get("card_id") or "").strip()
if card_id:
normalized["card_id"] = card_id
guide_priority = rule.get("guide_priority", None)
if isinstance(guide_priority, (int, float)) and not isinstance(guide_priority, bool):
normalized["guide_priority"] = int(guide_priority)
if isinstance(rule.get("auto_discovery"), bool):
normalized["auto_discovery"] = rule["auto_discovery"]
if "stealth_default" in rule:
normalized["stealth_default"] = bool(rule.get("stealth_default"))
route_aliases = rule.get("route_aliases", [])
if isinstance(route_aliases, list):
aliases: List[str] = []
seen = set()
for raw_alias in route_aliases:
alias = _normalize_domain(raw_alias)
if not alias or alias in seen:
continue
seen.add(alias)
aliases.append(alias)
normalized["route_aliases"] = aliases
return normalized
def _read_json_file(path: Path) -> Dict[str, Any]:
if not path.exists():
return {}
try:
with open(path, "r", encoding="utf-8-sig") as handle:
data = json.load(handle)
return data if isinstance(data, dict) else {}
except Exception:
return {}
def _build_cache_signature() -> Tuple[Tuple[str, float], ...]:
signature: List[Tuple[str, float]] = []
for path in (_SITE_RULES_FILE, _SITES_LOCAL_FILE):
try:
mtime = path.stat().st_mtime if path.exists() else 0.0
except Exception:
mtime = 0.0
signature.append((str(path), float(mtime)))
return tuple(signature)
def _load_rules_from_files() -> Dict[str, Dict[str, Any]]:
shipped = _read_json_file(_SITE_RULES_FILE).get("sites", {})
local = _read_json_file(_SITES_LOCAL_FILE).get("site_overrides", {})
merged: Dict[str, Dict[str, Any]] = {}
for source in (shipped, local):
if not isinstance(source, dict):
continue
for raw_domain, raw_rule in source.items():
domain = _normalize_domain(raw_domain)
if not domain:
continue
current = merged.setdefault(domain, {})
current.update(_normalize_rule(raw_rule))
return merged
def load_site_rules(force: bool = False) -> Dict[str, Dict[str, Any]]:
signature = _build_cache_signature()
if not force and _SITE_RULES_CACHE.get("signature") == signature:
return copy.deepcopy(_SITE_RULES_CACHE.get("rules", {}))
rules = _load_rules_from_files()
_SITE_RULES_CACHE["signature"] = signature
_SITE_RULES_CACHE["rules"] = rules
return copy.deepcopy(rules)
def get_site_rule(domain: str) -> Dict[str, Any]:
normalized = _normalize_domain(domain)
if not normalized:
return {}
return load_site_rules().get(normalized, {})
def build_route_alias_groups() -> Tuple[Tuple[str, ...], ...]:
groups: List[Tuple[str, ...]] = []
for domain, rule in load_site_rules().items():
ordered: List[str] = []
seen = set()
for item in list(rule.get("route_aliases", []) or []) + [domain]:
alias = _normalize_domain(item)
if not alias or alias in seen:
continue
seen.add(alias)
ordered.append(alias)
if len(ordered) >= 2:
groups.append(tuple(ordered))
return tuple(groups)
def derive_site_card_id(domain: str) -> str:
normalized = _normalize_domain(domain)
if not normalized:
return ""
rule = get_site_rule(normalized)
explicit = str(rule.get("card_id") or "").strip()
if explicit:
return explicit
parts = [part for part in normalized.split(".")[:-1] if part not in _IGNORED_SITE_ID_PARTS]
if parts:
return parts[0]
return normalized.split(".", 1)[0]
__all__ = [
"build_route_alias_groups",
"derive_site_card_id",
"get_site_rule",
"load_site_rules",
]
+256
View File
@@ -0,0 +1,256 @@
"""
site_url.py - URL helpers for identifying real remote sites and route domains.
"""
from __future__ import annotations
import ipaddress
import hashlib
from typing import Optional, List
from urllib.parse import urlparse, urlunparse
from app.utils.site_rules import build_route_alias_groups, load_site_rules
_LOCAL_HOSTS = {"localhost", "127.0.0.1", "::1", "0.0.0.0"}
_LOCAL_SUFFIXES = (
".local",
".lan",
".internal",
".localhost",
".test",
".example",
".invalid",
".home.arpa",
)
def extract_remote_site_domain(url: str) -> Optional[str]:
"""Return the hostname for a real remote website, otherwise None."""
raw = str(url or "").strip()
if not raw:
return None
try:
parsed = urlparse(raw)
except Exception:
return None
if parsed.scheme not in {"http", "https"}:
return None
hostname = (parsed.hostname or "").strip().lower().rstrip(".")
if not hostname:
return None
if hostname in _LOCAL_HOSTS or hostname.endswith(_LOCAL_SUFFIXES):
return None
try:
ip = ipaddress.ip_address(hostname)
if (
ip.is_loopback
or ip.is_private
or ip.is_link_local
or ip.is_reserved
or ip.is_unspecified
or ip.is_multicast
):
return None
return hostname
except ValueError:
pass
if "." not in hostname:
return None
return hostname
def is_remote_site_url(url: str) -> bool:
return extract_remote_site_domain(url) is not None
def normalize_route_domain(value: str) -> str:
"""Normalize a route-domain-like input into a lowercase hostname."""
raw = str(value or "").strip().lower()
if not raw:
return ""
candidate = raw
if "://" not in candidate:
candidate = f"https://{candidate.lstrip('/')}"
try:
parsed = urlparse(candidate)
hostname = (parsed.hostname or "").strip().lower().rstrip(".")
if hostname:
return hostname
except Exception:
pass
return raw.strip().strip("/").split("/", 1)[0].strip().strip(".")
def build_route_domain_aliases(value: str) -> List[str]:
"""Build compatible route-domain aliases for a hostname-like value."""
normalized = normalize_route_domain(value)
if not normalized:
return []
aliases: List[str] = []
seen = set()
def _add(item: str):
host = normalize_route_domain(item)
if host and host not in seen:
seen.add(host)
aliases.append(host)
_add(normalized)
if normalized.startswith("www."):
_add(normalized[4:])
else:
_add(f"www.{normalized}")
for group in build_route_alias_groups():
if normalized in group:
for alias in group:
_add(alias)
return aliases
def get_preferred_route_domain(value: str) -> str:
"""Return the preferred public route-domain alias for a site."""
normalized = normalize_route_domain(value)
if not normalized:
return ""
for group in build_route_alias_groups():
if normalized in group:
return group[0]
if normalized.startswith("www."):
return normalized[4:]
return normalized
def get_canonical_route_domain(value: str) -> str:
"""Return the configured site domain for aliases used in routing."""
normalized = normalize_route_domain(value)
if not normalized:
return ""
rules = load_site_rules()
if normalized in rules:
return normalized
if normalized.startswith("www.") and normalized[4:] in rules:
return normalized[4:]
for site_domain, rule in rules.items():
aliases = list(rule.get("route_aliases", []) or [])
for alias in aliases:
normalized_alias = normalize_route_domain(alias)
if not normalized_alias:
continue
if normalized == normalized_alias:
return site_domain
if normalized.startswith("www.") and normalized[4:] == normalized_alias:
return site_domain
if normalized.startswith("www."):
return normalized[4:]
return normalized
def route_domain_matches(target_domain: str, actual_domain: str) -> bool:
"""Whether the route-domain target can represent the actual page domain."""
target_aliases = build_route_domain_aliases(target_domain)
actual_aliases = build_route_domain_aliases(actual_domain)
if not target_aliases or not actual_aliases:
return False
for target in target_aliases:
for actual in actual_aliases:
if actual == target:
return True
if actual.endswith(f".{target}") or target.endswith(f".{actual}"):
return True
return False
def normalize_exact_tab_url(value: str) -> str:
"""Normalize a full tab URL for strict route matching."""
raw = str(value or "").strip()
if not raw:
return ""
try:
parsed = urlparse(raw)
except Exception:
return raw
scheme = str(parsed.scheme or "").strip().lower()
hostname = str(parsed.hostname or "").strip().lower().rstrip(".")
if not scheme or not hostname:
return raw
default_port = 80 if scheme == "http" else 443 if scheme == "https" else None
try:
port = parsed.port
except ValueError:
return raw
host_for_netloc = f"[{hostname}]" if ":" in hostname else hostname
netloc = host_for_netloc
if port and port != default_port:
netloc = f"{host_for_netloc}:{port}"
path = parsed.path or "/"
return urlunparse((
scheme,
netloc,
path,
parsed.params,
parsed.query,
parsed.fragment,
))
def tab_url_matches(target_url: str, actual_url: str) -> bool:
"""Whether two tab URLs match after strict normalization."""
target = normalize_exact_tab_url(target_url)
actual = normalize_exact_tab_url(actual_url)
return bool(target and actual and target == actual)
def encode_tab_url_route_token(value: str) -> str:
"""Encode a normalized tab URL into a short stable route token."""
normalized = normalize_exact_tab_url(value)
if not normalized:
return ""
digest = hashlib.sha1(normalized.encode("utf-8")).hexdigest()
return digest[:12]
def decode_tab_url_route_token(token: str) -> str:
"""Short route tokens are one-way hashes; keep API shape for compatibility."""
return str(token or "").strip().lower()
__all__ = [
"build_route_domain_aliases",
"decode_tab_url_route_token",
"encode_tab_url_route_token",
"extract_remote_site_domain",
"get_canonical_route_domain",
"get_preferred_route_domain",
"is_remote_site_url",
"normalize_route_domain",
"normalize_exact_tab_url",
"route_domain_matches",
"tab_url_matches",
]
+110
View File
@@ -0,0 +1,110 @@
"""
app/utils/system_clipboard.py - 平台原生剪贴板适配
职责:
- 保留 Windows 上的原生文件/图片剪贴板能力
- 为非 Windows 平台提供显式不可用信号,让上层走网页原生上传回退
"""
from __future__ import annotations
import io
import os
import struct
from app.utils.platform import is_windows
class ClipboardUnsupportedError(RuntimeError):
"""当前平台不支持该原生剪贴板能力。"""
class ClipboardDependencyError(RuntimeError):
"""缺少平台剪贴板依赖。"""
def supports_native_file_clipboard() -> bool:
return is_windows()
def supports_native_image_clipboard() -> bool:
return is_windows()
def copy_file_to_native_clipboard(filepath: str) -> None:
"""
使用系统原生文件剪贴板复制文件。
目前仅支持 Windows 的 CF_HDROP。
"""
if not supports_native_file_clipboard():
raise ClipboardUnsupportedError("native file clipboard is only supported on Windows")
try:
import win32clipboard
except ImportError as exc:
raise ClipboardDependencyError("pywin32 is required for the Windows file clipboard backend") from exc
abs_path = os.path.abspath(filepath)
if not os.path.exists(abs_path):
raise FileNotFoundError(abs_path)
file_list = abs_path + "\0"
file_list += "\0"
file_list_bytes = file_list.encode("utf-16-le")
header = struct.pack("IIIii", 20, 0, 0, 0, 1)
data = header + file_list_bytes
cf_hdrop = 15
win32clipboard.OpenClipboard()
try:
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(cf_hdrop, data)
finally:
win32clipboard.CloseClipboard()
def copy_image_to_native_clipboard(image_path: str) -> None:
"""
使用系统原生图片剪贴板复制图片。
目前仅支持 Windows 的 CF_DIB。
"""
if not supports_native_image_clipboard():
raise ClipboardUnsupportedError("native image clipboard is only supported on Windows")
try:
import win32clipboard
except ImportError as exc:
raise ClipboardDependencyError("pywin32 is required for the Windows image clipboard backend") from exc
from PIL import Image
with Image.open(image_path) as image:
with io.BytesIO() as output:
if image.mode != "RGB":
converted = image.convert("RGB")
try:
converted.save(output, "BMP")
finally:
converted.close()
else:
image.save(output, "BMP")
data = output.getvalue()[14:]
win32clipboard.OpenClipboard()
try:
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
finally:
win32clipboard.CloseClipboard()
__all__ = [
"ClipboardDependencyError",
"ClipboardUnsupportedError",
"copy_file_to_native_clipboard",
"copy_image_to_native_clipboard",
"supports_native_file_clipboard",
"supports_native_image_clipboard",
]
+106
View File
@@ -0,0 +1,106 @@
import re
from typing import Any, Dict, List
from app.utils.site_url import (
encode_tab_url_route_token,
normalize_exact_tab_url,
normalize_route_domain,
)
ROUTE_GROUP_ID_PATTERN = re.compile(r"^[a-z0-9][a-z0-9._-]{0,63}$")
ROUTE_GROUP_ALLOCATION_MODES = {"first_idle", "round_robin", "random"}
def normalize_route_group_id(value: Any) -> str:
group_id = str(value or "").strip().lower()
return group_id if ROUTE_GROUP_ID_PATTERN.fullmatch(group_id) else ""
def normalize_route_group_member(value: Any) -> Dict[str, Any]:
payload = {"url": value} if isinstance(value, str) else value
if not isinstance(payload, dict):
return {}
url = normalize_exact_tab_url(str(payload.get("url") or "").strip())
url_token = str(payload.get("url_token") or "").strip().lower()
if url and not url_token:
url_token = encode_tab_url_route_token(url)
if not url and not url_token:
return {}
try:
tab_index = int(payload.get("tab_index") or 0)
except (TypeError, ValueError):
tab_index = 0
member = {
"url": url,
"url_token": url_token,
}
if tab_index > 0:
member["tab_index"] = tab_index
return member
def route_group_member_key(member: Dict[str, Any]) -> str:
token = str(member.get("url_token") or "").strip().lower()
url = normalize_exact_tab_url(str(member.get("url") or "").strip())
try:
tab_index = int(member.get("tab_index") or 0)
except (TypeError, ValueError):
tab_index = 0
return f"{token or url}::#{tab_index if tab_index > 0 else '*'}"
def normalize_route_groups(value: Any) -> List[Dict[str, Any]]:
if isinstance(value, dict):
source = []
for key, item in value.items():
payload = dict(item) if isinstance(item, dict) else {"members": item}
payload.setdefault("id", key)
source.append(payload)
elif isinstance(value, list):
source = value
else:
source = []
normalized: List[Dict[str, Any]] = []
seen_groups = set()
for item in source:
if not isinstance(item, dict):
continue
group_id = normalize_route_group_id(item.get("id") or item.get("group_id"))
if not group_id or group_id in seen_groups:
continue
members: List[Dict[str, Any]] = []
seen_members = set()
for raw_member in item.get("members") or []:
member = normalize_route_group_member(raw_member)
member_key = route_group_member_key(member) if member else ""
if not member_key or member_key in seen_members:
continue
seen_members.add(member_key)
members.append(member)
allocation_mode = str(item.get("allocation_mode") or "round_robin").strip().lower()
if allocation_mode not in ROUTE_GROUP_ALLOCATION_MODES:
allocation_mode = "round_robin"
route_domain = normalize_route_domain(item.get("route_domain"))
normalized.append({
"id": group_id,
"name": str(item.get("name") or group_id).strip()[:100] or group_id,
"route_domain": route_domain,
"preset_name": str(item.get("preset_name") or "").strip()[:100],
"allocation_mode": allocation_mode,
"members": members,
})
seen_groups.add(group_id)
return normalized
def route_groups_by_id(value: Any) -> Dict[str, Dict[str, Any]]:
return {item["id"]: item for item in normalize_route_groups(value)}