Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix: detect when mDNS registers but nobody answers

Peach local maintenancef60c25b0fafc
diff --git a/.claude/skills/peach-cross-platform/SKILL.md b/.claude/skills/peach-cross-platform/SKILL.md
index 17c11cca7..29046085d 100644
--- a/.claude/skills/peach-cross-platform/SKILL.md
+++ b/.claude/skills/peach-cross-platform/SKILL.md
@@ -93,6 +93,16 @@ bundle 声明 `LSUIElement`,输出重定向到 `logs/macos-tray.log`。图标
开工前先 `sh scripts/sync_status.sh`:落后远端就先 `git pull --rebase`,别在旧代码上接着
写;账本是另一条链路(`peach.sync`),同一个脚本会一并报告。
+## macOS 的本地网络权限
+
+`peach serve` 从终端起 mDNS 正常,改由 launchd 起就再也没人应答——终端起的进程继承终端
+已授权的身份,launchd 作业是另一个主体且没有弹窗可点,多播被**静默**丢弃。zeroconf 自认
+注册成功,`/healthz` 照报 `peach.local`。发布器因此会自己查一次验证,验不过报
+`unreachable`。修法:系统设置 → 隐私与安全性 → 本地网络放行,或 `--no-mdns` 改用 IP。
+
+排查手法:同一条命令分别从 shell 和 launchd 起,各发一次 mDNS 查询对比——差异只在启动者
+时,先怀疑这个门。
+
## Git 陷阱
- **换行**由 `.gitattributes` 的 `* text=auto eol=lf` 固定,不要依赖各自的
diff --git a/README.md b/README.md
index fbb3d2471..1e1e53c54 100644
--- a/README.md
+++ b/README.md
@@ -34,6 +34,19 @@ Windows 口径,本机挂载点由 `src/peach/platform.py` 在读取时翻译
项目仓库不保存媒体、数据库、凭据、快照、封面或运行日志。
+### macOS 的「本地网络」权限会静默掐掉 mDNS
+
+`peach serve` 从终端起时 `peach.local` 一切正常,改由 LaunchAgent 起之后手机就再也解析不到
+——同一个二进制、同一台机器、同样的参数。原因是 macOS 的「本地网络」隐私门:终端起的进程
+继承终端已获授权的身份,launchd 起的作业是另一个主体,而且**没有弹窗可点**,多播被静默丢弃。
+zeroconf 自认为注册成功,`/healthz` 也照报 `peach.local`,实际一个包都发不出去。
+
+所以发布器现在会自己发一次 mDNS 查询验证,验不过就把状态报成 `unreachable` 并在日志里写清
+原因——「自称成功」比直接失败难查得多。
+
+修法二选一:系统设置 → 隐私与安全性 → 本地网络里允许该进程;或者 `--no-mdns` 关掉它,
+改用局域网 IP 访问(证书的 SAN 里已经带了本机地址)。
+
### 账本复制
三份副本:硬盘 `\<共享\>/database/ledger.db` 是权威副本,Windows 和 macOS 各持一份本地
diff --git a/src/peach/mdns.py b/src/peach/mdns.py
index f3edcfe2a..39bc3faa5 100644
--- a/src/peach/mdns.py
+++ b/src/peach/mdns.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import logging
import socket
+import time
import ipaddress
import threading
from collections.abc import Callable
@@ -63,6 +64,42 @@ def _explicit_resolver(address: str) -> Callable[[], str]:
return lambda: str(parsed)
+
+def answers_on_network(hostname: str, timeout: float = 2.0) -> bool:
+ """本机的 mDNS 记录在网上真的有人应答吗。
+
+ 注册成功不等于对外可见:macOS 的「本地网络」隐私门会**静默**掐掉多播。从终端起的
+ 进程继承终端已获授权的身份,launchd 起的作业是另一个主体、没有弹窗可点,于是
+ zeroconf 自认为注册成功,实际一个包都发不出去——`/healthz` 报着 `peach.local`,
+ 手机却怎么都解析不到。所以要自己发一次查询验一验。
+ """
+ query = b"\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00"
+ for label in hostname.split("."):
+ query += bytes([len(label)]) + label.encode()
+ query += b"\x00\x00\x01\x00\x01"
+ try:
+ probe = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ except OSError:
+ return False
+ probe.settimeout(timeout)
+ try:
+ probe.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 1)
+ probe.sendto(query, ("224.0.0.251", 5353))
+ deadline = time.monotonic() + timeout
+ while time.monotonic() < deadline:
+ try:
+ payload, _ = probe.recvfrom(2048)
+ except socket.timeout:
+ return False
+ if hostname.split(".")[0].encode() in payload:
+ return True
+ except OSError:
+ return False
+ finally:
+ probe.close()
+ return False
+
+
class MdnsPublisher:
"""Publish Peach exactly like the verified pre-migration web server.
@@ -127,7 +164,15 @@ class MdnsPublisher:
self._zeroconf = zeroconf
self._info = info
self.address = address
- self.status = self.hostname
+ # 注册成功不代表对外可见,验一次再宣告(见 answers_on_network)。
+ if answers_on_network(self.hostname):
+ self.status = self.hostname
+ else:
+ self.status = "unreachable"
+ LOGGER.error(
+ "%s 已注册但网上无人应答,多半是「本地网络」权限被静默拒绝:"
+ "系统设置 → 隐私与安全性 → 本地网络,允许该进程;"
+ "或用 --no-mdns 直接关掉并改用 IP 访问。", self.hostname)
def stop(self) -> None:
self._stop.set()
diff --git a/tests/test_mdns.py b/tests/test_mdns.py
index 417a53180..ed29b80ad 100644
--- a/tests/test_mdns.py
+++ b/tests/test_mdns.py
@@ -1,12 +1,13 @@
import unittest
from unittest.mock import MagicMock, patch
-from peach.mdns import MdnsPublisher, create_mdns_publisher, lan_ipv4
+from peach.mdns import MdnsPublisher, answers_on_network, create_mdns_publisher, lan_ipv4
class MdnsPublisherTests(unittest.TestCase):
def test_registers_and_unregisters_peach_local_on_all_interfaces(self):
- with patch("peach.mdns.Zeroconf") as zeroconf_type:
+ with patch("peach.mdns.Zeroconf") as zeroconf_type, \
+ patch("peach.mdns.answers_on_network", return_value=True):
publisher = MdnsPublisher(
"Peach.local", 80, address_resolver=lambda: "192.0.2.10"
)
@@ -37,7 +38,8 @@ class MdnsPublisherTests(unittest.TestCase):
current[0] = next(addresses, current[0])
return current[0]
- with patch("peach.mdns.Zeroconf") as zeroconf_type:
+ with patch("peach.mdns.Zeroconf") as zeroconf_type, \
+ patch("peach.mdns.answers_on_network", return_value=True):
publisher = MdnsPublisher(
"peach", 80, address_resolver=resolver, refresh_seconds=0,
)
@@ -59,7 +61,8 @@ class MdnsPublisherTests(unittest.TestCase):
except StopIteration:
raise RuntimeError("no publishable LAN IPv4 address") from None
- with patch("peach.mdns.Zeroconf"):
+ with patch("peach.mdns.Zeroconf"), \
+ patch("peach.mdns.answers_on_network", return_value=True):
publisher = MdnsPublisher(
"peach", 80, address_resolver=resolver, refresh_seconds=0,
)
@@ -71,7 +74,8 @@ class MdnsPublisherTests(unittest.TestCase):
def test_a_pinned_address_never_republishes(self):
"""生产上显式钉住地址时复查必须是空转,不能自己换成别的网卡。"""
- with patch("peach.mdns.Zeroconf") as zeroconf_type:
+ with patch("peach.mdns.Zeroconf") as zeroconf_type, \
+ patch("peach.mdns.answers_on_network", return_value=True):
publisher = create_mdns_publisher(
"peach", 80, address="192.0.2.10", refresh_seconds=0,
)
@@ -82,7 +86,8 @@ class MdnsPublisherTests(unittest.TestCase):
self.assertEqual(zeroconf_type.call_count, 1)
def test_secure_publication_uses_https_service(self):
- with patch("peach.mdns.Zeroconf") as zeroconf_type:
+ with patch("peach.mdns.Zeroconf") as zeroconf_type, \
+ patch("peach.mdns.answers_on_network", return_value=True):
publisher = MdnsPublisher(
"peach", 443, secure=True, address_resolver=lambda: "192.0.2.10"
)
@@ -100,7 +105,8 @@ class MdnsPublisherTests(unittest.TestCase):
self.assertIsInstance(create_mdns_publisher("peach", 80), MdnsPublisher)
def test_explicit_address_avoids_tunnel_route(self):
- with patch("peach.mdns.Zeroconf") as zeroconf_type:
+ with patch("peach.mdns.Zeroconf") as zeroconf_type, \
+ patch("peach.mdns.answers_on_network", return_value=True):
publisher = create_mdns_publisher(
"peach", 80, address="192.168.50.162",
)
@@ -121,5 +127,37 @@ class MdnsPublisherTests(unittest.TestCase):
self.assertEqual(lan_ipv4(), "192.168.50.162")
+class ReachabilityTests(unittest.TestCase):
+ """注册成功不等于对外可见。
+
+ macOS 的「本地网络」隐私门会静默掐掉多播:从终端起的进程继承终端已授权的身份,
+ launchd 起的作业是另一个主体、没有弹窗可点,于是 zeroconf 自认为注册成功、实际
+ 一个包都发不出去。`/healthz` 报着 `peach.local`,手机却怎么都解析不到——这种
+ 「自称成功」比直接失败难查得多,所以要自己发一次查询验一验。
+ """
+
+ def test_status_says_unreachable_when_nothing_answers(self):
+ with patch("peach.mdns.Zeroconf"), \
+ patch("peach.mdns.answers_on_network", return_value=False):
+ publisher = MdnsPublisher(
+ "peach", 80, address_resolver=lambda: "192.0.2.10", refresh_seconds=0)
+ publisher.start()
+ self.assertEqual(publisher.status, "unreachable")
+ publisher.stop()
+
+ def test_status_is_the_hostname_when_the_record_answers(self):
+ with patch("peach.mdns.Zeroconf"), \
+ patch("peach.mdns.answers_on_network", return_value=True):
+ publisher = MdnsPublisher(
+ "peach", 80, address_resolver=lambda: "192.0.2.10", refresh_seconds=0)
+ publisher.start()
+ self.assertEqual(publisher.status, "peach.local")
+ publisher.stop()
+
+ def test_probe_never_raises_on_a_dead_socket(self):
+ with patch("peach.mdns.socket.socket", side_effect=OSError("no multicast")):
+ self.assertFalse(answers_on_network("peach.local", timeout=0.1))
+
+
if __name__ == "__main__":
unittest.main()