Files
it-tools-relay/server.py
T

5415 lines
253 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""it-tools relay: translate upstream + license check + card activation.
Endpoints
GET /translate?... -> proxy Google Translate via host proxy (CN workaround)
GET /check -> license gate for nginx auth_request
headers: X-Machine-Id (10-16 digits), X-Tool (protected tool key)
200 = allowed (IT-Tools project authorization OR trial window open)
403 = blocked
POST /activate -> card activation proxy (body: card_key, machine_id, contact_qq)
uses server-side CARD_AUTH_KEY for /public/verify pre-check, then
/public/authorizations/activate. The key never leaves this container.
Authorization rules
* Query CardAuth /public/authorizations/query?bot_qq=<machine> (60s cache).
* Only cards of the PROJECT_NAME project count as authorized (other projects'
cards must not unlock this site).
* Network failure -> fail OPEN (auth server down must not lock paying users).
Trial state persisted to /app/data/trials.json (mounted volume).
"""
import cgi
import io
import json
import mimetypes
import os
import re
import shutil
import subprocess
import tempfile
import threading
import time
import zipfile
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib import parse, request
import mcp_ops
GEN_IMG_LOCK = threading.Lock()
UPSTREAM = "https://translate.googleapis.com/translate_a/single"
PROXY = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy") or ""
TIMEOUT = float(os.environ.get("RELAY_TIMEOUT", "25"))
AUTH_BASE = os.environ.get("AUTH_BASE", "https://auth.88531.cn/api")
CARD_AUTH_KEY = os.environ.get("CARD_AUTH_KEY", "")
# Optional admin login: cards bound to a user require a logged-in session to activate
AUTH_USER = os.environ.get("AUTH_USER", "")
AUTH_PASS = os.environ.get("AUTH_PASS", "")
PROJECT_NAME = os.environ.get("PROJECT_NAME", "IT-Tools")
# CardAuth project id of THIS deployment. Universal cards carry no project of
# their own -> activation must name one; the backend rejects them without it.
PROJECT_ID = int(os.environ.get("PROJECT_ID", "6")) # 6 = IT-TOOLS-PRO
# Names that also count as "this project": CardAuth renamed the project
# (old activations store project_name "IT-Tools", new ones "IT-TOOLS-PRO").
PROJECT_ALIASES = {
n.strip()
for n in os.environ.get("PROJECT_ALIASES", "IT-Tools,IT-TOOLS-PRO").split(",")
if n.strip()
} or {PROJECT_NAME}
TRIAL_FILE = os.environ.get("TRIAL_FILE", "/app/data/trials.json")
TRIAL_MS = int(os.environ.get("TRIAL_MS_MIN", "30")) * 60 * 1000
# AI 生图页内置免费:未授权用户每日次数(北京时间 0 点重置);自带 KEY 不计次
DAILY_FREE_LIMIT = int(os.environ.get("DAILY_FREE_LIMIT", "10"))
DAILY_FILE = os.environ.get("DAILY_FILE", "/app/data/daily_gen.json")
# Cloudflare Workers AI 免费池(flux/flux-*/dall-e-3 等内置模型的上游):
# 官方免费 10,000 Neurons/天,proxy 固定 1024x1024/4 步 = 57.6 Neurons/张 → 10000/57.6 ≈ 173 张/天
# (北京时间约 08:00 重置;账号级共享,超了 Free plan 直接报错)
CF_DAILY_LIMIT = int(os.environ.get("CF_DAILY_LIMIT", "173"))
CF_DAILY_FILE = os.environ.get("CF_DAILY_FILE", "/app/data/daily_gen_cf.json")
DAY_MS = 24 * 60 * 60 * 1000
TZ_OFFSET_MS = 8 * 60 * 60 * 1000 # 北京时间 UTC+8 —— 重置对齐自然日 0 点
PROTECTED_TOOLS = {"translator", "image-ocr", "pdf-ocr", "ai-retouch", "doc-convert", "image-tools", "pdf-tools", "video-tools", "audio-tools", "text-to-image", "ai-image-tools", "copywriting", "screenshot-qa", "video-subtitle", "ai-gen", "ai-text-edit", "face-swap", "poster-maker"}
MACHINE_RE = re.compile(r"^\d{10,16}$")
# verify() messages that unconditionally block activation
BLOCK_WORDS = ("不存在", "已过期", "过期", "作废", "禁用", "停用", "已禁")
_lock = threading.Lock()
def _opener(with_proxy: bool):
handlers = []
if with_proxy and PROXY:
handlers.append(request.ProxyHandler({"http": PROXY, "https": PROXY}))
elif not with_proxy:
handlers.append(request.ProxyHandler({})) # force direct
return request.build_opener(*handlers)
OPENER_PROXY = _opener(True)
OPENER_DIRECT = _opener(False)
# ---- authorization cache: machine -> (authorized, fetched_at) ----
_auth_cache: dict = {}
AUTH_CACHE_TTL = 60
def _is_this_project(item: dict) -> bool:
"""True when a query/admin result belongs to THIS site's project.
Matches by project_id (new records) or by any historical project name
(CardAuth renamed IT-Tools -> IT-TOOLS-PRO; old rows keep the old name).
"""
try:
pid = item.get("project_id")
if pid is not None and int(pid) == PROJECT_ID:
return True
except (TypeError, ValueError):
pass
return item.get("project_name") in PROJECT_ALIASES
def _list_has_ittools_auth(data: dict) -> bool:
"""Only cards of PROJECT_NAME with active status count.
Falls back to has_valid_auth when the payload has no usable list
(keeps working if CardAuth response shape changes).
"""
lst = data.get("list")
if not isinstance(lst, list) or not lst:
# empty list -> no cards at all
if lst == []:
return False
return bool(data.get("has_valid_auth"))
matched = False
for item in lst:
if not isinstance(item, dict):
continue
name = item.get("project_name")
if name is None and item.get("project_id") is None:
# shape unknown -> do not filter
return bool(data.get("has_valid_auth"))
matched = True
if (
_is_this_project(item)
and item.get("status") == "active"
and not item.get("is_expired", False)
):
return True
if matched:
return False
return bool(data.get("has_valid_auth"))
def query_authorized(machine: str) -> bool:
now = time.time()
hit = _auth_cache.get(machine)
if hit and now - hit[1] < AUTH_CACHE_TTL:
return hit[0]
ok = False
try:
url = f"{AUTH_BASE}/public/authorizations/query?bot_qq={machine}"
with OPENER_DIRECT.open(url, timeout=10) as resp:
data = json.loads(resp.read().decode("utf-8"))
if data.get("code") == 200:
ok = _list_has_ittools_auth(data.get("data") or {})
except Exception as exc: # noqa: BLE001
print(f"[check] auth query failed ({exc}) -> fail open", flush=True)
_auth_cache.pop(machine, None)
return True # fail open
_auth_cache[machine] = (ok, now)
return ok
# ---- trial persistence ----
def _load_trials() -> dict:
try:
with open(TRIAL_FILE, "r", encoding="utf-8") as fh:
return json.load(fh)
except Exception: # noqa: BLE001
return {}
def _save_trials(trials: dict) -> None:
os.makedirs(os.path.dirname(TRIAL_FILE), exist_ok=True)
tmp = TRIAL_FILE + ".tmp"
with open(tmp, "w", encoding="utf-8") as fh:
json.dump(trials, fh)
os.replace(tmp, TRIAL_FILE)
def _today_start(now_ms: int) -> int:
"""当天 00:00(北京时间)对应的时间戳;epoch 计算,与时区环境变量无关"""
return (now_ms + TZ_OFFSET_MS) // DAY_MS * DAY_MS - TZ_OFFSET_MS
def trial_allowed(machine: str, tool: str) -> bool:
key = f"{machine}|{tool}"
now_ms = int(time.time() * 1000)
with _lock:
trials = _load_trials()
start = trials.get(key)
today = _today_start(now_ms)
# 首次进入,或已跨到新的一天 -> 立刻开新一天的额度
# (额度只在用户当天真的进来时才起算,不会从 0 点空耗)
if start is None or start < today:
trials[key] = now_ms
_save_trials(trials)
return True
return (now_ms - start) < TRIAL_MS
# ---------- AI 生图页:每日免费次数(未授权;北京时间 0 点重置,自带 KEY 不计次) ----------
_DAILY_LOCK = threading.Lock()
def _load_daily() -> dict:
try:
with open(DAILY_FILE, encoding="utf-8") as f:
d = json.load(f)
return d if isinstance(d, dict) else {}
except Exception:
return {}
def _save_daily(d: dict) -> None:
try:
with open(DAILY_FILE, "w", encoding="utf-8") as f:
json.dump(d, f, ensure_ascii=False)
except Exception:
pass
def _day_key() -> int:
return _today_start(int(time.time() * 1000))
def daily_used(machine: str) -> int:
"""该机器今日已用内置免费生图次数(跨天自动归零)。"""
rec = _load_daily().get(machine)
if isinstance(rec, dict) and rec.get("day") == _day_key():
try:
return int(rec.get("n", 0))
except Exception:
return 0
return 0
def daily_consume(machine: str, model: str = "") -> None:
"""内置渠道生成成功后扣次:未授权机器计数照记;走 Cloudflare 的模型同时消耗全站共享池。"""
with _DAILY_LOCK:
d = _load_daily()
rec = d.get(machine)
if not (isinstance(rec, dict) and rec.get("day") == _day_key()):
rec = {"day": _day_key(), "n": 0}
try:
rec["n"] = int(rec.get("n", 0)) + 1
except Exception:
rec["n"] = 1
rec["day"] = _day_key()
d[machine] = rec
_save_daily(d)
if _upstream_of(model) == "cloudflare":
cf_consume()
def daily_remaining(machine: str) -> int:
return max(0, DAILY_FREE_LIMIT - daily_used(machine))
# ---------- 全站共享上游:Cloudflare 免费池(flux/flux-*/dall-e-3,官方 173 张/天) ----------
def _upstream_of(model: str) -> str:
"""内置模型 → 上游通道:pollinations-* 走 Pollinations(key 预算制);其余 flux/dall-e* 走 Cloudflare。"""
if (model or "").lower().startswith("pollinations"):
return "pollinations"
return "cloudflare"
def _cf_load() -> dict:
try:
with open(CF_DAILY_FILE, encoding="utf-8") as f:
d = json.load(f)
return d if isinstance(d, dict) else {}
except Exception:
return {}
def cf_used_today() -> int:
"""全站今日已消耗的 Cloudflare 免费池张数(北京时间 0 点归零;与官方 08:00 重置略有差异,取更早者保护)。"""
d = _cf_load()
if d.get("day") == _day_key():
try:
return int(d.get("n", 0))
except Exception:
return 0
return 0
def cf_consume() -> None:
with _DAILY_LOCK:
d = _cf_load()
if d.get("day") != _day_key():
d = {"day": _day_key(), "n": 0}
try:
d["n"] = int(d.get("n", 0)) + 1
except Exception:
d["n"] = 1
try:
with open(CF_DAILY_FILE, "w", encoding="utf-8") as f:
json.dump(d, f, ensure_ascii=False)
except Exception:
pass
def cf_remaining() -> int:
return max(0, CF_DAILY_LIMIT - cf_used_today())
def gen_block_reason(machine: str, own_provider: bool = False, model: str = "") -> str | None:
"""AI 生图门禁:返回 None=放行;否则返回拒绝原因码。
- own_provider=True(自带提供商/KEY):不耗平台额度,直接放行
- 未授权:每机每日 DAILY_FREE_LIMIT 次
- 内置走 Cloudflare 的模型:全站共享池 CF_DAILY_LIMIT 张/天(**已授权也保护**,上游是账号级免费池)
"""
if not machine or not MACHINE_RE.match(machine):
return "bad_machine"
if own_provider:
return None
authed = machine not in _load_revoked() and (query_authorized(machine) or _account_license_ok(machine))
if not authed and daily_remaining(machine) <= 0:
return "machine_quota"
if _upstream_of(model) == "cloudflare" and cf_remaining() <= 0:
return "cf_pool"
return None
def check_gen(machine: str, own_provider: bool = False, model: str = "") -> int:
return 200 if gen_block_reason(machine, own_provider, model) is None else 403
def gen_block_message(reason: str | None) -> str:
if reason == "machine_quota":
return f"需要授权,或今日内置免费生图次数({DAILY_FREE_LIMIT} 次/天)已用完——可配置自己的 API KEY 继续生成"
if reason == "cf_pool":
return (f"今日免费生图池已用完:Cloudflare 官方免费 10,000 Neurons/天 ≈ {CF_DAILY_LIMIT} 张"
f"(北京时间约 08:00 重置,全站共享)——可配置自己的 API KEY 继续生成")
return "需要授权或今日免费试用已用完"
def deactivate_card(machine_id: str) -> tuple[int, dict]:
"""Revoke authorization for this machine.
Local revocation list takes effect immediately (no CardAuth dependency);
syncing the revoke to CardAuth is best-effort.
"""
# 1) local blacklist - instant, offline-proof
rev = _load_revoked()
already = machine_id in rev
rev.add(machine_id)
_save_revoked(rev)
_auth_cache.pop(machine_id, None)
# 2) best-effort sync to CardAuth (ignored failures)
synced = 0
tok = _admin_token()
if tok:
auth_hdr = {"Authorization": f"Bearer {tok}"}
try:
res = _get_json(f"/authorizations?bot_qq={machine_id}&status=active&page=1&page_size=20", auth_hdr)
for item in ((res or {}).get("data") or {}).get("list") or []:
if not _is_this_project(item):
continue
try:
r = _put_json(f"/authorizations/{item['id']}/revoke", {"reason": "用户在客户端自助撤销"}, auth_hdr)
if (r or {}).get("code") == 200:
synced += 1
except Exception: # noqa: BLE001
pass
except Exception: # noqa: BLE001
pass
else:
print("[deactivate] admin login unavailable -> local revoke only", flush=True)
if already:
return 200, {"code": 200, "message": "本机已处于撤销状态", "data": {"synced": synced}}
return 200, {"code": 200, "message": "已撤销授权", "data": {"synced": synced}}
# ================= local revocation list (instant, no CardAuth needed) ==========
REVOKED_FILE = "/app/data/revoked.json"
def _load_revoked() -> set:
try:
with open(REVOKED_FILE, "r", encoding="utf-8") as fh:
data = json.load(fh)
return set(data) if isinstance(data, list) else set()
except Exception: # noqa: BLE001
return set()
def _save_revoked(s: set) -> None:
tmp = REVOKED_FILE + ".tmp"
with open(tmp, "w", encoding="utf-8") as fh:
json.dump(sorted(s), fh)
os.replace(tmp, REVOKED_FILE)
# ================= document format conversion (calibre ebook-convert) ==========
CONVERT_FMTS = {"pdf", "docx", "txt", "rtf", "odt", "epub", "mobi", "azw3", "html"}
CONVERT_TIMEOUT = 180
def _text_to_odt(text: str, out_path: str) -> None:
"""Build a minimal, valid ODT (OpenDocument Text) package from plain text."""
import html as _h
paras = "\n".join(
f'<text:p xml:space="preserve">{_h.escape(line) or "&#160;"}</text:p>'
for line in text.splitlines()
)
content = (
'<?xml version="1.0" encoding="UTF-8"?>'
'<office:document-content '
'xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" '
'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" '
'xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" '
'xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" '
'office:version="1.2">'
"<office:body><office:text>"
f"{paras}"
"</office:text></office:body></office:document-content>"
)
manifest = (
'<?xml version="1.0" encoding="UTF-8"?>'
'<manifest:manifest '
'xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" '
'manifest:version="1.2">'
'<manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.text"/>'
'<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>'
"</manifest:manifest>"
)
styles = (
'<?xml version="1.0" encoding="UTF-8"?>'
'<office:document-styles '
'xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" '
'xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" '
'xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" '
'office:version="1.2"><office:styles/></office:document-styles>'
)
with zipfile.ZipFile(out_path, "w") as zf:
zf.writestr("mimetype", "application/vnd.oasis.opendocument.text", zipfile.ZIP_STORED)
zf.writestr("META-INF/manifest.xml", manifest)
zf.writestr("content.xml", content)
zf.writestr("styles.xml", styles)
def _convert_files(tmpdir: str, src_names: list, fmt: str) -> list:
"""Run ebook-convert for each source file; returns converted file paths."""
done = []
for name in src_names:
src = os.path.join(tmpdir, name)
stem = os.path.splitext(name)[0]
out = os.path.join(tmpdir, f"{stem}.{fmt}")
env = {**os.environ, "QTWEBENGINE_CHROMIUM_FLAGS": "--no-sandbox"}
def _run_convert(src_path: str, dst_path: str):
try:
proc = subprocess.run(
["ebook-convert", src_path, dst_path],
capture_output=True, timeout=CONVERT_TIMEOUT, env=env,
)
except subprocess.TimeoutExpired:
raise RuntimeError(f"{name} 转换超时(>{CONVERT_TIMEOUT}s)")
if proc.returncode != 0 or not os.path.exists(dst_path):
tail = (proc.stderr or proc.stdout or b"").decode("utf-8", "ignore")[-300:]
raise RuntimeError(f"{name} 转换失败:{tail or '未知错误'}")
if fmt == "odt":
# calibre has no odt exporter; convert to text first, then wrap a minimal ODT package
tmp_txt = out + ".tmp.txt"
_run_convert(src, tmp_txt)
text = open(tmp_txt, "r", encoding="utf-8", errors="ignore").read()
os.remove(tmp_txt)
_text_to_odt(text, out)
else:
# calibre exports "htmlz" (zipped html) instead of bare html; unwrap it
target = out + ("z" if fmt == "html" else "")
_run_convert(src, target)
if fmt == "html":
with zipfile.ZipFile(target) as zf:
names = zf.namelist()
html_name = next((n for n in names if n.lower().endswith("index.html")), None) or next((n for n in names if n.lower().endswith(".html")), None)
if not html_name:
raise RuntimeError(f"{name} 转换失败:htmlz 内未找到 HTML 文件")
with open(out, "wb") as fh:
fh.write(zf.read(html_name))
os.remove(target)
done.append(out)
return done
# ================= IT site bookmarks (shared, file-backed) =================
SITES_DIR_DEFAULT = "/app/data"
SITES_CFG = os.path.join(SITES_DIR_DEFAULT, "sites-config.json")
SEED_SITES = [
{"name": "GitHub", "url": "https://github.com", "category": "社区", "desc": "全球最大代码托管与开源社区"},
{"name": "Stack Overflow", "url": "https://stackoverflow.com", "category": "社区", "desc": "程序员问答社区"},
{"name": "V2EX", "url": "https://www.v2ex.com", "category": "社区", "desc": "开发者创意工作者社区"},
{"name": "Hacker News", "url": "https://news.ycombinator.com", "category": "社区", "desc": "硅谷科技资讯与讨论"},
{"name": "掘金", "url": "https://juejin.cn", "category": "社区", "desc": "中文开发者技术社区"},
{"name": "思否 SegmentFault", "url": "https://segmentfault.com", "category": "社区", "desc": "中文技术问答与专栏"},
{"name": "CSDN", "url": "https://www.csdn.net", "category": "社区", "desc": "中文IT技术社区"},
{"name": "博客园", "url": "https://www.cnblogs.com", "category": "社区", "desc": "老牌中文开发博客社区"},
{"name": "MDN Web Docs", "url": "https://developer.mozilla.org/zh-CN/", "category": "文档", "desc": "Web 标准权威文档(中文)"},
{"name": "Vue 3 文档", "url": "https://cn.vuejs.org", "category": "文档", "desc": "渐进式框架官方中文文档"},
{"name": "React 中文文档", "url": "https://zh-hans.react.dev", "category": "文档", "desc": "React 官方中文站"},
{"name": "TypeScript 手册", "url": "https://www.typescriptlang.org/zh/docs/", "category": "文档", "desc": "TS 官方中文 handbook"},
{"name": "Node.js 文档", "url": "https://nodejs.org/docs/latest/api/", "category": "文档", "desc": "Node.js API 官方文档"},
{"name": "Python 文档", "url": "https://docs.python.org/zh-cn/3/", "category": "文档", "desc": "Python 3 官方中文文档"},
{"name": "Rust 中文文档", "url": "https://doc.rust-lang.org/book/", "category": "文档", "desc": "Rust 程序设计语言"},
{"name": "Docker 文档", "url": "https://docs.docker.com/", "category": "文档", "desc": "Docker 官方文档"},
{"name": "Linux 命令大全", "url": "https://www.runoob.com/linux/linux-command-manual.html", "category": "文档", "desc": "菜鸟教程 Linux 命令手册"},
{"name": "Can I Use", "url": "https://caniuse.com", "category": "工具", "desc": "浏览器特性兼容性查询"},
{"name": "Regex101", "url": "https://regex101.com", "category": "工具", "desc": "正则表达式在线调试与解释"},
{"name": "JSON.cn", "url": "https://json.cn", "category": "工具", "desc": "JSON 在线解析与格式化"},
{"name": "CodePen", "url": "https://codepen.io", "category": "工具", "desc": "前端在线编辑与演示"},
{"name": "StackBlitz", "url": "https://stackblitz.com", "category": "工具", "desc": "浏览器里的完整 VS Code 项目环境"},
{"name": "npm", "url": "https://www.npmjs.com", "category": "工具", "desc": "Node 包注册与搜索"},
{"name": "PyPI", "url": "https://pypi.org", "category": "工具", "desc": "Python 包索引"},
{"name": "Docker Hub", "url": "https://hub.docker.com", "category": "工具", "desc": "容器镜像仓库"},
{"name": "crates.io", "url": "https://crates.io", "category": "工具", "desc": "Rust 包注册中心"},
{"name": "VS Code Web", "url": "https://vscode.dev", "category": "工具", "desc": "浏览器版 VS Code"},
{"name": "Git 官方", "url": "https://git-scm.com/doc", "category": "工具", "desc": "Git 版本控制文档与下载"},
{"name": "Postman 替代 Hoppscotch", "url": "https://hoppscotch.io", "category": "工具", "desc": "开源在线 API 调试工具"},
{"name": "ChatGPT", "url": "https://chat.openai.com", "category": "AI", "desc": "OpenAI 对话助手"},
{"name": "DeepSeek", "url": "https://chat.deepseek.com", "category": "AI", "desc": "深度求索对话助手"},
{"name": "Kimi", "url": "https://kimi.moonshot.cn", "category": "AI", "desc": "月之暗面长文本助手"},
{"name": "通义千问", "url": "https://tongyi.aliyun.com", "category": "AI", "desc": "阿里云大模型助手"},
{"name": "GitHub Copilot Chat", "url": "https://github.com/features/copilot", "category": "AI", "desc": "AI 结对编程"},
{"name": "阿里云", "url": "https://www.aliyun.com", "category": "云服务", "desc": "阿里云计算与开发者服务"},
{"name": "腾讯云", "url": "https://cloud.tencent.com", "category": "云服务", "desc": "腾讯云计算开发者工具"},
{"name": "Cloudflare", "url": "https://dash.cloudflare.com", "category": "云服务", "desc": "CDN/DNS/Workers 控制台"},
{"name": "Vercel", "url": "https://vercel.com", "category": "云服务", "desc": "前端部署与 Serverless"},
{"name": "Figma", "url": "https://www.figma.com", "category": "设计", "desc": "界面设计与协作"},
{"name": "Iconfont", "url": "https://www.iconfont.cn", "category": "设计", "desc": "阿里矢量图标库"},
{"name": "Ant Design", "url": "https://ant.design", "category": "设计", "desc": "企业级 UI 设计体系"},
{"name": "Element Plus", "url": "https://element-plus.org", "category": "设计", "desc": "Vue 3 组件库"},
{"name": "Tailwind CSS", "url": "https://tailwindcss.com", "category": "设计", "desc": "原子化 CSS 框架"},
{"name": "DeepL 翻译", "url": "https://www.deepl.com/translator", "category": "其他", "desc": "高质量机器翻译"},
{"name": "Bing", "url": "https://www.bing.com", "category": "其他", "desc": "微软搜索引擎"},
{"name": "百度", "url": "https://www.baidu.com", "category": "其他", "desc": "中文搜索引擎"},
]
def _sites_cfg_dir() -> str:
try:
with open(SITES_CFG, "r", encoding="utf-8") as fh:
d = str(json.load(fh).get("dir") or "").strip()
if d.startswith("/"):
return d
except Exception: # noqa: BLE001
pass
return SITES_DIR_DEFAULT
def _sites_file() -> str:
return os.path.join(_sites_cfg_dir(), "sites.json")
def _load_sites() -> list:
try:
with open(_sites_file(), "r", encoding="utf-8") as fh:
data = json.load(fh)
if isinstance(data, list):
return data
except Exception: # noqa: BLE001
pass
return [dict(s, id=f"seed-{i:02d}") for i, s in enumerate(SEED_SITES)]
def _save_sites(sites: list) -> None:
path = _sites_file()
os.makedirs(os.path.dirname(path), exist_ok=True)
tmp = path + ".tmp"
with open(tmp, "w", encoding="utf-8") as fh:
json.dump(sites, fh, ensure_ascii=False, indent=2)
os.replace(tmp, path)
def _reco_file() -> str:
return os.path.join(DATA_DIR, "reco.json")
# 无痕改字可用的视觉理解模型(实测支持 image_url 输入)
VISION_MODELS = ["qwen3.8-max", "qwen3.7-plus", "kimi-k3", "deepseek-v4-flash-vision-exp", "minimax-m3.1-flash-free", "step-5-preview", "deepseek-v4.1-flash"]
# 首页推荐种子:新工具上线时的默认展示(管理后台可增删改)
DEFAULT_RECO = [
{"path": "/ai-image-lab", "icon": "🪄", "name": "AI 图片实验室", "desc": "智能抠图去背景、AI 无损放大变清晰、一键证件照", "enabled": True},
{"path": "/ai-chat", "icon": "🤖", "name": "AI 问答", "desc": "报错解读、文案写作、代码问题随口问,支持多轮对话", "enabled": True},
{"path": "/copywriting", "icon": "✍️", "name": "文案工具箱", "desc": "一键改写、降 AIGC 率、爆款标题、小红书/朋友圈文案", "enabled": True},
{"path": "/video-tools", "icon": "🎥", "name": "视频工具箱", "desc": "转码/压缩/水印/去水印/GIF + AI 字幕一条龙", "enabled": True},
{"path": "/seal-maker", "icon": "🔴", "name": "印章生成器", "desc": "公章一键生成,盖章到图片/PDF,三栏专业编辑器", "enabled": True},
{"path": "/signature-pad", "icon": "✍️", "name": "手写签名", "desc": "真实笔触模拟,透明 PNG,签合同一步到位", "enabled": True},
{"path": "/zh-convert", "icon": "🈶", "name": "简繁体转换", "desc": "OpenCC 词典,词组级准确互转", "enabled": True},
{"path": "/pinyin-converter", "icon": "🀄", "name": "汉字转拼音", "desc": "多音字智能转换,全读音标注", "enabled": True},
{"path": "/user-center", "icon": "👤", "name": "用户中心", "desc": "收藏/记录云端同步,换电脑登录即恢复", "enabled": True},
]
def _load_reco() -> list:
try:
with open(_reco_file(), "r", encoding="utf-8") as fh:
data = json.load(fh)
if isinstance(data, list) and data:
return data
except Exception: # noqa: BLE001
pass
return [dict(x) for x in DEFAULT_RECO]
def _save_reco(items: list) -> None:
path = _reco_file()
os.makedirs(os.path.dirname(path), exist_ok=True)
tmp = path + ".tmp"
with open(tmp, "w", encoding="utf-8") as fh:
json.dump(items, fh, ensure_ascii=False, indent=2)
os.replace(tmp, path)
def sites_api(method: str, path: str, payload: dict) -> tuple[int, dict]:
"""Shared bookmarks CRUD. path is after /sites."""
if method == "GET" and path == "":
sites = _load_sites()
return 200, {"code": 200, "message": "ok", "data": {"dir": _sites_file(), "sites": sites}}
if method == "POST" and path == "/config":
d = str(payload.get("dir") or "").strip()
if not d.startswith("/") or any(d.startswith(bad) for bad in ("/etc", "/usr", "/bin", "/sbin", "/boot", "/sys", "/proc", "/dev")):
return 400, {"code": 400, "message": "目录必须是绝对路径,且不能指向系统目录", "data": None}
try:
os.makedirs(d, exist_ok=True)
old = _load_sites()
cfg_tmp = SITES_CFG + ".tmp"
with open(cfg_tmp, "w", encoding="utf-8") as fh:
json.dump({"dir": d}, fh)
os.replace(cfg_tmp, SITES_CFG)
_save_sites(old) # migrate current data into new dir
except Exception as exc: # noqa: BLE001
return 400, {"code": 400, "message": f"设置失败: {exc}", "data": None}
return 200, {"code": 200, "message": "目录已更新,数据已迁移", "data": {"dir": _sites_file()}}
if method == "POST" and path == "":
name = str(payload.get("name") or "").strip()
url = str(payload.get("url") or "").strip()
if not name or not url:
return 400, {"code": 400, "message": "名称和网址不能为空", "data": None}
if not url.startswith(("http://", "https://")):
url = "https://" + url
sites = _load_sites()
item = {
"id": f"u{int(time.time() * 1000)}",
"name": name,
"url": url,
"category": str(payload.get("category") or "其他").strip() or "其他",
"desc": str(payload.get("desc") or "").strip(),
}
sites.append(item)
_save_sites(sites)
return 200, {"code": 200, "message": "已添加", "data": item}
sid = ""
if method in ("PUT", "DELETE") and path.startswith("/"):
sid = path[1:]
if method == "PUT" and sid:
sites = _load_sites()
for it in sites:
if it.get("id") == sid:
for k in ("name", "url", "category", "desc"):
if k in payload:
it[k] = str(payload[k]).strip()
if it["url"] and not it["url"].startswith(("http://", "https://")):
it["url"] = "https://" + it["url"]
_save_sites(sites)
return 200, {"code": 200, "message": "已更新", "data": it}
return 404, {"code": 404, "message": "条目不存在", "data": None}
if method == "DELETE" and sid:
sites = _load_sites()
keep = [it for it in sites if it.get("id") != sid]
if len(keep) == len(sites):
return 404, {"code": 404, "message": "条目不存在", "data": None}
_save_sites(keep)
return 200, {"code": 200, "message": "已删除", "data": None}
return 404, {"code": 404, "message": "not found", "data": None}
def _int_str(v, lo, hi, default=0):
try:
return max(lo, min(hi, int(float(v))))
except Exception:
return default
def check(machine: str, tool: str) -> int:
if not machine or not MACHINE_RE.match(machine):
return 403
if tool not in PROTECTED_TOOLS:
return 403
if machine in _load_revoked():
# user revoked on this client -> authorization ignored, trial applies
return 200 if trial_allowed(machine, tool) else 403
if query_authorized(machine):
return 200
# account-level license: this device belongs to a logged-in user whose card is active
if _account_license_ok(machine):
return 200
return 200 if trial_allowed(machine, tool) else 403
# ---- AI 换脸引擎:insightface buffalo_l 人脸检测 + inswapper_128 换脸(本机 CPU 推理) ----
_FACE_LOCK = threading.Lock()
_FACE_APP = None
_FACE_SWAP = None
_FACE_MODELS_DIR = os.environ.get("FACE_MODELS_DIR", "/app/face-models")
_GFPGAN_SESS = None
def _face_engine():
"""懒加载换脸引擎(线程安全;首次调用约 1~2s,之后常驻内存复用)。"""
global _FACE_APP, _FACE_SWAP
if _FACE_APP is None:
from insightface.app import FaceAnalysis
from insightface import model_zoo
app = FaceAnalysis(name="buffalo_l", root=_FACE_MODELS_DIR, providers=["CPUExecutionProvider"])
app.prepare(ctx_id=0, det_size=(640, 640))
swap = model_zoo.get_model(os.path.join(_FACE_MODELS_DIR, "inswapper_128.onnx"),
providers=["CPUExecutionProvider"])
_FACE_APP, _FACE_SWAP = app, swap
return _FACE_APP, _FACE_SWAP
def _largest_face(faces):
"""多张脸时取面积最大的一张(换脸默认作用于最显眼的人脸)。"""
return max(faces, key=lambda f: float((f.bbox[2] - f.bbox[0]) * (f.bbox[3] - f.bbox[1])))
def _gfpgan_session():
global _GFPGAN_SESS
if _GFPGAN_SESS is None:
import onnxruntime as _ort
path = os.path.join(_FACE_MODELS_DIR, "gfpgan_1.4.onnx")
if not os.path.exists(path):
return None
_GFPGAN_SESS = _ort.InferenceSession(path, providers=["CPUExecutionProvider"])
return _GFPGAN_SESS
def _gfpgan_enhance(img):
"""用 GFPGAN 对图中最大人脸做修复增强,返回增强后的整图(失败则原样返回)。"""
try:
import cv2 as _cv2
import numpy as _np
sess = _gfpgan_session()
if sess is None:
return img
app, _ = _face_engine()
faces = app.get(img)
if not faces:
return img
f = _largest_face(faces)
x1, y1, x2, y2 = [int(v) for v in f.bbox]
h, w = img.shape[:2]
# 扩展 bbox
pad = int(0.25 * max(x2 - x1, y2 - y1))
x1 = max(0, x1 - pad); y1 = max(0, y1 - pad)
x2 = min(w, x2 + pad); y2 = min(h, y2 + pad)
crop = img[y1:y2, x1:x2]
if crop.size == 0:
return img
inp = _cv2.resize(crop, (512, 512))
inp = _cv2.cvtColor(inp, _cv2.COLOR_BGR2RGB).astype("float32") / 255.0
inp = (inp - 0.5) / 0.5
inp = _np.transpose(inp, (2, 0, 1))[None, ...].astype("float32")
out = sess.run(None, {sess.get_inputs()[0].name: inp})[0]
out = out[0].transpose(1, 2, 0)
out = (out * 0.5 + 0.5) * 255.0
out = _np.clip(out, 0, 255).astype("uint8")
out = _cv2.cvtColor(out, _cv2.COLOR_RGB2BGR)
out = _cv2.resize(out, (x2 - x1, y2 - y1))
# 羽化混合
mask = _np.zeros((y2 - y1, x2 - x1), dtype="float32")
cx, cy = (x2 - x1) // 2, (y2 - y1) // 2
# 简单中心渐变蒙版
Y, X = _np.ogrid[:y2-y1, :x2-x1]
dist = _np.sqrt((X - cx) ** 2 + (Y - cy) ** 2)
r = min(cx, cy)
mask = _np.clip(1 - dist / max(r, 1), 0, 1)
mask = mask[..., None]
blended = (out.astype("float32") * mask + crop.astype("float32") * (1 - mask)).astype("uint8")
res = img.copy()
res[y1:y2, x1:x2] = blended
return res
except Exception as e:
print(f"[gfpgan] enhance failed: {e}", flush=True)
return img
def _face_swap_run(src_img, dst_img):
"""源人脸 -> 目标照片。返回 (jpg字节|None, 错误信息, 耗时ms)。加锁串行避免并发吃满 CPU。"""
import cv2 as _cv2
t0 = time.time()
with _FACE_LOCK:
app, swap = _face_engine()
src_faces = app.get(src_img)
dst_faces = app.get(dst_img)
if not src_faces:
return None, "源图中未检测到人脸,请换一张清晰的正脸照片", 0
if not dst_faces:
return None, "目标图中未检测到人脸,请换一张含人物的照片", 0
out = swap.get(dst_img, _largest_face(dst_faces), _largest_face(src_faces), paste_back=True)
# GFPGAN 修复增强(可选)
if os.environ.get("FACE_SWAP_GFPGAN", "1") != "0":
out = _gfpgan_enhance(out)
ok, buf = _cv2.imencode(".jpg", out, [int(_cv2.IMWRITE_JPEG_QUALITY), 92])
if not ok:
return None, "结果编码失败", 0
return buf.tobytes(), None, int((time.time() - t0) * 1000)
def _account_license_ok(machine: str) -> bool:
"""True when this device logged into an authorized account.
An account is authorized when either:
* it holds an active card (card_key + origin_machine active on CardAuth), or
* it is bound to at least one machine that itself is authorized on CardAuth
(covers cards activated on a device without account context).
Either way, every device that logs into this account inherits the license.
"""
try:
conn = _db()
rows = conn.execute("SELECT card_key, origin_machine, bound_machines FROM users").fetchall()
conn.close()
except Exception:
return False
for card_key, origin, bm_json in rows:
if not card_key and not bm_json:
continue
try:
machines = json.loads(bm_json or "[]")
except Exception:
machines = []
if machine not in machines:
continue
# account carries an active card
if card_key and origin and (origin == machine or query_authorized(origin)):
return True
# account shares a device that is itself authorized (max 4 lookups, cached)
for bm in machines[:4]:
if bm and bm != machine and bm not in _load_revoked() and query_authorized(bm):
return True
return False
# ---- card activation (server-side key) ----
_auth_state: dict = {"token": None}
def _admin_token(force: bool = False) -> str | None:
"""Login to CardAuth as admin and cache the access token."""
if not (AUTH_USER and AUTH_PASS):
return None
if _auth_state["token"] and not force:
return _auth_state["token"]
try:
res = _post_json("/auth/admin-login", {"username": AUTH_USER, "password": AUTH_PASS}, {}, timeout=15)
tok = (((res or {}).get("data") or {}).get("token") or {}).get("access_token")
if tok:
_auth_state["token"] = tok
return tok
print(f"[login] no token in response: {res}", flush=True)
except Exception as exc: # noqa: BLE001
print(f"[login] failed ({exc})", flush=True)
return None
def _get_json(path: str, headers: dict, timeout: int = 15) -> dict:
req = request.Request(f"{AUTH_BASE}{path}", headers=headers, method="GET")
with OPENER_DIRECT.open(req, timeout=timeout) as resp:
return json.loads(resp.read().decode("utf-8"))
def _put_json(path: str, payload: dict, headers: dict, timeout: int = 15) -> dict:
data = json.dumps(payload).encode("utf-8")
hdrs = {"Content-Type": "application/json", **headers}
req = request.Request(f"{AUTH_BASE}{path}", data=data, headers=hdrs, method="PUT")
with OPENER_DIRECT.open(req, timeout=timeout) as resp:
return json.loads(resp.read().decode("utf-8"))
def _post_json(path: str, payload: dict, headers: dict, timeout: int = 15) -> dict:
data = json.dumps(payload).encode("utf-8")
hdrs = {"Content-Type": "application/json", **headers}
req = request.Request(f"{AUTH_BASE}{path}", data=data, headers=hdrs, method="POST")
with OPENER_DIRECT.open(req, timeout=timeout) as resp:
return json.loads(resp.read().decode("utf-8"))
def activate_card(card_key: str, machine_id: str, contact_qq: str, replace: bool = False) -> tuple[int, dict]:
"""Pre-check with /public/verify (CARD_AUTH_KEY), then activate.
Returns (http_status, body_dict).
"""
tok = _admin_token()
auth_hdr = {"Authorization": f"Bearer {tok}"} if tok else {}
# replace: revoke this machine's existing authorizations first (user is
# swapping to a new card; confirmed on the client side)
if replace:
try:
deactivate_card(machine_id)
except Exception as exc: # noqa: BLE001
print(f"[activate] replace revoke failed ({exc}) -> continue", flush=True)
# NOTE: do NOT call /public/verify with an unused card here - in this
# CardAuth version the first verify call binds & consumes the card.
# activate (web channel, with admin session if configured).
# project_id: universal cards carry no project of their own - the backend
# rejects them unless the caller names one. Named-card activations ignore
# it (their own project wins), so always send it.
payload = {
"card_key": card_key,
"bot_qq": machine_id,
"contact_qq": contact_qq or machine_id,
"device_info": "IT-Tools Web",
"project_id": PROJECT_ID,
}
# Retry ONLY on explicit 401 (stale token). Any other failure is returned
# as-is: re-sending an activate that already consumed the card would burn it.
try:
ares = _post_json("/public/authorizations/activate", payload, auth_hdr)
except Exception as exc: # noqa: BLE001
return 502, {"code": 502, "message": f"授权服务器连接失败: {exc}", "data": None}
if (ares or {}).get("code") == 401 and (AUTH_USER and AUTH_PASS):
tok = _admin_token(force=True)
auth_hdr = {"Authorization": f"Bearer {tok}"} if tok else {}
try:
ares = _post_json("/public/authorizations/activate", payload, auth_hdr)
except Exception as exc: # noqa: BLE001
return 502, {"code": 502, "message": f"授权服务器连接失败: {exc}", "data": None}
code = 200 if (ares or {}).get("code") == 200 else 400
if code == 200:
_auth_cache.pop(machine_id, None) # invalidate cache -> fresh query
rev = _load_revoked()
if machine_id in rev:
rev.discard(machine_id)
_save_revoked(rev) # new activation lifts the local revocation
return code, ares
# ================= 用户系统(注册/登录/数据云同步/文件存储) =================
import hashlib
import hmac
import secrets
import sqlite3
import uuid
MAX_UPLOAD = int(os.environ.get("MAX_UPLOAD_MB", "50")) * 1024 * 1024
# storage model: admin panel edits the HOST path; the container sees it under HOST_PREFIX (-v /:/hostfs)
HOST_PREFIX = os.environ.get("HOST_PREFIX", "/hostfs")
DEFAULT_HOST_ROOT = os.environ.get("DEFAULT_HOST_ROOT", "/vol1/1000/HD1/docker/it-tools-relay/data")
_BOOT_DATA_DIR = "/app/data"
def _host_to_container(host_path: str) -> str:
if os.path.isdir(HOST_PREFIX):
return (HOST_PREFIX + host_path).rstrip("/")
return host_path # fallback: legacy deployment without host root mount
def _load_storage_root() -> str:
cfg = os.path.join(_host_to_container(DEFAULT_HOST_ROOT), "config.json")
try:
with open(cfg) as f:
root = json.load(f).get("storage_root")
if root:
container = _host_to_container(root)
if os.path.isdir(container):
return container
except Exception:
pass
return _host_to_container(DEFAULT_HOST_ROOT)
DATA_DIR = _load_storage_root()
USER_DB = os.environ.get("USER_DB", os.path.join(DATA_DIR, "users.db"))
SECRET_FILE = os.environ.get("SECRET_FILE", os.path.join(DATA_DIR, ".auth_secret"))
USERS_ROOT = os.environ.get("USERS_ROOT", os.path.join(DATA_DIR, "users"))
def _auth_secret():
if not os.path.exists(SECRET_FILE):
os.makedirs(os.path.dirname(SECRET_FILE), exist_ok=True)
with open(SECRET_FILE, "w") as f:
f.write(secrets.token_hex(32))
with open(SECRET_FILE) as f:
return f.read().strip()
def _db():
conn = sqlite3.connect(USER_DB)
conn.execute("CREATE TABLE IF NOT EXISTS users (username TEXT PRIMARY KEY, salt TEXT, pw_hash TEXT, created REAL)")
conn.execute("CREATE TABLE IF NOT EXISTS files (id TEXT PRIMARY KEY, username TEXT, name TEXT, size INTEGER, mime TEXT, created REAL)")
for stmt in (
"ALTER TABLE users ADD COLUMN card_key TEXT DEFAULT ''",
"ALTER TABLE users ADD COLUMN origin_machine TEXT DEFAULT ''",
"ALTER TABLE users ADD COLUMN bound_machines TEXT DEFAULT '[]'",
"ALTER TABLE users ADD COLUMN role TEXT DEFAULT 'user'",
"ALTER TABLE users ADD COLUMN disabled INTEGER DEFAULT 0",
"ALTER TABLE users ADD COLUMN last_login REAL DEFAULT 0",
):
try:
conn.execute(stmt)
except sqlite3.OperationalError:
pass
conn.commit()
return conn
DEFAULT_ADMIN = os.environ.get("DEFAULT_ADMIN_USER", "jinghui")
DEFAULT_ADMIN_PW = os.environ.get("DEFAULT_ADMIN_PW", "a3396336")
def _ensure_admin():
"""create the default admin account on first boot"""
try:
conn = _db()
row = conn.execute("SELECT username FROM users WHERE username=?", (DEFAULT_ADMIN,)).fetchone()
if not row:
salt = secrets.token_hex(16)
conn.execute(
"INSERT INTO users (username, salt, pw_hash, created, role) VALUES (?,?,?,?,'admin')",
(DEFAULT_ADMIN, salt, _hash_pw(DEFAULT_ADMIN_PW, salt), time.time()),
)
conn.commit()
else:
conn.execute("UPDATE users SET role='admin' WHERE username=?", (DEFAULT_ADMIN,))
conn.commit()
conn.close()
except Exception:
pass
def _bind_machine(username: str, machine: str):
"""remember devices that logged into this account (max 10)"""
if not machine or not MACHINE_RE.match(machine):
return
conn = _db()
row = conn.execute("SELECT bound_machines FROM users WHERE username=?", (username,)).fetchone()
try:
machines = json.loads(row[0] or "[]") if row else []
except Exception:
machines = []
if machine in machines:
machines.remove(machine)
machines.insert(0, machine)
conn.execute("UPDATE users SET bound_machines=? WHERE username=?", (json.dumps(machines[:10]), username))
conn.commit()
conn.close()
def _user_attr(username: str):
"""returns (role, disabled)"""
try:
conn = _db()
row = conn.execute("SELECT role, disabled FROM users WHERE username=?", (username,)).fetchone()
conn.close()
if row:
return (row[0] or "user"), bool(row[1])
except Exception:
pass
return "user", False
def _user_disabled(username: str) -> bool:
return _user_attr(username)[1]
def _save_user_license(username: str, card_key: str, machine: str):
"""store the card bound to this account after a successful activation"""
conn = _db()
conn.execute("UPDATE users SET card_key=?, origin_machine=? WHERE username=?",
(card_key, machine, username))
conn.commit()
conn.close()
_bind_machine(username, machine)
def _hash_pw(pw, salt):
return hashlib.pbkdf2_hmac("sha256", pw.encode(), salt.encode(), 120000).hex()
def _make_token(username):
exp = str(int(time.time()) + 7 * 86400)
payload = username + "." + exp
sig = hmac.new(_auth_secret().encode(), payload.encode(), hashlib.sha256).hexdigest()[:32]
return payload + "." + sig
def _parse_token(tok):
try:
username, exp, sig = tok.rsplit(".", 2)
payload = username + "." + exp
expect = hmac.new(_auth_secret().encode(), payload.encode(), hashlib.sha256).hexdigest()[:32]
if hmac.compare_digest(sig, expect) and int(exp) > time.time():
return username
except Exception:
pass
return None
def _user_dir(username):
d = os.path.join(USERS_ROOT, username)
os.makedirs(os.path.join(d, "files"), exist_ok=True)
return d
def _auth_user(handler):
tok = handler.headers.get("Authorization", "")
if tok.lower().startswith("bearer "):
tok = tok[7:]
return _parse_token(tok) if tok else None
TURNSTILE_SECRET = os.environ.get("TURNSTILE_SECRET", "")
ADMIN_TOKEN = os.environ.get("ADMIN_TOKEN", "")
USAGE_DB = os.environ.get("USAGE_DB", os.path.join(DATA_DIR, "usage.db"))
def _usage_db():
conn = sqlite3.connect(USAGE_DB)
conn.execute("CREATE TABLE IF NOT EXISTS usage (id INTEGER PRIMARY KEY AUTOINCREMENT, machine TEXT, username TEXT, tool TEXT, at REAL)")
conn.execute("CREATE INDEX IF NOT EXISTS idx_usage_at ON usage(at)")
conn.execute("CREATE INDEX IF NOT EXISTS idx_usage_tool ON usage(tool)")
conn.commit()
return conn
def log_usage(machine: str, tool: str):
"""record a tool usage event (best-effort, never blocks the request)"""
if not tool:
return
try:
username = ""
try:
conn = _db()
rows = conn.execute("SELECT username, bound_machines FROM users").fetchall()
conn.close()
for u, bm in rows:
if machine in json.loads(bm or "[]"):
username = u
break
except Exception:
pass
conn = _usage_db()
conn.execute("INSERT INTO usage (machine, username, tool, at) VALUES (?,?,?,?)",
(machine or "", username, tool, time.time()))
conn.commit()
conn.close()
except Exception:
pass
# ---- 前端工具使用上报(未登录访客也统计):10 分钟同机同工具去重 + 每 IP 限流防刷 ----
_USE_SEEN: dict = {} # "<machine|ip>|<tool>" -> last report ts
_USE_IP: dict = {} # ip -> [recent ts...]
_USE_LOCK = threading.Lock()
def usage_track(payload: dict, handler) -> tuple:
"""POST /usage:任意访客(含未登录)上报工具使用,落 usage.db。
10 分钟内同一机器+同一工具只计一次(防刷新刷量);每 IP 10 分钟上限 300 条。"""
tool = str(payload.get("tool") or "").strip()
if not tool or len(tool) > 64 or not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9\-_.]*", tool):
return 400, {"code": 400, "message": "无效 tool"}
machine = str(payload.get("machine_id") or "").strip()
if machine and not MACHINE_RE.match(machine):
machine = ""
try:
ip = (handler.headers.get("X-Real-IP") or "").strip() or handler.client_address[0]
except Exception:
ip = ""
now = time.time()
key = f"{machine or ip}|{tool}"
with _USE_LOCK:
if len(_USE_SEEN) > 20000: # 修剪过期键,防内存膨胀
for k in [k for k, t in _USE_SEEN.items() if now - t >= 600]:
_USE_SEEN.pop(k, None)
last = _USE_SEEN.get(key)
if last is not None and now - last < 600:
return 200, {"code": 200, "message": "ok", "data": {"dup": 1}}
recent = [t for t in _USE_IP.get(ip, []) if now - t < 600]
if len(recent) >= 300:
_USE_IP[ip] = recent
return 200, {"code": 200, "message": "ok", "data": {"capped": 1}}
recent.append(now)
_USE_IP[ip] = recent
_USE_SEEN[key] = now
log_usage(machine, tool)
return 200, {"code": 200, "message": "ok", "data": {"ok": 1}}
# ---- 在线统计:仅未登录访客,前端停留满 1 分钟才开始心跳 ----
ONLINE_WINDOW = 90.0 # 心跳每 30s 一次(后台标签被节流为 60s),超 90s 无心跳判离线
_ONLINE: dict = {} # machine_id -> 最近心跳时间戳(进程内存)
_ONLINE_LOCK = threading.Lock()
def online_beat(payload: dict) -> tuple:
"""POST /online:访客在线心跳。
- guest=true(未登录)→ 记为在线;guest=false(已登录)→ 移出统计。
- 「停留超过 1 分钟才算在线」由前端控制:打开满 60s 才发首次心跳。
"""
machine = str(payload.get("machine_id") or "").strip()
if not machine or not MACHINE_RE.match(machine):
return 400, {"code": 400, "message": "无效 machine_id"}
guest = bool(payload.get("guest"))
now = time.time()
with _ONLINE_LOCK:
if guest:
_ONLINE[machine] = now
else:
_ONLINE.pop(machine, None) # 登录用户不统计
if len(_ONLINE) > 5000: # 防内存膨胀:修剪过期
for k in [k for k, t in _ONLINE.items() if now - t > ONLINE_WINDOW]:
_ONLINE.pop(k, None)
count = sum(1 for t in _ONLINE.values() if now - t <= ONLINE_WINDOW)
return 200, {"code": 200, "message": "ok", "data": {"online": count}}
def online_count() -> int:
now = time.time()
with _ONLINE_LOCK:
return sum(1 for t in _ONLINE.values() if now - t <= ONLINE_WINDOW)
def _dir_size(path: str) -> int:
total = 0
for root, _dirs, files in os.walk(path):
for f in files:
try:
total += os.path.getsize(os.path.join(root, f))
except OSError:
pass
return total
def verify_turnstile(token: str) -> bool:
"""Cloudflare Turnstile human check. Skipped when secret is not configured."""
if not TURNSTILE_SECRET:
return True
if not token or len(token) > 4096:
return False
data = parse.urlencode({"secret": TURNSTILE_SECRET, "response": token}).encode()
req = request.Request("https://challenges.cloudflare.com/turnstile/v0/siteverify", data=data)
try:
with _opener(True) as resp:
body = json.loads(resp.read().decode("utf-8"))
return bool(body.get("success"))
except Exception:
return True # fail-open on network issues (do not lock out users)
USER_NAME_RE = re.compile(r"^[A-Za-z0-9_\u4e00-\u9fa5-]{3,32}$")
def _admin_auth(handler) -> bool:
# channel 1: configured admin token (bootstrap / emergency)
tok = handler.headers.get("X-Admin-Token", "")
if ADMIN_TOKEN and tok and tok == ADMIN_TOKEN:
return True
# query token fallback: <img src> / <a download> 无法携带 header,缩略图与下载走 ?token=
if not tok:
try:
tok = (parse.parse_qs(parse.urlparse(handler.path).query).get("token") or [""])[0]
except Exception:
tok = ""
if ADMIN_TOKEN and tok and tok == ADMIN_TOKEN:
return True
if tok:
acc = _parse_token(tok)
if acc:
role, disabled = _user_attr(acc)
if role == "admin" and not disabled:
return True
tok = ""
# channel 2: logged-in admin account
auth = handler.headers.get("Authorization", "")
if auth.lower().startswith("bearer "):
acc = _parse_token(auth[7:])
if acc:
role, disabled = _user_attr(acc)
if role == "admin" and not disabled:
return True
return False
def admin_api(method: str, sub: str, payload: dict, handler):
"""returns (status, body)"""
if not _admin_auth(handler):
return 403, {"code": 403, "message": "管理员令牌无效"}
if method == "GET" and sub == "/overview":
conn = _db()
users = conn.execute("SELECT username, created, card_key, bound_machines, role, disabled, last_login FROM users").fetchall()
conn.close()
out_users = []
total_files = 0
total_size = 0
for username, created, card_key, bm, role, disabled, last_login in users:
udir = os.path.join(USERS_ROOT, username)
fdir = os.path.join(udir, "files")
fcount = 0
fsize = 0
for root, _d, files in os.walk(fdir):
for f in files:
fcount += 1
try:
fsize += os.path.getsize(os.path.join(root, f))
except OSError:
pass
dsize = 0
dp = os.path.join(udir, "data.json")
if os.path.exists(dp):
try:
dsize = os.path.getsize(dp)
except OSError:
pass
total_files += fcount
total_size += fsize + dsize
try:
machines = json.loads(bm or "[]")
except Exception:
machines = []
out_users.append({
"username": username,
"created": int(created or 0),
"last_login": int(last_login or 0),
"files": fcount,
"file_size": fsize,
"data_size": dsize,
"total_size": fsize + dsize,
"devices": len(machines),
"licensed": bool(card_key),
"role": role or "user",
"disabled": bool(disabled),
})
out_users.sort(key=lambda x: -x["total_size"])
return 200, {"code": 200, "message": "ok", "data": {
"users": out_users,
"totals": {"users": len(out_users), "files": total_files, "size": total_size},
}}
# ---- 统一文件下载/缩略图:path 相对 DATA_DIR(users/、gen_images/、mcp_files/ 白名单)----
# 鉴权走 _admin_auth(支持 header 或 ?token=,因 <img src>/<a download> 无法带 header)
if method == "GET" and sub == "/file":
qs = parse.parse_qs(parse.urlparse(handler.path).query)
rel = (qs.get("path") or [""])[0]
as_dl = (qs.get("dl") or [""])[0] == "1"
disp_q = (qs.get("name") or [""])[0]
if not rel or rel.startswith("/") or ".." in rel.split("/"):
return 400, {"code": 400, "message": "参数错误"}
if rel.split("/")[0] not in ("users", "gen_images", "mcp_files"):
return 404, {"code": 404, "message": "文件不存在"}
root = os.path.realpath(DATA_DIR)
fp = os.path.realpath(os.path.join(root, rel))
if not fp.startswith(root + os.sep) or not os.path.isfile(fp):
return 404, {"code": 404, "message": "文件不存在"}
try:
with open(fp, "rb") as fh:
data = fh.read()
except OSError:
return 404, {"code": 404, "message": "文件不存在"}
disp = os.path.basename(disp_q.replace(chr(92), "/")) if disp_q else os.path.basename(fp)
disp = re.sub(r'[\r\n"]+', '', disp)[:180] or os.path.basename(fp)
ctype = mimetypes.guess_type(fp)[0] or "application/octet-stream"
handler.send_response(200)
handler.send_header("Content-Type", ctype)
handler.send_header("Content-Length", str(len(data)))
if as_dl:
handler.send_header("Content-Disposition", "attachment; filename*=UTF-8''" + parse.quote(disp))
handler.end_headers()
handler.wfile.write(data)
return None, None
# ---- 文件管理:默认全量聚合(所有用户文件 + AI 生成图片 + MCP 产出),50/页分页 ----
if method == "GET" and sub.startswith("/files"):
qs = parse.parse_qs(parse.urlparse(handler.path).query)
username = (qs.get("username") or [""])[0]
scope = (qs.get("scope") or [""])[0]
kind = (qs.get("kind") or [""])[0] # 按功能筛选:gen/face-swap/poster/... 或 __none__=未分类
try:
page = max(1, int((qs.get("page") or ["1"])[0]))
except Exception:
page = 1
try:
page_size = min(200, max(1, int((qs.get("page_size") or ["50"])[0])))
except Exception:
page_size = 50
if username and ("/" in username or ".." in username):
return 400, {"code": 400, "message": "参数错误"}
IMG_EXT = {"png", "jpg", "jpeg", "gif", "webp", "bmp", "svg", "avif", "ico"}
items = []
want_user = scope not in ("gen", "mcp")
want_gen = scope in ("", "gen")
want_mcp = scope in ("", "mcp")
if username: # 指定用户时只看该用户文件(兼容旧下拉语义)
want_user, want_gen, want_mcp = True, False, False
# 1) 用户上传文件(磁盘是 uuid 名 -> DB 取真实文件名/mime)
if want_user:
db_meta = {}
try:
conn = _db()
for fid, un, nm, mi in conn.execute("SELECT id, username, name, mime FROM files").fetchall():
db_meta[(un, fid)] = (nm or fid, mi or "")
conn.close()
except Exception:
db_meta = {}
uroots = [username] if username else (
sorted(os.listdir(USERS_ROOT)) if os.path.isdir(USERS_ROOT) else [])
for u in uroots:
fdir = os.path.join(USERS_ROOT, u, "files")
if not os.path.isdir(fdir):
continue
for root, _d, fs in os.walk(fdir):
ukind = os.path.basename(root) if root != fdir else ""
for f in fs:
fp = os.path.join(root, f)
try:
st = os.stat(fp)
except OSError:
continue
nm, mi = db_meta.get((u, f), (f, ""))
ext = os.path.splitext(nm)[1].lower().lstrip(".")
items.append({
"id": f, "name": nm, "kind": ukind, "size": st.st_size,
"mtime": int(st.st_mtime), "source": "user", "username": u,
"mime": mi, "is_image": mi.startswith("image/") or ext in IMG_EXT,
"file_type": (ext or (mi.split("/")[-1] if mi else "file") or "file").upper(),
"path": os.path.relpath(fp, DATA_DIR),
})
# 2) AI 生成图片(换脸/生图/海报/编辑,跨所有机器聚合 gen_images/*/)
if want_gen:
groot = os.path.join(DATA_DIR, "gen_images")
for m in (sorted(os.listdir(groot)) if os.path.isdir(groot) else []):
gdir = os.path.join(groot, m)
if not os.path.isdir(gdir):
continue
idx = {}
try:
idxp = os.path.join(gdir, "index.json")
for it in (json.load(open(idxp, encoding="utf-8")) if os.path.exists(idxp) else []):
if isinstance(it, dict):
idx[str(it.get("id"))] = it
except Exception:
idx = {}
for f in os.listdir(gdir):
if f == "index.json":
continue
fp = os.path.join(gdir, f)
try:
st = os.stat(fp)
except OSError:
continue
iid = os.path.splitext(f)[0]
meta = idx.get(iid, {})
ext = os.path.splitext(f)[1].lower().lstrip(".")
items.append({
"id": iid, "name": f, "kind": str(meta.get("kind") or "gen"),
"size": st.st_size, "mtime": int(st.st_mtime), "machine": m,
"prompt": str(meta.get("prompt") or ""), "url": f"/relay/ai/img/{m}/{iid}",
"source": "gen", "username": "", "mime": f"image/{ext}",
"is_image": ext in IMG_EXT, "file_type": (ext or "img").upper(),
"path": os.path.relpath(fp, DATA_DIR),
})
# 3) MCP 产出文件(data/mcp_files/)
if want_mcp:
mroot = os.path.join(DATA_DIR, "mcp_files")
for f in (sorted(os.listdir(mroot)) if os.path.isdir(mroot) else []):
fp = os.path.join(mroot, f)
if not os.path.isfile(fp):
continue
try:
st = os.stat(fp)
except OSError:
continue
ext = os.path.splitext(f)[1].lower().lstrip(".")
items.append({
"id": f, "name": f, "kind": "mcp", "size": st.st_size,
"mtime": int(st.st_mtime), "source": "mcp", "username": "",
"mime": "", "is_image": ext in IMG_EXT,
"file_type": (ext or "file").upper(),
"path": os.path.relpath(fp, DATA_DIR),
})
# 按功能(kind)分布:在 kind 过滤之前统计,下拉计数反映 scope/username 下的全集
_kc: dict = {}
for x in items:
k = str(x.get("kind") or "").strip() or "__none__"
_kc[k] = _kc.get(k, 0) + 1
kinds = sorted(({"k": k, "n": n} for k, n in _kc.items()), key=lambda y: (-y["n"], y["k"]))
if kind:
if kind == "__none__":
items = [x for x in items if not str(x.get("kind") or "").strip()]
else:
items = [x for x in items if str(x.get("kind") or "").strip() == kind]
items.sort(key=lambda x: -x["mtime"])
total = len(items)
total_size = sum(x["size"] for x in items)
pages = max(1, (total + page_size - 1) // page_size)
page = min(page, pages)
start = (page - 1) * page_size
page_items = items[start:start + page_size]
return 200, {"code": 200, "message": "ok", "data": {
"files": page_items, "total": total, "total_size": total_size,
"page": page, "page_size": page_size, "pages": pages, "count": len(page_items),
"kinds": kinds,
}}
if method == "POST" and sub == "/cleanup":
before = int(payload.get("before") or 0)
username = str(payload.get("username") or "")
if before <= 0:
return 400, {"code": 400, "message": "缺少时间参数"}
removed = 0
freed = 0
roots = []
if username:
roots.append((username, os.path.join(_user_dir(username), "files")))
else:
if os.path.isdir(USERS_ROOT):
for u in os.listdir(USERS_ROOT):
roots.append((u, os.path.join(USERS_ROOT, u, "files")))
for _u, fdir in roots:
if not os.path.isdir(fdir):
continue
for root, _d, fs in os.walk(fdir, topdown=False):
for f in fs:
fp = os.path.join(root, f)
try:
st = os.stat(fp)
except OSError:
continue
if st.st_mtime < before:
try:
freed += st.st_size
removed += 1
os.remove(fp)
except OSError:
pass
try:
if not os.listdir(root):
os.rmdir(root)
except OSError:
pass
return 200, {"code": 200, "message": "ok", "data": {"removed": removed, "freed": freed}}
if method == "POST" and sub == "/delete-file":
file_id = str(payload.get("id") or "")
username = str(payload.get("username") or "")
# MCP 文件删除:username 特殊标记 __mcp__,id=文件名(可含扩展名)
if username == "__mcp__":
if not file_id or file_id != os.path.basename(file_id) or file_id in (".", ".."):
return 400, {"code": 400, "message": "参数错误"}
mroot = os.path.realpath(os.path.join(DATA_DIR, "mcp_files"))
fp2 = os.path.realpath(os.path.join(mroot, file_id))
if not fp2.startswith(mroot + os.sep) or not os.path.isfile(fp2):
return 404, {"code": 404, "message": "文件不存在"}
size2 = os.path.getsize(fp2)
os.remove(fp2)
return 200, {"code": 200, "message": "ok", "data": {"freed": size2}}
if not re.match(r"^[A-Za-z0-9_-]{8,64}$", file_id) or not username:
return 400, {"code": 400, "message": "参数错误"}
# AI 生成图片删除:username 为特殊标记 __gen__,带 machine 定位 gen_images/<machine>/
if username == "__gen__":
machine = str(payload.get("machine") or "")
if not re.fullmatch(r"\d{10,16}", machine):
return 400, {"code": 400, "message": "参数错误"}
gdir = os.path.join(DATA_DIR, "gen_images", machine)
target = None
if os.path.isdir(gdir):
for fn in os.listdir(gdir):
if fn.startswith(file_id + ".") and os.path.isfile(os.path.join(gdir, fn)):
target = os.path.join(gdir, fn)
break
if not target:
return 404, {"code": 404, "message": "文件不存在"}
size = os.path.getsize(target)
os.remove(target)
idxp = os.path.join(gdir, "index.json")
with GEN_IMG_LOCK:
try:
idx = json.load(open(idxp, encoding="utf-8")) if os.path.exists(idxp) else []
idx = [it for it in idx if str((it or {}).get("id")) != file_id]
with open(idxp, "w", encoding="utf-8") as f:
json.dump(idx, f, ensure_ascii=False)
except Exception:
pass
return 200, {"code": 200, "message": "ok", "data": {"freed": size}}
base = os.path.join(_user_dir(username), "files")
target = os.path.join(base, file_id)
if not os.path.exists(target):
# search kind subdirs
target = None
for d in os.listdir(base) if os.path.isdir(base) else []:
p2 = os.path.join(base, d, file_id)
if os.path.exists(p2):
target = p2
break
if not target or not os.path.exists(target):
return 404, {"code": 404, "message": "文件不存在"}
size = os.path.getsize(target)
os.remove(target)
conn = _db()
conn.execute("DELETE FROM files WHERE id=?", (file_id,))
conn.commit()
conn.close()
return 200, {"code": 200, "message": "ok", "data": {"freed": size}}
if method == "POST" and sub == "/user-create":
username = str(payload.get("username") or "").strip()
password = str(payload.get("password") or "")
role = "admin" if payload.get("role") == "admin" else "user"
if not USER_NAME_RE.match(username):
return 400, {"code": 400, "message": "用户名需 3-32 位字母/数字/下划线/中文"}
if len(password) < 6:
return 400, {"code": 400, "message": "密码至少 6 位"}
salt = secrets.token_hex(16)
try:
conn = _db()
conn.execute("INSERT INTO users (username, salt, pw_hash, created, role) VALUES (?,?,?,?,?)",
(username, salt, _hash_pw(password, salt), time.time(), role))
conn.commit()
conn.close()
except sqlite3.IntegrityError:
return 409, {"code": 409, "message": "用户名已存在"}
_user_dir(username)
return 200, {"code": 200, "message": "ok"}
if method == "POST" and sub == "/user-update":
username = str(payload.get("username") or "").strip()
if not username:
return 400, {"code": 400, "message": "缺少用户名"}
conn = _db()
row = conn.execute("SELECT username FROM users WHERE username=?", (username,)).fetchone()
if not row:
conn.close()
return 404, {"code": 404, "message": "用户不存在"}
sets = []
args = []
new_pw = str(payload.get("password") or "")
if new_pw:
if len(new_pw) < 6:
conn.close()
return 400, {"code": 400, "message": "密码至少 6 位"}
salt = secrets.token_hex(16)
sets += ["salt=?", "pw_hash=?"]
args += [salt, _hash_pw(new_pw, salt)]
if "role" in payload:
role = "admin" if payload.get("role") == "admin" else "user"
# do not demote the default admin
if username == DEFAULT_ADMIN and role != "admin":
conn.close()
return 400, {"code": 400, "message": "默认管理员不可降级"}
sets += ["role=?"]
args.append(role)
if "disabled" in payload:
disabled = 1 if payload.get("disabled") else 0
if username == DEFAULT_ADMIN and disabled:
conn.close()
return 400, {"code": 400, "message": "默认管理员不可禁用"}
sets += ["disabled=?"]
args.append(disabled)
if not sets:
conn.close()
return 400, {"code": 400, "message": "没有要修改的内容"}
args.append(username)
conn.execute(f"UPDATE users SET {', '.join(sets)} WHERE username=?", args)
conn.commit()
conn.close()
return 200, {"code": 200, "message": "ok"}
if method == "POST" and sub == "/user-delete":
username = str(payload.get("username") or "").strip()
if not username:
return 400, {"code": 400, "message": "缺少用户名"}
if username == DEFAULT_ADMIN:
return 400, {"code": 400, "message": "默认管理员不可删除"}
conn = _db()
row = conn.execute("SELECT username FROM users WHERE username=?", (username,)).fetchone()
if not row:
conn.close()
return 404, {"code": 404, "message": "用户不存在"}
conn.execute("DELETE FROM users WHERE username=?", (username,))
conn.execute("DELETE FROM files WHERE username=?", (username,))
conn.commit()
conn.close()
udir = os.path.join(USERS_ROOT, username)
if os.path.isdir(udir):
shutil.rmtree(udir, ignore_errors=True)
return 200, {"code": 200, "message": "ok"}
if method == "GET" and sub == "/reco":
return 200, {"code": 200, "message": "ok", "data": _load_reco()}
if method == "POST" and sub == "/reco":
items = payload.get("items")
if not isinstance(items, list):
return 400, {"code": 400, "message": "items 必须是数组"}
clean = []
for it in items[:200]:
if not isinstance(it, dict):
continue
clean.append({
"path": str(it.get("path") or "").strip(),
"icon": str(it.get("icon") or "📌")[:8],
"name": str(it.get("name") or "").strip()[:40],
"desc": str(it.get("desc") or "").strip()[:120],
"enabled": bool(it.get("enabled", True)),
})
_save_reco(clean)
return 200, {"code": 200, "message": "ok"}
# ---------- 站内留言反馈(管理端查看/改状态/删除) ----------
if method == "GET" and sub == "/feedback":
items = sorted(_load_feedback(), key=lambda x: float(x.get("ts") or 0), reverse=True)
return 200, {"code": 200, "message": "ok", "data": items}
if method == "POST" and sub == "/feedback":
fid = str(payload.get("id") or "")
if not fid:
return 400, {"code": 400, "message": "缺少留言 id"}
with FEEDBACK_LOCK:
items = _load_feedback()
hit = next((it for it in items if it.get("id") == fid), None)
if not hit:
return 404, {"code": 404, "message": "留言不存在"}
if str(payload.get("action") or "") == "delete":
items = [it for it in items if it.get("id") != fid]
else:
status = str(payload.get("status") or "new")
hit["status"] = status if status in ("new", "replied", "done") else "new"
_save_feedback(items)
return 200, {"code": 200, "message": "ok"}
if method == "GET" and sub == "/storage":
total = _dir_size(USERS_ROOT) if os.path.isdir(USERS_ROOT) else 0
if os.path.isdir(os.path.join(DATA_DIR, "gen_images")):
total += _dir_size(os.path.join(DATA_DIR, "gen_images"))
for extra in ("users.db", "usage.db", "trials.json"):
fp = os.path.join(DATA_DIR, extra)
if os.path.exists(fp):
try:
total += os.path.getsize(fp)
except OSError:
pass
# current host path (strip prefix)
host_root = DATA_DIR
if os.path.isdir(HOST_PREFIX) and DATA_DIR.startswith(HOST_PREFIX):
host_root = DATA_DIR[len(HOST_PREFIX):] or "/"
return 200, {"code": 200, "message": "ok", "data": {
"root": host_root,
"container_root": DATA_DIR,
"users_dir": USERS_ROOT,
"size": total,
"host_mounted": os.path.isdir(HOST_PREFIX),
}}
if method == "POST" and sub == "/storage":
new_host_root = str(payload.get("root") or "").strip().rstrip("/")
if not new_host_root or not new_host_root.startswith("/"):
return 400, {"code": 400, "message": "请输入宿主机绝对路径,如 /vol1/xxx 或 /vol2/xxx"}
# block dangerous roots
banned = ("/bin", "/sbin", "/usr", "/etc", "/proc", "/sys", "/dev", "/boot", "/lib", "/root", "/home")
for b in banned:
if new_host_root == b or new_host_root.startswith(b + "/"):
return 400, {"code": 400, "message": "不允许使用系统目录"}
if not os.path.isdir(HOST_PREFIX):
return 400, {"code": 400, "message": "容器未挂载宿主机根目录(缺少 -v /:/hostfs 挂载)"}
new_root = _host_to_container(new_host_root)
if new_root == DATA_DIR:
return 400, {"code": 400, "message": "与当前路径相同,无需修改"}
try:
os.makedirs(new_root, exist_ok=True)
probe = os.path.join(new_root, ".write-test")
with open(probe, "w") as f:
f.write("ok")
os.remove(probe)
except Exception as e:
return 400, {"code": 400, "message": f"目标路径不可写: {e}"}
# migrate existing data (users dir + AI 生成图片 + db files)
moved = []
old_root = DATA_DIR
for item in ("users", "gen_images"):
src = os.path.join(old_root, item)
dst = os.path.join(new_root, item)
if os.path.isdir(src) and not os.path.isdir(dst):
shutil.move(src, dst)
moved.append(item)
for item in ("users.db", "usage.db", "trials.json"):
src = os.path.join(old_root, item)
dst = os.path.join(new_root, item)
if os.path.exists(src) and not os.path.exists(dst):
shutil.move(src, dst)
moved.append(item)
# persist config in the default host root (survives data root moves)
cfg = os.path.join(_host_to_container(DEFAULT_HOST_ROOT), "config.json")
try:
os.makedirs(os.path.dirname(cfg), exist_ok=True)
with open(cfg, "w") as f:
json.dump({"storage_root": new_host_root}, f)
except Exception:
return 500, {"code": 500, "message": "配置写入失败"}
# restart to take effect (restart always brings the container back)
import threading as _th
def _restart():
os._exit(0)
_th.Timer(1.5, _restart).start()
return 200, {"code": 200, "message": "ok", "data": {"root": new_root, "moved": moved, "restarting": True}}
if method == "GET" and sub == "/usage":
qs = parse.parse_qs(parse.urlparse(handler.path).query)
days = _int_str((qs.get("days") or ["7"])[0], 1, 365, 7)
since = time.time() - days * 86400
conn = _usage_db()
by_tool = conn.execute("SELECT tool, COUNT(*) c FROM usage WHERE at>? GROUP BY tool ORDER BY c DESC LIMIT 30", (since,)).fetchall()
by_user = conn.execute("SELECT username, COUNT(*) c, MAX(at) last FROM usage WHERE at>? AND username!='' GROUP BY username ORDER BY c DESC LIMIT 50", (since,)).fetchall()
total = conn.execute("SELECT COUNT(*) FROM usage WHERE at>?", (since,)).fetchone()[0]
# 未登录访客(username 为空)聚合:访客的功能使用也统计并展示
g = conn.execute("SELECT COUNT(*) c, MAX(at) last FROM usage WHERE at>? AND username=''", (since,)).fetchone()
conn.close()
return 200, {"code": 200, "message": "ok", "data": {
"days": days,
"total": total,
"by_tool": [{"tool": t, "count": c} for t, c in by_tool],
"by_user": [{"username": u, "count": c, "last": int(l or 0)} for u, c, l in by_user],
"guest": {"count": int(g[0] or 0), "last": int(g[1] or 0)},
"online": online_count(),
}}
if method == "GET" and sub == "/mcp-usage":
qs = parse.parse_qs(parse.urlparse(handler.path).query)
days = _int_str((qs.get("days") or ["7"])[0], 1, 365, 7)
return 200, {"code": 200, "message": "ok", "data": mcp_ops.mcp_usage_query(days)}
return 404, {"code": 404, "message": "unknown admin api"}
def _req_machine(handler):
return handler.headers.get("X-Machine-Id", "") if handler else ""
# ---- 本地图片验证码(替代 Cloudflare Turnstile:不依赖外网,服务端出图) ----
try:
from PIL import Image, ImageDraw, ImageFont
_HAS_PIL = True
except Exception: # pragma: no cover
_HAS_PIL = False
CAPTCHA_TTL = 300 # 验证码有效期(秒)
_CAPTCHA_MAX = 5000 # 内存上限(防撑爆)
_CAPTCHAS = {} # id -> (code, expire_ts),单次核销
_CAPTCHA_LOCK = threading.Lock()
_CAPTCHA_CHARS = "ABCDEFGHJKMNPQRSTUVWXYZ23456789" # 去掉易混字符 0/O/1/I/L
def _gen_captcha():
"""返回 (captcha_id, png_base64)。4 位干扰码,5 分钟过期,取用即销毁。"""
import base64
import random
import uuid
code = "".join(random.choice(_CAPTCHA_CHARS) for _ in range(4))
cid = uuid.uuid4().hex
w, h = 148, 52
img = Image.new("RGB", (w, h), (247, 248, 251))
d = ImageDraw.Draw(img)
try:
font = ImageFont.load_default(size=36)
except TypeError: # pragma: no cover
font = ImageFont.load_default()
# 干扰线 + 噪点
for _ in range(7):
d.line([random.randrange(w), random.randrange(h),
random.randrange(w), random.randrange(h)],
fill=(random.randint(160, 210), random.randint(160, 210), random.randint(160, 210)),
width=1)
for _ in range(60):
d.point((random.randrange(w), random.randrange(h)),
fill=(random.randint(150, 220), random.randint(150, 220), random.randint(150, 220)))
# 逐字带抖动绘制
x = 14
for ch in code:
d.text((x, 6 + random.randint(-4, 4)), ch, font=font,
fill=(random.randint(10, 90), random.randint(10, 90), random.randint(10, 90)))
x += 33
buf = io.BytesIO()
img.save(buf, format="PNG")
now = time.time()
with _CAPTCHA_LOCK:
for k in [k for k, v in _CAPTCHAS.items() if v[1] < now]:
_CAPTCHAS.pop(k, None)
while len(_CAPTCHAS) >= _CAPTCHA_MAX:
_CAPTCHAS.pop(next(iter(_CAPTCHAS)), None)
_CAPTCHAS[cid] = (code, now + CAPTCHA_TTL)
return cid, base64.b64encode(buf.getvalue()).decode()
def verify_captcha(payload) -> bool:
"""核销 captcha_id + captcha_code(单次有效,大小写不敏感)。"""
cid = str(payload.get("captcha_id") or "")
got = str(payload.get("captcha_code") or "").strip().upper()
if not cid or not got:
return False
with _CAPTCHA_LOCK:
entry = _CAPTCHAS.pop(cid, None) # 无论对错都销毁,防爆破
if not entry:
return False
stored, exp = entry
if time.time() > exp:
return False
import secrets as _secrets
return _secrets.compare_digest(stored, got)
# ============ 站内留言反馈(独立于卡密/授权系统,任何人可用) ============
# ---- CardAuth hub 接入(项目 6):在线统计上报 + 公告推送,失败开放(不可达不影响站点) ----
HUB_TIMEOUT = 8
def hub_announcements(limit: int = 20) -> tuple[int, dict]:
"""GET /hub/announcements — 服务端拉本项目公告,双保险过滤 project_id。"""
try:
res = _get_json(
f"/public/announcements?target=client&project_id={PROJECT_ID}&limit={max(1, min(int(limit), 50))}",
{}, timeout=HUB_TIMEOUT)
except Exception as exc: # noqa: BLE001
print(f"[hub] announcements failed: {exc}", flush=True)
return 503, {"code": 503, "message": "公告服务暂不可用",
"data": {"list": [], "popup": []}}
data = (res or {}).get("data") or {}
def mine(arr) -> list:
out = []
for a in arr or []:
if not isinstance(a, dict):
continue
pid = a.get("project_id")
if pid in (None, "", 0) or str(pid) == str(PROJECT_ID):
out.append(a)
return out
return 200, {"code": 200, "message": "ok",
"data": {"list": mine(data.get("list")), "popup": mine(data.get("popup"))}}
def hub_report(payload: dict) -> tuple[int, dict]:
"""POST /hub/report {machine_id, client_version, device_info}
— 安装登记(安装统计)+ 心跳(在线统计),顺带返回版本推送信息。"""
machine = str(payload.get("machine_id") or "")
version = str(payload.get("client_version") or "0")[:40]
device = str(payload.get("device_info") or "")[:200] or "web"
if not MACHINE_RE.match(machine):
return 400, {"code": 400, "message": "机器码格式错误"}
if not (CARD_AUTH_KEY and PROJECT_ID):
return 503, {"code": 503, "message": "授权中心未配置"}
hdr = {"X-Api-Key": CARD_AUTH_KEY, "X-Project-Id": str(PROJECT_ID)}
out: dict = {"install_id": None, "latest_version": "",
"update_available": False, "update_required": False,
"download_url": None, "changelog": None, "online": False}
# 心跳必须在 check-update 之前:实测 CardAuth heartbeat 会把
# client_version/device_info 清空,check-update 放最后确保版本号落库
try:
hb = _post_json("/public/installations/heartbeat", {"machine_id": machine},
hdr, timeout=HUB_TIMEOUT)
out["online"] = (hb or {}).get("code") == 200
except Exception as exc: # noqa: BLE001
print(f"[hub] heartbeat failed: {exc}", flush=True)
try:
res = _post_json("/public/installations/check-update",
{"machine_id": machine, "client_version": version,
"device_info": device}, hdr, timeout=HUB_TIMEOUT)
d = (res or {}).get("data") or {}
out.update({
"install_id": d.get("install_id"),
"latest_version": d.get("latest_version") or "",
"update_available": bool(d.get("update_available")),
"update_required": bool(d.get("update_required")),
"download_url": d.get("download_url"),
"changelog": d.get("changelog"),
})
except Exception as exc: # noqa: BLE001
print(f"[hub] check-update failed: {exc}", flush=True)
return 503, {"code": 503, "message": "授权中心暂不可用", "data": out}
return 200, {"code": 200, "message": "ok", "data": out}
# GET /feedback/captcha 图形验证码
# POST /feedback {type,content,contact,captcha_id,captcha_code}
FEEDBACK_FILE = os.path.join(DATA_DIR, "feedback.json")
FEEDBACK_TYPES = ("功能建议", "意见反馈")
FEEDBACK_WINDOW = 3600 # 同一 IP 两次提交的最小间隔(秒,1 小时)
FEEDBACK_MAX = 2000 # 存档上限,超出丢弃最旧
FEEDBACK_LOCK = threading.Lock()
def _load_feedback() -> list:
try:
with open(FEEDBACK_FILE, "r", encoding="utf-8") as fh:
data = json.load(fh)
return data if isinstance(data, list) else []
except Exception: # noqa: BLE001
return []
def _save_feedback(items: list) -> None:
tmp = FEEDBACK_FILE + ".tmp"
with open(tmp, "w", encoding="utf-8") as fh:
json.dump(items, fh, ensure_ascii=False, indent=1)
os.replace(tmp, FEEDBACK_FILE)
def _ip_is_public(value: str) -> bool:
try:
import ipaddress
ip = ipaddress.ip_address(value)
except Exception: # noqa: BLE001
return False
return not (
ip.is_private or ip.is_loopback or ip.is_link_local
or ip.is_reserved or ip.is_multicast
)
def _real_client_ip(handler) -> str:
"""取真实客户端 IP(限频用)。
链路:浏览器 → nps/frp 隧道(注入真实 IP 的 X-Forwarded-For)
→ nginx(再追加 $remote_addr = 隧道/内网地址)→ relay。
因此 XFF 从右往左取第一个公网地址:先跳过隧道与内网地址,
客户端在链路最左侧伪造的头也不会被采信。
"""
raw = handler.headers.get("X-Forwarded-For") or handler.headers.get("X-Real-IP") or ""
parts = [p.strip() for p in raw.split(",") if p.strip()]
for part in reversed(parts):
if _ip_is_public(part):
return part
if parts:
return parts[0]
try:
return handler.client_address[0] or "unknown"
except Exception: # noqa: BLE001
return "unknown"
def feedback_api(method: str, payload: dict, handler) -> tuple:
"""公开留言接口:GET 出图形验证码,POST 提交留言。"""
if method == "GET":
if not _HAS_PIL:
return 503, {"code": 503, "message": "验证码组件未就绪,请稍后再试"}
cid, img_b64 = _gen_captcha()
return 200, {"code": 200, "message": "ok", "data": {"id": cid, "img": img_b64}}
# POST:先人机验证,再业务校验,最后同 IP 1 小时限频
if not verify_captcha(payload):
return 403, {"code": 403, "message": "人机验证失败,请刷新验证码后重试"}
ftype = str(payload.get("type") or "").strip()
if ftype not in FEEDBACK_TYPES:
return 400, {"code": 400, "message": "请选择留言类型:功能建议 / 意见反馈"}
content = str(payload.get("content") or "").replace("\r\n", "\n").strip()
if not content:
return 400, {"code": 400, "message": "请填写留言内容"}
if len(content) > 1000:
return 400, {"code": 400, "message": "留言内容最多 1000 字"}
contact = str(payload.get("contact") or "").strip()
if len(contact) > 64:
return 400, {"code": 400, "message": "联系方式最多 64 字"}
if any(ord(c) < 32 for c in contact):
return 400, {"code": 400, "message": "联系方式含非法字符"}
ip = _real_client_ip(handler)
now = time.time()
with FEEDBACK_LOCK:
items = _load_feedback()
last_ts = 0.0
for it in reversed(items):
if it.get("ip") == ip:
try:
last_ts = float(it.get("ts") or 0)
except Exception: # noqa: BLE001
last_ts = 0.0
break
if now - last_ts < FEEDBACK_WINDOW:
wait_min = max(int((FEEDBACK_WINDOW - (now - last_ts)) / 60), 1)
return 429, {"code": 429, "message": f"同一 IP 一小时内只能提交一次,请 {wait_min} 分钟后再试"}
items.append({
"id": f"{int(now)}-{secrets.token_hex(4)}",
"ts": int(now),
"ip": ip,
"type": ftype,
"content": content,
"contact": contact,
"status": "new",
})
if len(items) > FEEDBACK_MAX:
items = items[-FEEDBACK_MAX:]
_save_feedback(items)
print(f"[feedback] new from {ip} [{ftype}] {len(content)} chars", flush=True)
return 200, {"code": 200, "message": "ok"}
def user_api(method, sub, payload, handler):
"""returns (status, body_dict)"""
if method == "GET" and sub == "/captcha":
if not _HAS_PIL:
return 503, {"code": 503, "message": "验证码组件未就绪"}
cid, img_b64 = _gen_captcha()
return 200, {"code": 200, "message": "ok", "data": {"id": cid, "img": img_b64}}
if method == "POST" and sub == "/register":
if not verify_captcha(payload):
return 403, {"code": 403, "message": "人机验证失败,请刷新验证码后重试"}
username = str(payload.get("username") or "").strip()
password = str(payload.get("password") or "")
if not USER_NAME_RE.match(username):
return 400, {"code": 400, "message": "用户名需 3-32 位字母/数字/下划线/中文"}
if len(password) < 6:
return 400, {"code": 400, "message": "密码至少 6 位"}
conn = _db()
try:
salt = secrets.token_hex(16)
conn.execute("INSERT INTO users (username, salt, pw_hash, created, last_login) VALUES (?,?,?,?,?)", (username, salt, _hash_pw(password, salt), time.time(), time.time()))
conn.commit()
except sqlite3.IntegrityError:
return 409, {"code": 409, "message": "用户名已被注册"}
finally:
conn.close()
_user_dir(username)
_bind_machine(username, _req_machine(handler))
return 200, {"code": 200, "message": "ok", "data": {"token": _make_token(username), "username": username}}
if method == "POST" and sub == "/login":
username = str(payload.get("username") or "").strip()
password = str(payload.get("password") or "")
# admin accounts skip the human check (admin panel login has no captcha widget)
role, _disabled = _user_attr(username)
if role != "admin" and not verify_captcha(payload):
return 403, {"code": 403, "message": "人机验证失败,请刷新验证码后重试"}
conn = _db()
row = conn.execute("SELECT salt, pw_hash FROM users WHERE username=?", (username,)).fetchone()
conn.close()
if not row or _hash_pw(password, row[0]) != row[1]:
return 401, {"code": 401, "message": "用户名或密码错误"}
_user_dir(username)
_bind_machine(username, _req_machine(handler))
try:
_conn2 = _db()
_conn2.execute("UPDATE users SET last_login=? WHERE username=?", (time.time(), username))
_conn2.commit()
_conn2.close()
except Exception:
pass
role, disabled = _user_attr(username)
if disabled:
return 403, {"code": 403, "message": "账号已被禁用,请联系管理员"}
return 200, {"code": 200, "message": "ok", "data": {"token": _make_token(username), "username": username, "role": role or "user"}}
# ---- below requires auth ----
username = _auth_user(handler)
if not username:
return 401, {"code": 401, "message": "请先登录"}
if _user_disabled(username):
return 403, {"code": 403, "message": "账号已被禁用,请联系管理员"}
if method == "GET" and sub == "/me":
role, _d = _user_attr(username)
return 200, {"code": 200, "message": "ok", "data": {"username": username, "role": role}}
if method == "GET" and sub == "/data":
path = os.path.join(_user_dir(username), "data.json")
if not os.path.exists(path):
return 200, {"code": 200, "message": "ok", "data": {"data": {}, "updated": 0}}
try:
with open(path) as f:
saved = json.load(f)
except Exception:
saved = {}
return 200, {"code": 200, "message": "ok", "data": {"data": saved.get("data", {}), "updated": saved.get("updated", 0)}}
if method == "POST" and sub == "/data":
data = payload.get("data")
if not isinstance(data, dict):
return 400, {"code": 400, "message": "data 需为对象"}
path = os.path.join(_user_dir(username), "data.json")
tmp = path + ".tmp"
with open(tmp, "w") as f:
json.dump({"data": data, "updated": time.time()}, f, ensure_ascii=False)
os.replace(tmp, path)
return 200, {"code": 200, "message": "ok"}
if method == "POST" and sub == "/files":
files = payload.get("_files") or {}
fields = payload.get("_fields") or {}
fobj = files.get("file")
if not fobj:
return 400, {"code": 400, "message": "缺少 file 字段"}
fname, fdata, fmime = fobj
if len(fdata) > MAX_UPLOAD:
return 413, {"code": 413, "message": "文件超过 " + str(MAX_UPLOAD // 1024 // 1024) + "MB 限制"}
if not fdata:
return 400, {"code": 400, "message": "文件为空"}
file_id = uuid.uuid4().hex[:16]
safe_name = os.path.basename(str(fname))[:120] or "file"
kind = re.sub(r"[^A-Za-z0-9_-]", "", str(fields.get("kind") or ""))[:32]
fdir = os.path.join(_user_dir(username), "files")
os.makedirs(fdir, exist_ok=True)
if kind:
fdir = os.path.join(fdir, kind)
os.makedirs(fdir, exist_ok=True)
with open(os.path.join(fdir, file_id), "wb") as f:
f.write(fdata)
conn = _db()
conn.execute("INSERT INTO files VALUES (?,?,?,?,?,?)", (file_id, username, safe_name, len(fdata), fmime, time.time()))
conn.commit()
conn.close()
return 200, {"code": 200, "message": "ok", "data": {"id": file_id, "name": safe_name, "size": len(fdata), "kind": kind}}
if method == "GET" and sub == "/files":
conn = _db()
rows = conn.execute("SELECT id, name, size, mime, created FROM files WHERE username=? ORDER BY created DESC LIMIT 200", (username,)).fetchall()
conn.close()
files = [{"id": r[0], "name": r[1], "size": r[2], "mime": r[3], "created": int(r[4])} for r in rows]
return 200, {"code": 200, "message": "ok", "data": {"files": files}}
if sub.startswith("/file/"):
file_id = sub[len("/file/"):]
if not re.match(r"^[A-Za-z0-9_-]{8,64}$", file_id):
return 400, {"code": 400, "message": "文件ID无效"}
conn = _db()
row = conn.execute("SELECT id, username, name, size, mime, created FROM files WHERE id=?", (file_id,)).fetchone()
conn.close()
if not row or row[1] != username:
return 404, {"code": 404, "message": "文件不存在"}
fpath = None
base = os.path.join(_user_dir(username), "files")
if os.path.exists(os.path.join(base, file_id)):
fpath = os.path.join(base, file_id)
else:
for d in os.listdir(base):
p = os.path.join(base, d, file_id)
if os.path.exists(p):
fpath = p
break
if not fpath:
return 404, {"code": 404, "message": "文件不存在"}
if method == "GET":
if not os.path.exists(fpath):
return 404, {"code": 404, "message": "文件不存在"}
with open(fpath, "rb") as f:
data = f.read()
handler.send_response(200)
handler.send_header("Content-Type", row[4] or "application/octet-stream")
handler.send_header("Content-Length", str(len(data)))
handler.send_header("Content-Disposition", "attachment; filename*=UTF-8''" + parse.quote(row[2]))
handler.end_headers()
handler.wfile.write(data)
return None, None
if method == "DELETE":
if fpath and os.path.exists(fpath):
os.remove(fpath)
conn = _db()
conn.execute("DELETE FROM files WHERE id=?", (file_id,))
conn.commit()
conn.close()
return 200, {"code": 200, "message": "ok"}
return 404, {"code": 404, "message": "unknown user api"}
class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def log_message(self, fmt, *args): # quiet
pass
def _reply(self, status: int, body: bytes = b"", ctype="text/plain"):
self.send_response(status)
self.send_header("Content-Type", ctype)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
if body:
self.wfile.write(body)
# ---------- GET ----------
def _handle_user(self, method):
parsed = parse.urlparse(self.path)
sub = parsed.path[len("/user"):]
payload = {}
if method == "POST":
try:
ctype = self.headers.get("Content-Type", "")
length = int(self.headers.get("Content-Length") or 0)
if length > MAX_UPLOAD + 1024 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "文件过大"}, ensure_ascii=False).encode(), "application/json")
return
raw = self.rfile.read(length) if length else b"{}"
if ctype.startswith("multipart/form-data"):
env = {"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype, "CONTENT_LENGTH": str(length)}
fs = cgi.FieldStorage(fp=io.BytesIO(raw), environ=env, keep_blank_values=True)
files = {}
fields = {}
for k in fs.keys():
item = fs[k]
if getattr(item, "filename", None):
files[k] = (item.filename, item.file.read(), item.type or "application/octet-stream")
else:
fields[k] = item.value
payload = {"_fields": fields, "_files": files}
else:
payload = json.loads(raw.decode("utf-8") or "{}")
except Exception:
self._reply(400, json.dumps({"code": 400, "message": "无效请求体"}, ensure_ascii=False).encode(), "application/json")
return
status, body = user_api(method, sub, payload, self)
if status is None:
return # response already sent (file download)
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_admin(self, method):
parsed = parse.urlparse(self.path)
sub = parsed.path[len("/admin"):]
payload = {}
if method == "POST":
try:
length = int(self.headers.get("Content-Length") or 0)
raw = self.rfile.read(length) if length else b"{}"
payload = json.loads(raw.decode("utf-8") or "{}")
except Exception:
self._reply(400, json.dumps({"code": 400, "message": "无效请求体"}, ensure_ascii=False).encode(), "application/json")
return
status, body = admin_api(method, sub, payload, self)
if status is None:
return # 二进制下载分支已自行写出响应
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
# ---------- 网速测试(用户浏览器 <-> 本站服务器) ----------
_SPEED_CHUNK = (os.urandom(65536) + bytes(range(256)) * 256) * 2 # 256KB 混合块
def _handle_speed_get(self, parsed):
"""下载测速:吐 N 字节数据(上限 30MB/次),no-store 防缓存。"""
try:
qs = parse.parse_qs(parsed.query)
n = int((qs.get("bytes") or ["1048576"])[0])
except Exception:
n = 1048576
n = max(1024, min(n, 30 * 1024 * 1024))
self.send_response(200)
self.send_header("Content-Type", "application/octet-stream")
self.send_header("Content-Length", str(n))
self.send_header("Cache-Control", "no-store, no-cache, must-revalidate")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
chunk = self._SPEED_CHUNK
try:
left = n
while left > 0:
b = chunk if left >= len(chunk) else chunk[:left]
self.wfile.write(b)
left -= len(b)
except (BrokenPipeError, ConnectionResetError):
pass
def _handle_speed_post(self):
"""上传测速:收满数据丢弃,回报收到字节数。"""
try:
length = int(self.headers.get("Content-Length") or 0)
except Exception:
length = 0
length = max(0, min(length, 60 * 1024 * 1024))
got = 0
try:
while got < length:
b = self.rfile.read(min(65536, length - got))
if not b:
break
got += len(b)
except (BrokenPipeError, ConnectionResetError):
pass
body = json.dumps({"code": 200, "message": "ok", "data": {"received": got}}, ensure_ascii=False).encode("utf-8")
self._reply(200, body, "application/json")
def do_GET(self):
parsed = parse.urlparse(self.path)
if parsed.path == "/speed":
self._handle_speed_get(parsed)
return
if parsed.path == "/ai/vision-models":
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": VISION_MODELS}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/ai/image-models":
self._handle_image_models()
return
if parsed.path == "/ai/img-history":
self._handle_img_history()
return
if parsed.path == "/draft/load":
self._handle_draft_load(parsed)
return
if parsed.path == "/draft/history":
self._handle_draft_history(parsed)
return
if parsed.path.startswith("/ai/img/"):
self._handle_img_file(parsed)
return
if parsed.path == "/ai/models":
self._handle_ai_models()
return
if parsed.path == "/mcp":
info = {"code": 200,
"message": "IT-TOOLS-PRO MCP endpoint (Streamable HTTP / JSON-RPC 2.0)",
"protocol": "mcp",
"endpoint": "/mcp",
"auth": "Authorization: Bearer <MCP令牌>(站内「MCP 接入」页购买卡密后生成)",
"tools": [t["name"] for t in mcp_ops.TOOLS]}
self._reply(200, json.dumps(info, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/mcp/tokens":
qs = parse.parse_qs(parsed.query)
status, body = mcp_ops.tokens_api("GET", {}, qs)
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path.startswith("/mcp/file/"):
mcp_ops.handle_get_file(self, parsed.path[len("/mcp/file/"):])
return
if parsed.path.startswith("/admin"):
self._handle_admin("GET")
return
if parsed.path.startswith("/user"):
self._handle_user("GET")
return
if parsed.path == "/auth-status":
qs = parse.parse_qs(parsed.query)
machine = (qs.get("machine_id") or [""])[0]
if not MACHINE_RE.match(machine):
self._reply(400, json.dumps({"code": 400, "message": "机器码格式错误"}, ensure_ascii=False).encode(), "application/json")
return
authorized = False
if machine not in _load_revoked():
authorized = query_authorized(machine) or _account_license_ok(machine)
body = {"code": 200, "message": "ok", "data": {"authorized": authorized}}
self._reply(200, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/feedback/captcha":
status, body = feedback_api("GET", {}, self)
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/hub/announcements":
qs = parse.parse_qs(parsed.query or "")
try:
limit = int((qs.get("limit") or ["20"])[0] or 20)
except ValueError:
limit = 20
status, body = hub_announcements(limit)
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/reco":
items = [x for x in _load_reco() if x.get("enabled")][:24]
body = json.dumps({"code": 200, "message": "ok", "data": items}, ensure_ascii=False).encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
return
if parsed.path == "/news":
data = _load_news_file()
if not data.get("columns"):
# 缓存为空(首次):同步抓一次
try:
_maybe_refresh_news()
data = _load_news_file()
except Exception as e: # noqa: BLE001
print(f"[news] on-demand failed: {e}", flush=True)
# 股票列:交易时段按需重抓(60s 节流),其余列仍每天 8 点更新
data = _refresh_stock_if_stale(data)
# 开盘状态:前端在股票列显示 交易中/休市
data = dict(data or {})
data["market"] = _market_status()
body = {"code": 200, "message": "ok", "data": data}
self._reply(200, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/stock/quotes":
q = parse.parse_qs(parsed.query or "")
codes = (q.get("codes") or [""])[0]
items = _fetch_quotes([c for c in codes.split(",") if c])
body = {"code": 200, "message": "ok", "data": {"items": items}}
self._reply(200, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/sites":
status, body = sites_api("GET", "", {})
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/check":
machine = self.headers.get("X-Machine-Id", "")
tool = self.headers.get("X-Tool", "")
status = check(machine, tool)
if status == 200:
log_usage(machine, tool)
self._reply(status, b"ok" if status == 200 else b"forbidden")
return
if parsed.path == "/translate":
# 本地翻译:走内网 NewAPI LLM,不再请求谷歌(网络不可达导致 JSON 解析失败)
qs = parse.parse_qs(parsed.query or "")
q = (qs.get("q") or [""])[0]
sl = (qs.get("sl") or ["auto"])[0]
tl = (qs.get("tl") or ["zh-CN"])[0]
try:
translated, detected = _translate_local(q, sl, tl)
# 返回谷歌兼容结构:[[ [译文, 原文, ...] ], null, 检测语言],前端零改动
payload = json.dumps(
[[[translated, q, None, None, 10]], None, detected],
ensure_ascii=False,
).encode("utf-8")
self._reply(200, payload, "application/json; charset=utf-8")
except Exception as exc: # noqa: BLE001
payload = f'{{"error":"relay error: {exc}"}}'.encode()
self._reply(502, payload, "application/json")
return
# ---- IP 归属地(仅 /ip-geo?ip= 指定查询)----
# 安全红线(2026-10-30):/my-ip 已永久删除。本服务处于 frp/反代链路后,
# XFF/X-Real-IP 拿到的是服务器出口地址而非真实客户端 IP,任何"回显接入方 IP"
# 的端点都会泄露服务器 IP。客户端公网 IP 必须由浏览器侧直接查外部服务获得。
if parsed.path == "/my-ip":
self._reply(404, json.dumps({"code": 404, "message": "已下线:客户端 IP 请在浏览器侧查询,服务端不提供回显(避免暴露服务器出口地址)"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/ip-geo":
ipq = (parse.parse_qs(parsed.query or "").get("ip") or [""])[0].strip()
if not ipq:
self._reply(400, json.dumps({"code": 400, "message": "缺少 ip 参数:请先在浏览器侧获取客户端公网 IP 再查询归属地"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if not _ip_valid(ipq):
self._reply(400, json.dumps({"code": 400, "message": "IP 格式错误"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
data = _ip_info(ipq)
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": data}, ensure_ascii=False).encode("utf-8"), "application/json")
return
# ---- 代理/VPN 判定(/ip-check?ip= 浏览器侧自己的公网 IP)----
if parsed.path == "/ip-check":
ipq = (parse.parse_qs(parsed.query or "").get("ip") or [""])[0].strip()
if not ipq or not _ip_valid(ipq):
self._reply(400, json.dumps({"code": 400, "message": "缺少或非法 ip 参数"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
data = _ip_check(ipq)
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": data}, ensure_ascii=False).encode("utf-8"), "application/json")
return
self._reply(404, b"not found")
# ---------- POST ----------
def _read_payload(self) -> dict:
try:
length = int(self.headers.get("Content-Length") or 0)
raw = self.rfile.read(length) if length else b"{}"
return json.loads(raw.decode("utf-8") or "{}")
except Exception: # noqa: BLE001
return {}
# ---------- AI 图片:抠图 / 放大 ----------
_bg_session = None
def _handle_removebg(self):
machine = self.headers.get("X-Machine-Id", "")
if check(machine, "ai-image-tools") != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
length = int(self.headers.get("Content-Length") or 0)
if length > 30 * 1024 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "图片超过 30MB 限制"}, ensure_ascii=False).encode(), "application/json")
return
raw = self.rfile.read(length) if length else b""
if not raw:
self._reply(400, json.dumps({"code": 400, "message": "未收到图片数据"}, ensure_ascii=False).encode(), "application/json")
return
from rembg import new_session, remove as _bgremove
cls = type(self)
if cls._bg_session is None:
cls._bg_session = new_session("u2net")
out = _bgremove(raw, session=cls._bg_session)
log_usage(machine, "ai-image-tools")
self._reply(200, out, "image/png")
except Exception as e: # noqa: BLE001
self._reply(500, json.dumps({"code": 500, "message": f"抠图失败: {e}"}, ensure_ascii=False).encode(), "application/json")
def _handle_upscale(self):
machine = self.headers.get("X-Machine-Id", "")
if check(machine, "ai-image-tools") != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
q = parse.parse_qs(parse.urlparse(self.path).query)
scale = (q.get("scale") or ["2"])[0]
noise = (q.get("noise") or ["1"])[0]
if scale not in ("2", "4"):
scale = "2"
if noise not in ("-1", "0", "1", "2", "3"):
noise = "1"
length = int(self.headers.get("Content-Length") or 0)
if length > 30 * 1024 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "图片超过 30MB 限制"}, ensure_ascii=False).encode(), "application/json")
return
raw = self.rfile.read(length) if length else b""
if not raw:
self._reply(400, json.dumps({"code": 400, "message": "未收到图片数据"}, ensure_ascii=False).encode(), "application/json")
return
ctype = (self.headers.get("Content-Type") or "").split(";")[0].strip()
ext = {"image/png": "png", "image/webp": "webp"}.get(ctype, "jpg")
wd = "/tmp/up-" + uuid.uuid4().hex[:12]
os.makedirs(wd, exist_ok=True)
fin = os.path.join(wd, f"in.{ext}")
fout = os.path.join(wd, f"out.{ext}")
with open(fin, "wb") as f:
f.write(raw)
import subprocess as _sp
r = _sp.run(
["/app/waifu2x/waifu2x-ncnn-vulkan", "-i", fin, "-o", fout, "-s", scale, "-n", noise, "-g", "-1", "-t", "128"],
capture_output=True, text=True, timeout=180,
)
if r.returncode != 0 or not os.path.exists(fout):
raise RuntimeError((r.stderr or r.stdout or "waifu2x failed")[-300:])
with open(fout, "rb") as f:
out = f.read()
shutil.rmtree(wd, ignore_errors=True)
log_usage(machine, "ai-image-tools")
self._reply(200, out, ctype or "image/jpeg")
except Exception as e: # noqa: BLE001
self._reply(500, json.dumps({"code": 500, "message": f"放大失败: {e}"}, ensure_ascii=False).encode(), "application/json")
# ---------- 共用 LLM 调用 ----------
def _llm_chat(self, messages: list, model: str = "") -> str:
key = os.environ.get("NEWAPI_KEY", "")
upstream = os.environ.get("NEWAPI_URL", "http://192.168.68.19:3099/v1/chat/completions")
model = (model or os.environ.get("NEWAPI_MODEL", "qwen3.8-flash"))[:60]
if not key:
raise RuntimeError("AI 服务未配置")
body = json.dumps({"model": model, "messages": messages, "temperature": 0.7, "max_tokens": 4096}).encode()
req = request.Request(upstream, data=body, headers={"Content-Type": "application/json", "Authorization": "Bearer " + key})
resp = request.urlopen(req, timeout=180)
data = json.loads(resp.read())
if "error" in data:
raise RuntimeError(data["error"].get("message", "upstream error"))
return data["choices"][0]["message"]["content"]
# ---------- 文案工具箱 ----------
COPY_SCENES = {
"rewrite": "你是一名资深中文编辑。请改写用户提供的文字,保持原意但改变表达方式和句式结构,语言自然流畅,直接输出改写后的正文,不要解释。",
"deaigc": "你是消除 AI 腔调的中文改写专家。把用户提供的文字改写得更像真人手写:加入口语化表达、长短句交错、具体细节,降低 AI 生成痕迹(避免排比、避免'首先其次最后'式结构、避免空洞套话),保持原意,直接输出改写后的正文。",
"titles": "你是爆款标题专家。根据用户提供的主题,生成 10 个吸引点击的中文标题,要求:每行一个、不超过 30 字、风格多样(悬念/数字/痛点/对比/好奇),直接输出标题列表,不要其他文字。",
"xhs": "你是小红书文案高手。根据用户提供的主题写一篇小红书笔记:开头抓人(1-2 句)、正文分段带 emoji、结尾加 5-8 个话题标签(#开头)。总字数 300-600 字,语气亲切接地气,直接输出笔记内容。",
"moments": "你是朋友圈文案高手。根据用户提供的主题写 3 条不同风格的微信朋友圈短文案:一条文艺、一条幽默、一条简洁有梗,每条不超过 60 字、可带 1-2 个 emoji,直接输出 3 条文案(每条一行)。",
}
def _handle_copy(self):
payload = self._read_payload()
machine = str(payload.get("machine") or "")
if check(machine, "copywriting") != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
scene = str(payload.get("scene") or "")
if scene not in self.COPY_SCENES:
self._reply(400, json.dumps({"code": 400, "message": "无效的场景"}, ensure_ascii=False).encode(), "application/json")
return
text = str(payload.get("text") or "").strip()[:8000]
if not text:
self._reply(400, json.dumps({"code": 400, "message": "请输入内容"}, ensure_ascii=False).encode(), "application/json")
return
try:
reply = self._llm_chat([
{"role": "system", "content": self.COPY_SCENES[scene]},
{"role": "user", "content": text},
], model=(str(payload.get("model") or "") if str(payload.get("model") or "") in ("mimo-v2.6-flash", "glm-5.3-flash") else ""))
log_usage(machine, "copywriting")
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"reply": reply}}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(502, json.dumps({"code": 502, "message": f"AI 服务异常: {e}"}, ensure_ascii=False).encode(), "application/json")
# ---------- 文档翻译:图片/扫描件 OCR ----------
_DOC_IMG_EXTS = (".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif", ".tiff", ".tif")
def _doc_ocr_one(self, img_bytes):
"""调用宿主 rapidocr 识别一张图,按阅读顺序(行聚类)拼成文本"""
import secrets as _sec
boundary = "----td" + _sec.token_hex(8)
parts = []
parts.append(("--%s\r\nContent-Disposition: form-data; name=\"image_file\"; filename=\"page.png\"\r\nContent-Type: application/octet-stream\r\n\r\n" % boundary).encode())
parts.append(img_bytes)
parts.append(("\r\n--%s--\r\n" % boundary).encode())
url = os.environ.get("OCR_URL", "http://192.168.68.19:5000/ocr")
req = request.Request(url, data=b"".join(parts), headers={"Content-Type": "multipart/form-data; boundary=%s" % boundary})
d = json.loads(request.urlopen(req, timeout=120).read())
items = []
for k in sorted(d.keys(), key=lambda x: int(x) if str(x).isdigit() else 0):
it = d[k]
txt = str(it.get("rec_txt") or "").strip()
pts = it.get("dt_boxes")
if not txt:
continue
y = 0.0
if isinstance(pts, list) and pts:
y = sum(float(p[1]) for p in pts) / len(pts)
items.append((y, txt))
items.sort(key=lambda t: t[0])
lines, prev_y, line = [], None, []
for y, txt in items:
if prev_y is None or abs(y - prev_y) <= 12:
line.append(txt)
prev_y = y if prev_y is None else (prev_y * (len(line) - 1) + y) / len(line)
else:
lines.append(" ".join(line))
line, prev_y = [txt], y
if line:
lines.append(" ".join(line))
return "\n".join(lines)
def _doc_ocr_pdf(self, src, wd, max_pages=30):
"""扫描版 PDF:pdftoppm 渲染成 PNG 后逐页 OCR;返回 (text, pages)"""
info = subprocess.run(["pdfinfo", src], capture_output=True, timeout=60)
pages_total = 0
for ln in (info.stdout or b"").decode("utf-8", "ignore").splitlines():
if ln.startswith("Pages:"):
pages_total = int(ln.split(":", 1)[1].strip() or 0)
if pages_total <= 0:
return "", 0
capped = min(pages_total, max_pages)
subprocess.run(["pdftoppm", "-png", "-r", "150", "-f", "1", "-l", str(capped), src, os.path.join(wd, "pg")], capture_output=True, timeout=300)
imgs = [os.path.join(wd, f) for f in os.listdir(wd) if f.startswith("pg") and f.endswith(".png")]
imgs.sort(key=lambda p: int(re.search(r"-(\d+)\.png$", p).group(1)) if re.search(r"-(\d+)\.png$", p) else 0)
texts = []
for p in imgs:
with open(p, "rb") as f:
t = self._doc_ocr_one(f.read())
if t.strip():
texts.append(t)
return "\n\n".join(texts), len(imgs)
# ---------- 文档翻译 ----------
def _handle_translate_doc(self):
machine = self.headers.get("X-Machine-Id", "")
if check(machine, "translator") != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
ctype = self.headers.get("Content-Type", "")
length = int(self.headers.get("Content-Length") or 0)
if length > 20 * 1024 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "文档超过 20MB 限制"}, ensure_ascii=False).encode(), "application/json")
return
raw = self.rfile.read(length) if length else b""
if not raw:
self._reply(400, json.dumps({"code": 400, "message": "未收到文件"}, ensure_ascii=False).encode(), "application/json")
return
if "multipart" not in ctype:
self._reply(400, json.dumps({"code": 400, "message": "请用表单上传文件"}, ensure_ascii=False).encode(), "application/json")
return
env = {"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype, "CONTENT_LENGTH": str(length)}
fs = cgi.FieldStorage(fp=io.BytesIO(raw), environ=env, keep_blank_values=True)
fitem = fs["file"] if "file" in fs.keys() else None
if fitem is None or not fitem.filename:
self._reply(400, json.dumps({"code": 400, "message": "缺少文件"}, ensure_ascii=False).encode(), "application/json")
return
raw_to = (fs["to"].value if "to" in fs.keys() else "") or "zh-CN" # 新前端传语言码;旧前端传中文标签(下一步统一映射)
fname = os.path.basename(fitem.filename or "doc")
ext = os.path.splitext(fname)[1].lower()
allowed = {".txt", ".md", ".docx", ".doc", ".pdf", ".html", ".htm", ".epub", ".rtf", ".odt"} | set(self._DOC_IMG_EXTS)
if ext not in allowed:
self._reply(400, json.dumps({"code": 400, "message": f"不支持的格式 {ext}(支持 txt/md/docx/pdf/html/epub/rtf/odt 与 png/jpg/webp 等图片,扫描件自动 OCR)"}, ensure_ascii=False).encode(), "application/json")
return
wd = "/tmp/tdoc-" + uuid.uuid4().hex[:12]
os.makedirs(wd, exist_ok=True)
src = os.path.join(wd, "in" + ext)
with open(src, "wb") as f:
f.write(fitem.file.read())
# 提取纯文本(非纯文字内容自动先 OCR 识别)
ocr_pages = 0
if ext in self._DOC_IMG_EXTS:
# 图片/截图:直接 OCR 识别文字
with open(src, "rb") as f:
text = self._doc_ocr_one(f.read())
ocr_pages = 1
elif ext in (".txt", ".md"):
rawb = open(src, "rb").read()
text = None
for enc in ("utf-8", "gb18030", "utf-16", "latin-1"):
try:
text = rawb.decode(enc)
break
except Exception: # noqa: BLE001
continue
text = text or ""
else:
outtxt = os.path.join(wd, "out.txt")
r = subprocess.run(["ebook-convert", src, outtxt], capture_output=True, timeout=180)
text = open(outtxt, "r", encoding="utf-8", errors="ignore").read() if r.returncode == 0 and os.path.exists(outtxt) else ""
if ext == ".pdf" and len(text.strip()) < 50:
# 无可提取文字层 → 疑似扫描版/图片型 PDF:逐页渲染 OCR
ocr_text, ocr_pages = self._doc_ocr_pdf(src, wd)
if ocr_text.strip():
text = ocr_text
if not text.strip():
shutil.rmtree(wd, ignore_errors=True)
self._reply(400, json.dumps({"code": 400, "message": "文档解析失败或未能识别到文字(扫描件请确保图片清晰、未加密)"}, ensure_ascii=False).encode(), "application/json")
return
text = re.sub(r"\n{4,}", "\n\n\n", text or "").strip()
if not text:
shutil.rmtree(wd, ignore_errors=True)
self._reply(400, json.dumps({"code": 400, "message": "未从文档中提取到文字"}, ensure_ascii=False).encode(), "application/json")
return
if len(text) > 60000:
shutil.rmtree(wd, ignore_errors=True)
self._reply(413, json.dumps({"code": 413, "message": "文档过长(≤6万字符)"}, ensure_ascii=False).encode(), "application/json")
return
# 目标语种 → 本地 LibreTranslate 码(仅加载 zh/en;不支持时报清晰错误,不再调 LLM)
to_code = _DOC_LANG_LABELS.get(raw_to.strip(), "") or _tr_norm_lang(raw_to)
if not _lt_code(to_code):
shutil.rmtree(wd, ignore_errors=True)
self._reply(400, json.dumps({"code": 400, "message": "文档翻译已切换本地开源引擎(不限流、不耗 AI 额度):当前支持中文/英文文档,其他语种暂未开放"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
# 分块翻译(按段落聚合,每块约 1100 字,交给本地 LibreTranslate)
paras = text.split("\n")
chunks = []
cur = ""
for para in paras:
if len(cur) + len(para) + 1 > 1100 and cur:
chunks.append(cur)
cur = para
else:
cur = cur + "\n" + para if cur else para
if cur:
chunks.append(cur)
# 并发翻译分块(本地引擎,几十块也就几十秒)
from concurrent.futures import ThreadPoolExecutor
def _tr(item):
idx, ch = item
# 目标英文时源语强制中文:中英混排文本 auto 检测常误判为 en,导致原样回显不翻译
src = "zh" if to_code == "en" else "auto"
out, _det = _translate_local(ch, src, to_code)
return idx, out.strip()
workers = min(8, max(1, len(chunks)))
with ThreadPoolExecutor(max_workers=workers) as ex:
results = list(ex.map(_tr, list(enumerate(chunks))))
results.sort(key=lambda x: x[0])
translated = [t for _, t in results]
result_text = "\n".join(translated)
shutil.rmtree(wd, ignore_errors=True)
log_usage(machine, "translator")
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"text": result_text, "chars": len(text), "blocks": len(chunks), "ocr": ocr_pages > 0, "ocr_pages": ocr_pages}}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(500, json.dumps({"code": 500, "message": f"文档翻译失败: {e}"}, ensure_ascii=False).encode(), "application/json")
def _handle_txt_to_docx(self):
payload = self._read_payload()
machine = str(payload.get("machine") or "")
if check(machine, "translator") != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
text = str(payload.get("text") or "")
if not text.strip() or len(text) > 400000:
self._reply(400, json.dumps({"code": 400, "message": "文本内容无效"}, ensure_ascii=False).encode(), "application/json")
return
try:
wd = "/tmp/tdocx-" + uuid.uuid4().hex[:12]
os.makedirs(wd, exist_ok=True)
src = os.path.join(wd, "doc.txt")
dst = os.path.join(wd, "doc.docx")
with open(src, "w", encoding="utf-8") as f:
f.write(text)
r = subprocess.run(["ebook-convert", src, dst], capture_output=True, timeout=120)
if r.returncode != 0 or not os.path.exists(dst):
shutil.rmtree(wd, ignore_errors=True)
self._reply(500, json.dumps({"code": 500, "message": "DOCX 转换失败"}, ensure_ascii=False).encode(), "application/json")
return
with open(dst, "rb") as f:
out = f.read()
shutil.rmtree(wd, ignore_errors=True)
self.send_response(200)
self.send_header("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
self.send_header("Content-Disposition", "attachment; filename*=UTF-8''%E8%AF%91%E6%96%87.docx")
self.send_header("Content-Length", str(len(out)))
self.end_headers()
self.wfile.write(out)
except Exception as e: # noqa: BLE001
self._reply(500, json.dumps({"code": 500, "message": f"转换失败: {e}"}, ensure_ascii=False).encode(), "application/json")
# ---------- 可用模型列表(透传 NewAPI) ----------
def _handle_ai_models(self):
try:
key = os.environ.get("NEWAPI_KEY", "")
base = os.environ.get("NEWAPI_URL", "http://192.168.68.19:3099/v1/chat/completions").replace("/chat/completions", "") + "/models"
if not key:
raise RuntimeError("AI 服务未配置")
req = request.Request(base, headers={"Authorization": "Bearer " + key})
resp = request.urlopen(req, timeout=20)
data = json.loads(resp.read())
ids = [m.get("id", "") for m in data.get("data", [])]
# 过滤:排除生图/视频等非对话模型
skip = ("flux", "dall-e", "schnell", "sana", "-anime", "image", "video", "turbo-any-dark")
chat_models = [i for i in ids if i and not any(s in i.lower() for s in skip)]
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"models": chat_models}}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(502, json.dumps({"code": 502, "message": f"模型列表获取失败: {e}"}, ensure_ascii=False).encode(), "application/json")
# ---------- 截图理解 ----------
def _handle_vision(self):
payload = self._read_payload()
machine = str(payload.get("machine") or "")
if check(machine, "image-tools") != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
b64 = str(payload.get("image") or "")
question = str(payload.get("question") or "").strip()[:1000] or "提取图片中的所有文字和关键信息,结构化整理输出。"
if not b64:
self._reply(400, json.dumps({"code": 400, "message": "未收到图片"}, ensure_ascii=False).encode(), "application/json")
return
if len(b64) > 20 * 1024 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "图片过大(≤15MB)"}, ensure_ascii=False).encode(), "application/json")
return
try:
reply = self._llm_chat([
{"role": "system", "content": "你是截图理解助手,能看懂图片内容(截图、票据、合同、表格、报错)。用中文简洁准确地回答,涉及金额/日期/编号等关键信息必须原样保留。"},
{"role": "user", "content": [
{"type": "text", "text": question},
{"type": "image_url", "image_url": {"url": "data:image/png;base64," + b64}},
]},
])
log_usage(machine, "image-tools")
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"reply": reply}}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(502, json.dumps({"code": 502, "message": f"AI 服务异常: {e}"}, ensure_ascii=False).encode(), "application/json")
# ---------- 视频字幕:转录 + 翻译 ----------
def _handle_subtitle(self):
machine = self.headers.get("X-Machine-Id", "")
if check(machine, "video-tools") != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
ctype = self.headers.get("Content-Type", "")
length = int(self.headers.get("Content-Length") or 0)
if length > 500 * 1024 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "视频超过 500MB 限制"}, ensure_ascii=False).encode(), "application/json")
return
raw = self.rfile.read(length) if length else b""
if not raw:
self._reply(400, json.dumps({"code": 400, "message": "未收到视频数据"}, ensure_ascii=False).encode(), "application/json")
return
import cgi as _cgi
import audio_ops
wd = "/tmp/sub-" + uuid.uuid4().hex[:12]
os.makedirs(wd, exist_ok=True)
video_bytes = raw
if "multipart" in ctype:
env = {"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype, "CONTENT_LENGTH": str(length)}
fs = _cgi.FieldStorage(fp=__import__("io").BytesIO(raw), environ=env, keep_blank_values=True)
fitem = fs["file"] if "file" in fs.keys() else None
if fitem is not None and fitem.filename:
video_bytes = fitem.file.read()
vin = os.path.join(wd, "in.mp4")
with open(vin, "wb") as f:
f.write(video_bytes)
wav = os.path.join(wd, "audio.wav")
r = subprocess.run(
["ffmpeg", "-y", "-i", vin, "-vn", "-ac", "1", "-ar", "16000", wav],
capture_output=True, timeout=600,
)
if r.returncode != 0 or not os.path.exists(wav):
shutil.rmtree(wd, ignore_errors=True)
self._reply(400, json.dumps({"code": 400, "message": "视频无音轨或格式不支持"}, ensure_ascii=False).encode(), "application/json")
return
q = parse.parse_qs(parse.urlparse(self.path).query)
lang = (q.get("lang") or [""])[0]
result = audio_ops.run_asr(wav, lang)
shutil.rmtree(wd, ignore_errors=True)
log_usage(machine, "video-tools")
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": result}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
import traceback
traceback.print_exc()
self._reply(500, json.dumps({"code": 500, "message": f"字幕生成失败: {e}"}, ensure_ascii=False).encode(), "application/json")
def _handle_translate_srt(self):
payload = self._read_payload()
machine = str(payload.get("machine") or "")
if check(machine, "video-tools") != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
srt = str(payload.get("srt") or "").strip()
target = str(payload.get("target") or "en")
if not srt or len(srt) > 60000:
self._reply(400, json.dumps({"code": 400, "message": "字幕内容无效"}, ensure_ascii=False).encode(), "application/json")
return
tname = "英文" if target == "en" else "日文" if target == "ja" else "英文"
try:
reply = self._llm_chat([
{"role": "system", "content": f"你是专业字幕翻译。把用户提供的 SRT 字幕翻译成{tname}:保留序号、时间轴和空行结构完全不变,只翻译每条字幕的文字部分;译文保持口语化、简洁(每行尽量不超过原长的 1.3 倍)。只输出翻译后的 SRT,不要解释。"},
{"role": "user", "content": srt},
])
log_usage(machine, "video-tools")
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"reply": reply}}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(502, json.dumps({"code": 502, "message": f"翻译失败: {e}"}, ensure_ascii=False).encode(), "application/json")
# ---------- AI 问答 ----------
def _handle_tool_help(self):
"""工具页「问AI」:介绍当前工具的用法与特色。免费开放(不在 PROTECTED_TOOLS,不做授权检查)。"""
payload = self._read_payload()
name = str(payload.get("name") or "").strip()[:60]
desc = str(payload.get("description") or "").strip()[:300]
path = str(payload.get("path") or "").strip()[:80]
if not name:
self._reply(400, json.dumps({"code": 400, "message": "缺少工具名称"}, ensure_ascii=False).encode(), "application/json")
return
system = (
"你是 IT-TOOLS-PRO 工具站的功能讲解助手。用户正停在某个工具页面,并点击了「问AI」按钮,"
"请介绍**当前这一个工具**。\n\n"
f"工具名称:{name}\n"
f"页面路径:{path}\n"
f"官方简介:{desc or '(无)'}\n\n"
"严格要求:\n"
"1. 只介绍上面这个工具本身,不要推荐其它工具,不要回答与它无关的问题。\n"
"2. 用简体中文,Markdown 格式,总长 300 字以内,只用这三个小节(### 标题):\n"
" ### 使用方法\n ### 特色亮点\n ### 小提示\n"
"3. 使用方法写成 3-5 步的编号列表,特色亮点写 3-4 个要点,小提示给 1 条实用建议。\n"
"4. 不要输出开场白和结束语,直接从小节开始。"
)
key = os.environ.get("NEWAPI_KEY", "")
upstream = os.environ.get("NEWAPI_URL", "http://192.168.68.19:3099/v1/chat/completions")
model = os.environ.get("NEWAPI_MODEL", "mimo-v2.6-flash")
if not key:
self._reply(500, json.dumps({"code": 500, "message": "AI 服务未配置"}, ensure_ascii=False).encode(), "application/json")
return
try:
body = json.dumps({"model": model, "messages": [{"role": "system", "content": system}],
"temperature": 0.5, "max_tokens": 1200}).encode()
req = request.Request(upstream, data=body, headers={"Content-Type": "application/json", "Authorization": "Bearer " + key})
resp = request.urlopen(req, timeout=90)
data = json.loads(resp.read())
text = data["choices"][0]["message"]["content"]
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"reply": text}}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(502, json.dumps({"code": 502, "message": f"AI 服务异常: {e}"}, ensure_ascii=False).encode(), "application/json")
def _handle_aichat(self):
# AI 客服为免费引导功能(帮助用户发现工具),不做授权校验
payload = self._read_payload()
machine = str(payload.get("machine") or "")
prompt = str(payload.get("prompt") or "").strip()[:4000]
if not prompt:
self._reply(400, json.dumps({"code": 400, "message": "请输入问题"}, ensure_ascii=False).encode(), "application/json")
return
history = payload.get("history") or []
if not isinstance(history, list):
history = []
history = [m for m in history[-8:] if isinstance(m, dict) and m.get("role") in ("user", "assistant") and isinstance(m.get("content"), str)]
# 只回答本站功能:前端实时传入工具清单,嵌入系统提示限定作答范围
catalog = str(payload.get("tools") or "").strip()[:6000]
if not catalog:
catalog = "(见站内左侧工具菜单)"
system = (
"你是 IT-TOOLS-PRO 工具站的专属 AI 助手。你只能回答与本站功能相关的问题。\n\n"
"本站功能清单(按分类):\n" + catalog + "\n\n"
"严格规则:\n"
"1. 只回答上述清单范围内、与本站工具/功能有关的问题"
"(例如:某功能在哪、怎么用、选哪个工具、功能区别、某工具能否完成某任务)。\n"
"2. 对于清单范围之外的问题(闲聊、其他网站、通用知识问答、写代码、翻译句子、"
"新闻天气、算命等),一律礼貌拒绝,"
"并简短引导用户提问与本站功能相关的问题,可提示左侧菜单里的工具。\n"
"3. 推荐工具时使用清单中的工具名称。\n"
"4. 用简体中文简洁、直接地回答,代码用代码块包裹。"
)
messages = [{"role": "system", "content": system}] + history + [{"role": "user", "content": prompt}]
key = os.environ.get("NEWAPI_KEY", "")
upstream = os.environ.get("NEWAPI_URL", "http://192.168.68.19:3099/v1/chat/completions")
model = os.environ.get("NEWAPI_MODEL", "big-pickle")
if not key:
self._reply(500, json.dumps({"code": 500, "message": "AI 服务未配置"}, ensure_ascii=False).encode(), "application/json")
return
want = str(payload.get("model") or "")
if want in ("mimo-v2.6-flash", "glm-5.3-flash"):
model = want
try:
body = json.dumps({"model": model, "messages": messages, "temperature": 0.7, "max_tokens": 2048}).encode()
req = request.Request(upstream, data=body, headers={"Content-Type": "application/json", "Authorization": "Bearer " + key})
resp = request.urlopen(req, timeout=180)
data = json.loads(resp.read())
text = data["choices"][0]["message"]["content"]
log_usage(machine, "ai-chat")
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"reply": text}}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(502, json.dumps({"code": 502, "message": f"AI 服务异常: {e}"}, ensure_ascii=False).encode(), "application/json")
def _handle_video(self):
import uuid as _uuid
import video_ops
machine = self.headers.get("X-Machine-Id", "")
status = check(machine, "video-tools")
if status != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
log_usage(machine, "video-tools")
try:
ctype = self.headers.get("Content-Type", "")
length = int(self.headers.get("Content-Length") or 0)
if length > video_ops.MAX_INPUT + 1024 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "文件超过 2GB 限制"}, ensure_ascii=False).encode(), "application/json")
return
raw = self.rfile.read(length) if length else b""
env = {"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype, "CONTENT_LENGTH": str(length)}
fs = cgi.FieldStorage(fp=io.BytesIO(raw), environ=env, keep_blank_values=True)
fobj = fs["file"] if ("file" in fs.keys()) else None
if fobj is None or not fobj.filename:
self._reply(400, json.dumps({"code": 400, "message": "缺少视频文件"}, ensure_ascii=False).encode(), "application/json")
return
fname = os.path.basename(fobj.filename or "video")
ext = os.path.splitext(fname)[1].lower()
if ext not in video_ops.ALLOWED_EXT:
self._reply(400, json.dumps({"code": 400, "message": "不支持的文件类型 " + ext}, ensure_ascii=False).encode(), "application/json")
return
op = (fs["op"].value if "op" in fs.keys() else "") or ""
params = {}
wm_bytes = None
wm_name = ""
for k in fs.keys():
item = fs[k]
if k == "file" or k == "op":
continue
if getattr(item, "filename", None):
if k == "wm_file":
wm_bytes = item.file.read()
wm_name = os.path.basename(item.filename or "wm.png")
continue
params[k] = item.value
fdata = fobj.file.read()
if not fdata:
self._reply(400, json.dumps({"code": 400, "message": "文件为空"}, ensure_ascii=False).encode(), "application/json")
return
workdir = "/tmp/video-" + _uuid.uuid4().hex[:12]
os.makedirs(workdir, exist_ok=True)
fin = os.path.join(workdir, "in" + ext)
with open(fin, "wb") as f:
f.write(fdata)
duration = video_ops.probe_duration(fin)
argv, out_ext = video_ops.build_cmd(op, fin, None, params)
fout = os.path.join(workdir, "out." + out_ext)
argv[argv.index(fout) if fout in argv else len(argv) - 1] = fout
video_ops.run_ffmpeg(argv)
with open(fout, "rb") as f:
out = f.read()
base = os.path.splitext(fname)[0][:80]
out_name = base + "-out." + out_ext
self.send_response(200)
self.send_header("Content-Type", "application/octet-stream")
self.send_header("Content-Length", str(len(out)))
self.send_header("Content-Disposition",
"attachment; filename=\"result." + out_ext + "\"; filename*=UTF-8''" + parse.quote(out_name))
self.send_header("X-Duration-Seconds", str(round(duration, 2)))
self.end_headers()
self.wfile.write(out)
except ValueError as e:
self._reply(400, json.dumps({"code": 400, "message": str(e)}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(500, json.dumps({"code": 500, "message": "处理失败: " + str(e)[:300]}, ensure_ascii=False).encode("utf-8"), "application/json")
finally:
try:
if "workdir" in dir() and workdir and os.path.isdir(workdir):
shutil.rmtree(workdir, ignore_errors=True)
except Exception:
pass
def _handle_tts(self):
import audio_ops
machine = self.headers.get("X-Machine-Id", "")
status = check(machine, "audio-tools")
if status != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
log_usage(machine, "audio-tools-tts")
try:
length = int(self.headers.get("Content-Length") or 0)
raw = self.rfile.read(length) if length else b"{}"
payload = json.loads(raw.decode("utf-8") or "{}")
import uuid as _uuid
out_path = "/tmp/tts-" + _uuid.uuid4().hex[:10] + ".mp3"
try:
audio_ops.run_tts(
str(payload.get("text") or ""),
str(payload.get("voice") or ""),
_int_str(payload.get("rate"), -50, 100, 0),
_int_str(payload.get("pitch"), -50, 50, 0),
out_path,
)
with open(out_path, "rb") as f:
out = f.read()
finally:
try:
os.remove(out_path)
except Exception:
pass
self.send_response(200)
self.send_header("Content-Type", "audio/mpeg")
self.send_header("Content-Length", str(len(out)))
self.end_headers()
self.wfile.write(out)
except ValueError as e:
self._reply(400, json.dumps({"code": 400, "message": str(e)}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(500, json.dumps({"code": 500, "message": "合成失败: " + str(e)[:200]}, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_clone(self):
import audio_ops
import uuid as _uuid
parsed = parse.urlparse(self.path)
machine = self.headers.get("X-Machine-Id", "")
status = check(machine, "audio-tools")
if status != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/clone/prepare":
# multipart: reference audio -> stored for the voice-clone pipeline
try:
ctype = self.headers.get("Content-Type", "")
length = int(self.headers.get("Content-Length") or 0)
if length > 35 * 1024 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "参考音频过大"}, ensure_ascii=False).encode(), "application/json")
return
raw = self.rfile.read(length) if length else b""
env = {"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype, "CONTENT_LENGTH": str(length)}
fs = cgi.FieldStorage(fp=io.BytesIO(raw), environ=env, keep_blank_values=True)
fobj = fs["file"] if ("file" in fs.keys()) else None
if fobj is None or not fobj.filename:
self._reply(400, json.dumps({"code": 400, "message": "缺少参考音频文件"}, ensure_ascii=False).encode(), "application/json")
return
ref = audio_ops.clone_prepare(fobj.file.read(), os.path.basename(fobj.filename))
log_usage(machine, "audio-tools-clone")
body = {"code": 200, "message": "ok", "data": {"ref_file": ref}}
self._reply(200, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
except ValueError as e:
self._reply(400, json.dumps({"code": 400, "message": str(e)}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(500, json.dumps({"code": 500, "message": "上传失败: " + str(e)[:200]}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/clone/tts":
try:
length = int(self.headers.get("Content-Length") or 0)
raw = self.rfile.read(length) if length else b"{}"
pl = json.loads(raw.decode("utf-8") or "{}")
out_path = "/tmp/clone-" + _uuid.uuid4().hex[:10] + ".wav"
try:
audio_ops.clone_tts(
str(pl.get("ref_file") or ""),
str(pl.get("prompt_text") or ""),
str(pl.get("text") or ""),
str(pl.get("text_lang") or "zh"),
str(pl.get("prompt_lang") or "zh"),
out_path,
)
with open(out_path, "rb") as f:
out = f.read()
finally:
try:
os.remove(out_path)
except Exception:
pass
self.send_response(200)
self.send_header("Content-Type", "audio/wav")
self.send_header("Content-Length", str(len(out)))
self.end_headers()
self.wfile.write(out)
except ValueError as e:
self._reply(400, json.dumps({"code": 400, "message": str(e)}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as e: # noqa: BLE001
self._reply(500, json.dumps({"code": 500, "message": "克隆合成失败: " + str(e)[:200]}, ensure_ascii=False).encode("utf-8"), "application/json")
return
self._reply(404, b"not found")
def _handle_asr(self):
import audio_ops
import uuid as _uuid
import video_ops
machine = self.headers.get("X-Machine-Id", "")
status = check(machine, "audio-tools")
if status != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
log_usage(machine, "audio-tools-asr")
workdir = None
try:
ctype = self.headers.get("Content-Type", "")
length = int(self.headers.get("Content-Length") or 0)
if length > audio_ops.MAX_ASR_MB * 1024 * 1024 + 1024 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "音频超过 " + str(audio_ops.MAX_ASR_MB) + "MB 限制"}, ensure_ascii=False).encode(), "application/json")
return
raw = self.rfile.read(length) if length else b""
env = {"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype, "CONTENT_LENGTH": str(length)}
fs = cgi.FieldStorage(fp=io.BytesIO(raw), environ=env, keep_blank_values=True)
fobj = fs["file"] if ("file" in fs.keys()) else None
if fobj is None or not fobj.filename:
self._reply(400, json.dumps({"code": 400, "message": "缺少音频文件"}, ensure_ascii=False).encode(), "application/json")
return
fname = os.path.basename(fobj.filename or "audio")
ext = os.path.splitext(fname)[1].lower()
if ext not in (".mp3", ".wav", ".m4a", ".ogg", ".flac", ".aac", ".webm", ".mp4", ".wma", ".opus"):
self._reply(400, json.dumps({"code": 400, "message": "不支持的音频格式 " + ext}, ensure_ascii=False).encode(), "application/json")
return
lang = fs["language"].value if "language" in fs.keys() else ""
workdir = "/tmp/asr-" + _uuid.uuid4().hex[:10]
os.makedirs(workdir, exist_ok=True)
fpath = os.path.join(workdir, "in" + ext)
with open(fpath, "wb") as f:
f.write(fobj.file.read())
# normalize to wav mono 16k for whisper (handles mp4/m4a containers too)
wav = os.path.join(workdir, "in.wav")
argv, _ = video_ops.build_cmd("audio", fpath, None, {"fmt": "wav"})
fout = os.path.join(workdir, "tmp.wav")
argv[argv.index(fout) if fout in argv else len(argv) - 1] = fout
video_ops.run_ffmpeg(argv, timeout=600)
result = audio_ops.run_asr(fout, lang)
body = json.dumps({"code": 200, "message": "ok", "data": result}, ensure_ascii=False).encode("utf-8")
self._reply(200, body, "application/json")
except Exception as e: # noqa: BLE001
self._reply(500, json.dumps({"code": 500, "message": "识别失败: " + str(e)[:200]}, ensure_ascii=False).encode("utf-8"), "application/json")
finally:
if workdir and os.path.isdir(workdir):
shutil.rmtree(workdir, ignore_errors=True)
def do_POST(self):
parsed = parse.urlparse(self.path)
if parsed.path == "/speed":
self._handle_speed_post()
return
if parsed.path == "/feedback":
payload = self._read_payload()
status, body = feedback_api("POST", payload, self)
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/hub/report":
payload = self._read_payload() or {}
status, body = hub_report(payload)
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/usage":
payload = self._read_payload() or {}
status, body = usage_track(payload, self)
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/online":
payload = self._read_payload() or {}
status, body = online_beat(payload)
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/mcp":
status, body, ctype = mcp_ops.handle_post(self)
self._reply(status, body, ctype)
return
if parsed.path == "/mcp/tokens":
payload = self._read_payload() or {}
status, body = mcp_ops.tokens_api("POST", payload, {})
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path.startswith("/admin"):
self._handle_admin("POST")
return
if parsed.path.startswith("/user"):
self._handle_user("POST")
return
if parsed.path == "/tts":
self._handle_tts()
return
if parsed.path == "/asr":
self._handle_asr()
return
if parsed.path.startswith("/clone"):
self._handle_clone()
return
if parsed.path == "/video":
self._handle_video()
return
if parsed.path == "/remove-bg":
self._handle_removebg()
return
if parsed.path.startswith("/upscale"):
self._handle_upscale()
return
if parsed.path == "/ai/chat":
self._handle_aichat()
return
if parsed.path == "/ai/tool-help":
self._handle_tool_help()
return
if parsed.path == "/ai/copy":
self._handle_copy()
return
if parsed.path == "/ai/models":
self._handle_ai_models()
return
if parsed.path == "/ai/quota":
self._handle_ai_quota()
return
if parsed.path == "/ai/custom-models":
self._handle_custom_models()
return
if parsed.path == "/ai/gen-image":
self._handle_gen_image()
return
if parsed.path == "/ai/img-save":
self._handle_img_save()
return
if parsed.path == "/draft/save":
self._handle_draft_save()
return
if parsed.path == "/draft/delete":
self._handle_draft_delete()
return
if parsed.path == "/ai/edit-image":
self._handle_edit_image()
return
if parsed.path == "/ai/face-swap":
self._handle_face_swap()
return
if parsed.path == "/ai/seamless-edit":
self._handle_seamless_edit()
return
if parsed.path == "/translate-doc":
self._handle_translate_doc()
return
if parsed.path == "/txt-to-docx":
self._handle_txt_to_docx()
return
if parsed.path == "/ai/vision":
self._handle_vision()
return
if parsed.path == "/subtitle":
self._handle_subtitle()
return
if parsed.path == "/ai/translate-srt":
self._handle_translate_srt()
return
if parsed.path == "/convert":
self._handle_convert()
return
if parsed.path == "/sites" or parsed.path == "/sites/config":
sub = parsed.path[len("/sites"):]
status, body = sites_api("POST", sub, self._read_payload())
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/deactivate":
try:
length = int(self.headers.get("Content-Length") or 0)
raw = self.rfile.read(length) if length else b"{}"
payload = json.loads(raw.decode("utf-8") or "{}")
except Exception: # noqa: BLE001
self._reply(400, json.dumps({"code": 400, "message": "无效请求体"}, ensure_ascii=False).encode(), "application/json")
return
machine_id = str(payload.get("machine_id") or "").strip()
if not MACHINE_RE.match(machine_id):
self._reply(400, json.dumps({"code": 400, "message": "机器码格式错误"}, ensure_ascii=False).encode(), "application/json")
return
status, body = deactivate_card(machine_id)
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
if parsed.path == "/activate":
try:
length = int(self.headers.get("Content-Length") or 0)
raw = self.rfile.read(length) if length else b"{}"
payload = json.loads(raw.decode("utf-8") or "{}")
except Exception: # noqa: BLE001
self._reply(400, json.dumps({"code": 400, "message": "无效请求体"}, ensure_ascii=False).encode(), "application/json")
return
card_key = str(payload.get("card_key") or "").strip()
machine_id = str(payload.get("machine_id") or "").strip()
contact_qq = str(payload.get("contact_qq") or "").strip()
replace = bool(payload.get("replace"))
if not card_key:
self._reply(400, json.dumps({"code": 400, "message": "请输入卡密"}, ensure_ascii=False).encode(), "application/json")
return
if not MACHINE_RE.match(machine_id):
self._reply(400, json.dumps({"code": 400, "message": "机器码格式错误"}, ensure_ascii=False).encode(), "application/json")
return
status, body = activate_card(card_key, machine_id, contact_qq, replace=replace)
if status == 200:
# bind card to the logged-in account so it follows the user across devices
tok = self.headers.get("Authorization", "")
if tok.lower().startswith("bearer "):
acc = _parse_token(tok[7:])
if acc:
try:
_save_user_license(acc, card_key, machine_id)
except Exception:
pass
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
self._reply(404, b"not found")
# ---------- 中文提示词自动转英文(Flux/SDXL 系对英文理解最好) ----------
_ZH_RE = re.compile(r"[\u4e00-\u9fff]")
_trans_cache: dict = {}
def _translate_prompt(self, prompt: str):
"""含中文才翻;成功 (英文, True);失败/未配置回退 (原文, False),绝不阻断生图。"""
if not prompt or not self._ZH_RE.search(prompt):
return prompt, False
cached = self._trans_cache.get(prompt)
if cached:
return cached, True
key = os.environ.get("NEWAPI_KEY", "")
if not key:
return prompt, False
try:
body = json.dumps({
"model": os.environ.get("IMG_PROMPT_MODEL", "mimo-v2.6-flash"),
"messages": [
{"role": "system", "content": (
"You are an expert prompt engineer for text-to-image models (Flux / SDXL / DALL-E). "
"Translate the user's Chinese image prompt into ONE high-quality English image-generation prompt. "
"Preserve every style, composition, aspect-ratio, lighting and detail keyword; you may expand "
"slightly for natural flow. Output ONLY the English prompt itself - no quotes, no explanations, "
"no Chinese characters, no markdown.")},
{"role": "user", "content": prompt},
],
"temperature": 0.3, "max_tokens": 500,
}).encode()
req = request.Request(
os.environ.get("NEWAPI_URL", "http://192.168.68.19:3099/v1/chat/completions"),
data=body, headers={"Content-Type": "application/json", "Authorization": "Bearer " + key})
data = json.loads(request.urlopen(req, timeout=30).read())
en = str(data.get("choices", [{}])[0].get("message", {}).get("content") or "").strip()
en = en.strip("`\"'\u201c\u201d ").strip()
if not en or self._ZH_RE.search(en[:120]):
return prompt, False
if len(self._trans_cache) >= 300:
self._trans_cache.clear()
self._trans_cache[prompt] = en
print(f"[img] prompt translated: {prompt[:40]} -> {en[:70]}", flush=True)
return en, True
except Exception as e: # noqa: BLE001
print(f"[img] translate failed, use original: {e}", flush=True)
return prompt, False
# ---------- AI 生图 (NEWAPI) ----------
def _nm_base(self):
return os.environ.get("NEWAPI_URL", "http://192.168.68.19:3099/v1/chat/completions").replace("/chat/completions", "")
def _nm_image_models(self):
key = os.environ.get("NEWAPI_KEY", "")
req = request.Request(self._nm_base() + "/models", headers={"Authorization": "Bearer " + key})
d = json.loads(request.urlopen(req, timeout=30).read())
names = [m.get("id", "") for m in d.get("data", [])]
out = []
for n in names:
if n.startswith(("dall-e", "flux", "pollinations")):
# pollinations 通道仅文生图;dall-e/flux 理论支持图生图(视上游通道)
out.append({"id": n, "i2i": not n.startswith("pollinations")})
return out
def _nm_images_gen(self, model, prompt, size):
key = os.environ.get("NEWAPI_KEY", "")
body = json.dumps({"model": model, "prompt": prompt, "n": 1, "size": size}).encode()
req = request.Request(self._nm_base() + "/images/generations", data=body,
headers={"Content-Type": "application/json", "Authorization": "Bearer " + key})
d = json.loads(request.urlopen(req, timeout=150).read())
if "error" in d:
raise RuntimeError(d["error"].get("message", "upstream error"))
return d
def _nm_images_edit(self, model, prompt, img_bytes, filename):
key = os.environ.get("NEWAPI_KEY", "")
boundary = "----nm" + secrets.token_hex(8)
parts = []
def fld(name, val):
parts.append(f"--{boundary}\r\nContent-Disposition: form-data; name=\"{name}\"\r\n\r\n{val}\r\n".encode())
fld("model", model)
fld("prompt", prompt)
parts.append(f"--{boundary}\r\nContent-Disposition: form-data; name=\"image\"; filename=\"{filename}\"\r\nContent-Type: application/octet-stream\r\n\r\n".encode())
parts.append(img_bytes)
parts.append(b"\r\n")
parts.append(f"--{boundary}--\r\n".encode())
req = request.Request(self._nm_base() + "/images/edits", data=b"".join(parts),
headers={"Content-Type": f"multipart/form-data; boundary={boundary}", "Authorization": "Bearer " + key})
d = json.loads(request.urlopen(req, timeout=150).read())
if "error" in d:
raise RuntimeError(d["error"].get("message", "upstream error"))
return d
def _img_reply_ok(self, d):
item = (d.get("data") or [{}])[0]
url = item.get("url") or ""
b64 = item.get("b64_json") or ""
if not url and not b64:
raise RuntimeError("上游未返回图片")
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"url": url, "b64": b64, "img_url": (d.get("img_url") or "") if isinstance(d, dict) else "", "prompt_en": (d.get("prompt_en") or "") if isinstance(d, dict) else ""}}, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_image_models(self):
try:
models = self._nm_image_models()
except Exception as e: # noqa: BLE001
print(f"[img] models failed: {e}", flush=True)
models = []
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": models}, ensure_ascii=False).encode("utf-8"), "application/json")
# ---------- AI 生图:自定义提供商 + 历史落盘 ----------
@staticmethod
def _gen_machine_ok(machine):
return bool(re.fullmatch(r"[A-Za-z0-9_-]{4,64}", machine or ""))
def _provider_images_gen(self, provider, model, prompt, size):
"""用用户自己的 base_url + API KEY 走 OpenAI 兼容 images 接口(KEY 仅请求透传,不落盘)。"""
base = str(provider.get("base_url") or "").strip().rstrip("/")
if not base.startswith(("http://", "https://")):
raise RuntimeError("接口地址必须以 http(s):// 开头")
if base.endswith("/v1"):
url = base + "/images/generations"
elif base.endswith("/images/generations"):
url = base
else:
url = base + "/v1/images/generations"
body = json.dumps({"model": model, "prompt": prompt, "n": 1, "size": size}).encode("utf-8")
key = str(provider.get("api_key") or "").strip()
req = request.Request(url, data=body, headers={
"Content-Type": "application/json",
"Authorization": "Bearer " + key,
"User-Agent": "Mozilla/5.0",
})
with request.urlopen(req, timeout=120) as resp:
d = json.loads(resp.read().decode("utf-8", "replace"))
if isinstance(d, dict) and d.get("data"):
return d
msg = ""
if isinstance(d, dict):
err = d.get("error")
if isinstance(err, dict):
msg = str(err.get("message") or "")
else:
msg = str(err or d.get("message") or "")
raise RuntimeError(msg[:300] or "上游未返回图片")
def _hist_owner(self, machine):
"""历史归属:登录账号优先,未登录回落机器码(铁规则:只看本人历史)。"""
u = _auth_user(self)
return u if u else machine
def _persist_gen_image(self, machine, d, model, prompt, w, h, kind="gen", owner=None):
"""把生成结果落盘并写入该机器的历史索引;返回站内图片 URL(任何失败返回空串)。"""
try:
if not self._gen_machine_ok(machine):
return ""
if owner is None:
owner = self._hist_owner(machine)
items = (d or {}).get("data") or []
if not items:
return ""
item = items[0] if isinstance(items[0], dict) else {}
raw = None
if item.get("b64_json"):
import base64 as _b64
raw = _b64.b64decode(item["b64_json"])
elif item.get("url"):
ureq = request.Request(item["url"], headers={"User-Agent": "Mozilla/5.0"})
with request.urlopen(ureq, timeout=60) as rr:
raw = rr.read()
if not raw or len(raw) < 200:
return ""
if not (w and h):
try:
from PIL import Image as _PIL
import io as _io
_im = _PIL.open(_io.BytesIO(raw))
w, h = _im.size
except Exception:
w, h = 0, 0
if raw[:8] == b"\x89PNG\r\n\x1a\n":
ext = ".png"
elif raw[:2] == b"\xff\xd8":
ext = ".jpg"
elif raw[:6] in (b"GIF87a", b"GIF89a"):
ext = ".gif"
elif len(raw) > 12 and raw[8:12] == b"WEBP":
ext = ".webp"
else:
ext = ".png"
gen_dir = os.path.join(DATA_DIR, "gen_images", machine)
os.makedirs(gen_dir, exist_ok=True)
iid = f"{int(time.time())}_{uuid.uuid4().hex[:8]}"
fname = iid + ext
with open(os.path.join(gen_dir, fname), "wb") as f:
f.write(raw)
idx_path = os.path.join(gen_dir, "index.json")
with GEN_IMG_LOCK:
idx = []
if os.path.exists(idx_path):
try:
idx = json.load(open(idx_path, encoding="utf-8"))
if not isinstance(idx, list):
idx = []
except Exception:
idx = []
idx.insert(0, {"id": iid, "file": fname, "at": int(time.time()),
"model": str(model)[:60], "prompt": str(prompt)[:500],
"kind": str(kind or "gen")[:20],
"owner": str(owner)[:64],
"w": w, "h": h})
while len(idx) > 100:
old = idx.pop() or {}
fn = str(old.get("file") or "")
if fn and "/" not in fn and "\\" not in fn:
try:
os.remove(os.path.join(gen_dir, fn))
except Exception:
pass
with open(idx_path, "w", encoding="utf-8") as f:
json.dump(idx, f, ensure_ascii=False)
return f"/relay/ai/img/{machine}/{iid}"
except Exception as e: # noqa: BLE001
print(f"[ai-gen] persist failed: {e}", flush=True)
return ""
def _handle_ai_quota(self):
"""AI 生图页额度查询:授权状态 + 机器今日剩余 + 当前模型上游的真实免费配额。"""
payload = self._read_payload()
machine = str(payload.get("machine") or "")
model = str(payload.get("model") or "")[:60]
if not machine or not MACHINE_RE.match(machine):
self._reply(400, json.dumps({"code": 400, "message": "bad machine"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
authed = machine not in _load_revoked() and (query_authorized(machine) or _account_license_ok(machine))
used = daily_used(machine)
data = {
"authorized": authed,
"limit": DAILY_FREE_LIMIT,
"used": used if not authed else 0,
"remaining": -1 if authed else max(0, DAILY_FREE_LIMIT - used),
"cf_daily": CF_DAILY_LIMIT, # CF 免费池官方换算张数(前端模型下拉后缀用)
}
# 上游真实免费配额(按当前所选模型的通道返回,前端「模型」区如实展示)
up = _upstream_of(model) if model else ""
data["upstream"] = up
if up == "cloudflare":
data["upstream_daily"] = CF_DAILY_LIMIT
data["upstream_used"] = cf_used_today()
data["upstream_remaining"] = cf_remaining()
data["upstream_note"] = (f"Cloudflare Workers AI 官方免费 10,000 Neurons/天"
f"≈ {CF_DAILY_LIMIT} 张(1024×1024/4 步),北京时间约 08:00 重置,账号级共享")
elif up == "pollinations":
data["upstream_daily"] = None
data["upstream_used"] = None
data["upstream_remaining"] = None
data["upstream_note"] = "Pollinations 按 API Key 预算(pollen 余额)计费,无固定每日张数"
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": data}, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_custom_models(self):
"""测试用户的 OpenAI 兼容提供商并拉取模型列表(KEY 只进本次请求头,不落盘)。"""
payload = self._read_payload()
machine = str(payload.get("machine") or "")
if check_gen(machine, True) != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
base = str(payload.get("base_url") or "").strip().rstrip("/")
key = str(payload.get("api_key") or "").strip()
if not base.startswith(("http://", "https://")):
self._reply(400, json.dumps({"code": 400, "message": "接口地址必须以 http(s):// 开头"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if not key:
self._reply(400, json.dumps({"code": 400, "message": "请填写 API KEY"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if base.endswith("/v1"):
url = base + "/models"
elif base.endswith("/models"):
url = base
else:
url = base + "/v1/models"
req = request.Request(url, headers={"Authorization": "Bearer " + key, "User-Agent": "Mozilla/5.0"})
try:
with request.urlopen(req, timeout=20) as resp:
d = json.loads(resp.read().decode("utf-8", "replace"))
except Exception as e: # noqa: BLE001
msg = str(e)
if isinstance(e, request.HTTPError):
try:
msg = e.read().decode("utf-8", "replace")[:300]
except Exception:
pass
self._reply(502, json.dumps({"code": 502, "message": f"连接失败:{msg}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
items = d.get("data") if isinstance(d, dict) else None
ids = [str(m.get("id")) for m in (items or []) if isinstance(m, dict) and m.get("id")]
if not ids:
self._reply(502, json.dumps({"code": 502, "message": "上游未返回模型列表(请确认接口地址为 OpenAI 兼容格式)"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": ids[:200]}, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_img_save(self):
"""导出海报/成片落盘:客户端把 PNG base64 POST 上来,写入该机器的图片历史。"""
payload = self._read_payload()
machine = str(payload.get("machine") or "")
tool = str(payload.get("tool") or "poster-maker")[:40]
if tool in PROTECTED_TOOLS:
if check(machine, tool) != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
elif not MACHINE_RE.match(machine or ""):
# 免费工具历史落盘:仅要求合法机器码,不走授权门禁
self._reply(400, json.dumps({"code": 400, "message": "bad machine"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
b64 = str(payload.get("b64") or "")
if "," in b64[:100]:
b64 = b64.split(",", 1)[1]
if not b64:
self._reply(400, json.dumps({"code": 400, "message": "缺少图片数据"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if len(b64) > 40 * 1024 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "图片超过 30MB 限制"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
prompt = str(payload.get("prompt") or "海报导出")[:500]
model = str(payload.get("model") or "AI海报")[:60]
d = {"data": [{"b64_json": b64}]}
img_url = self._persist_gen_image(machine, d, model, prompt, 0, 0, kind=str(payload.get("kind") or "poster")[:20])
if not img_url:
self._reply(500, json.dumps({"code": 500, "message": "保存失败"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
log_usage(machine, tool)
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"img_url": img_url}}, ensure_ascii=False).encode("utf-8"), "application/json")
# ---------- 全站草稿自动保存(每 1 分钟 / 立即保存按钮 / 离开拦截)----------
@staticmethod
def _draft_path(machine: str, tool: str) -> str:
"""校验并映射到 DATA_DIR/drafts/<machine>/<tool>.json;非法返回空串。"""
m = MACHINE_RE.match(machine or "")
t = re.sub(r"[^A-Za-z0-9_-]", "", tool or "")[:64]
if not m or not t:
return ""
return os.path.join(DATA_DIR, "drafts", m.group(0), t + ".json")
def _handle_draft_save(self):
payload = self._read_payload()
machine = str(payload.get("machine") or "")
tool = str(payload.get("tool") or "")
path = self._draft_path(machine, tool)
if not path:
self._reply(400, json.dumps({"code": 400, "message": "bad machine/tool"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
state = payload.get("state")
try:
raw = json.dumps(state, ensure_ascii=False)
except (TypeError, ValueError):
self._reply(400, json.dumps({"code": 400, "message": "bad state"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if len(raw.encode("utf-8")) > 300 * 1024:
self._reply(413, json.dumps({"code": 413, "message": "草稿超过 300KB 限制"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
rec = {"tool": tool[:64], "machine": machine, "ts": int(time.time()), "state": state}
tmp = path + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(rec, f, ensure_ascii=False)
os.replace(tmp, path)
except OSError:
self._reply(500, json.dumps({"code": 500, "message": "保存失败"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
# 追加历史版本(保留最近 20 版,供前端「找回」面板选择回填)
try:
hpath = path[:-5] + ".hist.json"
hist = []
if os.path.isfile(hpath):
with open(hpath, "r", encoding="utf-8") as f:
loaded = json.load(f)
if isinstance(loaded, list):
hist = [h for h in loaded if isinstance(h, dict)]
# 连续保存相同内容不重复入档(只更新最新一条时间戳)
if hist and hist[0].get("state") == state:
hist[0]["ts"] = rec["ts"]
elif hist and hist[0].get("ts") == rec["ts"]:
# 同一秒内的连续保存:按最新内容覆盖,避免出现两条同时间条目
hist[0] = {"ts": rec["ts"], "state": state}
else:
hist.insert(0, {"ts": rec["ts"], "state": state})
hist = hist[:20]
tmp2 = hpath + ".tmp"
with open(tmp2, "w", encoding="utf-8") as f:
json.dump(hist, f, ensure_ascii=False)
os.replace(tmp2, hpath)
except (OSError, ValueError, TypeError):
pass # 历史写入失败不影响主保存
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"ts": rec["ts"]}}, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_draft_load(self, parsed):
qs = parse.parse_qs(parsed.query)
machine = str((qs.get("machine") or [""])[0] or "")
tool = str((qs.get("tool") or [""])[0] or "")
try:
want_ts = int((qs.get("ts") or ["0"])[0] or 0)
except ValueError:
want_ts = 0
path = self._draft_path(machine, tool)
if not path:
self._reply(400, json.dumps({"code": 400, "message": "bad machine/tool"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
data = None
# 指定 ts:从历史版本里找回该条
if want_ts > 0:
hpath = path[:-5] + ".hist.json"
if os.path.isfile(hpath):
try:
with open(hpath, "r", encoding="utf-8") as f:
hist = json.load(f)
if isinstance(hist, list):
for h in hist:
if isinstance(h, dict) and int(h.get("ts") or 0) == want_ts:
data = {"state": h.get("state"), "ts": want_ts}
break
except (OSError, ValueError, TypeError):
data = None
else:
if os.path.isfile(path):
try:
with open(path, "r", encoding="utf-8") as f:
rec = json.load(f)
data = {"state": rec.get("state"), "ts": int(rec.get("ts") or 0)}
except (OSError, ValueError):
data = None
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": data}, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_draft_history(self, parsed):
"""历史版本清单(只回 ts/bytes,不回 state,面板按需逐条取)。"""
qs = parse.parse_qs(parsed.query)
machine = str((qs.get("machine") or [""])[0] or "")
tool = str((qs.get("tool") or [""])[0] or "")
path = self._draft_path(machine, tool)
if not path:
self._reply(400, json.dumps({"code": 400, "message": "bad machine/tool"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
items = []
hpath = path[:-5] + ".hist.json"
if os.path.isfile(hpath):
try:
with open(hpath, "r", encoding="utf-8") as f:
hist = json.load(f)
if isinstance(hist, list):
for h in hist:
if not isinstance(h, dict):
continue
st = h.get("state")
try:
b = len(json.dumps(st, ensure_ascii=False).encode("utf-8")) if st is not None else 0
except (TypeError, ValueError):
b = 0
items.append({"ts": int(h.get("ts") or 0), "bytes": b})
except (OSError, ValueError):
items = []
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"items": items}}, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_draft_delete(self):
payload = self._read_payload()
machine = str(payload.get("machine") or "")
tool = str(payload.get("tool") or "")
path = self._draft_path(machine, tool)
if not path:
self._reply(400, json.dumps({"code": 400, "message": "bad machine/tool"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
if os.path.isfile(path):
os.remove(path)
except OSError:
pass
self._reply(200, json.dumps({"code": 200, "message": "ok"}, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_img_history(self):
parsed = parse.urlparse(self.path)
qs = parse.parse_qs(parsed.query)
machine = str((qs.get("machine") or [""])[0] or "")
fkind = str((qs.get("kind") or [""])[0] or "")
if not self._gen_machine_ok(machine):
self._reply(400, json.dumps({"code": 400, "message": "bad machine"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
# 铁规则:只看本人历史。登录=账号本人条目(跨设备)+ 本机匿名条目;
# 未登录=仅本机匿名条目(owner 为空或等于机器码,含迁移前旧记录)。
me = _auth_user(self)
root = os.path.join(DATA_DIR, "gen_images")
pairs = []
if me:
try:
for name in sorted(os.listdir(root)):
ip = os.path.join(root, name, "index.json")
if not os.path.isfile(ip):
continue
try:
its = json.load(open(ip, encoding="utf-8"))
except Exception:
continue
if isinstance(its, list):
pairs.append((name, its))
except Exception:
pairs = []
else:
its = []
ip = os.path.join(root, machine, "index.json")
if os.path.exists(ip):
try:
its = json.load(open(ip, encoding="utf-8"))
except Exception:
its = []
if not isinstance(its, list):
its = []
pairs.append((machine, its))
items = []
for name, its in pairs:
for it in its:
if isinstance(it, dict):
it["_dir"] = name
items.append(it)
if fkind:
# kind 过滤:逗号分隔多值;旧记录无 kind 字段视为 "gen"
want = {k for k in fkind.split(",") if k}
items = [it for it in items if str(it.get("kind") or "gen") in want]
vis = []
for it in items:
o = str(it.get("owner") or "")
d = str(it.get("_dir") or machine)
if me:
ok = (o == me) or (d == machine and (not o or o == machine))
else:
ok = (d == machine) and (not o or o == machine)
if ok:
it["img_url"] = f"/relay/ai/img/{d}/{it.get('id', '')}"
vis.append(it)
vis.sort(key=lambda x: int(x.get("at") or 0), reverse=True)
# 铁规则:所有功能历史最多显示 100 条(按时间倒序取前 100)
vis = vis[:100]
for it in vis:
it.pop("_dir", None)
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": vis}, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_img_file(self, parsed):
# 兼容两条链路:直连 relay(/relay/ai/img/m/id)与经 nginx(/ai/img/m/id)
parts = parsed.path.split("/")
if len(parts) < 5 or parts[-3] != "img":
self._reply(404, b"not found")
return
machine, iid = parts[-2], parts[-1]
if not self._gen_machine_ok(machine) or not re.fullmatch(r"[A-Za-z0-9_\-]{4,64}", iid or ""):
self._reply(404, b"not found")
return
gen_dir = os.path.join(DATA_DIR, "gen_images", machine)
found = None
if os.path.isdir(gen_dir):
for fn in os.listdir(gen_dir):
if fn.startswith(iid + ".") and os.path.isfile(os.path.join(gen_dir, fn)):
found = os.path.join(gen_dir, fn)
break
if not found:
self._reply(404, b"not found")
return
ext = os.path.splitext(found)[1].lower()
ctype = {".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg",
".gif": "image/gif", ".webp": "image/webp"}.get(ext, "image/png")
try:
with open(found, "rb") as f:
raw = f.read()
except Exception:
self._reply(404, b"not found")
return
qs = parse.parse_qs(parsed.query)
dl = str((qs.get("dl") or [""])[0] or "")
self.send_response(200)
self.send_header("Content-Type", ctype)
self.send_header("Content-Length", str(len(raw)))
self.send_header("Cache-Control", "private, max-age=86400")
if dl == "1":
self.send_header("Content-Disposition", f'attachment; filename="{iid}{ext}"')
self.end_headers()
try:
self.wfile.write(raw)
except (BrokenPipeError, ConnectionResetError):
pass
def _handle_gen_image(self):
payload = self._read_payload()
machine = str(payload.get("machine") or "")
# 先解析自带提供商与模型:自带 KEY 不消耗平台额度;模型决定上游共享池判定
provider = payload.get("provider") if isinstance(payload.get("provider"), dict) else None
own_provider = bool(provider and str(provider.get("base_url") or "").strip() and str(provider.get("api_key") or "").strip())
model = str(payload.get("model") or "pollinations-flux")[:60]
reason = gen_block_reason(machine, own_provider, model)
if reason is not None:
self._reply(403, json.dumps({"code": 403, "message": gen_block_message(reason)}, ensure_ascii=False).encode("utf-8"), "application/json")
return
prompt = str(payload.get("prompt") or "").strip()[:2000]
if not prompt:
self._reply(400, json.dumps({"code": 400, "message": "请输入提示词"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
w = int(payload.get("width") or 1024)
h = int(payload.get("height") or 1024)
except Exception:
w, h = 1024, 1024
w = max(16, min(2048, w))
h = max(16, min(2048, h))
# 中文提示词自动转英文再发上游(Flux/SDXL 英文效果最好;翻译失败回退原文)
gen_prompt, translated = self._translate_prompt(prompt)
try:
if own_provider:
d = self._provider_images_gen(provider, model, gen_prompt, f"{w}x{h}")
else:
d = self._nm_images_gen(model, gen_prompt, f"{w}x{h}")
daily_consume(machine, model) # 内置渠道成功才扣次(机器计数 + CF 共享池)
except Exception as e: # noqa: BLE001
print(f"[img] gen failed: {e}", flush=True)
self._reply(502, json.dumps({"code": 502, "message": f"生图失败:{e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
# 落盘保存到该机器的历史(失败不阻断响应)
try:
kind = str(payload.get("kind") or "gen")[:20]
except Exception:
kind = "gen"
img_url = self._persist_gen_image(machine, d, model, prompt, w, h, kind=kind) # 历史存用户原文
if isinstance(d, dict) and img_url:
d["img_url"] = img_url
if translated and isinstance(d, dict):
d["prompt_en"] = gen_prompt # 实际发给上游的英文提示词(供前端提示)
self._img_reply_ok(d)
def _handle_edit_image(self):
machine = self.headers.get("X-Machine-Id", "")
ctype = self.headers.get("Content-Type", "")
if "multipart/form-data" not in ctype:
self._reply(400, json.dumps({"code": 400, "message": "需要 multipart 上传"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype})
prompt = str(form.getvalue("prompt") or "").strip()[:2000]
model = str(form.getvalue("model") or "dall-e-3")[:60]
# 门禁放模型解析之后:模型决定上游(Cloudflare 共享池已授权也保护)
reason = gen_block_reason(machine, False, model)
if reason is not None:
self._reply(403, json.dumps({"code": 403, "message": gen_block_message(reason)}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if not prompt:
self._reply(400, json.dumps({"code": 400, "message": "请输入提示词"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
up = form["image"] if "image" in form else None
# 注意:cgi.FieldStorage 禁止真值判断(bool 会抛 TypeError),用 is None 判空
if up is None or not getattr(up, "filename", None):
self._reply(400, json.dumps({"code": 400, "message": "请上传图片"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
img = up.file.read()
gen_prompt, translated = self._translate_prompt(prompt)
d = self._nm_images_edit(model, gen_prompt, img, up.filename)
daily_consume(machine, model) # 内置渠道成功才扣次(机器计数 + CF 共享池)
if translated and isinstance(d, dict):
d["prompt_en"] = gen_prompt
# 铁规则:生成即保存到后台存储目录(失败静默,不阻断响应)
img_url = self._persist_gen_image(machine, d, model, prompt, 0, 0, kind="edit")
if img_url and isinstance(d, dict):
d["img_url"] = img_url
self._img_reply_ok(d)
except Exception as e: # noqa: BLE001
print(f"[img] edit failed: {e}", flush=True)
self._reply(502, json.dumps({"code": 502, "message": f"图生图失败:{e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
# ---------- 无痕改字:OCR 定位 + vision 选字块 + 本地像素引擎无缝重绘 ----------
def _se_ocr(self, img_bytes):
"""调用宿主 rapidocr 返回 [{text, box(4角)}]"""
boundary = "----se" + __import__("secrets").token_hex(8)
parts = []
parts.append(f"--{boundary}\r\nContent-Disposition: form-data; name=\"image_file\"; filename=\"img.png\"\r\nContent-Type: application/octet-stream\r\n\r\n".encode())
parts.append(img_bytes)
parts.append(f"\r\n--{boundary}--\r\n".encode())
req = request.Request(os.environ.get("OCR_URL", "http://192.168.68.19:5000/ocr"), data=b"".join(parts),
headers={"Content-Type": f"multipart/form-data; boundary={boundary}"})
d = json.loads(request.urlopen(req, timeout=90).read())
out = []
for k in sorted(d.keys(), key=lambda x: int(x) if str(x).isdigit() else 0):
it = d[k]
txt = str(it.get("rec_txt") or "").strip()
pts = it.get("dt_boxes")
if txt and isinstance(pts, list) and len(pts) >= 4:
out.append({"text": txt, "box": [[float(p[0]), float(p[1])] for p in pts[:4]]})
return out
def _se_vision_pick(self, model, img_bytes, ocr_items, prompt):
"""vision 模型看图+OCR 清单+用户需求 → 选出要改的字块与新文字(JSON)"""
from PIL import Image as _Im
im = _Im.open(io.BytesIO(img_bytes)).convert("RGB")
if max(im.size) > 1024:
k = 1024 / max(im.size)
im = im.resize((int(im.width * k), int(im.height * k)))
buf = io.BytesIO()
im.save(buf, "JPEG", quality=85)
b64 = __import__("base64").b64encode(buf.getvalue()).decode()
listing = "\n".join(f"[{i}] {o['text']}" for i, o in enumerate(ocr_items))
user = (
f"图中OCR识别到的文字块(编号:内容):\n{listing}\n\n"
f"用户需求:{prompt}\n\n"
"请判断需要修改哪些文字块、新文字是什么。只输出JSON,不要其它内容:\n"
'{"edits":[{"idx":0,"new_text":"新文字"}],"note":"一句话说明"}\n'
"规则:idx为上面的编号;new_text为空字符串表示只删除不新增;无需修改时输出{\"edits\":[]};最多3处。"
)
key = os.environ.get("NEWAPI_KEY", "")
body = json.dumps({
"model": model, "max_tokens": 600, "temperature": 0.1,
"messages": [{"role": "user", "content": [
{"type": "text", "text": user},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}},
]}],
}).encode()
req = request.Request(self._nm_base() + "/chat/completions", data=body,
headers={"Content-Type": "application/json", "Authorization": "Bearer " + key})
d = json.loads(request.urlopen(req, timeout=150).read())
content = (d.get("choices") or [{}])[0].get("message", {}).get("content") or ""
m = re.search(r"\{.*\}", content, re.S)
if not m:
raise RuntimeError("模型未返回有效指令")
parsed = json.loads(m.group(0))
edits = []
for e in (parsed.get("edits") or [])[:3]:
try:
idx = int(e.get("idx"))
except Exception:
continue
if 0 <= idx < len(ocr_items):
edits.append({"idx": idx, "new_text": str(e.get("new_text") or "").strip()[:40]})
return edits, str(parsed.get("note") or "").strip()[:120]
@staticmethod
def _se_detect_style(im_arr, box):
"""从框像素推断角度/字号/颜色/粗细(与前端 PsEditor.detectTextStyle 同算法)"""
import numpy as _np
import math as _math
(p0, p1, p2, p3) = box
angle = _math.degrees(_math.atan2(p1[1] - p0[1], p1[0] - p0[0]))
if angle > 90:
angle -= 180
if angle <= -90:
angle += 180
midT = ((p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2)
midB = ((p3[0] + p2[0]) / 2, (p3[1] + p2[1]) / 2)
bh = _math.hypot(midB[0] - midT[0], midB[1] - midT[1])
bw = (_math.hypot(p1[0] - p0[0], p1[1] - p0[1]) + _math.hypot(p2[0] - p3[0], p2[1] - p3[1])) / 2
cx = (p0[0] + p1[0] + p2[0] + p3[0]) / 4
cy = (p0[1] + p1[1] + p2[1] + p3[1]) / 4
size = max(10, int(round(bh * 0.82)))
H, W = im_arr.shape[:2]
x0 = max(0, int(min(p[0] for p in box)))
y0 = max(0, int(min(p[1] for p in box)))
x1 = min(W, int(max(p[0] for p in box)) + 1)
y1 = min(H, int(max(p[1] for p in box)) + 1)
if x1 - x0 < 4 or y1 - y0 < 4:
return None
crop = im_arr[y0:y1, x0:x1].astype(_np.float32)
hh, ww = crop.shape[:2]
a = _math.radians(angle)
ca, sa = _math.cos(a), _math.sin(a)
ys, xs = _np.mgrid[0:hh, 0:ww]
ox = xs + x0 + 0.5 - cx
oy = ys + y0 + 0.5 - cy
lx = ox * ca + oy * sa
ly = -ox * sa + oy * ca
ringR = max(bw / 2, bh / 2) + bh * 0.5
inside = (_np.abs(lx) <= bw / 2) & (_np.abs(ly) <= bh / 2)
ring = (_np.abs(lx) <= ringR) & (_np.abs(ly) <= ringR) & ~inside
if inside.sum() < 20 or ring.sum() < 8:
return None
bg = _np.median(crop[ring], axis=0) # crop[ring] 已是 (N,3)
dist = _np.linalg.norm(crop - bg[None, None, :], axis=2)
ink = inside & (dist > 60)
if ink.sum() < 6 or ink.sum() / max(1, inside.sum()) < 0.008:
lum = 0.299 * bg[0] + 0.587 * bg[1] + 0.114 * bg[2]
color = (255, 255, 255) if lum < 110 else (0, 0, 0)
return {"size": size, "color": color, "bold": True, "angle": round(angle, 1), "cx": cx, "cy": cy, "bw": bw}
color = tuple(int(v) for v in _np.median(crop[ink], axis=0))
# 粗细:中心行带墨迹横向行程中位数
bold = False
band = ink & (_np.abs(ly) < bh * 0.14)
if band.sum() > 10:
runs = []
for r in range(hh):
row = band[r]
c2 = 0
for v in row:
if v:
c2 += 1
elif c2:
runs.append(c2)
c2 = 0
if c2:
runs.append(c2)
if runs:
runs.sort()
bold = runs[len(runs) // 2] / size > 0.11
return {"size": size, "color": color, "bold": bold, "angle": round(angle, 1), "cx": cx, "cy": cy, "bw": bw}
@staticmethod
def _se_inpaint(arr, box, style):
"""框内文字笔画从周边背景扩散重建(Jacobi 迭代),返回新数组"""
import numpy as _np
H, W = arr.shape[:2]
padX = max(2, int(round(style["bw"] * 0.05)))
padY = max(2, int(round(style["size"] * 0.12)))
cx, cy = style["cx"], style["cy"]
x0 = max(0, int(cx - style["bw"] / 2) - padX)
y0 = max(0, int(cy - style["size"] * 0.9) - padY)
x1 = min(W, int(cx + style["bw"] / 2) + padX)
y1 = min(H, int(cy + style["size"] * 0.9) + padY)
if x1 - x0 < 4 or y1 - y0 < 4:
return arr
region = arr[y0:y1, x0:x1].astype(_np.float32)
# 蒙版:与框周边中位色距离>42 的笔画像素,外扩1px
border = _np.concatenate([region[0], region[-1], region[:, 0], region[:, -1]])
bg = _np.median(border, axis=0)
mask = (_np.linalg.norm(region - bg[None, None, :], axis=2) > 42)
if not mask.any():
mask = _np.ones(mask.shape, dtype=bool) # 实心底→整块重建
m = mask.copy()
m[1:, :] |= mask[:-1, :]
m[:-1, :] |= mask[1:, :]
m[:, 1:] |= mask[:, :-1]
m[:, :-1] |= mask[:, 1:]
mask = m
out = region.copy()
known = ~mask
if not known.any():
return arr
out[mask] = out[known].reshape(-1, 3).mean(0)
for _ in range(320):
padded = _np.pad(out, ((1, 1), (1, 1), (0, 0)), mode="edge")
avg = (padded[:-2, 1:-1] + padded[2:, 1:-1] + padded[1:-1, :-2] + padded[1:-1, 2:]) / 4.0
out[mask] = avg[mask]
res = arr.copy()
res[y0:y1, x0:x1] = _np.clip(out, 0, 255).astype(_np.uint8)
return res
@staticmethod
def _font(bold, size):
from PIL import ImageFont
base = "/hostfs/usr/share/fonts/opentype/noto/NotoSansCJK-%s.ttc"
name = "Bold" if bold else "Regular"
for idx in (2, 0):
try:
return ImageFont.truetype(base % name, size, index=idx)
except Exception:
continue
try:
return ImageFont.truetype(base % name, size)
except Exception:
return ImageFont.load_default()
@classmethod
def _se_draw_text(cls, im, text, style, scale=1.0):
"""按检测样式在原位重绘新字(含角度),过框宽自动缩号"""
from PIL import Image as _Im, ImageDraw as _Draw
import math as _math
size = max(8, int(round(style["size"] * scale)))
font = cls._font(style["bold"], size)
probe = _Im.new("RGBA", (8, 8), (0, 0, 0, 0))
pd = _Draw.Draw(probe)
bb = pd.textbbox((0, 0), text, font=font)
tw, th = bb[2] - bb[0], bb[3] - bb[1]
if tw <= 0:
return im
pad = size
layer = _Im.new("RGBA", (tw + pad * 2, th + pad * 2), (0, 0, 0, 0))
ld = _Draw.Draw(layer)
ld.text((pad - bb[0], pad - bb[1]), text, font=font, fill=style["color"] + (255,))
# 新字过原框宽 1.15 倍 → 整体等比缩小
avail = style["bw"] * 1.15
if layer.width > avail and avail > 0:
k = avail / layer.width
layer = layer.resize((max(1, int(layer.width * k)), max(1, int(layer.height * k))), _Im.LANCZOS)
if abs(style["angle"]) > 0.05:
# 画布正角=顺时针;PIL 正角=逆时针 → 取负
layer = layer.rotate(-style["angle"], expand=True, resample=_Im.BICUBIC)
x = int(round(style["cx"] - layer.width / 2))
y = int(round(style["cy"] - layer.height / 2))
im.paste(layer, (x, y), layer)
return im
@staticmethod
def _se_fit_size(im, size_opt, custom_w, custom_h):
"""默认保持原尺寸;比例/自定义 → 等比包含 + 边缘中位色补边"""
import numpy as _np
from PIL import Image as _ImX
ratios = {"4:3": 4 / 3, "3:4": 3 / 4, "1:1": 1.0}
tw = th = None
if size_opt in ratios:
r = ratios[size_opt]
cur = im.width / im.height
if abs(cur - r) < 0.01:
return im
if cur > r:
th = im.height
tw = int(round(th * r))
else:
tw = im.width
th = int(round(tw / r))
elif size_opt == "custom" and custom_w and custom_h:
tw, th = max(64, min(2048, custom_w)), max(64, min(2048, custom_h))
if not tw:
return im
arr = _np.asarray(im.convert("RGB"))
edge = _np.concatenate([arr[0], arr[-1], arr[:, 0], arr[:, -1]]).astype(_np.float32)
col = tuple(int(v) for v in _np.median(edge, axis=0))
canvas = _ImX.new("RGB", (tw, th), col)
k = min(tw / im.width, th / im.height)
im2 = im.resize((max(1, int(im.width * k)), max(1, int(im.height * k))), _ImX.LANCZOS)
canvas.paste(im2, ((tw - im2.width) // 2, (th - im2.height) // 2))
return canvas
def _handle_face_swap(self):
"""AI换脸(授权功能):源人脸照片 -> 目标照片,本机 CPU 推理,结果走 /ai/img 站内图链路。"""
machine = self.headers.get("X-Machine-Id", "")
code = check(machine, "face-swap")
if code != 200:
self._reply(403, json.dumps({"code": 403, "message": "AI换脸是授权功能:试用期内可免费体验,正式使用请先激活卡密"},
ensure_ascii=False).encode("utf-8"), "application/json")
return
log_usage(machine, "face-swap")
ctype = self.headers.get("Content-Type") or ""
if "multipart/form-data" not in ctype:
self._reply(400, json.dumps({"code": 400, "message": "需要 multipart/form-data"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,
environ={"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype})
except Exception as e: # noqa: BLE001
self._reply(400, json.dumps({"code": 400, "message": f"表单解析失败: {e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if str(form.getvalue("consent") or "") != "1":
self._reply(400, json.dumps({"code": 400, "message": "请先勾选「我承诺仅使用已获本人授权的照片进行换脸」"},
ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
src_raw = form["source"].file.read() if "source" in form else b""
dst_raw = form["target"].file.read() if "target" in form else b""
except Exception:
src_raw, dst_raw = b"", b""
if not src_raw or not dst_raw:
self._reply(400, json.dumps({"code": 400, "message": "请同时上传源人脸照片与目标照片"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if len(src_raw) > 15 * 1024 * 1024 or len(dst_raw) > 15 * 1024 * 1024:
self._reply(400, json.dumps({"code": 400, "message": "单张图片不能超过 15MB"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
import cv2 as _cv2
import numpy as _np
src_img = _cv2.imdecode(_np.frombuffer(src_raw, _np.uint8), _cv2.IMREAD_COLOR)
dst_img = _cv2.imdecode(_np.frombuffer(dst_raw, _np.uint8), _cv2.IMREAD_COLOR)
if src_img is None or dst_img is None:
self._reply(400, json.dumps({"code": 400, "message": "图片解码失败,请上传 JPG/PNG 等常见格式"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
out_jpg, msg, ms = _face_swap_run(src_img, dst_img)
except Exception as e: # noqa: BLE001
print(f"[face-swap] failed: {e}", flush=True)
self._reply(500, json.dumps({"code": 500, "message": f"换脸失败: {e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if out_jpg is None:
self._reply(400, json.dumps({"code": 400, "message": msg}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
gen_dir = os.path.join(DATA_DIR, "gen_images", machine)
os.makedirs(gen_dir, exist_ok=True)
iid = f"{int(time.time())}_{uuid.uuid4().hex[:8]}"
with open(os.path.join(gen_dir, iid + ".jpg"), "wb") as f:
f.write(out_jpg)
idx_path = os.path.join(gen_dir, "index.json")
with GEN_IMG_LOCK:
idx = []
if os.path.exists(idx_path):
try:
idx = json.load(open(idx_path, encoding="utf-8"))
if not isinstance(idx, list):
idx = []
except Exception:
idx = []
idx.insert(0, {"id": iid, "file": iid + ".jpg", "at": int(time.time()),
"model": "AI换脸", "prompt": "face-swap",
"kind": "face-swap",
"owner": str(self._hist_owner(machine))[:64],
"w": int(dst_img.shape[1]), "h": int(dst_img.shape[0])})
while len(idx) > 100:
old = idx.pop() or {}
fn = str(old.get("file") or "")
if fn and "/" not in fn and "\\" not in fn:
try:
os.remove(os.path.join(gen_dir, fn))
except Exception:
pass
with open(idx_path, "w", encoding="utf-8") as f:
json.dump(idx, f, ensure_ascii=False)
except Exception as e: # noqa: BLE001
print(f"[face-swap] persist failed: {e}", flush=True)
self._reply(500, json.dumps({"code": 500, "message": "结果保存失败"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
body = json.dumps({"code": 200, "message": "ok",
"data": {"url": f"/relay/ai/img/{machine}/{iid}", "elapsed_ms": ms}},
ensure_ascii=False).encode("utf-8")
self._reply(200, body, "application/json")
def _handle_seamless_edit(self):
machine = self.headers.get("X-Machine-Id", "")
if check(machine, "ai-text-edit") != 200:
self._reply(403, json.dumps({"code": 403, "message": "需要授权或今日免费试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
ctype = self.headers.get("Content-Type", "")
if "multipart/form-data" not in ctype:
self._reply(400, json.dumps({"code": 400, "message": "需要 multipart 上传"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype})
prompt = str(form.getvalue("prompt") or "").strip()[:200]
model = str(form.getvalue("model") or "qwen3.8-max")[:60]
size_opt = str(form.getvalue("size") or "default")[:10]
try:
count = max(1, min(3, int(form.getvalue("count") or 1)))
except Exception:
count = 1
try:
custom_w = int(form.getvalue("custom_w") or 0)
custom_h = int(form.getvalue("custom_h") or 0)
except Exception:
custom_w = custom_h = 0
if not prompt:
self._reply(400, json.dumps({"code": 400, "message": "请描述要修改的文字和样式"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
up = form["image"] if "image" in form else None
if up is None or not getattr(up, "filename", None):
self._reply(400, json.dumps({"code": 400, "message": "请上传图片"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
img = up.file.read()
if len(img) > 24 * 1024 * 1024:
self._reply(400, json.dumps({"code": 400, "message": "图片超过 24MB"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
from PIL import Image as _Im
base_im = _Im.open(io.BytesIO(img)).convert("RGB")
except Exception as e: # noqa: BLE001
self._reply(400, json.dumps({"code": 400, "message": f"图片解析失败:{e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
ocr_items = self._se_ocr(img)
except Exception as e: # noqa: BLE001
print(f"[se] ocr failed: {e}", flush=True)
self._reply(502, json.dumps({"code": 502, "message": f"文字识别失败:{e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if not ocr_items:
self._reply(400, json.dumps({"code": 400, "message": "未识别到图中文字,可换更清晰的截图"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
edits, note = self._se_vision_pick(model, img, ocr_items, prompt)
except Exception as e: # noqa: BLE001
print(f"[se] vision failed: {e}", flush=True)
self._reply(502, json.dumps({"code": 502, "message": f"AI 理解失败:{e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if not edits:
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"images": [], "edits": [], "note": note or "未找到需要修改的文字,请描述得更具体(写明原文和新文字)"}}, ensure_ascii=False).encode("utf-8"), "application/json")
return
import numpy as _np
orig_arr = _np.asarray(base_im)
# 逐处改字:检测样式 → 无缝擦除 → 重绘新字
results = []
for e in edits:
o = ocr_items[e["idx"]]
st = self._se_detect_style(orig_arr, o["box"])
if not st:
continue
results.append({"old": o["text"], "new": e["new_text"], "style": f"{st['size']}px · {'粗体' if st['bold'] else '常规'} · {st['angle']}°", "_box": o["box"], "_st": st})
if not results:
self._reply(502, json.dumps({"code": 502, "message": "未能定位要修改的文字块"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
scales = {1: [1.0], 2: [0.95, 1.06], 3: [0.9, 1.0, 1.08]}[count]
images = []
try:
from PIL import Image as _Im
for s in scales:
work = base_im.copy()
for r in results:
arr = _np.asarray(work)
arr = self._se_inpaint(arr, r["_box"], r["_st"])
work = _Im.fromarray(arr)
if r["new"]:
work = self._se_draw_text(work, r["new"], r["_st"], scale=s)
work = self._se_fit_size(work, size_opt, custom_w, custom_h)
buf = io.BytesIO()
work.save(buf, "PNG")
images.append({"b64": __import__("base64").b64encode(buf.getvalue()).decode(), "scale": s})
except Exception as e: # noqa: BLE001
print(f"[se] render failed: {e}", flush=True)
self._reply(502, json.dumps({"code": 502, "message": f"重绘失败:{e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
# 落盘到该机器图片历史(铁规则:生成即保存,失败不阻断响应)
try:
for im in images:
self._persist_gen_image(machine, {"data": [{"b64_json": im["b64"]}]},
"无痕改字", prompt, base_im.size[0], base_im.size[1],
kind="seamless")
except Exception as _pe: # noqa: BLE001
print(f"[se] persist failed: {_pe}", flush=True)
pub = [{"old": r["old"], "new": r["new"] or "(仅删除)", "style": r["style"]} for r in results]
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"images": images, "edits": pub, "note": note}}, ensure_ascii=False).encode("utf-8"), "application/json")
# ---------- conversion ----------
def _handle_convert(self):
try:
machine = self.headers.get("X-Machine-Id", "")
tool = self.headers.get("X-Tool", "")
if check(machine, tool) != 200:
self._reply(403, json.dumps({"code": 403, "message": "授权校验未通过或今日试用已用完"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
ctype = self.headers.get("Content-Type", "")
if "multipart/form-data" not in ctype:
self._reply(400, json.dumps({"code": 400, "message": "需要 multipart 上传"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype})
fmt = (form.getvalue("format") or "").lower()
if fmt not in CONVERT_FMTS:
self._reply(400, json.dumps({"code": 400, "message": f"不支持的目标格式:{fmt}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
items = form["files"] if "files" in form else []
if not isinstance(items, list):
items = [items]
items = [it for it in items if getattr(it, "filename", None)]
if not items:
self._reply(400, json.dumps({"code": 400, "message": "请选择要转换的文件"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
tmpdir = tempfile.mkdtemp(prefix="convert-")
try:
src_names = []
for it in items:
safe = os.path.basename(it.filename).replace("\\", "_")
safe = re.sub(r"[^\w.\-()\u4e00-\u9fa5]+", "_", safe) or "file"
with open(os.path.join(tmpdir, safe), "wb") as fh:
shutil.copyfileobj(it.file, fh)
src_names.append(safe)
outs = _convert_files(tmpdir, src_names, fmt)
if len(outs) == 1:
data = open(outs[0], "rb").read()
fname = os.path.basename(outs[0])
self.send_response(200)
self.send_header("Content-Type", "application/octet-stream")
self.send_header("Content-Disposition", f"attachment; filename*=UTF-8''{parse.quote(fname)}")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
else:
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
for o in outs:
zf.write(o, os.path.basename(o))
data = buf.getvalue()
self.send_response(200)
self.send_header("Content-Type", "application/zip")
self.send_header("Content-Disposition", "attachment; filename=converted.zip")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
except RuntimeError as exc:
self._reply(400, json.dumps({"code": 400, "message": str(exc)}, ensure_ascii=False).encode("utf-8"), "application/json")
except Exception as exc: # noqa: BLE001
self._reply(500, json.dumps({"code": 500, "message": f"转换服务错误:{exc}"}, ensure_ascii=False).encode("utf-8"), "application/json")
# ---------- PUT / DELETE ----------
def do_PUT(self):
parsed = parse.urlparse(self.path)
if parsed.path.startswith("/sites/"):
sub = parsed.path[len("/sites"):]
status, body = sites_api("PUT", sub, self._read_payload())
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
self._reply(404, b"not found")
def do_DELETE(self):
parsed = parse.urlparse(self.path)
if parsed.path.startswith("/user"):
self._handle_user("DELETE")
return
if parsed.path.startswith("/sites/"):
sub = parsed.path[len("/sites"):]
status, body = sites_api("DELETE", sub, {})
self._reply(status, json.dumps(body, ensure_ascii=False).encode("utf-8"), "application/json")
return
self._reply(404, b"not found")
# ---- 首页 5 列资讯:每天北京时间 08:00 抓取,缓存到 data/news.json ----
import xml.etree.ElementTree as _ET
from datetime import datetime as _dt, timedelta as _td, timezone as _tz
from email.utils import parsedate_to_datetime as _pdt
NEWS_FILE = os.environ.get("NEWS_FILE", "/app/data/news.json")
_CST = _tz(_td(hours=8))
_BBC_RSS = "https://feeds.bbci.co.uk/news/world/rss.xml"
_TC_AI_RSS = "https://techcrunch.com/category/artificial-intelligence/feed/"
_ITHOME_RSS = "https://www.ithome.com/rss/" # IT之家:权威中文 IT 资讯
_STOCK_URL = ("https://push2.eastmoney.com/api/qt/clist/get?pn=1&pz=10&po=1&np=1"
"&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23&fields=f12,f14,f3")
# AI 相关概念板块(东方财富 board code)——「AI 最热」股票列的股票池来源
_AI_STOCK_BOARDS = [
"BK0800", # 人工智能
"BK1127", # AI 芯片
"BK1134", # 算力概念
"BK1629", # AI 应用
"BK0809", # AI 智能体
"BK1153", # 多模态 AI
"BK1111", # AIGC 概念
"BK1128", # CPO 概念
"BK1182", # 智谱 AI 概念
"BK1172", # AI 语料
"BK0891", # 国产芯片
"BK1137", # 存储芯片
]
def _cst_now():
return _dt.now(_CST)
def _norm_time(s):
"""任意时间字符串 -> 北京时间 MM-DD HH:MM"""
if not s:
return ""
s = str(s).strip()
dt = None
try:
dt = _pdt(s)
except Exception:
try:
dt = _dt.fromisoformat(s.replace("Z", "+00:00"))
except Exception:
return ""
if dt.tzinfo is None:
dt = dt.replace(tzinfo=_tz.utc)
return dt.astimezone(_CST).strftime("%m-%d %H:%M")
def _http_get(opener, url, timeout=20):
req = request.Request(url, headers={"User-Agent": "Mozilla/5.0 (IT-Tools-News)", "Accept": "*/*"})
with opener.open(req, timeout=timeout) as resp:
return resp.read()
def _strip_html(s, cap=100):
s = re.sub(r"<[^>]+>", " ", s or "")
s = re.sub(r"\s+", " ", s).strip()
return s[:cap]
def _parse_rss(data, limit=10):
"""RSS 2.0 / Atom -> [{title,url,time,desc}]"""
items = []
try:
root = _ET.fromstring(data)
except Exception:
return items
for it in root.iter("item"):
t = (it.findtext("title") or "").strip()
link = (it.findtext("link") or "").strip()
pub = (it.findtext("pubDate") or "").strip()
desc = _strip_html(it.findtext("description") or "")
if t:
items.append({"title": t, "url": link, "time": _norm_time(pub), "desc": desc})
if len(items) >= limit:
break
if items:
return items
ns = "{http://www.w3.org/2005/Atom}"
for it in root.iter(f"{ns}entry"):
t = (it.findtext(f"{ns}title") or "").strip()
link = ""
l = it.find(f"{ns}link")
if l is not None:
link = l.get("href", "")
pub = (it.findtext(f"{ns}published") or it.findtext(f"{ns}updated") or "").strip()
desc = _strip_html(it.findtext(f"{ns}summary") or it.findtext(f"{ns}content") or "")
if t:
items.append({"title": t, "url": link, "time": _norm_time(pub), "desc": desc})
if len(items) >= limit:
break
return items
def _fetch_github():
q = (_cst_now() - _td(days=7)).strftime("%Y-%m-%d")
url = f"https://api.github.com/search/repositories?q=created:%3E{q}&sort=stars&order=desc&per_page=10"
j = json.loads(_http_get(OPENER_PROXY, url))
out = []
for r in j.get("items", [])[:10]:
out.append({
"title": f"{r.get('full_name','')} \u2b50{r.get('stargazers_count',0)}",
"url": r.get("html_url", ""),
"time": _norm_time(r.get("created_at") or r.get("pushed_at")),
"desc": _strip_html(r.get("description") or ""),
})
return out
def _fetch_stock():
j = json.loads(_http_get(OPENER_DIRECT, _STOCK_URL))
diff = ((j.get("data") or {}).get("diff")) or []
if isinstance(diff, dict):
diff = list(diff.values())
today = _cst_now().strftime("%m-%d")
out = []
for r in diff[:10]:
name, code, pct = r.get("f14", ""), r.get("f12", ""), r.get("f3", 0)
if not name:
continue
out.append({
"title": f"{name} {code} {pct:+.2f}%",
"url": f"https://quote.eastmoney.com/{code}.html",
"time": today,
})
return out
def _fetch_ai_stock():
"""「AI 最热」:聚合 AI 概念板块成分股,按成交额降序取前 10。
用成交额而非涨幅排序——涨幅榜常年被小盘股占据,成交额榜才是资金真正关注的主线龙头。"""
pool = {}
for bk in _AI_STOCK_BOARDS:
url = ("https://push2.eastmoney.com/api/qt/clist/get?pn=1&pz=200&po=1&np=1"
"&fltt=2&invt=2&fid=f6"
f"&fs=b:{bk}&fields=f2,f3,f6,f12,f14")
try:
j = json.loads(_http_get(OPENER_DIRECT, url))
except Exception as e: # noqa: BLE001
print(f"[news] ai board {bk} failed: {e}", flush=True)
continue
diff = ((j.get("data") or {}).get("diff")) or []
if isinstance(diff, dict):
diff = list(diff.values())
for r in diff:
code, name = str(r.get("f12") or ""), str(r.get("f14") or "")
pct, price, turn = r.get("f3"), r.get("f2"), r.get("f6")
if not code or not name or not isinstance(pct, (int, float)):
continue
turn = turn if isinstance(turn, (int, float)) else 0
# 同一只股票可能属于多个板块,取最大成交额并集
if code not in pool or turn > pool[code]["turn"]:
pool[code] = {"code": code, "name": name, "pct": pct,
"price": price, "turn": turn}
if not pool:
return []
today = _cst_now().strftime("%m-%d")
out = []
for s in sorted(pool.values(), key=lambda x: -x["turn"])[:10]:
price = s["price"]
price_s = f"{price}" if isinstance(price, (int, float)) else "-"
out.append({
"title": f"{s['name']} {s['code']} {s['pct']:+.2f}%",
"desc": f"现价 {price_s} · 成交额 {s['turn'] / 1e8:.1f}亿",
"url": f"https://quote.eastmoney.com/{s['code']}.html",
"time": today,
})
return out
_STOCK_QUOTE_CACHE = {"t": 0, "key": "", "val": []}
def _fetch_quotes(codes, max_age=20):
"""按代码批量取实时行情(自选股)。codes: ['600519','000858',...] 上限 50"""
codes = [str(c).strip() for c in codes if str(c).strip()][:50]
codes = [c for c in codes if re.fullmatch(r"\d{6}", c)]
if not codes:
return []
key = ",".join(codes)
if _STOCK_QUOTE_CACHE["key"] == key and time.time() - _STOCK_QUOTE_CACHE["t"] < max_age:
return _STOCK_QUOTE_CACHE["val"]
secids = ",".join(("1." if c[0] == "6" else "0.") + c for c in codes)
url = ("https://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&invt=2"
f"&fields=f2,f3,f12,f14&secids={secids}")
j = json.loads(_http_get(OPENER_DIRECT, url))
diff = ((j.get("data") or {}).get("diff")) or []
if isinstance(diff, dict):
diff = list(diff.values())
today = _cst_now().strftime("%m-%d")
out = []
for r in diff:
code, name = str(r.get("f12") or ""), str(r.get("f14") or "")
if not code or not name:
continue
price, pct = r.get("f2"), r.get("f3")
if not isinstance(pct, (int, float)):
pct = 0
price_s = f"{price}" if isinstance(price, (int, float)) else "-"
out.append({
"title": f"{name} {code} {pct:+.2f}%",
"desc": f"现价 {price_s}" if price_s != "-" else "",
"url": f"https://quote.eastmoney.com/{code}.html",
"time": today,
"pct": pct,
"code": code,
"name": name,
})
_STOCK_QUOTE_CACHE.update({"t": time.time(), "key": key, "val": out})
return out
def _llm_chat_nm(messages: list, model: str = "", timeout: int = 120) -> str:
"""模块级 LLM 调用(新闻标题翻译用,NewAPI 本地)"""
key = os.environ.get("NEWAPI_KEY", "")
upstream = os.environ.get("NEWAPI_URL", "http://192.168.68.19:3099/v1/chat/completions")
model = (model or os.environ.get("NEWAPI_MODEL", "qwen3.8-flash"))[:60]
if not key:
raise RuntimeError("AI 服务未配置")
body = json.dumps({"model": model, "messages": messages, "temperature": 0.3, "max_tokens": 4096}).encode()
req = request.Request(upstream, data=body, headers={"Content-Type": "application/json", "Authorization": "Bearer " + key})
resp = request.urlopen(req, timeout=timeout)
data = json.loads(resp.read())
if "error" in data:
raise RuntimeError(data["error"].get("message", "upstream error"))
return data["choices"][0]["message"]["content"]
# ---- 本地翻译(在线翻译 + 文档翻译):LibreTranslate(本机 Docker 离线模型,不限流不耗 LLM) ----
# 文档翻译目标语种:旧前端传中文标签、新前端传语言码,两种都兼容
_DOC_LANG_LABELS = {
"中文(简体)": "zh-CN", "中文(繁体)": "zh-TW", "英语": "en", "日语": "ja",
"韩语": "ko", "法语": "fr", "德语": "de", "西班牙语": "es", "俄语": "ru",
"葡萄牙语": "pt", "意大利语": "it", "阿拉伯语": "ar", "越南语": "vi",
"泰语": "th", "印尼语": "id",
}
_TR_LANG_NAMES = {
"zh-CN": "简体中文", "zh-TW": "繁体中文", "en": "英语", "ja": "日语",
"ko": "韩语", "fr": "法语", "de": "德语", "es": "西班牙语", "ru": "俄语",
"pt": "葡萄牙语", "it": "意大利语", "ar": "阿拉伯语", "vi": "越南语",
"th": "泰语", "id": "印尼语",
}
_TR_LANG_SET = set(_TR_LANG_NAMES)
# LibreTranslate(本机 Docker,容器名 libretranslate,仅加载 zh/en 模型)
_LT_URL = os.environ.get("LT_URL", "http://libretranslate:5000/translate")
# 内网直连:显式禁用环境代理(relay 容器带 HTTPS_PROXY,会把内网请求也送进代理)
_LT_OPENER = request.build_opener(request.ProxyHandler({}))
_LT_SRC_MAP = {
"zh-cn": "zh", "zh-tw": "zh", "zh": "zh", "zh-hans": "zh", "zh-hant": "zh",
"en": "en", "en-us": "en", "en-gb": "en",
}
def _lt_code(gcode: str) -> str:
"""谷歌系语言码 → LibreTranslate 码;未加载语种返回 ''(触发 MyMemory 兜底)。"""
return _LT_SRC_MAP.get((gcode or "").strip().lower(), "")
# Argos zh→en 偶发把整段输出成全大写(确定性引擎怪癖):转正常句式时保留缩写大写
_LT_ACRONYMS = {
"JSON", "API", "HTML", "CSS", "HTTP", "HTTPS", "URL", "UUID", "PDF", "DOCX",
"SQL", "USB", "WIFI", "AI", "CSV", "ZIP", "IT", "PC", "GUI", "CLI", "CPU",
"GPU", "RAM", "SSD", "DVD", "VIP", "ID", "OK", "SDK", "APP", "IT-TOOLS-PRO",
}
def _lt_fixcase(s: str) -> str:
"""译文中 >80% 字母为大写时转正常句式(句首大写,其余小写,缩写词保持大写)。"""
letters = [c for c in s if c.isalpha()]
if len(letters) < 10:
return s
if sum(1 for c in letters if c.isupper()) / len(letters) < 0.8:
return s
fixed = []
for w in s.split(" "):
bare = w.strip(".,;:!?()[]{}\"'—-")
if bare and bare.isupper() and len(bare) >= 2 and bare in _LT_ACRONYMS:
fixed.append(w)
else:
fixed.append(w.lower() if bare else w)
out = " ".join(fixed)
return re.sub(r"(^|[\n.!?]\s*)([a-z])", lambda m: m.group(1) + m.group(2).upper(), out)
# ---------- 本机IP查询:IP 归属地(免费端点 /my-ip、/ip-geo 用) ----------
_IPGEO_CACHE = {}
def _ip_valid(s: str) -> bool:
import ipaddress
try:
ipaddress.ip_address(s)
return True
except Exception:
return False
def _ip_info(ip):
"""归属地查询:ip-api 中文数据 + 内存缓存;内网地址只标记 lan 不外查。"""
ip = (ip or "").strip()
if not ip:
return None
import ipaddress
try:
addr = ipaddress.ip_address(ip)
except Exception:
return None
lan = bool(addr.is_private or addr.is_loopback or addr.is_link_local)
cached = _IPGEO_CACHE.get(ip)
if cached is not None:
return cached
info = {"ip": ip, "lan": lan, "country": "", "region": "", "city": "", "isp": "", "as": ""}
if not lan:
try:
url = "http://ip-api.com/json/%s?lang=zh-CN&fields=status,country,regionName,city,isp,as" % ip
with request.urlopen(url, timeout=6) as resp:
d = json.loads(resp.read().decode("utf-8", "ignore"))
if d.get("status") == "success":
info = {
"ip": ip, "lan": False,
"country": d.get("country") or "",
"region": d.get("regionName") or "",
"city": d.get("city") or "",
"isp": d.get("isp") or "",
"as": d.get("as") or "",
}
except Exception:
pass
if len(_IPGEO_CACHE) > 1500:
_IPGEO_CACHE.clear()
_IPGEO_CACHE[ip] = info
return info
# ---------- 代理/VPN 判定(proxycheck.io 免 key,内存缓存) ----------
_IPCHECK_CACHE = {}
def _ip_check(ip):
"""出口 IP 代理/VPN/数据中心判定:proxycheck.io + 内存缓存;内网地址不外查。
返回 {ip, lan, proxy(bool|None), type, provider, hostname, asn};
查询失败 proxy=None(前端降级为“无法判定”)。"""
ip = (ip or "").strip()
if not ip or not _ip_valid(ip):
return None
import ipaddress
addr = ipaddress.ip_address(ip)
lan = bool(addr.is_private or addr.is_loopback or addr.is_link_local)
cached = _IPCHECK_CACHE.get(ip)
if cached is not None:
return cached
out = {"ip": ip, "lan": lan, "proxy": None, "type": "", "provider": "", "hostname": "", "asn": ""}
if not lan:
try:
url = "http://proxycheck.io/v2/%s?vpn=1&asn=1" % ip
with request.urlopen(url, timeout=8) as resp:
d = json.loads(resp.read().decode("utf-8", "ignore"))
if d.get("status") == "ok":
rec = d.get(ip) or {}
out = {
"ip": ip, "lan": False,
"proxy": str(rec.get("proxy") or "").lower() == "yes",
"type": rec.get("type") or "",
"provider": rec.get("provider") or rec.get("organisation") or "",
"hostname": rec.get("hostname") or "",
"asn": rec.get("asn") or "",
}
except Exception:
pass
if len(_IPCHECK_CACHE) > 1500:
_IPCHECK_CACHE.clear()
_IPCHECK_CACHE[ip] = out
return out
def _tr_chunks(text: str, limit: int = 1200):
"""按行/长度切块,保留换行结构;返回 [(块文本, 块后是否接换行)]"""
lines = text.split("\n")
units = []
for idx, ln in enumerate(lines):
nl = idx < len(lines) - 1
while len(ln) > limit: # 超长单行硬切(切点不补换行)
units.append((ln[:limit], False))
ln = ln[limit:]
units.append((ln, nl))
chunks, cur, cur_nl = [], "", False
for frag, nl in units:
sep = "\n" if cur and cur_nl else ""
if len(cur) + len(sep) + len(frag) <= limit:
cur = cur + sep + frag
cur_nl = nl
else:
if cur:
chunks.append((cur, cur_nl))
cur, cur_nl = frag, nl
if cur:
chunks.append((cur, cur_nl))
return chunks or [("", False)]
def _tr_norm_lang(raw: str) -> str:
lang = (raw or "").strip().lower().replace("_", "-")
if lang in _TR_LANG_SET:
return lang
if lang in ("zh", "zh-cn", "zh-hans", "zh-hans-cn", "cmn"):
return "zh-CN"
if lang in ("zh-tw", "zh-hant", "zh-hant-tw"):
return "zh-TW"
for code in _TR_LANG_SET:
if lang.startswith(code.lower()[:2]):
return code
return ""
def _translate_local(q: str, sl: str, tl: str):
"""本地开源翻译:LibreTranslate(本机 Docker,离线 Argos 模型),不消耗 LLM 额度。
返回 (译文, 检测到的源语言谷歌码)。语种未加载时抛错 → 前端自动转 MyMemory 兜底。"""
text = (q or "").strip()
if not text:
return "", ("" if sl == "auto" else sl)
tgt = _lt_code(tl)
if not tgt:
raise RuntimeError(f"local translate: target '{tl}' not loaded")
src = "auto"
if sl and sl != "auto":
src = _lt_code(sl)
if not src:
raise RuntimeError(f"local translate: source '{sl}' not loaded")
detected = "" if src == "auto" else src
parts = []
for chunk, nl in _tr_chunks(text):
if not chunk:
parts.append(("", nl))
continue
body = json.dumps(
{"q": chunk, "source": src, "target": tgt, "format": "text"}
).encode("utf-8")
req_obj = request.Request(
_LT_URL, data=body, headers={"Content-Type": "application/json"}
)
with _LT_OPENER.open(req_obj, timeout=120) as resp:
obj = json.loads(resp.read().decode("utf-8"))
if not isinstance(obj, dict) or "translatedText" not in obj:
raise RuntimeError(f"local translate: {obj}")
parts.append((_lt_fixcase(str(obj["translatedText"])), nl))
if not detected:
dl = str((obj.get("detectedLanguage") or {}).get("language", ""))
detected = {"zh-Hans": "zh-CN", "zh-Hant": "zh-TW"}.get(dl, dl)
translated = "".join(t + ("\n" if nl else "") for t, nl in parts)
if sl != "auto":
detected = sl
return translated, detected
def _is_urlish(s: str) -> bool:
s = (s or "").strip().lower()
return s.startswith("http") or "article url" in s
class _TrMismatch(Exception):
"""LLM 返回条数与输入不符(响应正常,可拆半重试)"""
def _translate_chunk(chunk: list, condense: bool = False, depth: int = 0) -> list:
"""翻译一批;数量不符拆半重试(快),超时/异常立即放弃(避免级联拖慢)"""
if not chunk:
return chunk
try:
if condense:
instr = ("把下面 JSON 数组里每条英文翻译成简体中文,写成约 36~40 字的简介(两行、信息完整、尽量写满,不要过短)。"
"输出同样长度的 JSON 字符串数组,顺序一致,只输出 JSON。已是中文的原样保留。")
else:
instr = ("把下面 JSON 数组里每条英文新闻标题翻译成简体中文,人名/机构/产品名保留英文(可跟中文意译)。"
"输出同样长度的 JSON 字符串数组,顺序完全一致,只输出 JSON(不要代码块标记、不要解释)。"
"已是中文的条目原样保留。")
prompt = instr + "\n" + json.dumps(list(chunk), ensure_ascii=False)
out = _llm_chat_nm([{"role": "user", "content": prompt}], timeout=(60 if depth else 120))
s = out.strip()
if s.startswith("```"):
s = re.sub(r"^```[a-zA-Z]*\s*", "", s)
s = re.sub(r"\s*```$", "", s).strip()
arr = json.loads(s)
if isinstance(arr, list) and len(arr) == len(chunk):
return [str(x) for x in arr]
raise _TrMismatch(f"count {len(arr) if isinstance(arr, list) else 'non-list'} != {len(chunk)}")
except _TrMismatch:
# 数量不符(响应正常):拆半重试,更小批次更可靠,最多 2 层
if len(chunk) > 1 and depth < 2:
mid = len(chunk) // 2
return (_translate_chunk(chunk[:mid], condense, depth + 1)
+ _translate_chunk(chunk[mid:], condense, depth + 1))
print(f"[news] translate mismatch give up ({len(chunk)})", flush=True)
return chunk
except Exception as e: # noqa: BLE001
# 超时/网络错误:立即放弃,避免层层重试拖慢每日抓取
print(f"[news] translate timeout/error ({len(chunk)}): {e}", flush=True)
return chunk
def _translate_chunked(texts: list, condense: bool = False) -> list:
"""分批 + 并行翻译:分块避免单次过大,块间并行缩短总时长"""
CH = 4 if condense else 10 # 小批量减少单次长输出, 避开慢LLM超时
chunks = [texts[i:i + CH] for i in range(0, len(texts), CH)]
if not chunks:
return []
if len(chunks) == 1:
return _translate_chunk(chunks[0], condense)
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=min(2, len(chunks))) as ex:
results = list(ex.map(lambda c: _translate_chunk(c, condense), chunks))
out = []
for r in results:
out.extend(r)
return out
def _fetch_news():
cols = []
def _col(key, title, icon, fn):
try:
items = (fn() or [])[:10]
except Exception as e: # noqa: BLE001
print(f"[news] {key} failed: {e}", flush=True)
items = []
cols.append({"key": key, "title": title, "icon": icon, "items": items})
_col("github", "GITHUB", "\U0001f419", _fetch_github)
_col("world", "\u56fd\u9645\u65b0\u95fb", "\U0001f30d",
lambda: _parse_rss(_http_get(OPENER_PROXY, _BBC_RSS)))
_col("ai", "AI\u8d44\u8baf", "\U0001f916",
lambda: _parse_rss(_http_get(OPENER_PROXY, _TC_AI_RSS)))
_col("it", "IT\u6700\u65b0\u6280\u672f", "\U0001f4bb",
lambda: _parse_rss(_http_get(OPENER_DIRECT, _ITHOME_RSS)))
_col("stock", "AI\u6700\u70ed\u80a1\u7968", "\U0001f4c8", _fetch_ai_stock)
# 翻译:world/ai 英文标题与简介、github 英文简介(it=IT之家中文、stock 中文均免翻)
en_title_keys = {"world", "ai"}
en_desc_keys = {"github", "world", "ai"}
title_texts, title_refs = [], []
desc_texts, desc_refs = [], []
for ci, c in enumerate(cols):
for ii, it in enumerate(c["items"]):
if c["key"] in en_title_keys:
title_texts.append(it["title"])
title_refs.append((ci, ii))
d = it.get("desc") or ""
if _is_urlish(d):
cols[ci]["items"][ii]["desc"] = "" # 丢弃 URL 类
elif d and c["key"] in en_desc_keys:
desc_texts.append(d) # 英文简介:翻译压缩
desc_refs.append((ci, ii))
# it/stock 中文简介:保留,稍后去套话+截断
if title_texts:
for (ci, ii), t in zip(title_refs, _translate_chunked(title_texts, condense=False)):
cols[ci]["items"][ii]["title"] = t
if desc_texts:
for (ci, ii), t in zip(desc_refs, _translate_chunked(desc_texts, condense=True)):
cols[ci]["items"][ii]["desc"] = t
# 简介:去来源+日期套话(如"IT之家10月13日消息,")+ 统一截断 40 字(约满 2 行)
for c in cols:
for it in c["items"]:
d = it.get("desc") or ""
d = re.sub(r"^(?:IT之家|快科技|cnBeta)[^,,]{0,25}消息[,,、\s]*", "", d).strip()
if len(d) > 40:
d = d[:40] + "…"
it["desc"] = d
return cols
def _load_news_file():
try:
with open(NEWS_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return {}
def _save_news_file(data):
try:
os.makedirs(os.path.dirname(NEWS_FILE), exist_ok=True)
tmp = NEWS_FILE + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False)
os.replace(tmp, NEWS_FILE)
except Exception as e: # noqa: BLE001
print(f"[news] save failed: {e}", flush=True)
def _is_trading_time():
"""A股交易时段(北京时间):周一至周五 9:15-15:05(含午休,此时段内重抓开销极小)"""
now = _cst_now()
if now.weekday() >= 5:
return False
hm = now.hour * 100 + now.minute
return 915 <= hm <= 1505
def _market_status():
"""A股开盘状态:交易日 9:30-11:30 / 13:00-15:00 为交易中,其余休市(节假日未单独识别)"""
now = _cst_now()
if now.weekday() >= 5:
return "closed"
hm = now.hour * 100 + now.minute
if 930 <= hm <= 1130 or 1300 <= hm <= 1500:
return "open"
return "closed"
def _refresh_stock_if_stale(data, max_age=60):
"""「AI 最热股票」列高频刷新:交易时段内按需重抓(其余 4 列仍每天 08:00 更新)。
失败/非交易时段直接返回原数据,绝不影响 /news 正常返回。"""
try:
if not data.get("columns") or not _is_trading_time():
return data
if time.time() - float(data.get("stock_updated") or 0) < max_age:
return data
fresh = _fetch_ai_stock()
if not fresh:
return data
for c in data["columns"]:
if c.get("key") == "stock":
c["items"] = fresh
break
else:
return data
data["stock_updated"] = int(time.time())
_save_news_file(data)
print(f"[news] stock refreshed ({len(fresh)} items)", flush=True)
except Exception as e: # noqa: BLE001
print(f"[news] stock refresh failed: {e}", flush=True)
return data
def _maybe_refresh_news():
data = _load_news_file() or {}
now = _cst_now()
today, hour = now.strftime("%Y-%m-%d"), now.hour
last = data.get("date", "")
after8 = bool(data.get("after8"))
need = False
if not data.get("columns"):
need = True # first boot / empty
elif last != today and hour >= 8:
need = True # new day, past 08:00 CST
elif last == today and not after8 and hour >= 8:
need = True # booted before 08:00, now past it
if not need:
return
cols = _fetch_news()
_save_news_file({"date": today, "after8": hour >= 8, "updated": int(time.time()), "columns": cols})
print(f"[news] refreshed {today} {hour}h counts={[len(c['items']) for c in cols]}", flush=True)
def _news_loop():
while True:
try:
_maybe_refresh_news()
except Exception as e: # noqa: BLE001
print(f"[news] loop error: {e}", flush=True)
time.sleep(600)
if __name__ == "__main__":
_ensure_admin()
mcp_ops.setup(
data_dir=DATA_DIR,
usage_db=USAGE_DB,
query_authorized=query_authorized,
account_ok=_account_license_ok,
machine_ok=lambda m: bool(MACHINE_RE.match(m)),
public_base=os.environ.get("MCP_PUBLIC_BASE", ""),
)
threading.Thread(target=_news_loop, daemon=True).start()
ThreadingHTTPServer(("0.0.0.0", 8091), Handler).serve_forever()