Repository navigation
Expand file tree
/
Copy pathMultipleHostsRangeValidator.cs
More file actions
112 lines (88 loc) · 3.75 KB
/
Copy pathMultipleHostsRangeValidator.cs
File metadata and controls
112 lines (88 loc) · 3.75 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
using NETworkManager.Localization.Resources;
using NETworkManager.Models.Network;
using NETworkManager.Utilities;
using System;
using System.DirectoryServices.ActiveDirectory;
using System.Globalization;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Controls;
namespace NETworkManager.Validators;
public class MultipleHostsRangeValidator : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var isValid = true;
if (value == null)
return new ValidationResult(false, Strings.EnterValidIPScanRange);
foreach (var ipHostOrRange in ((string)value)
.Replace(" ", "")
.Split([';', '\r', '\n'], StringSplitOptions.RemoveEmptyEntries))
{
// 192.168.0.1
if (RegexHelper.IPv4AddressRegex().IsMatch(ipHostOrRange))
continue;
// 192.168.0.0/24
if (RegexHelper.IPv4AddressCidrRegex().IsMatch(ipHostOrRange))
continue;
// 192.168.0.0/255.255.255.0
if (RegexHelper.IPv4AddressSubnetmaskRegex().IsMatch(ipHostOrRange))
continue;
// 192.168.0.1-100
if (RegexHelper.IPv4AddressShortRangeRegex().IsMatch(ipHostOrRange))
{
var shortRange = ipHostOrRange.Split('-');
var shortBase = shortRange[0][..shortRange[0].LastIndexOf('.')];
if (IPv4Address.ToInt32(IPAddress.Parse(shortRange[0])) >
IPv4Address.ToInt32(IPAddress.Parse($"{shortBase}.{shortRange[1]}")))
isValid = false;
continue;
}
// 192.168.0.0 - 192.168.0.100
if (RegexHelper.IPv4AddressRangeRegex().IsMatch(ipHostOrRange))
{
var range = ipHostOrRange.Split('-');
if (IPv4Address.ToInt32(IPAddress.Parse(range[0])) > IPv4Address.ToInt32(IPAddress.Parse(range[1])))
isValid = false;
continue;
}
// 192.168.[50-100].1
if (RegexHelper.IPv4AddressSpecialRangeRegex().IsMatch(ipHostOrRange))
{
var octets = ipHostOrRange.Split('.');
foreach (var octet in octets)
{
// Match [50-100]
if (!Regex.IsMatch(octet, RegexHelper.SpecialRangeRegex))
continue;
foreach (var numberOrRange in octet.Substring(1, octet.Length - 2).Split(','))
{
if (!numberOrRange.Contains('-'))
continue;
// 50-100 --> {50, 100}
var rangeNumber = numberOrRange.Split('-');
if (int.Parse(rangeNumber[0]) > int.Parse(rangeNumber[1]))
isValid = false;
}
}
continue;
}
// 2001:db8:85a3::8a2e:370:7334
if (Regex.IsMatch(ipHostOrRange, RegexHelper.IPv6AddressRegex))
continue;
// server-01.example.com
if (RegexHelper.HostnameOrDomainRegex().IsMatch(ipHostOrRange))
continue;
// server-01.example.com/24
if (Regex.IsMatch(ipHostOrRange, RegexHelper.HostnameOrDomainWithCidrRegex))
continue;
// server-01.example.com/255.255.255.0
if (Regex.IsMatch(ipHostOrRange, RegexHelper.HostnameOrDomainWithSubnetmaskRegex))
continue;
isValid = false;
}
return isValid
? ValidationResult.ValidResult
: new ValidationResult(false, Strings.EnterValidIPScanRange);
}
}