Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Ping Monitore - Add multiple hosts #1933

Merged
merged 8 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions Source/NETworkManager.Localization/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions Source/NETworkManager.Localization/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ First make a backup copy of your profile files before enabling encryption!</valu
<value>Hostname or IP address</value>
</data>
<data name="Hosts" xml:space="preserve">
<value>Hosts</value>
<value>Host(s)</value>
</data>
<data name="ID" xml:space="preserve">
<value>ID</value>
Expand Down Expand Up @@ -1759,9 +1759,6 @@ Profile files are not affected!</value>
<data name="Yellow" xml:space="preserve">
<value>Yellow</value>
</data>
<data name="EnterValidValueBetween25and100" xml:space="preserve">
<value>Enter a valid value between 25 and 100!</value>
</data>
<data name="ProfileWithThisNameAlreadyExists" xml:space="preserve">
<value>A profile with this name already exists!</value>
</data>
Expand Down Expand Up @@ -3314,8 +3311,8 @@ If the option is disabled again, the values are no longer modified. However, the
<data name="SNTPServerWithThisNameAlreadyExists" xml:space="preserve">
<value>An SNTP server with this name already exists!</value>
</data>
<data name="EnterValidDomainOrIPAddress" xml:space="preserve">
<value>Enter a valid domain (like "example.com") or a valid IP address (like 192.168.178.1)!</value>
<data name="EnterValidHostnameOrIPAddress" xml:space="preserve">
<value>Enter a valid hostname (like "server-01" or "example.com") or a valid IP address (like 192.168.178.1)!</value>
</data>
<data name="Servers" xml:space="preserve">
<value>Server(s)</value>
Expand Down
8 changes: 4 additions & 4 deletions Source/NETworkManager.Utilities/DNS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task<DNSResultIPAddress> ResolveAAsync(string query)
if (result.HasError)
return new DNSResultIPAddress(result.HasError, $"{result.NameServer}: {result.ErrorMessage}");

return new DNSResultIPAddress(result.Answers.ARecords().FirstOrDefault().Address);
return new DNSResultIPAddress(result.Answers.ARecords().FirstOrDefault()?.Address);
}
catch (DnsResponseException ex)
{
Expand All @@ -98,7 +98,7 @@ public async Task<DNSResultIPAddress> ResolveAaaaAsync(string query)
if (result.HasError)
return new DNSResultIPAddress(result.HasError, $"{result.NameServer}: {result.ErrorMessage}");

return new DNSResultIPAddress(result.Answers.AaaaRecords().FirstOrDefault().Address);
return new DNSResultIPAddress(result.Answers.AaaaRecords().FirstOrDefault()?.Address);
}
catch (DnsResponseException ex)
{
Expand All @@ -120,7 +120,7 @@ public async Task<DNSResultString> ResolveCnameAsync(string query)
if (result.HasError)
return new DNSResultString(result.HasError, $"{result.NameServer}: {result.ErrorMessage}");

return new DNSResultString(result.Answers.CnameRecords().FirstOrDefault().CanonicalName);
return new DNSResultString(result.Answers.CnameRecords().FirstOrDefault()?.CanonicalName);
}
catch (DnsResponseException ex)
{
Expand All @@ -142,7 +142,7 @@ public async Task<DNSResultString> ResolvePtrAsync(IPAddress ipAddress)
if (result.HasError)
return new DNSResultString(result.HasError, $"{result.NameServer}: {result.ErrorMessage}");

return new DNSResultString(result.Answers.PtrRecords().FirstOrDefault().PtrDomainName);
return new DNSResultString(result.Answers.PtrRecords().FirstOrDefault()?.PtrDomainName);
}
catch (DnsResponseException ex)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/NETworkManager.Utilities/DNSResultString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
///
/// </summary>
public class DNSResultString : DNSResult
{
{
public string Value { get; set; }

public DNSResultString(bool hasError, string errorMessage) : base(hasError, errorMessage)
Expand Down
7 changes: 2 additions & 5 deletions Source/NETworkManager.Utilities/RegexHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ public static class RegexHelper
// Match a subnet from 192.168.178.0/192.0.0.0 to 192.168.178.0/255.255.255.255
public const string IPv4AddressSubnetmaskRegex = @"^" + IPv4AddressValues + @"\/" + SubnetmaskValues + @"$";

// Match IPv6 address
// Match IPv6 address like ::1
public const string IPv6AddressRegex = @"(?:^|(?<=\s))(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(?=\s|$)";

// Match IPv6 address within a string
public const string IPv6AddressExctractRegex = IPv6AddressRegex;

// Match a subnet like 2001:0db8::/64
public const string IPv6AddressCidrRegex = @"^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8])){1}?$";

Expand All @@ -57,7 +54,7 @@ public static class RegexHelper
// Private hostname values
private const string HostnameValues = @"(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])";

// Hostname regex
// Hostname regex like server-01 or server-01.example.com
public const string HostnameRegex = @"^" + HostnameValues + @"$";

// Match a hostname with cidr like server-01.example.com/24
Expand Down
19 changes: 0 additions & 19 deletions Source/NETworkManager.Validators/DomainOrIPAddressValidator.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using NETworkManager.Localization.Resources;
using NETworkManager.Utilities;

namespace NETworkManager.Validators
{
public class IPAddressOrHostnameAsRangeValidator : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var input = (value as string)?.Trim();

if (string.IsNullOrEmpty(input))
return new ValidationResult(false, Strings.EnterValidHostnameOrIPAddress);

foreach (var item in input.Split(";"))
{
var localItem = item.Trim();

// Check if it is a valid IPv4 address like 192.168.0.1, a valid IPv6 address like ::1 or a valid hostname like server-01 or server-01.example.com
var isValid = Regex.IsMatch(localItem, RegexHelper.IPv4AddressRegex) ||
Regex.IsMatch(localItem, RegexHelper.IPv6AddressRegex) ||
Regex.IsMatch(localItem, RegexHelper.HostnameRegex);

if (!isValid)
return new ValidationResult(false, Strings.EnterValidHostnameOrIPAddress);
}

return ValidationResult.ValidResult;
}
}
}
33 changes: 33 additions & 0 deletions Source/NETworkManager.Validators/IPAddressOrHostnameValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using NETworkManager.Localization.Resources;
using NETworkManager.Utilities;

namespace NETworkManager.Validators
{
public class IPAddressOrHostnameValidator : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var input = (value as string)?.Trim();

if(string.IsNullOrEmpty(input))
return new ValidationResult(false, Strings.EnterValidHostnameOrIPAddress);

// Check if it is a valid IPv4 address like 192.168.0.1
if (Regex.IsMatch(input, RegexHelper.IPv4AddressRegex))
return ValidationResult.ValidResult;

// Check if it is a valid IPv6 address like ::1
if (Regex.IsMatch(input, RegexHelper.IPv6AddressRegex))
return ValidationResult.ValidResult;

// Check if it is a valid hostname like server-01 or server-01.example.com
if (Regex.IsMatch(input, RegexHelper.HostnameRegex))
return ValidationResult.ValidResult;

return new ValidationResult(false, Strings.EnterValidHostnameOrIPAddress);
}
}
}
20 changes: 0 additions & 20 deletions Source/NETworkManager.Validators/OpacityTextboxValidator.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ private Task CheckConnectionInternetAsync(CancellationToken ct)

var result = await httpResponse.Content.ReadAsStringAsync();

var match = Regex.Match(result, RegexHelper.IPv6AddressExctractRegex);
var match = Regex.Match(result, RegexHelper.IPv6AddressRegex);

if (match.Success)
{
Expand Down
Loading