using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Text.Json.Serialization; using System.Threading.Tasks; using System.Web; using Furion; using Furion.RemoteRequest.Extensions; using UAParser; namespace Myshipping.Core; /// /// HTTP网络工具 /// public static class HttpNewUtil { /// /// 客户端IP地址 /// public static string Ip { get { var result = string.Empty; if (App.HttpContext != null) { result = GetWebClientIp(); } if (string.IsNullOrEmpty(result)) { result = GetLanIp(); } return result; } } /// /// 得到客户端IP地址 /// /// 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; } /// /// 得到局域网IP地址 /// /// public static string GetLanIp() { foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName())) { if (hostAddress.AddressFamily == AddressFamily.InterNetwork) { return hostAddress.ToString(); } } return string.Empty; } /// /// 得到远程Ip地址 /// /// private static string GetWebRemoteIp() { if (App.HttpContext?.Connection?.RemoteIpAddress == null) return string.Empty; var ip = App.HttpContext?.Connection?.RemoteIpAddress.ToString(); if (App.HttpContext == null) return ip; if (App.HttpContext.Request.Headers.ContainsKey("X-Real-IP")) { ip = App.HttpContext.Request.Headers["X-Real-IP"].ToString(); } if (App.HttpContext.Request.Headers.ContainsKey("X-Forwarded-For")) { ip = App.HttpContext.Request.Headers["X-Forwarded-For"].ToString(); } return ip; } /// /// 请求UserAgent信息 /// public static string UserAgent { get { string userAgent = App.HttpContext?.Request?.Headers["User-Agent"]; return userAgent; } } /// /// 请求Url /// public static string Url { get { var url = new StringBuilder().Append(App.HttpContext?.Request?.Scheme).Append("://") .Append(App.HttpContext?.Request?.Host).Append(App.HttpContext?.Request?.PathBase) .Append(App.HttpContext?.Request?.Path).Append(App.HttpContext?.Request?.QueryString).ToString(); return url; } } /// /// 得到操作系统版本 /// /// 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; } /// /// 公网信息 /// 慎用,如果不是直接请求接口,而是通过代理转发,拿到的是服务器的公网信息 /// /// public static async Task WanInfo() { const string url = "http://whois.pconline.com.cn/ipJson.jsp"; var resultStr = await url.GetAsStringAsync(); resultStr = resultStr[(resultStr.IndexOf("IPCallBack(", StringComparison.Ordinal) + "IPCallBack(".Length)..] .TrimEnd(); resultStr = resultStr[..^3]; var result = resultStr.ToObject(); return result; } /// /// 根据IP地址获取公网信息 /// /// public static async Task WanInfo(string ip) { var url = $"http://whois.pconline.com.cn/ipJson.jsp?ip={ip}"; var resultStr = await url.GetAsStringAsync(); resultStr = resultStr[(resultStr.IndexOf("IPCallBack(", StringComparison.Ordinal) + "IPCallBack(".Length)..] .TrimEnd(); resultStr = resultStr[..^3]; var result = resultStr.ToObject(); return result; } /// /// UserAgent信息 /// /// public static UserAgentInfoModel UserAgentInfo() { var parser = Parser.GetDefault(); var clientInfo = parser.Parse(UserAgent); var result = new UserAgentInfoModel { PhoneModel = clientInfo.Device.ToString(), OS = clientInfo.OS.ToString(), Browser = clientInfo.UA.ToString() }; return result; } private static readonly char[] reserveChar = { '/', '?', '*', ':', '|', '\\', '<', '>', '\"' }; /// /// 远程路径Encode处理,会保证开头是/,结尾也是/ /// /// /// public static string EncodeRemotePath(string remotePath) { if (remotePath == "/") { return remotePath; } var endWith = remotePath.EndsWith("/"); var part = remotePath.Split('/'); remotePath = ""; foreach (var s in part) { if (s == "") continue; if (remotePath != "") { remotePath += "/"; } remotePath += HttpUtility.UrlEncode(s).Replace("+", "%20"); } remotePath = (remotePath.StartsWith("/") ? "" : "/") + remotePath + (endWith ? "/" : ""); return remotePath; } /// /// 标准化远程目录路径,会保证开头是/,结尾也是/ ,如果命名不规范,存在保留字符,会返回空字符 /// /// 要标准化的远程路径 /// public static string StandardizationRemotePath(string remotePath) { if (string.IsNullOrEmpty(remotePath)) { return ""; } if (!remotePath.StartsWith("/")) { remotePath = "/" + remotePath; } if (!remotePath.EndsWith("/")) { remotePath = remotePath + "/"; } var index1 = 1; while (index1 < remotePath.Length) { var index2 = remotePath.IndexOf('/', index1); if (index2 == index1) { return ""; } var folderName = remotePath.Substring(index1, index2 - index1); if (folderName.IndexOfAny(reserveChar) != -1) { return ""; } index1 = index2 + 1; } return remotePath; } } /// /// 万网Ip信息Model类 /// public class WhoisIPInfoModel { /// /// Ip地址 /// public string Ip { get; set; } /// /// 省份 /// public string Pro { get; set; } /// /// 省份邮政编码 /// public string ProCode { get; set; } /// /// 城市 /// public string City { get; set; } /// /// 城市邮政编码 /// public string CityCode { get; set; } /// /// 地理信息 /// [JsonPropertyName("addr")] public string Address { get; set; } /// /// 运营商 /// public string Operator => Address[(Pro.Length + City.Length)..].Trim(); } /// /// UserAgent 信息Model类 /// public class UserAgentInfoModel { /// /// 手机型号 /// public string PhoneModel { get; set; } /// /// 操作系统(版本) /// public string OS { get; set; } /// /// 浏览器(版本) /// public string Browser { get; set; } }