Skip to content

Commit bf1b01c

Browse files
committed
Feature: Parse input and profile server field
1 parent 86c7877 commit bf1b01c

14 files changed

+233
-287
lines changed

Source/NETworkManager.Models/Network/HostRangeHelper.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using ControlzEx.Standard;
2-
using NETworkManager.Utilities;
1+
using NETworkManager.Utilities;
32
using System.Collections.Concurrent;
43
using System.Collections.Generic;
54
using System.Linq;
@@ -148,7 +147,7 @@ private static (List<(IPAddress ipAddress, string hostname)> hosts, List<string>
148147
break;
149148

150149
// example.com
151-
case var _ when Regex.IsMatch(host, RegexHelper.HostnameOrDomainRegex):
150+
case var _ when RegexHelper.HostnameOrDomainRegex().IsMatch(host):
152151
using (var dnsResolverTask =
153152
DNSClientHelper.ResolveAorAaaaAsync(host, dnsResolveHostnamePreferIPv4))
154153
{

Source/NETworkManager.Models/Network/ServerConnectionInfo.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class ServerConnectionInfo
1010
/// </summary>
1111
public ServerConnectionInfo()
1212
{
13+
1314
}
1415

1516
/// <summary>
@@ -52,6 +53,20 @@ public ServerConnectionInfo(string server, int port, TransportProtocol transport
5253
/// </summary>
5354
public TransportProtocol TransportProtocol { get; set; }
5455

56+
// public static ServerConnectionInfo Parse (string input)
57+
// {
58+
59+
60+
// return null;
61+
// }
62+
63+
//public static bool TryParse (string input, out ServerConnectionInfo serverConnectionInfo)
64+
// {
65+
// serverConnectionInfo = null;
66+
67+
// return false;
68+
// }
69+
5570
/// <summary>
5671
/// Returns a string that represents the current object.
5772
/// </summary>

Source/NETworkManager.Models/Network/ServerConnectionInfoProfile.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class ServerConnectionInfoProfile
1212
/// </summary>
1313
public ServerConnectionInfoProfile()
1414
{
15+
1516
}
1617

1718
/// <summary>

Source/NETworkManager.Utilities/RegexHelper.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ public static partial class RegexHelper
2020
/// Represents the regular expression pattern used to validate CIDR notation values for IPv4 subnet masks.
2121
/// </summary>
2222
private const string CidrRegexValues = @"([1-9]|[1-2][0-9]|3[0-2])";
23-
23+
24+
/// <summary>
25+
/// Represents a regular expression pattern that matches valid hostnames or fully qualified domain names (FQDNs).
26+
/// </summary>
27+
private const string HostnameOrDomainValues =
28+
@"(?=.{1,255}$)(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z0-9-]{1,63})*\.?";
29+
2430
/// <summary>
2531
/// Provides a compiled regular expression that matches valid IPv4 addresses in dot-decimal notation like "192.168.178.0".
2632
/// </summary>
@@ -71,7 +77,7 @@ public static partial class RegexHelper
7177
public static partial Regex IPv4AddressCidrRegex();
7278

7379
/// <summary>
74-
/// Creates a regular expression that matches IPv4 addresses, allowing for a special range in one or more octets like
80+
/// Provides a compiled regular expression that matches IPv4 addresses, allowing for a special range in one or more octets like
7581
/// "192.168.[0-50].1".
7682
/// </summary>
7783
/// <returns>A <see cref="Regex"/> instance that matches IPv4 addresses with support for custom special ranges like
@@ -101,12 +107,15 @@ public static partial class RegexHelper
101107
public const string SpecialRangeRegex =
102108
@"\[((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)-(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))([,]((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)-(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))))*\]";
103109

104-
// Private hostname values
105-
private const string HostnameOrDomainValues =
106-
@"(?=.{1,255}$)(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z0-9-]{1,63})*\.?";
110+
107111

108-
// Hostname regex like server-01 or server-01.example.com
109-
public const string HostnameOrDomainRegex = $@"^{HostnameOrDomainValues}$";
112+
/// <summary>
113+
/// Provides a compiled regular expression that matches valid hostnames or fully qualified domain names (FQDNs) like
114+
/// server-01 or server-01.example.com.
115+
/// </summary>
116+
/// <returns> A <see cref="Regex"/> instance that matches valid hostnames or FQDNs.</returns>
117+
[GeneratedRegex($@"^{HostnameOrDomainValues}$")]
118+
public static partial Regex HostnameOrDomainRegex();
110119

111120
// Match a hostname with cidr like server-01.example.com/24
112121
public const string HostnameOrDomainWithCidrRegex = $@"^{HostnameOrDomainValues}\/{CidrRegexValues}$";

Source/NETworkManager.Validators/EmptyOrWindowsDomainValidator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Globalization;
2-
using System.Text.RegularExpressions;
32
using System.Windows.Controls;
43
using NETworkManager.Localization.Resources;
54
using NETworkManager.Utilities;
@@ -19,7 +18,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
1918
if (domain.Equals("."))
2019
return ValidationResult.ValidResult;
2120

22-
return Regex.IsMatch(domain, RegexHelper.HostnameOrDomainRegex)
21+
return RegexHelper.HostnameOrDomainRegex().IsMatch(domain)
2322
? ValidationResult.ValidResult
2423
: new ValidationResult(false, Strings.EnterValidDomain);
2524
}

Source/NETworkManager.Validators/HostsFileEntryHostnameValidator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using NETworkManager.Utilities;
33
using System.Diagnostics;
44
using System.Globalization;
5-
using System.Text.RegularExpressions;
65
using System.Windows.Controls;
76

87
namespace NETworkManager.Validators;
@@ -19,7 +18,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
1918

2019
foreach (var hostname in input.Split(' '))
2120
{
22-
if (Regex.IsMatch(hostname, RegexHelper.HostnameOrDomainRegex) == false)
21+
if (RegexHelper.HostnameOrDomainRegex().IsMatch(hostname) == false)
2322
{
2423
isValid = false;
2524
break;

Source/NETworkManager.Validators/IPAddressOrHostnameAsRangeValidator.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System.Globalization;
1+
using NETworkManager.Localization.Resources;
2+
using NETworkManager.Utilities;
3+
using System.DirectoryServices.ActiveDirectory;
4+
using System.Globalization;
25
using System.Text.RegularExpressions;
36
using System.Windows.Controls;
4-
using NETworkManager.Localization.Resources;
5-
using NETworkManager.Utilities;
67

78
namespace NETworkManager.Validators;
89

@@ -22,7 +23,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
2223
// 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
2324
var isValid = RegexHelper.IPv4AddressRegex().IsMatch(localItem) ||
2425
Regex.IsMatch(localItem, RegexHelper.IPv6AddressRegex) ||
25-
Regex.IsMatch(localItem, RegexHelper.HostnameOrDomainRegex);
26+
RegexHelper.HostnameOrDomainRegex().IsMatch(localItem);
2627

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

Source/NETworkManager.Validators/IPAddressOrHostnameValidator.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System.Globalization;
1+
using NETworkManager.Localization.Resources;
2+
using NETworkManager.Utilities;
3+
using System.DirectoryServices.ActiveDirectory;
4+
using System.Globalization;
25
using System.Text.RegularExpressions;
36
using System.Windows.Controls;
4-
using NETworkManager.Localization.Resources;
5-
using NETworkManager.Utilities;
67

78
namespace NETworkManager.Validators;
89

@@ -24,7 +25,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
2425
return ValidationResult.ValidResult;
2526

2627
// Check if it is a valid hostname like server-01 or server-01.example.com
27-
if (Regex.IsMatch(input, RegexHelper.HostnameOrDomainRegex))
28+
if(RegexHelper.HostnameOrDomainRegex().IsMatch(input))
2829
return ValidationResult.ValidResult;
2930

3031
return new ValidationResult(false, Strings.EnterValidHostnameOrIPAddress);

Source/NETworkManager.Validators/MultipleHostsRangeValidator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using NETworkManager.Localization.Resources;
22
using NETworkManager.Models.Network;
33
using NETworkManager.Utilities;
4+
using System.DirectoryServices.ActiveDirectory;
45
using System.Globalization;
56
using System.Net;
67
using System.Text.RegularExpressions;
@@ -74,7 +75,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
7475
continue;
7576

7677
// server-01.example.com
77-
if (Regex.IsMatch(ipHostOrRange, RegexHelper.HostnameOrDomainRegex))
78+
if (RegexHelper.HostnameOrDomainRegex().IsMatch(ipHostOrRange))
7879
continue;
7980

8081
// server-01.example.com/24

Source/NETworkManager.Validators/RemoteDesktopHostnameAndPortValidator.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System.Globalization;
1+
using NETworkManager.Localization.Resources;
2+
using NETworkManager.Utilities;
3+
using System.DirectoryServices.ActiveDirectory;
4+
using System.Globalization;
25
using System.Text.RegularExpressions;
36
using System.Windows.Controls;
4-
using NETworkManager.Localization.Resources;
5-
using NETworkManager.Utilities;
67

78
namespace NETworkManager.Validators;
89

@@ -16,15 +17,15 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
1617
{
1718
var hostnameAndPortValues = hostnameAndPort.Split(':');
1819

19-
if (Regex.IsMatch(hostnameAndPortValues[0], RegexHelper.HostnameOrDomainRegex) &&
20+
if (RegexHelper.HostnameOrDomainRegex().IsMatch(hostnameAndPortValues[0]) &&
2021
!string.IsNullOrEmpty(hostnameAndPortValues[1]) &&
2122
Regex.IsMatch(hostnameAndPortValues[1], RegexHelper.PortRegex))
2223
return ValidationResult.ValidResult;
2324

2425
return new ValidationResult(false, Strings.EnterValidHostnameAndPort);
2526
}
2627

27-
return Regex.IsMatch((string)value, RegexHelper.HostnameOrDomainRegex)
28+
return RegexHelper.HostnameOrDomainRegex().IsMatch((string)value)
2829
? ValidationResult.ValidResult
2930
: new ValidationResult(false, Strings.EnterValidHostname);
3031
}

0 commit comments

Comments
 (0)