| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using TeamAAS_VP.Core;
- namespace TeamAAS_VP.ValidationRules
- {
- public class IPAddressValidationRule : ValidationRule
- {
- public override ValidationResult Validate(object value, CultureInfo cultureInfo)
- {
- string input = value as string;
- // 检查输入是否为空
- if (string.IsNullOrWhiteSpace(input))
- return new ValidationResult(false, "IP address cannot be empty.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
- // 检查是否包含全角字符
- if (ContainsFullWidthCharacters(input))
- return new ValidationResult(false, "IP address contains full-width characters.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
- // 检查是否为有效的 IPv4 格式
- if (!IsValidIPv4Format(input))
- return new ValidationResult(false, "Invalid IP address format.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
-
- // 检查每个部分的数值是否在 0 到 255 之间
- if (!IsValidIPv4Range(input))
- return new ValidationResult(false, "IP address out of range.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
- var ipaddress = Common.ParseIpWithLeadingZeros(input);
- if (ipaddress == null)
- {
- return new ValidationResult(false, "Invalid IP address.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
- }
- input = ipaddress.ToString();
- // 检查是否为环回地址 (127.x.x.x)
- if (IsLoopbackAddress(input))
- return new ValidationResult(false, "Invalid IP address.".ToString().Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
- // 检查是否为全局 IP 地址
- if (IsGlobalIPAddress(input))
- return new ValidationResult(false, "Global IP addresses are not allowed.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
- return ValidationResult.ValidResult;
- }
- // 检查是否包含全角字符
- public bool ContainsFullWidthCharacters(string input)
- {
- foreach (char c in input)
- {
- if (c >= 0xFF01 && c <= 0xFF5E) // 全角字符范围
- return true;
- }
- return false;
- }
- // 检查是否为有效的 IPv4 格式
- public bool IsValidIPv4Format(string input)
- {
- string pattern = @"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$";
- return Regex.IsMatch(input, pattern);
- }
- // 检查每个部分的数值是否在 0 到 255 之间
- public bool IsValidIPv4Range(string input)
- {
- string[] parts = input.Split('.');
- foreach (string part in parts)
- {
- if (!int.TryParse(part, out int number) || number < 0 || number > 255)
- return false;
- }
- return true;
- }
- //检查是否为环回地址(127.x.x.x)
- public bool IsLoopbackAddress(string input)
- {
- string[] parts = input.Split('.');
- if (parts.Length != 4)
- return false;
- return parts[0] == "127";
- }
- // 检查是否为全局 IP 地址
- public bool IsGlobalIPAddress(string input)
- {
- string[] parts = input.Split('.');
- int firstPart = int.Parse(parts[0]);
- // 私有 IP 地址范围:
- // 10.0.0.0 - 10.255.255.255
- // 172.16.0.0 - 172.31.255.255
- // 192.168.0.0 - 192.168.255.255
- if (firstPart == 10 ||
- (firstPart == 172 && int.Parse(parts[1]) >= 16 && int.Parse(parts[1]) <= 31) ||
- (firstPart == 192 && int.Parse(parts[1]) == 168))
- {
- return false; // 是私有 IP 地址
- }
- return true; // 是全局 IP 地址
- }
- }
- }
|