You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
264 lines
7.7 KiB
264 lines
7.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Serilog;
|
|
using Wood.Util.Browser;
|
|
|
|
namespace Wood.Util
|
|
{
|
|
public class NetHelper
|
|
{
|
|
private static HttpContext HttpContext
|
|
{
|
|
get { return GlobalContext.ServiceProvider?.GetService<IHttpContextAccessor>()!.HttpContext!; }
|
|
}
|
|
|
|
public static string Ip
|
|
{
|
|
get
|
|
{
|
|
string result = string.Empty;
|
|
if (HttpContext != null)
|
|
{
|
|
result = GetWebClientIp();
|
|
}
|
|
if (string.IsNullOrEmpty(result))
|
|
{
|
|
result = GetLanIp();
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
private static string GetWebClientIp()
|
|
{
|
|
try
|
|
{
|
|
string ip = GetWebRemoteIp();
|
|
foreach (var hostAddress in Dns.GetHostAddresses(ip))
|
|
{
|
|
if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
|
|
{
|
|
return hostAddress.ToString();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex,"获取IP失败!");
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
public static string GetLanIp()
|
|
{
|
|
try
|
|
{
|
|
foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
|
|
{
|
|
if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
|
|
{
|
|
return hostAddress.ToString();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "获取LanIP失败!");
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
|
|
|
|
private static string GetWebRemoteIp()
|
|
{
|
|
try
|
|
{
|
|
string? ip = HttpContext?.Connection?.RemoteIpAddress?.ToString();
|
|
if (HttpContext != null && HttpContext.Request != null)
|
|
{
|
|
if (HttpContext.Request.Headers.ContainsKey("X-Real-IP"))
|
|
{
|
|
ip = HttpContext.Request.Headers["X-Real-IP"].ToString();
|
|
}
|
|
|
|
if (HttpContext.Request.Headers.ContainsKey("X-Forwarded-For"))
|
|
{
|
|
ip = HttpContext.Request.Headers["X-Forwarded-For"].ToString();
|
|
}
|
|
}
|
|
return ip??"";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "获取客户端远程IP失败!");
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
public static string Browser
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
var browser = HttpContext.Request.Headers["User-Agent"];
|
|
var agent = UserAgent.ToString();
|
|
return BrowserHelper.GetBrwoserInfo(agent);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "获取端远程浏览器信息失败!");
|
|
}
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
public static string UserAgent
|
|
{
|
|
get
|
|
{
|
|
string? userAgent = string.Empty;
|
|
try
|
|
{
|
|
userAgent = HttpContext?.Request?.Headers["User-Agent"];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "获取端远程浏览器【User-Agent】失败!");
|
|
}
|
|
return userAgent??string.Empty;
|
|
}
|
|
}
|
|
|
|
#region 判断是否是外网IP
|
|
public static bool IsInnerIP(string ipAddress)
|
|
{
|
|
bool isInnerIp = false;
|
|
long ipNum = GetIpNum(ipAddress);
|
|
/**
|
|
私有IP:A类 10.0.0.0-10.255.255.255
|
|
B类 172.16.0.0-172.31.255.255
|
|
C类 192.168.0.0-192.168.255.255
|
|
当然,还有127这个网段是环回地址
|
|
**/
|
|
long aBegin = GetIpNum("10.0.0.0");
|
|
long aEnd = GetIpNum("10.255.255.255");
|
|
long bBegin = GetIpNum("172.16.0.0");
|
|
long bEnd = GetIpNum("172.31.255.255");
|
|
long cBegin = GetIpNum("192.168.0.0");
|
|
long cEnd = GetIpNum("192.168.255.255");
|
|
isInnerIp = IsInner(ipNum, aBegin, aEnd) || IsInner(ipNum, bBegin, bEnd) || IsInner(ipNum, cBegin, cEnd) || ipAddress.Equals("127.0.0.1");
|
|
return isInnerIp;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 把IP地址转换为Long型数字
|
|
/// </summary>
|
|
/// <param name="ipAddress">IP地址字符串</param>
|
|
/// <returns></returns>
|
|
private static long GetIpNum(string ipAddress)
|
|
{
|
|
string[] ip = ipAddress.Split('.');
|
|
long a = int.Parse(ip[0]);
|
|
long b = int.Parse(ip[1]);
|
|
long c = int.Parse(ip[2]);
|
|
long d = int.Parse(ip[3]);
|
|
|
|
long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
|
|
return ipNum;
|
|
}
|
|
|
|
private static bool IsInner(long userIp, long begin, long end)
|
|
{
|
|
return (userIp >= begin) && (userIp <= end);
|
|
}
|
|
#endregion
|
|
|
|
public static string GetOSVersion()
|
|
{
|
|
var osVersion = string.Empty;
|
|
try
|
|
{
|
|
var userAgent = UserAgent;
|
|
if (userAgent.Contains("NT 10"))
|
|
{
|
|
osVersion = "Windows 10";
|
|
}
|
|
else if (userAgent.Contains("NT 6.3"))
|
|
{
|
|
osVersion = "Windows 8";
|
|
}
|
|
else if (userAgent.Contains("NT 6.1"))
|
|
{
|
|
osVersion = "Windows 7";
|
|
}
|
|
else if (userAgent.Contains("NT 6.0"))
|
|
{
|
|
osVersion = "Windows Vista/Server 2008";
|
|
}
|
|
else if (userAgent.Contains("NT 5.2"))
|
|
{
|
|
osVersion = "Windows Server 2003";
|
|
}
|
|
else if (userAgent.Contains("NT 5.1"))
|
|
{
|
|
osVersion = "Windows XP";
|
|
}
|
|
else if (userAgent.Contains("NT 5"))
|
|
{
|
|
osVersion = "Windows 2000";
|
|
}
|
|
else if (userAgent.Contains("NT 4"))
|
|
{
|
|
osVersion = "Windows NT4";
|
|
}
|
|
else if (userAgent.Contains("Android"))
|
|
{
|
|
osVersion = "Android";
|
|
}
|
|
else if (userAgent.Contains("Me"))
|
|
{
|
|
osVersion = "Windows Me";
|
|
}
|
|
else if (userAgent.Contains("98"))
|
|
{
|
|
osVersion = "Windows 98";
|
|
}
|
|
else if (userAgent.Contains("95"))
|
|
{
|
|
osVersion = "Windows 95";
|
|
}
|
|
else if (userAgent.Contains("Mac"))
|
|
{
|
|
osVersion = "Mac";
|
|
}
|
|
else if (userAgent.Contains("Unix"))
|
|
{
|
|
osVersion = "UNIX";
|
|
}
|
|
else if (userAgent.Contains("Linux"))
|
|
{
|
|
osVersion = "Linux";
|
|
}
|
|
else if (userAgent.Contains("SunOS"))
|
|
{
|
|
osVersion = "SunOS";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "获取OS版本信息失败!");
|
|
}
|
|
return osVersion;
|
|
}
|
|
}
|
|
}
|
|
|