Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix: serve HTTPS on macOS, and stop the pf redirect from eating loopback traffic

Peach local maintenance8963ae0619cb
diff --git a/README.md b/README.md
index 87946369e..719d34a25 100644
--- a/README.md
+++ b/README.md
@@ -153,11 +153,20 @@ sh scripts/setup_macos_port80.sh check # 先验证,不需要 root
sudo sh scripts/setup_macos_port80.sh install
```
-`rdr-anchor` 必须落在 translation 段:pf 要求规则严格按 options → normalization →
-queueing → translation → filtering 排列,追加到 `/etc/pf.conf` 末尾会落在
-`anchor "com.apple/*"`(filtering)之后并报 `Rules must be in order`。脚本因此插在最后
-一条 `rdr-anchor`/`nat-anchor` 之后,并且**先在临时文件上 `pfctl -n -f` 验证过才动系统
-文件**——`/etc/pf.conf` 写坏了,开机时整个包过滤都加载不起来。
+它同时转 80 → 8900 和 443 → 8443;HTTPS 用的还是 `peach-data/secrets/tls` 里那套本机
+CA 证书(`CN=peach.local`,有效期到 2027-09)。
+
+两个 pf 上踩过的坑:
+
+- **`rdr-anchor` 必须落在 translation 段。** pf 要求规则严格按 options → normalization
+ → queueing → translation → filtering 排列,追加到 `/etc/pf.conf` 末尾会落在
+ `anchor "com.apple/*"`(filtering)之后并报 `Rules must be in order`。脚本因此插在
+ 最后一条 `rdr-anchor`/`nat-anchor` 之后,并且**先在临时文件上 `pfctl -n -f` 验证过
+ 才动系统文件**——`/etc/pf.conf` 写坏了,开机时整个包过滤都加载不起来。
+- **高位端口要显式 `no rdr` 排除。** 只加转发规则时,直连 `127.0.0.1:8900` 会 TCP 连得上、
+ HTTP 却永远收不到响应:lo0 上的 rdr 状态把这条流也一起翻译了。托盘的健康检查正好走这个
+ 地址,于是服务明明在跑却一直被判成「未运行」。translation 规则是第一条命中就生效
+ (和 filter 的最后一条命中相反),所以 `no rdr` 必须写在转发规则前面。
跨两台机器开发时换行口径由 `.gitattributes` 的 `* text=auto eol=lf` 固定。不要依赖各自的
`core.autocrlf`:2026-08 之前 Windows 侧把 105 个文件整体改写成 CRLF,`git status` 长期显示
diff --git a/scripts/setup_macos_port80.sh b/scripts/setup_macos_port80.sh
index e84642ded..2aea3a01d 100644
--- a/scripts/setup_macos_port80.sh
+++ b/scripts/setup_macos_port80.sh
@@ -1,5 +1,5 @@
#!/bin/sh
-# 让 http://peach.local/ 不带端口也能打开。
+# 让 http(s)://peach.local/ 不带端口也能打开。
#
# macOS 和其他 Unix 一样,1024 以下的端口只有 root 能监听。Peach 在 macOS 上跑在
# 非特权的 8900(见 peach.tray.MACOS_PORT),所以 `peach.local:8900` 通、
@@ -26,6 +26,7 @@ ANCHOR_NAME="gg.lmd.peach"
ANCHOR_FILE="/etc/pf.anchors/${ANCHOR_NAME}"
DAEMON="/Library/LaunchDaemons/${ANCHOR_NAME}.pf.plist"
TARGET_PORT="${PEACH_PORT:-8900}"
+TLS_PORT="${PEACH_TLS_PORT:-8443}"
ACTION="${1:-install}"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
@@ -66,15 +67,24 @@ if [ "$ACTION" = "uninstall" ]; then
grep -v "$ANCHOR_NAME" /etc/pf.conf > "$WORK/pf.conf"
cp "$WORK/pf.conf" /etc/pf.conf
pfctl -f /etc/pf.conf 2>/dev/null || true
- echo "已移除 80 -> ${TARGET_PORT} 的重定向"
+ echo "已移除 80/443 的重定向"
exit 0
fi
cat > "$ANCHOR_FILE" <<RULES
# 由 scripts/setup_macos_port80.sh 生成,勿手改。
+#
+# translation 规则是**第一条命中就生效**(和 filter 的最后一条命中相反),所以
+# 例外必须写在前面。没有这两条 \`no rdr\` 时,直连 127.0.0.1:${TARGET_PORT} 会 TCP
+# 连得上、HTTP 却永远收不到响应——lo0 上的 rdr 状态把这条流也一起翻译了。托盘的
+# 健康检查正好走这个地址,于是服务明明在跑却一直被判成「未运行」。
+no rdr on lo0 proto tcp from any to any port ${TARGET_PORT}
+no rdr on lo0 proto tcp from any to any port ${TLS_PORT}
# 本机自己访问走 lo0,其他设备走真实网卡,两条都要。
rdr pass on lo0 inet proto tcp from any to any port 80 -> 127.0.0.1 port ${TARGET_PORT}
rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port ${TARGET_PORT}
+rdr pass on lo0 inet proto tcp from any to any port 443 -> 127.0.0.1 port ${TLS_PORT}
+rdr pass inet proto tcp from any to any port 443 -> 127.0.0.1 port ${TLS_PORT}
RULES
# 先在临时文件上验证,通过了才动系统文件。
@@ -104,6 +114,7 @@ pfctl -f /etc/pf.conf
launchctl bootout system "$DAEMON" 2>/dev/null || true
launchctl bootstrap system "$DAEMON"
-echo "已把 80 重定向到 ${TARGET_PORT}"
-echo " 验证:curl -sI --noproxy '*' http://peach.local/healthz | head -1"
+echo "已把 80 -> ${TARGET_PORT}、443 -> ${TLS_PORT}"
+echo " 验证:curl -s --noproxy '*' -o /dev/null -w '%{http_code}\\n' http://peach.local/healthz"
+echo " curl -s --noproxy '*' -o /dev/null -w '%{http_code}\\n' https://peach.local/healthz"
echo " 移除:sudo sh $0 uninstall"
diff --git a/src/peach/api.py b/src/peach/api.py
index 9950cc43b..482bb2637 100644
--- a/src/peach/api.py
+++ b/src/peach/api.py
@@ -185,7 +185,9 @@ def create_app(
response.headers["Referrer-Policy"] = "same-origin"
return response
- @app.get("/healthz")
+ # 健康检查常被 HEAD 探测(`curl -I`、各种 uptime 工具)。本仓库其他公开端点
+ # 都显式声明了 GET+HEAD,只有这个漏了,HEAD 会拿到 405。
+ @app.api_route("/healthz", methods=["GET", "HEAD"])
def healthz() -> dict[str, Any]:
# 不探测/迁移数据库;健康检查必须无副作用。
ffmpeg = resolver.ffmpeg()
diff --git a/src/peach/tray.py b/src/peach/tray.py
index f0a59d81e..70693fa28 100644
--- a/src/peach/tray.py
+++ b/src/peach/tray.py
@@ -27,6 +27,8 @@ DEFAULT_LAN_ADDRESS = "192.168.50.162"
#: macOS 菜单栏项拉起的端口。80/443 在 macOS 上要 root,开发机不该为一个菜单栏图标
#: 去要管理员权限;本机 CA 的那套 TLS 材料也是给 Windows 生产实例签的。
MACOS_PORT = 8900
+#: HTTPS 同理,443 也要 root,所以走 8443。
+MACOS_TLS_PORT = 8443
#: 单击菜单栏/托盘图标打开的地址。Windows 生产实例有本机 CA 的 HTTPS 和 80/443;
#: macOS 是开发环境,走非特权端口的 HTTP。
OPEN_URL = (
@@ -256,20 +258,40 @@ def _peach_executable() -> Path:
def build_macos_service_specs() -> tuple[ServiceSpec, ...]:
- """macOS 只起一个非特权端口的 HTTP 服务。
+ """macOS 用非特权端口起 HTTP,有 TLS 材料时再加一个 HTTPS。
- 不钉 `--mdns-address`:这台机器会换网络,钉死等于换个 Wi-Fi 就打不开
- (见 `peach.mdns` 的地址复查)。
+ 80/443 在 macOS 上要 root,所以服务本身跑在 8900/8443,由 pf 把 80/443 转过去
+ (`scripts/setup_macos_port80.sh`)。不钉 `--mdns-address`:这台机器会换网络,
+ 钉死等于换个 Wi-Fi 就打不开(见 `peach.mdns` 的地址复查)。
+
+ 证书缺失时**只是不起 HTTPS**,不像 Windows 那样直接报错——macOS 这份是开发环境,
+ 没有本机 CA 也应该能用。
"""
peach = str(_peach_executable())
- return (
+ specs = [
ServiceSpec(
"http",
f"http://127.0.0.1:{MACOS_PORT}/healthz",
(peach, "serve", "--host", "0.0.0.0", "--port", str(MACOS_PORT)),
True,
),
- )
+ ]
+ cert_dir = SECRETS_DIR / "tls"
+ ca, cert, key = (cert_dir / n for n in
+ ("peach-local-ca.crt", "peach.crt", "peach.key"))
+ if all(path.is_file() for path in (ca, cert, key)):
+ specs.append(ServiceSpec(
+ "https",
+ # 证书签的是 `peach.local`,SAN 里的 IP 是 Windows 那台的,所以只能按名字校验。
+ f"https://peach.local:{MACOS_TLS_PORT}/healthz",
+ (
+ peach, "serve", "--host", "0.0.0.0", "--port", str(MACOS_TLS_PORT),
+ "--no-mdns",
+ "--ssl-certfile", str(cert), "--ssl-keyfile", str(key),
+ ),
+ str(ca),
+ ))
+ return tuple(specs)
def build_service_specs(lan_address: str | None = None) -> tuple[ServiceSpec, ...]:
diff --git a/tests/test_tray.py b/tests/test_tray.py
index c64251c81..23b4eba2d 100644
--- a/tests/test_tray.py
+++ b/tests/test_tray.py
@@ -20,6 +20,7 @@ def load_script(name: str):
return module
+from peach.config import SECRETS_DIR
from peach.tray import (
AlreadyRunning, PeachTray, ServiceManager, ServiceSpec, SingleInstance,
apply_macos_template, build_service_specs, create_icon, enable_hidpi,
@@ -124,15 +125,29 @@ class MacMenuBarTests(unittest.TestCase):
self.assertTrue(opaque, "模板图不该整张透明")
self.assertTrue(all(p[:3] == (0, 0, 0) for p in opaque), "模板图的颜色必须全黑")
- def test_service_spec_avoids_privileged_ports_and_a_pinned_address(self):
- """80/443 在 macOS 上要 root;地址钉死等于换个 Wi-Fi 就打不开。"""
+ def test_service_specs_avoid_privileged_ports_and_a_pinned_address(self):
+ """80/443 在 macOS 上要 root,所以服务跑在高位端口、由 pf 转发过去;
+ 地址钉死等于换个 Wi-Fi 就打不开。"""
specs = build_service_specs()
- self.assertEqual(len(specs), 1)
- command = specs[0].command
- self.assertIn("--port", command)
- self.assertGreater(int(command[command.index("--port") + 1]), 1024)
- self.assertNotIn("--mdns-address", command)
- self.assertNotIn("--ssl-certfile", command)
+ self.assertIn(len(specs), (1, 2)) # 没有 TLS 材料时只有 http
+ self.assertEqual(specs[0].name, "http")
+ for spec in specs:
+ command = spec.command
+ self.assertIn("--port", command)
+ self.assertGreater(int(command[command.index("--port") + 1]), 1024)
+ self.assertNotIn("--mdns-address", command)
+
+ def test_https_spec_only_appears_with_real_tls_material(self):
+ """macOS 这份是开发环境:没有本机 CA 也应该能用,不像 Windows 那样直接报错。"""
+ specs = {spec.name: spec for spec in build_service_specs()}
+ material = (SECRETS_DIR / "tls" / "peach.crt").is_file()
+ self.assertEqual("https" in specs, material)
+ if material:
+ command = specs["https"].command
+ self.assertIn("--ssl-certfile", command)
+ # 两份服务只能有一份发布 mDNS,否则同名记录互相打架。
+ self.assertIn("--no-mdns", command)
+ self.assertTrue(str(specs["https"].verify).endswith("peach-local-ca.crt"))
class TemplateHookTests(unittest.TestCase):