mirror of
https://github.com/ldqk/Masuit.Tools.git
synced 2026-10-17 05:11:32 +08:00
内网IP检测增加一些网段并支持IPv6
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace Masuit.Tools
|
||||
{
|
||||
@@ -11,38 +8,37 @@ namespace Masuit.Tools
|
||||
/// <summary>
|
||||
/// 判断IP是否是私有地址
|
||||
/// </summary>
|
||||
/// <param name="myIPAddress"></param>
|
||||
/// <param name="ip"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsPrivateIP(this IPAddress myIPAddress)
|
||||
public static bool IsPrivateIP(this IPAddress ip)
|
||||
{
|
||||
if (IPAddress.IsLoopback(myIPAddress)) return true;
|
||||
|
||||
if (myIPAddress.AddressFamily == AddressFamily.InterNetwork)
|
||||
if (IPAddress.IsLoopback(ip)) return true;
|
||||
byte[] bytes = ip.GetAddressBytes();
|
||||
return ip.AddressFamily switch
|
||||
{
|
||||
byte[] ipBytes = myIPAddress.GetAddressBytes();
|
||||
|
||||
// 10.0.0.0/24
|
||||
if (ipBytes[0] == 10)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// 169.254.0.0/16
|
||||
if (ipBytes[0] == 169 && ipBytes[1] == 254)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// 172.16.0.0/16
|
||||
if (ipBytes[0] == 172 && ipBytes[1] == 16)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// 192.168.0.0/16
|
||||
if (ipBytes[0] == 192 && ipBytes[1] == 168)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
AddressFamily.InterNetwork when bytes[0] == 10 => true,
|
||||
AddressFamily.InterNetwork when bytes[0] == 100 && bytes[1] >= 64 && bytes[1] <= 127 => true,
|
||||
AddressFamily.InterNetwork when bytes[0] == 169 && bytes[1] == 254 => true,
|
||||
AddressFamily.InterNetwork when bytes[0] == 172 && bytes[1] == 16 => true,
|
||||
AddressFamily.InterNetwork when bytes[0] == 192 && bytes[1] == 88 && bytes[2] == 99 => true,
|
||||
AddressFamily.InterNetwork when bytes[0] == 192 && bytes[1] == 168 => true,
|
||||
AddressFamily.InterNetwork when bytes[0] == 198 && bytes[1] == 18 => true,
|
||||
AddressFamily.InterNetwork when bytes[0] == 198 && bytes[1] == 51 && bytes[2] == 100 => true,
|
||||
AddressFamily.InterNetwork when bytes[0] == 203 && bytes[1] == 0 && bytes[2] == 113 => true,
|
||||
AddressFamily.InterNetworkV6 when ip.IsIPv6Teredo || ip.IsIPv6LinkLocal || ip.IsIPv6Multicast || ip.IsIPv6SiteLocal => true,
|
||||
AddressFamily.InterNetworkV6 when ip.ToString().StartsWith("::") => true,
|
||||
AddressFamily.InterNetworkV6 when ip.ToString().StartsWith("64:ff9b::") => true,
|
||||
AddressFamily.InterNetworkV6 when ip.ToString().StartsWith("100::") => true,
|
||||
AddressFamily.InterNetworkV6 when ip.ToString().StartsWith("2001::") => true,
|
||||
AddressFamily.InterNetworkV6 when ip.ToString().StartsWith("2001:2") => true,
|
||||
AddressFamily.InterNetworkV6 when ip.ToString().StartsWith("2001:db8:") => true,
|
||||
AddressFamily.InterNetworkV6 when ip.ToString().StartsWith("2002:") => true,
|
||||
AddressFamily.InterNetworkV6 when ip.ToString().StartsWith("fc") => true,
|
||||
AddressFamily.InterNetworkV6 when ip.ToString().StartsWith("fd") => true,
|
||||
AddressFamily.InterNetworkV6 when ip.ToString().StartsWith("fe") => true,
|
||||
AddressFamily.InterNetworkV6 when bytes[0] == 255 => true,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<CodeAnalysisRuleSet />
|
||||
<Version>2.3.1.7</Version>
|
||||
<Version>2.3.1.8</Version>
|
||||
<Authors>懒得勤快</Authors>
|
||||
<Description>Masuit.Tools基础公共库</Description>
|
||||
<Copyright>懒得勤快,长空X</Copyright>
|
||||
@@ -20,9 +20,9 @@
|
||||
<RepositoryType>Github</RepositoryType>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
|
||||
<FileVersion>2.3.1.7</FileVersion>
|
||||
<FileVersion>2.3.1.8</FileVersion>
|
||||
<Company>masuit.com</Company>
|
||||
<AssemblyVersion>2.3.1.7</AssemblyVersion>
|
||||
<AssemblyVersion>2.3.1.8</AssemblyVersion>
|
||||
<PackageLicenseUrl>https://github.com/ldqk/Masuit.Tools/blob/master/LICENSE</PackageLicenseUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
@@ -20,10 +20,10 @@
|
||||
<UserSecretsId>830c282f-f7c1-42be-8651-4cd06ac8e73f</UserSecretsId>
|
||||
<RepositoryType>Github</RepositoryType>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<Version>2.3.1.7</Version>
|
||||
<FileVersion>2.3.1.7</FileVersion>
|
||||
<Version>2.3.1.8</Version>
|
||||
<FileVersion>2.3.1.8</FileVersion>
|
||||
<Company>masuit.com</Company>
|
||||
<AssemblyVersion>2.3.1.7</AssemblyVersion>
|
||||
<AssemblyVersion>2.3.1.8</AssemblyVersion>
|
||||
<Authors>懒得勤快X</Authors>
|
||||
<RepositoryUrl>https://github.com/ldqk/Masuit.Tools</RepositoryUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
Binary file not shown.
@@ -4,7 +4,7 @@
|
||||
<!--*-->
|
||||
<id>Masuit.Tools.Net</id>
|
||||
<!--*-->
|
||||
<version>2.3.1.7</version>
|
||||
<version>2.3.1.8</version>
|
||||
<title>Masuit.Tools</title>
|
||||
<!--*-->
|
||||
<authors>masuit.com</authors>
|
||||
@@ -22,7 +22,7 @@
|
||||
<copyright>Copyright © 懒得勤快</copyright>
|
||||
<tags>Masuit.Tools,工具库,Utility,Crypt,Extensions</tags>
|
||||
<dependencies>
|
||||
<dependency id="Masuit.Tools.Abstractions" version="2.3.1.7" />
|
||||
<dependency id="Masuit.Tools.Abstractions" version="2.3.1.8" />
|
||||
</dependencies>
|
||||
</metadata>
|
||||
</package>
|
||||
@@ -1,4 +1,5 @@
|
||||
using Masuit.Tools.Security;
|
||||
using Masuit.Tools;
|
||||
using Masuit.Tools.Security;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System;
|
||||
@@ -10,6 +11,7 @@ namespace NetCoreTest
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("100.125.124.105".IsPrivateIP());
|
||||
var rsaKey = RsaCrypt.GenerateRsaKeys(RsaKeyType.PKCS8, 2048);
|
||||
Console.WriteLine(rsaKey.PrivateKey);
|
||||
Console.WriteLine(rsaKey.PublicKey);
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.8" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user