初始化
This commit is contained in:
94
Face.Services/Tool/System/IPHelper.cs
Normal file
94
Face.Services/Tool/System/IPHelper.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
|
||||
namespace Face.Services.Tool
|
||||
{
|
||||
public class IPHelper
|
||||
{
|
||||
public static string GetIP()
|
||||
{
|
||||
if (HttpContext.Current == null)
|
||||
{
|
||||
throw new Exception("HttpContext.Current为null");
|
||||
}
|
||||
|
||||
string ip = string.Empty;
|
||||
if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
|
||||
ip = Convert.ToString(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
|
||||
if (string.IsNullOrEmpty(ip))
|
||||
ip = Convert.ToString(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
|
||||
if (IPAddress.TryParse(ip, out var ips))
|
||||
{
|
||||
byte[] ipBytes = ips.GetAddressBytes();
|
||||
if (ipBytes[0] == 10) return ip;
|
||||
if (ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31) return ip;
|
||||
if (ipBytes[0] == 192 && ipBytes[1] == 168) return ip;
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
/// <summary>获取客户端的IP,可以取到代理后的IP
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetClientIp()
|
||||
{
|
||||
string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
|
||||
if (!string.IsNullOrEmpty(result))
|
||||
{
|
||||
//可能有代理
|
||||
if (result.IndexOf(".", StringComparison.Ordinal) == -1)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
if (result.IndexOf(",", StringComparison.Ordinal) != -1)
|
||||
{
|
||||
//有“,”,估计多个代理。取第一个不是内网的IP。
|
||||
result = result.Replace(" ", "").Replace("'", "");
|
||||
string[] temparyip = result.Split(",;".ToCharArray());
|
||||
foreach (string t in temparyip)
|
||||
{
|
||||
if (IsIpAddress(t)
|
||||
&& t.Substring(0, 3) != "10."
|
||||
&& t.Substring(0, 7) != "192.168"
|
||||
&& t.Substring(0, 7) != "172.16.")
|
||||
{
|
||||
return t; //找到不是内网的地址
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (IsIpAddress(result)) //代理即是IP格式
|
||||
return result;
|
||||
else
|
||||
result = null; //代理中的内容 非IP,取IP
|
||||
}
|
||||
}
|
||||
if (string.IsNullOrEmpty(result))
|
||||
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
|
||||
if (string.IsNullOrEmpty(result))
|
||||
result = HttpContext.Current.Request.UserHostAddress;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>判断是否是IP地址格式 0.0.0.0
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="str">待判断的IP地址</param>
|
||||
/// <returns>true or false</returns>
|
||||
public static bool IsIpAddress(string str)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str) || str.Length < 7 || str.Length > 15) return false;
|
||||
|
||||
const string regformat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$";
|
||||
|
||||
Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
|
||||
return regex.IsMatch(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user