Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix: publish Windows DNS-SD on actual LAN host

Peach local maintenance1e7e9b690302
diff --git a/docs/HANDOFF.md b/docs/HANDOFF.md
index 34793cb04..5b033de57 100644
--- a/docs/HANDOFF.md
+++ b/docs/HANDOFF.md
@@ -21,7 +21,7 @@ This is durable operating knowledge, not a per-session transcript.
## Operations
- Main command: `peach serve|migrate` after editable installation.
-- `peach serve` 默认发布 `peach.local`;使用 `--no-mdns` 可关闭。Windows 走系统 DNS-SD (`DnsServiceRegister`),其他平台走 zeroconf;mDNS 注册在 FastAPI lifespan 中执行,不能在事件循环内同步阻塞。health 的 `mdns_backend` 必须与运行态一致,不能只凭 `mdns` 字符串判断可用。
+- `peach serve` 默认发布 DNS-SD;使用 `--no-mdns` 可关闭。Windows 走系统 `DnsServiceRegister`,`Peach` 是服务实例,health 的 `mdns` 是实际可访问主机(当前 `LMD-DST.local`),不是品牌别名;其他平台走 zeroconf。注册在 FastAPI lifespan 中执行,不能在事件循环内同步阻塞。不要再把 Windows 的服务注册成功写成 `peach.local` A 记录成功。
- TLS 仅在同时提供 `--ssl-certfile` 和 `--ssl-keyfile` 时启用;`.local` 使用本地 CA,不使用 Let's Encrypt,证书和私钥留在 `R:\peach-data\secrets`。
- FastAPI is the only Web server. `web_contract.py` contains the stable JSON surface; do not recreate a parallel `http.server` or dynamic legacy loader.
- FFmpeg/ffprobe resolve from explicit environment overrides, then `R:\peach-data\tools\ffmpeg\bin`, then `PATH`. No active code may fall back to the Stash private directory.
diff --git a/docs/STATUS.md b/docs/STATUS.md
index 066fbaeca..5d28b4f98 100644
--- a/docs/STATUS.md
+++ b/docs/STATUS.md
@@ -4,8 +4,8 @@ Last verified: 2026-08-14
## Runtime
-- Production Peach: FastAPI `0.2.0` from `R:\peach-app` on `0.0.0.0:80`, server PID observed as 49196; health mode is `fastapi`.
-- Windows mDNS now uses the native DNS-SD API and restarted health reports `mdns_backend=windows-dns-sd`, address `192.168.50.162`. A second LAN device still needs to confirm `http://peach.local/`; no firewall or router rule was changed.
+- Production Peach: FastAPI `0.2.0` from `R:\peach-app` on `0.0.0.0:80`, server PID observed as 27900; health mode is `fastapi`.
+- Windows mDNS now correctly publishes `Peach` as the DNS-SD service and `LMD-DST.local` as its actual SRV host on interface 9/address `192.168.50.162`. The earlier `peach.local` claim was invalid because `DnsServiceRegister` does not create arbitrary host aliases. A second LAN device still needs to confirm `http://LMD-DST.local/`; no firewall or router rule was changed.
- Stash: `127.0.0.1:9999`, PID observed as 35332.
- Traffic monitor: running through `RM-TrafficWatch` from `R:\peach-app\.venv`; all probe/sheets tasks use the same project environment.
- Runtime Python: 3.14.7. The removed system Python 3.12 invalidated the old venv, which was rebuilt and reverified before production start.
diff --git a/docs/adr/0004-network-and-profile-boundary.md b/docs/adr/0004-network-and-profile-boundary.md
index 05b5b78a7..cbe20e395 100644
--- a/docs/adr/0004-network-and-profile-boundary.md
+++ b/docs/adr/0004-network-and-profile-boundary.md
@@ -9,7 +9,7 @@
- online 资产、追更和外部数据源是正式能力,不再受“绝不出网”旧约束;每个 connector 必须显式授权、控频、标注来源与保留策略。
- schema 预留 `app_user`/`profile`,当前只创建本地默认身份,不开放注册、密码找回、公共会话或复杂 RBAC。
- 远程访问以后可选择 VPN/Tunnel,或 DDNS + 反向代理 + HTTPS;应用本身不直接裸露公网端口。
-- 局域网服务由 FastAPI 生命周期发布 `peach.local` mDNS;可用 `--no-mdns` 关闭,不再依赖旧 HTTP server。Windows 使用系统 `DnsServiceRegister` DNS-SD 后端,避免 Python zeroconf 与 CrossPaste/PikPak/Steam 等多个 UDP 5353 使用者竞争;其他平台保留 zeroconf 后端。
+- 局域网服务由 FastAPI 生命周期发布 DNS-SD;可用 `--no-mdns` 关闭,不再依赖旧 HTTP server。Windows 使用系统 `DnsServiceRegister`,以 `Peach` 为服务实例、实际计算机名(当前 `LMD-DST.local`)为 SRV 主机,并绑定首选 LAN 接口;该 API 不创建任意 `peach.local` A 记录。若必须使用品牌化主机别名,应由路由器本地 DNS/反代 DNS 或经验证的专用 responder 提供。其他平台保留 zeroconf,可直接发布自定义 `.local` 主机名。
- Uvicorn 支持显式证书/私钥的本机 TLS。`.local` 不能申请公开受信的 Let's Encrypt 证书;局域网需要 TLS 时使用受各客户端信任的本地 CA(如 mkcert),证书材料只放 `R:\peach-data\secrets`。
- 公网启用前必须完成强口令/身份、TLS、反代可信头、CSRF/速率限制、secret storage、审计与备份恢复验收。
diff --git a/src/peach/api.py b/src/peach/api.py
index 1b1dc4188..1f6f9cad4 100644
--- a/src/peach/api.py
+++ b/src/peach/api.py
@@ -103,6 +103,7 @@ def create_app(settings: PeachSettings | None = None) -> FastAPI:
"ffmpeg": ffmpeg.source if ffmpeg else "unavailable",
"mdns": mdns.status if mdns is not None else "disabled",
"mdns_backend": mdns.backend if mdns is not None else None,
+ "mdns_service": mdns.name if mdns is not None else None,
"mdns_address": mdns.address if mdns is not None else None,
"scheme": "https" if settings.tls_enabled else "http"}
diff --git a/src/peach/mdns.py b/src/peach/mdns.py
index 0596221b2..68dbf790a 100644
--- a/src/peach/mdns.py
+++ b/src/peach/mdns.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import ctypes
import os
import socket
+import struct
import threading
from collections.abc import Callable
from ctypes import wintypes
@@ -32,6 +33,19 @@ def lan_ipv4() -> str:
sock.close()
+def lan_interface_index() -> int:
+ """Return the interface Windows would use for the same LAN route as ``lan_ipv4``."""
+ iphlpapi = ctypes.WinDLL("iphlpapi", use_last_error=True)
+ iphlpapi.GetBestInterface.argtypes = [wintypes.ULONG, ctypes.POINTER(wintypes.ULONG)]
+ iphlpapi.GetBestInterface.restype = wintypes.DWORD
+ destination = struct.unpack("<I", socket.inet_aton("192.0.2.1"))[0]
+ index = wintypes.ULONG()
+ result = int(iphlpapi.GetBestInterface(destination, ctypes.byref(index)))
+ if result != 0:
+ raise OSError(result, "GetBestInterface failed")
+ return int(index.value)
+
+
class MdnsPublisher:
backend = "zeroconf"
@@ -133,7 +147,12 @@ def _load_dnsapi():
class WindowsMdnsPublisher:
- """Advertise through Windows DNS-SD instead of competing for UDP 5353."""
+ """Advertise through Windows DNS-SD instead of competing for UDP 5353.
+
+ Windows DNS-SD registers a service instance; it does not create an arbitrary host
+ alias. The SRV target therefore uses the actual Windows host name, while ``name``
+ remains the user-facing service instance (``Peach``).
+ """
backend = "windows-dns-sd"
@@ -144,12 +163,17 @@ class WindowsMdnsPublisher:
secure: bool = False,
address_resolver: Callable[[], str] = lan_ipv4,
dnsapi_factory: Callable[[], object] = _load_dnsapi,
+ interface_resolver: Callable[[], int] = lan_interface_index,
+ host_resolver: Callable[[], str] = socket.gethostname,
) -> None:
self.name = _normalized_name(name)
self.port = port
self.secure = secure
self._address_resolver = address_resolver
self._dnsapi_factory = dnsapi_factory
+ self._interface_resolver = interface_resolver
+ self._host_resolver = host_resolver
+ self.host_name = self._host_resolver().strip().removesuffix(".")
self._dnsapi = None
self._instance: int | None = None
self._request: _DnsServiceRegisterRequest | None = None
@@ -161,20 +185,21 @@ class WindowsMdnsPublisher:
@property
def hostname(self) -> str:
- return f"{self.name}.local"
+ return f"{self.host_name}.local"
def start(self) -> None:
if self._request is not None:
return
address = self._address_resolver()
dnsapi = self._dnsapi_factory()
+ interface_index = self._interface_resolver()
service = "_https._tcp.local" if self.secure else "_http._tcp.local"
instance_name = f"{self.name}.{service}"
ip4 = wintypes.ULONG.from_buffer_copy(socket.inet_aton(address))
keys = (wintypes.LPCWSTR * 2)("path", "scheme")
values = (wintypes.LPCWSTR * 2)("/", "https" if self.secure else "http")
instance = dnsapi.DnsServiceConstructInstance(
- instance_name, self.hostname, ctypes.byref(ip4), None, self.port,
+ instance_name, self.host_name, ctypes.byref(ip4), None, self.port,
0, 0, 2, keys, values,
)
if not instance:
@@ -191,7 +216,7 @@ class WindowsMdnsPublisher:
self._event.set()
request = _DnsServiceRegisterRequest(
- 1, 0, instance, completed, None, None, False,
+ 1, interface_index, instance, completed, None, None, False,
)
result = int(dnsapi.DnsServiceRegister(ctypes.byref(request), None))
if result != DNS_REQUEST_PENDING:
diff --git a/tests/test_mdns.py b/tests/test_mdns.py
index 6240d5986..e75e8e661 100644
--- a/tests/test_mdns.py
+++ b/tests/test_mdns.py
@@ -63,10 +63,13 @@ class MdnsPublisherTests(unittest.TestCase):
publisher = WindowsMdnsPublisher(
"peach", 80, address_resolver=lambda: "192.0.2.10",
dnsapi_factory=lambda: dnsapi,
+ interface_resolver=lambda: 9, host_resolver=lambda: "LMD-DST",
)
publisher.start()
- self.assertEqual(publisher.status, "peach.local")
+ self.assertEqual(publisher.status, "LMD-DST.local")
self.assertEqual(publisher.backend, "windows-dns-sd")
+ self.assertEqual(publisher._request.InterfaceIndex, 9)
+ self.assertEqual(publisher.hostname, "LMD-DST.local")
publisher.stop()
self.assertEqual(publisher.status, "stopped")