From ea773b17a15643054cd4f4049f8887103f44abdd Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Tue, 11 Aug 2026 21:13:32 +0200 Subject: [PATCH] feat(agent): Dahua/Imou DVRIP broadcast discovery Fixes #55 Sends the DVRIP probe (ff00000000000000000000000000fa0500000000) 3x over UDP broadcast on port 34569 and parses the NetWork.NetCommon JSON replies (HostIP, HostName, MAC, HttpPort, Ret). Finds Dahua/Imou cameras even when RTSP/ONVIF are disabled, with MAC/vendor from the reply itself. - Filter Ret != 100 error replies, require HostIP+HostName - Devices are created with OpenPorts [37777] (DVRIP) so they pass the camera-evidence filter and get classified Standard by the Central - Transposed from go2rtc internal/dvrip --- .../Services/NetworkCameraScanner.cs | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/Heimdall.Agent/Services/NetworkCameraScanner.cs b/src/Heimdall.Agent/Services/NetworkCameraScanner.cs index 6d1e4ab..2fa5064 100644 --- a/src/Heimdall.Agent/Services/NetworkCameraScanner.cs +++ b/src/Heimdall.Agent/Services/NetworkCameraScanner.cs @@ -5,6 +5,7 @@ using System.Net.NetworkInformation; using System.Net.Sockets; using System.Security.Cryptography; using System.Text; +using System.Text.Json; using System.Xml.Linq; using Heimdall.Application.Discovery; using Heimdall.Application.DTOs; @@ -182,9 +183,122 @@ public sealed class NetworkCameraScanner { var devices = new List(); devices.AddRange(await DiscoverOnvifMulticastAsync(ct).ConfigureAwait(false)); + devices.AddRange(await DiscoverDvripAsync(ct).ConfigureAwait(false)); return devices; } + /// + /// Dahua/Imou DVRIP discovery broadcast (UDP port 34569): sends the DVRIP + /// probe 3 times (devices sometimes miss it) and parses the JSON + /// NetWork.NetCommon replies (HostIP, HostName, MAC, SN, HttpPort…). + /// Finds Dahua/Imou cameras even with RTSP/ONVIF disabled. + /// Transposed from go2rtc internal/dvrip. + /// + private async Task> DiscoverDvripAsync(CancellationToken ct) + { + var devices = new List(); + try + { + using var udp = new UdpClient(); + udp.Client.Bind(new IPEndPoint(IPAddress.Any, 34569)); + udp.Client.ReceiveTimeout = 2000; + + var probe = Convert.FromHexString("ff00000000000000000000000000fa0500000000"); + var broadcast = new IPEndPoint(IPAddress.Broadcast, 34569); + for (var i = 0; i < 3; i++) + { + await udp.SendAsync(probe, broadcast, ct).ConfigureAwait(false); + await Task.Delay(100, ct).ConfigureAwait(false); + } + + while (!ct.IsCancellationRequested) + { + UdpReceiveResult result; + try + { + result = await udp.ReceiveAsync(ct).ConfigureAwait(false); + } + catch (SocketException) + { + break; // receive timeout + } + catch (OperationCanceledException) + { + break; + } + + var json = Encoding.UTF8.GetString(result.Buffer); + var info = ParseDvripNetCommon(json); + if (info is null || info.Ret is >= 0 and not 100) + continue; // error reply or non-DVRIP packet + + devices.Add(new AgentDiscoveredDevice( + Ip: info.HostIp, + IsReachable: true, + OpenPorts: [37777], + MacAddress: info.Mac, + Vendor: "Dahua", + Hostname: info.HostName, + HttpServerHeader: null, + HttpStatusCode: null, + HttpPort: info.HttpPort > 0 ? info.HttpPort : null, + OnvifDeviceServiceUrl: null, + OnvifName: null, + OnvifHardware: null, + OnvifPort: null, + HomeKitName: null, + HomeKitDeviceId: null, + HomeKitModel: null, + CredentialLabel: null, + StreamUrls: null)); + } + + if (devices.Count > 0) + _logger.LogInformation("Camera discovery: {Count} DVRIP device(s) via broadcast", devices.Count); + } + catch (OperationCanceledException) when (ct.IsCancellationRequested) + { + // cancelled + } + catch (Exception ex) + { + _logger.LogWarning(ex, "DVRIP discovery failed"); + } + + return devices; + } + + private sealed record DvripNetCommon(string HostIp, string HostName, string? Mac, int HttpPort, int Ret); + + private static DvripNetCommon? ParseDvripNetCommon(string json) + { + try + { + using var doc = JsonDocument.Parse(json); + if (!doc.RootElement.TryGetProperty("NetWork.NetCommon", out var nc)) + return null; + + var hostIp = GetJsonString(nc, "HostIP"); + var hostName = GetJsonString(nc, "HostName"); + if (string.IsNullOrEmpty(hostIp) || string.IsNullOrEmpty(hostName)) + return null; + + var ret = doc.RootElement.TryGetProperty("Ret", out var retEl) && retEl.TryGetInt32(out var r) ? r : -1; + return new DvripNetCommon( + hostIp, hostName, GetJsonString(nc, "MAC"), GetJsonInt(nc, "HttpPort"), ret); + } + catch + { + return null; + } + } + + private static string? GetJsonString(JsonElement el, string name) + => el.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.String ? v.GetString() : null; + + private static int GetJsonInt(JsonElement el, string name) + => el.TryGetProperty(name, out var v) && v.TryGetInt32(out var i) ? i : 0; + /// /// ONVIF WS-Discovery multicast probe (Feature Discovery spec §5.3): /// send one UDP probe to 239.255.255.250:3702, collect replies for ~5 s.