feat(agent): découverte mDNS/HomeKit — probe multicast _hap._tcp (Fixes #62)

- HomeKitMdnsProbe : paquet DNS PTR brut sur 224.0.0.251:5353, écoute 2 s
  avec timeout explicite Task.WhenAny (jamais de hang, cf. 491a253)
- Parseur DNS complet : PTR/SRV/TXT/A avec pointeurs de compression, TXT
  (ci/id/md/sf), dédup par IP, mapping IP via SRV target
- Filtre catégories 17 (camera) et 18 (doorbell) — Strix pkg/probe/mdns.go
- Intégré au scan broadcast de NetworkCameraScanner ; devices HomeKit avec
  HomeKitName/HomeKitDeviceId/HomeKitModel remplis → classés HomeKit côté central
- Les devices HomeKit ne génèrent plus de fausses URLs RTSP (flux HAP)
- Tests : 4 (parseur PTR/SRV/TXT/A, filtrage catégories, dédup, no-hang)
This commit is contained in:
Arnaud Dartois committed 2026-08-11 22:16:19 +02:00
1 parent f713abc7ca
commit e5b035745d
4 files changed
+714 -1

No files matched your search

@@ -56,12 +56,17 @@ public sealed class NetworkCameraScanner
private static readonly XNamespace WsseNs = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
private readonly ILogger<NetworkCameraScanner> _logger;
private readonly HomeKitMdnsProbe _homeKitProbe;
private readonly string? _localIp;
private readonly string? _gatewayIp;
public NetworkCameraScanner(ILogger<NetworkCameraScanner> logger)
public NetworkCameraScanner(
ILogger<NetworkCameraScanner> logger,
HomeKitMdnsProbe? homeKitProbe = null)
{
_logger = logger;
_homeKitProbe = homeKitProbe ?? new HomeKitMdnsProbe(
Microsoft.Extensions.Logging.Abstractions.NullLogger<HomeKitMdnsProbe>.Instance);
var (localIp, gatewayIp) = GetLocalNetworkInfo();
_localIp = localIp;
_gatewayIp = gatewayIp;
@@ -162,6 +167,12 @@ public sealed class NetworkCameraScanner
if (!HasDefinitiveCameraEvidence(device) && !HasCameraPorts(device))
return null;
// HomeKit accessories are proven by the mDNS advertisement (category
// ci=17/18): no RTSP candidates apply — the stream is a HAP session
// (GetImage snapshot test, ticket #67/#65), never a guessed rtsp:// URL.
if (!string.IsNullOrEmpty(device.HomeKitName))
return device;
// Real ONVIF GetStreamUri URLs (agent enrichment) take priority over
// guessed templates: keep them even when the stream test fails — the
// camera is already proven by ONVIF, the stream may just need auth.
@@ -201,6 +212,50 @@ public sealed class NetworkCameraScanner
var devices = new List<AgentDiscoveredDevice>();
devices.AddRange(await DiscoverOnvifMulticastAsync(credentialPool, ct).ConfigureAwait(false));
devices.AddRange(await DiscoverDvripAsync(ct).ConfigureAwait(false));
devices.AddRange(await DiscoverHomeKitAsync(ct).ConfigureAwait(false));
return devices;
}
/// <summary>
/// HomeKit accessory discovery via mDNS multicast (_hap._tcp.local,
/// 224.0.0.251:5353). Only camera (ci=17) and doorbell (ci=18) categories
/// are kept; the HomeKit* fields of the device are filled for the Central
/// ClassifyAgentDevice HomeKit path.
/// </summary>
private async Task<List<AgentDiscoveredDevice>> DiscoverHomeKitAsync(CancellationToken ct)
{
var devices = new List<AgentDiscoveredDevice>();
try
{
var found = await _homeKitProbe.DiscoverAsync(ct).ConfigureAwait(false);
foreach (var accessory in found)
{
devices.Add(new AgentDiscoveredDevice(
Ip: accessory.Ip,
IsReachable: true,
OpenPorts: [accessory.Port],
MacAddress: null,
Vendor: "Apple",
Hostname: accessory.Name,
HttpServerHeader: null,
HttpStatusCode: null,
HttpPort: null,
OnvifDeviceServiceUrl: null,
OnvifName: null,
OnvifHardware: null,
OnvifPort: null,
HomeKitName: accessory.Name,
HomeKitDeviceId: accessory.DeviceId,
HomeKitModel: accessory.Model,
CredentialLabel: null,
StreamUrls: null));
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "HomeKit mDNS discovery failed");
}
return devices;
}