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
This commit is contained in:
1 parent
1bc4c73f23
commit
ea773b17a1
1 file changed
+114
@@ -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<AgentDiscoveredDevice>();
|
||||
devices.AddRange(await DiscoverOnvifMulticastAsync(ct).ConfigureAwait(false));
|
||||
devices.AddRange(await DiscoverDvripAsync(ct).ConfigureAwait(false));
|
||||
return devices;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
private async Task<List<AgentDiscoveredDevice>> DiscoverDvripAsync(CancellationToken ct)
|
||||
{
|
||||
var devices = new List<AgentDiscoveredDevice>();
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
|
||||
Reference in new issue
Block a user