Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(web): 整站拒绝搜索引擎收录

longmeidao2812cf87038f
diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.md
index 5bcff86bf..fb08e6baf 100644
--- a/docs/OPERATIONS.md
+++ b/docs/OPERATIONS.md
@@ -176,6 +176,12 @@ curl -s --noproxy '*' -o /dev/null -w '%{http_code}\n' https://peach.local/healt
- 发布入口是 `scripts/build_windows.ps1`:先用 `scripts/generate_brand_assets.py` 生成方形 Logo 与多尺寸 `.ico`,再构建单一 `dist/Peach/Peach.exe`;无参数运行托盘,`serve`/`migrate` 运行 CLI。桌面快捷方式和开机自启都在配置页的「开机自启」一组里开关,由 `src/peach/desktop_startup.py` 落盘;`scripts/manage_tray_startup.ps1` 只作发布包的排障入口。
- Cloudflare Quick Tunnel 分源码和独立包两条 origin:源码托盘取实际 HTTPS 子服务(Windows 是局域网 `:443`,macOS/手工服务按实际 TLS 端口),附项目 CA 与 mDNS SNI;独立包只取回环 HTTP `127.0.0.1:<server.port>`,不能拿源码的 443 或明文跳转口代替。`https://try.cloudflare.com/` 是介绍页,真正入口是 cloudflared 输出的随机 `https://<id>.trycloudflare.com`。开关默认关闭,只有 Peach 已设置访问密码时才允许启动;随机 URL 只写数据根状态文件,停止服务会清理子进程和状态。
- 独立包构建把固定版本的官方 `cloudflared-windows-amd64.exe` 作为旁路文件放在 `Peach.exe` 旁边,并用 `scripts/cloudflared-windows.json` 的 SHA-256 校验;构建前运行 `scripts/fetch_cloudflared.ps1`,或给 `build_windows.ps1 -CloudflaredPath` 一个同哈希文件。源码运行不携带二进制,按设置文件、`PEACH_CLOUDFLARED`、PATH 顺序查找。Quick Tunnel 还需要出站 TCP/UDP `7844`,被防火墙或代理阻断时必须显示失败,不得把已申请 URL 当作可用入口。
+- 命名隧道(Named Tunnel)是源码开发环境专用的第二种形态,地址固定、重启不变。独立包读到 `tunnel.mode = "named"` 会拒绝启动隧道并报「独立包只支持临时链接」,配置页在那里也不渲染这一块。四步:
+ 1. 在 Cloudflare Zero Trust 的 Networks → Tunnels 里新建一条 Cloudflared 隧道,拿到它的隧道令牌(Token)。
+ 2. 给这条隧道加一个 Public hostname,主机名选自己的域名,Service 填本机 origin——形态与临时链接那条一致:源码部署是局域网 `https://<局域网 IP>:443`(macOS 按实际 TLS 端口),并在 Additional settings 里把 Origin Server Name 填成 `<mdns_name>.local`、把项目 CA 交给 CA Pool,否则本机自签证书过不了校验。
+ 3. 在配置页的「Cloudflare 公网入口」里切到「命名隧道」,填公开主机名和隧道令牌,按「保存配置」。令牌只写进设置文件的 `[tunnel] token`,页面不回显,账本、日志与 `/healthz` 都没有它;换令牌就填一个新的,留空表示沿用已存的那份。
+ 4. 建议在 Cloudflare Access 里给这个主机名配一条身份策略。那一层完全在 Cloudflare 后台,Peach 不读也不管;Peach 自己的访问密码仍是启动隧道的前提,两道门并存。
+- 整站拒绝搜索引擎收录:每个响应都带 `X-Robots-Tag: noindex, nofollow, noarchive`(页面、静态资源与 API 一视同仁),`/robots.txt` 不要求登录地回 `Disallow: /`,主页面与登录页另有 `<meta name="robots">`。这是公网入口开着时的默认态度,不需要也不应该按部署开关。
- 对外测试包由 `.github/workflows/release.yml` 承担:`build_windows.ps1 -Standalone` 用 PyInstaller onedir 生成完整 Windows 程序目录,压缩后由不检出源码的消费任务运行 `scripts/smoke_desktop.py`;冒烟会完成首启、以 `0.0.0.0` 重启、从本机局域网地址读取 `/healthz`、核对 mDNS 已装配并用访问密码登录。`v<__version__>` tag 与版本不一致会失败,通过制品验收才创建 GitHub 预发布并附 SHA256;`workflow_dispatch` 只生成和验收 artifact。macOS 独立包另列待办,源码菜单栏构建入口仍为 `build_macos_app.py`。
- 刷新源码运行态不要用 Computer Use 点托盘:`python scripts/restart_windows_tray.py` 按精确 EXE 路径找到 pystray 隐藏窗口、发送正常停止消息、等托盘自行关闭子服务,再静默启动并核对新托盘重新拥有两个服务;找不到唯一窗口或退出超时就拒绝,绝不强杀后另启。`--swap-from <暂存包>` 让它在旧托盘退出后、新托盘启动前顺手换掉二进制,那是整条链路上唯一一个目标文件没有进程持有的窗口;换生产二进制用下面那条入口,不要单独调它。打包托盘窗口不在时命令自动改走源码托盘:`find_source_tray_windows()` 按同一窗口类名加「命令行含 `peach.tray`」认源码托盘,`restart_source_tray()` 照抄原命令行(含 `--show`/数据根)等托盘退出后自起,不换包不备份;`--source` 显式跳过打包入口。
- 换掉生产托盘二进制走 `python scripts/deploy_windows_tray.py`,在主检出里用项目 venv 的 Python 跑:拒绝脏检出 → 按 HEAD 提交号在 `<数据根>/state/source-sync-build/<commit>/` 构建 → 让暂存包自己跑一次 `migrate status` → 停旧托盘、原地换 `dist/Peach/Peach.exe`(旧的留成 `Peach.pre-source-sync-<时间>.exe`)、起新托盘 → 用项目 CA 严格校验读生产 HTTPS 口的 `/healthz`。验收按回话的是谁分成两条:独立发行版里回话的就是冻结进程自己,核对它报的 `build_commit` 等于这次打包的提交;本机这套形态下回话的是 venv 里的源码进程——托盘刻意把两个子服务交回项目 venv(`src/peach/tray.py` 的 `_peach_executable()`),那个进程没有第二个版本,`build_commit` 结构上恒为 None——这时核对生产入口那个文件的 sha256 等于这次打出来的暂存包,而托盘正是换完之后才从这个文件起来并拿到两个子服务的。两条都不认版本号:版本号一次发布才推一格,同一个号下有很多个构建,只比版本号证明不了二进制换掉了。结果打印成一份 JSON,`step` 说明停在哪一步。新托盘起不来或拿不到两个子服务时自动换回备份并重开旧托盘;`--staged <路径>` 复用已经打好的包、跳过构建。不要就地构建 `dist/Peach/`:运行中的托盘持有那个文件,PyInstaller 清目录会撞上 WinError 5。换生产入口本身仍要当场授权,脚本不代替那次授权。
diff --git a/docs/STATUS.md b/docs/STATUS.md
index 4af389bae..33cfa17eb 100644
--- a/docs/STATUS.md
+++ b/docs/STATUS.md
@@ -26,7 +26,7 @@
- 本机运行 Python 3.14;`requires-python` 下限 3.12,GitHub Actions 同时测 3.12 与 3.14;Windows FFmpeg/ffprobe 位于 `peach-data/tools/ffmpeg`,macOS 走 PATH。
- 发行名 `peach`,目录名 `peach-app`。macOS 落后 master 一组有顺序的操作(待办「待执行的操作」第 30 条),做完前别重启菜单栏:无口令的 `peach serve --host 0.0.0.0` 会拒绝启动。
- 扫描与采集显示项目、动作与等待时长;无进展 120 秒预警,单项外部动作(资料 90 秒、封面 240 秒)超预算跳过该项计入可重试,「重试未完成项」重跑原任务失败集合。问题写入 `state/library-processing-<job_id>.issues.jsonl`(带标题与路径),接口按 `job_id` 分页,页面错误折叠给前 20 条与日志地址。已部署核对。
-- Cloudflare Quick Tunnel 已接入:默认关闭,配置页开关启停,须先启用访问密码;随机地址只写数据根状态文件。
+- Cloudflare 公网入口默认关闭,配置页启停,须先设访问密码;临时链接地址只写状态文件,命名隧道限源码环境、令牌只存设置文件。整站 `X-Robots-Tag: noindex` 加 `/robots.txt`。
## 批处理进度
diff --git a/src/peach/api.py b/src/peach/api.py
index 4cd37379c..0b2fe1ded 100644
--- a/src/peach/api.py
+++ b/src/peach/api.py
@@ -346,6 +346,9 @@ def create_app(
response.headers["Cache-Control"] = "no-store"
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["Referrer-Policy"] = "same-origin"
+ # 公网入口开着的时候整个站都在互联网上。这一条覆盖每一个响应——页面、静态
+ # 资源和 API 都算,页面里的 `<meta name="robots">` 只管得到 HTML 那几页。
+ response.headers["X-Robots-Tag"] = routes_pages.ROBOTS_TAG
return response
# JSON 契约、页面脚本与样式是仅有的几类大文本响应,压下去省的字节最多:
diff --git a/src/peach/routes_auth.py b/src/peach/routes_auth.py
index c79778425..fb3d9f90c 100644
--- a/src/peach/routes_auth.py
+++ b/src/peach/routes_auth.py
@@ -199,7 +199,11 @@ def login_html(next_path: str, *, invalid: bool = False) -> str:
return (
'<!doctype html><html lang="zh-CN"><meta charset="utf-8">'
'<meta name="viewport" content="width=device-width,initial-scale=1">'
- '<meta name="color-scheme" content="light dark"><title>登录 Peach</title>'
+ '<meta name="color-scheme" content="light dark">'
+ # 公网入口在跑的时候这一页就在互联网上,不希望它进任何搜索结果。
+ # 响应头那一份(`X-Robots-Tag`)管所有响应,这一行管只读 HTML 的爬虫。
+ '<meta name="robots" content="noindex, nofollow">'
+ '<title>登录 Peach</title>'
# 图标声明和主站同一份。书签地址是 `/`,没有会话时这一页就是它实际停在的地方:
# 这里不声明,浏览器只会去要 `/favicon.ico`,把「这个站没有图标」记进书签。
'<link rel="icon" href="/favicon.ico" type="image/x-icon">'
diff --git a/src/peach/routes_pages.py b/src/peach/routes_pages.py
index 916f8f698..cabcaa73a 100644
--- a/src/peach/routes_pages.py
+++ b/src/peach/routes_pages.py
@@ -42,6 +42,11 @@ from .web_state import FAVICON
router = APIRouter()
+#: 整站的收录态度,`api.py` 的中间件给每个响应都挂上这一份。Peach 是一个人的私人
+#: 馆藏,任何一次公网暴露都不该在搜索引擎里留下痕迹:不收录、不跟随、不留快照。
+ROBOTS_TAG = "noindex, nofollow, noarchive"
+ROBOTS_TXT = "User-agent: *\nDisallow: /\n"
+
#: 回环地址的三种写法。既用来判提交端点的调用方,也用来判「只有这台电脑」那个监听选择。
_LOOPBACK = frozenset({"127.0.0.1", "::1", "localhost"})
@@ -903,6 +908,18 @@ def app_bundle(request: Request, name: str,
return asset_response(request, path, media)
[email protected]_route("/robots.txt", methods=["GET", "HEAD"])
+def robots_txt():
+ """不要求登录:爬虫拿不到会话,被 401 挡住就等于没读到这份声明。
+
+ 响应头里那一条才是真闸门(登录页、资产与 API 都带着它);这一份只是把同一个
+ 结论放在爬虫会主动来取的固定路径上。
+ """
+ response = PlainTextResponse(ROBOTS_TXT)
+ response.headers["Cache-Control"] = "no-store"
+ return response
+
+
@router.api_route("/favicon.svg", methods=["GET", "HEAD"])
def favicon():
response = Response(FAVICON, media_type="image/svg+xml")
diff --git a/tests/test_fastapi_api.py b/tests/test_fastapi_api.py
index 6cac863c3..58421ba28 100644
--- a/tests/test_fastapi_api.py
+++ b/tests/test_fastapi_api.py
@@ -134,6 +134,52 @@ class MediaCacheHeaderTests(unittest.TestCase):
self.assertNotIn('"public, max-age=31536000, immutable"', source)
[email protected](HAS_DEPS, "FastAPI/httpx 尚未安装")
+class RobotsTests(unittest.IsolatedAsyncioTestCase):
+ """整站不进搜索引擎。
+
+ 响应头覆盖每一个响应,`/robots.txt` 与两页 `<meta>` 覆盖爬虫会主动去读的位置。
+ 公网入口一开,这个站就在互联网上,收录了就再也收不回来,所以不按部署开关。
+ """
+
+ @classmethod
+ def setUpClass(cls):
+ cls.tmp = tempfile.TemporaryDirectory()
+ root = Path(cls.tmp.name).resolve()
+ cls.app = create_app(PeachSettings(
+ configured=True, db_path=root / "ledger.db", follow_state_root=root / "state"))
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.app.state.http_transport.close()
+ cls.tmp.cleanup()
+
+ def client(self):
+ return httpx.AsyncClient(
+ transport=httpx.ASGITransport(app=self.app), base_url="http://testserver")
+
+ async def test_robots_txt_is_readable_without_signing_in(self):
+ async with self.client() as client:
+ response = await client.get("/robots.txt")
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.text, "User-agent: *\nDisallow: /\n")
+
+ async def test_every_response_carries_the_robots_header(self):
+ async with self.client() as client:
+ for path in ("/robots.txt", "/healthz", "/login", "/favicon.ico", "/api/items"):
+ with self.subTest(path=path):
+ response = await client.get(path)
+ self.assertEqual(
+ response.headers["x-robots-tag"], "noindex, nofollow, noarchive")
+
+ def test_the_login_page_and_the_app_shell_declare_noindex(self):
+ # 这份服务没有口令,`/login` 会直接跳回首页;声明本身在页面源里。
+ self.assertIn('<meta name="robots" content="noindex, nofollow">',
+ routes_auth.login_html("/"))
+ shell = (ROOT / "web" / "index.html").read_text(encoding="utf-8")
+ self.assertIn('<meta name="robots" content="noindex, nofollow">', shell)
+
+
@unittest.skipUnless(HAS_DEPS, "FastAPI/httpx 尚未安装")
class FastApiContractTests(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
@@ -2212,6 +2258,8 @@ PUBLIC_ROUTES = {
("GET,HEAD", "/favicon.ico"),
("GET,HEAD", "/favicon.svg"),
("GET,HEAD", "/peach-logo.png"),
+ # 拒绝收录的声明。爬虫没有会话,被 401 挡住等于这份声明根本没被读到。
+ ("GET,HEAD", "/robots.txt"),
}
diff --git a/web/index.html b/web/index.html
index 7119ee61c..2a1cf9816 100644
--- a/web/index.html
+++ b/web/index.html
@@ -3,6 +3,8 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
+<!-- 私人馆藏不进搜索结果。响应头 X-Robots-Tag 管所有响应,这一行管只读 HTML 的爬虫。 -->
+<meta name="robots" content="noindex, nofollow">
<meta name="theme-color" content="#FFFFFF" media="(prefers-color-scheme: light)" data-theme-color="light">
<meta name="theme-color" content="#080A0D" media="(prefers-color-scheme: dark)" data-theme-color="dark">
<link rel="icon" href="/favicon.ico" type="image/x-icon">