You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
5.4 KiB
C#

1 year ago
using DS.Module.Core.ServiceExtensions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace DS.Module.Core;
/// <summary>
/// HTTP工具类
/// </summary>
public static class HttpNewUtil
{
/// <summary>
/// Http上下文访问器
/// </summary>
public static IHttpContextAccessor HttpContextAccessor => ServiceLocator.Instance.GetRequiredService<IHttpContextAccessor>();
/// <summary>
/// 当前Http上下文
/// </summary>
public static HttpContext HttpContext => HttpContextAccessor?.HttpContext;
// /// <summary>
// /// 初始化Web操作
// /// </summary>
// public static void Configure (IHttpContextAccessor _accessor) {
// try {
// HttpContextAccessor = _accessor;
// }
// catch {
// }
// }
/// <summary>
/// 客户端IP地址
/// </summary>
public static string Ip
{
get
{
var result = string.Empty;
if (HttpContext != null)
{
result = GetWebClientIp();
}
if (string.IsNullOrEmpty(result))
{
result = GetLanIp();
}
return result;
}
}
/// <summary>
/// 得到客户端IP地址
/// </summary>
/// <returns></returns>
private static string GetWebClientIp()
{
var ip = GetWebRemoteIp();
foreach (var hostAddress in Dns.GetHostAddresses(ip))
{
if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
{
return hostAddress.ToString();
}
}
return string.Empty;
}
/// <summary>
/// 得到局域网IP地址
/// </summary>
/// <returns></returns>
public static string GetLanIp()
{
foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
{
return hostAddress.ToString();
}
}
return string.Empty;
}
/// <summary>
/// 得到远程Ip地址
/// </summary>
/// <returns></returns>
private static string GetWebRemoteIp()
{
if (HttpContext?.Connection?.RemoteIpAddress == null)
return string.Empty;
var ip = HttpContext?.Connection?.RemoteIpAddress.ToString();
if (HttpContext == null)
return ip;
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;
}
/// <summary>
/// 请求UserAgent信息
/// </summary>
public static string UserAgent
{
get
{
string userAgent = HttpContext?.Request?.Headers["User-Agent"];
return userAgent;
}
}
/// <summary>
/// 请求Url
/// </summary>
public static string Url
{
get
{
var url = new StringBuilder().Append(HttpContext?.Request?.Scheme).Append("://")
.Append(HttpContext?.Request?.Host).Append(HttpContext?.Request?.PathBase)
.Append(HttpContext?.Request?.Path).Append(HttpContext?.Request?.QueryString).ToString();
return url;
}
}
/// <summary>
/// 得到操作系统版本
/// </summary>
/// <returns></returns>
public static string GetOSVersion()
{
var osVersion = string.Empty;
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";
}
return osVersion;
}
}