-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscoveryService.cs
More file actions
213 lines (184 loc) · 7.03 KB
/
Copy pathDiscoveryService.cs
File metadata and controls
213 lines (184 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using ThingConnect.Pulse.Server.Data;
namespace ThingConnect.Pulse.Server.Services.Monitoring;
/// <summary>
/// Implementation of discovery service for expanding network targets.
/// </summary>
public sealed class DiscoveryService : IDiscoveryService
{
private readonly ILogger<DiscoveryService> _logger;
private static readonly Regex CidrRegex = new(@"^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/(\d{1,2})$",
RegexOptions.Compiled);
private static readonly Regex WildcardRegex = new(@"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.\*$",
RegexOptions.Compiled);
public DiscoveryService(ILogger<DiscoveryService> logger)
{
_logger = logger;
}
public IEnumerable<string> ExpandCidr(string cidr)
{
Match match = CidrRegex.Match(cidr);
if (!match.Success)
{
_logger.LogWarning("Invalid CIDR format: {Cidr}", cidr);
yield break;
}
string baseIp = match.Groups[1].Value;
int prefixLength = int.Parse(match.Groups[2].Value);
if (prefixLength < 0 || prefixLength > 32)
{
_logger.LogWarning("Invalid CIDR prefix length: {PrefixLength}", prefixLength);
yield break;
}
if (!IPAddress.TryParse(baseIp, out IPAddress? ipAddress))
{
_logger.LogWarning("Invalid IP address in CIDR: {BaseIp}", baseIp);
yield break;
}
byte[] addressBytes = ipAddress.GetAddressBytes();
uint addressInt = BitConverter.ToUInt32(addressBytes, 0);
int hostBits = 32 - prefixLength;
uint hostCount = (uint)(1 << hostBits);
uint networkAddress = addressInt & (0xFFFFFFFF << hostBits);
// Skip network and broadcast addresses for practical use
uint startAddress = networkAddress + 1;
uint endAddress = networkAddress + hostCount - 1;
for (uint address = startAddress; address < endAddress && address > networkAddress; address++)
{
byte[] bytes = BitConverter.GetBytes(address);
System.Array.Reverse(bytes);
var ip = new IPAddress(bytes);
yield return ip.ToString();
}
}
public IEnumerable<string> ExpandWildcard(string wildcard, int startRange = 1, int endRange = 254)
{
Match match = WildcardRegex.Match(wildcard);
if (!match.Success)
{
_logger.LogWarning("Invalid wildcard format: {Wildcard}", wildcard);
yield break;
}
int octet1 = int.Parse(match.Groups[1].Value);
int octet2 = int.Parse(match.Groups[2].Value);
int octet3 = int.Parse(match.Groups[3].Value);
if (octet1 > 255 || octet2 > 255 || octet3 > 255)
{
_logger.LogWarning("Invalid IP octets in wildcard: {Wildcard}", wildcard);
yield break;
}
for (int octet4 = startRange; octet4 <= endRange; octet4++)
{
yield return $"{octet1}.{octet2}.{octet3}.{octet4}";
}
}
public async Task<IEnumerable<string>> ResolveHostnameAsync(string hostname, CancellationToken cancellationToken = default)
{
try
{
IPAddress[] addresses = await Dns.GetHostAddressesAsync(hostname);
return addresses
.Where(addr => addr.AddressFamily == AddressFamily.InterNetwork) // IPv4 only for now
.Select(addr => addr.ToString())
.ToList();
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to resolve hostname: {Hostname}", hostname);
return Enumerable.Empty<string>();
}
}
public async Task<IEnumerable<Data.Endpoint>> ExpandTargetsAsync(IEnumerable<dynamic> configTargets,
CancellationToken cancellationToken = default)
{
var endpoints = new List<Data.Endpoint>();
foreach (dynamic target in configTargets)
{
try
{
dynamic expandedEndpoints = await ExpandSingleTargetAsync(target, cancellationToken);
endpoints.AddRange(expandedEndpoints);
}
catch (Exception ex)
{
dynamic targetStr = target?.ToString() ?? "null";
Microsoft.Extensions.Logging.LoggerExtensions.LogError(_logger, ex, "Failed to expand target: " + targetStr);
}
}
_logger.LogInformation("Expanded {TargetCount} targets into {EndpointCount} endpoints",
configTargets.Count(), endpoints.Count);
return endpoints;
}
private async Task<IEnumerable<Data.Endpoint>> ExpandSingleTargetAsync(dynamic target,
CancellationToken cancellationToken)
{
var endpoints = new List<Data.Endpoint>();
var hosts = new List<string>();
// Extract host specification
if (target.host != null)
{
string host = (string)target.host;
// Check if it's an IP address or hostname
if (IPAddress.TryParse(host, out _))
{
hosts.Add(host);
}
else
{
IEnumerable<string> resolvedHosts = await ResolveHostnameAsync(host, cancellationToken);
hosts.AddRange(resolvedHosts);
}
}
else if (target.cidr != null)
{
string cidr = (string)target.cidr;
hosts.AddRange(ExpandCidr(cidr));
}
else if (target.wildcard != null)
{
string wildcard = (string)target.wildcard;
hosts.AddRange(ExpandWildcard(wildcard));
}
// Create endpoint for each expanded host
foreach (string host in hosts)
{
dynamic endpoint = CreateEndpointFromTarget(target, host);
endpoints.Add(endpoint);
}
return endpoints;
}
private static Data.Endpoint CreateEndpointFromTarget(dynamic target, string host)
{
// Parse probe type
ProbeType probeType = ProbeType.icmp; // default
if (target.type != null)
{
Enum.TryParse<ProbeType>((string)target.type, ignoreCase: true, out probeType);
}
else if (target.probe_type != null)
{
Enum.TryParse<ProbeType>((string)target.probe_type, ignoreCase: true, out probeType);
}
var endpoint = new Data.Endpoint
{
Id = Guid.NewGuid(),
Name = target.name ?? $"{probeType.ToString().ToUpper()} {host}",
GroupId = target.group ?? "default",
Type = probeType,
Host = host,
Port = target.port,
HttpPath = target.path,
HttpMatch = target.expect_text,
IntervalSeconds = target.interval_seconds ?? 30,
TimeoutMs = target.timeout_ms ?? 2000,
Retries = target.retries ?? 2,
ExpectedRttMs = target.expected_rtt_ms,
Enabled = target.enabled ?? true,
Notes = target.notes
};
return endpoint;
}
}