IPAddressValidationRule.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using System.Windows.Controls;
  10. using TeamAAS_VP.Core;
  11. namespace TeamAAS_VP.ValidationRules
  12. {
  13. public class IPAddressValidationRule : ValidationRule
  14. {
  15. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  16. {
  17. string input = value as string;
  18. // 检查输入是否为空
  19. if (string.IsNullOrWhiteSpace(input))
  20. return new ValidationResult(false, "IP address cannot be empty.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  21. // 检查是否包含全角字符
  22. if (ContainsFullWidthCharacters(input))
  23. return new ValidationResult(false, "IP address contains full-width characters.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  24. // 检查是否为有效的 IPv4 格式
  25. if (!IsValidIPv4Format(input))
  26. return new ValidationResult(false, "Invalid IP address format.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  27. // 检查每个部分的数值是否在 0 到 255 之间
  28. if (!IsValidIPv4Range(input))
  29. return new ValidationResult(false, "IP address out of range.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  30. var ipaddress = Common.ParseIpWithLeadingZeros(input);
  31. if (ipaddress == null)
  32. {
  33. return new ValidationResult(false, "Invalid IP address.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  34. }
  35. input = ipaddress.ToString();
  36. // 检查是否为环回地址 (127.x.x.x)
  37. if (IsLoopbackAddress(input))
  38. return new ValidationResult(false, "Invalid IP address.".ToString().Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  39. // 检查是否为全局 IP 地址
  40. if (IsGlobalIPAddress(input))
  41. return new ValidationResult(false, "Global IP addresses are not allowed.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  42. return ValidationResult.ValidResult;
  43. }
  44. // 检查是否包含全角字符
  45. public bool ContainsFullWidthCharacters(string input)
  46. {
  47. foreach (char c in input)
  48. {
  49. if (c >= 0xFF01 && c <= 0xFF5E) // 全角字符范围
  50. return true;
  51. }
  52. return false;
  53. }
  54. // 检查是否为有效的 IPv4 格式
  55. public bool IsValidIPv4Format(string input)
  56. {
  57. string pattern = @"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$";
  58. return Regex.IsMatch(input, pattern);
  59. }
  60. // 检查每个部分的数值是否在 0 到 255 之间
  61. public bool IsValidIPv4Range(string input)
  62. {
  63. string[] parts = input.Split('.');
  64. foreach (string part in parts)
  65. {
  66. if (!int.TryParse(part, out int number) || number < 0 || number > 255)
  67. return false;
  68. }
  69. return true;
  70. }
  71. //检查是否为环回地址(127.x.x.x)
  72. public bool IsLoopbackAddress(string input)
  73. {
  74. string[] parts = input.Split('.');
  75. if (parts.Length != 4)
  76. return false;
  77. return parts[0] == "127";
  78. }
  79. // 检查是否为全局 IP 地址
  80. public bool IsGlobalIPAddress(string input)
  81. {
  82. string[] parts = input.Split('.');
  83. int firstPart = int.Parse(parts[0]);
  84. // 私有 IP 地址范围:
  85. // 10.0.0.0 - 10.255.255.255
  86. // 172.16.0.0 - 172.31.255.255
  87. // 192.168.0.0 - 192.168.255.255
  88. if (firstPart == 10 ||
  89. (firstPart == 172 && int.Parse(parts[1]) >= 16 && int.Parse(parts[1]) <= 31) ||
  90. (firstPart == 192 && int.Parse(parts[1]) == 168))
  91. {
  92. return false; // 是私有 IP 地址
  93. }
  94. return true; // 是全局 IP 地址
  95. }
  96. }
  97. }