mirror of
https://git.oceanpay.cc/danial/kami_itunes_june.git
synced 2025-12-18 22:31:24 +00:00
111 lines
2.3 KiB
C#
111 lines
2.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net;
|
||
|
||
namespace AppleBatch_June.Utils
|
||
{
|
||
public class IpIsRange
|
||
{
|
||
private static string[] AllowIPRanges = new string[3] { "10.0.0.0-10.255.255.255", "172.16.0.0-172.31.255.255", "192.168.0.0-192.168.255.255" };
|
||
|
||
public static bool TheIpIsRange(string ip)
|
||
{
|
||
bool result = false;
|
||
string[] allowIPRanges = AllowIPRanges;
|
||
foreach (string ranges in allowIPRanges)
|
||
{
|
||
if (TheIpIsRangeStr(ip, ranges))
|
||
{
|
||
result = true;
|
||
break;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
private static bool TheIpIsRangeStr(string ip, string ranges)
|
||
{
|
||
bool result = false;
|
||
TryParseRanges(ranges, out var count, out var start_ip, out var end_ip);
|
||
if (ip == "::1")
|
||
{
|
||
ip = "127.0.0.1";
|
||
}
|
||
try
|
||
{
|
||
IPAddress.Parse(ip);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
throw new ApplicationException("要检测的IP地址无效");
|
||
}
|
||
if (count == 1 && ip == start_ip)
|
||
{
|
||
result = true;
|
||
}
|
||
else if (count == 2)
|
||
{
|
||
byte[] array = Get4Byte(start_ip);
|
||
byte[] array2 = Get4Byte(end_ip);
|
||
byte[] array3 = Get4Byte(ip);
|
||
bool flag = true;
|
||
for (int i = 0; i < 4; i++)
|
||
{
|
||
if (array3[i] > array2[i] || array3[i] < array[i])
|
||
{
|
||
flag = false;
|
||
break;
|
||
}
|
||
}
|
||
result = flag;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
private static void TryParseRanges(string ranges, out int count, out string start_ip, out string end_ip)
|
||
{
|
||
string[] array = ranges.Split('-');
|
||
if (array.Count() != 2 && array.Count() != 1)
|
||
{
|
||
throw new ApplicationException("IP范围指定格式不正确,可以指定一个IP,如果是一个范围请用“-”分隔");
|
||
}
|
||
count = array.Count();
|
||
start_ip = array[0];
|
||
end_ip = "";
|
||
try
|
||
{
|
||
IPAddress.Parse(array[0]);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
throw new ApplicationException("IP地址无效");
|
||
}
|
||
if (array.Count() == 2)
|
||
{
|
||
end_ip = array[1];
|
||
try
|
||
{
|
||
IPAddress.Parse(array[1]);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
throw new ApplicationException("IP地址无效");
|
||
}
|
||
}
|
||
}
|
||
|
||
private static byte[] Get4Byte(string ip)
|
||
{
|
||
string[] array = ip.Split('.');
|
||
List<byte> list = new List<byte>();
|
||
string[] array2 = array;
|
||
foreach (string value in array2)
|
||
{
|
||
list.Add(Convert.ToByte(value));
|
||
}
|
||
return list.ToArray();
|
||
}
|
||
}
|
||
}
|