/// <summary> /// 获取IP地址位置 /// </summary> /// <param name="province">返回省份</param> /// <param name="city">返回城市</param> public static string GetAddress(out string province, out string city) { return(GetAddress(Req.GetIP(), out province, out city)); }
/// <summary> /// 查找指定IP位置 /// </summary> /// <param name="ip"></param> /// <returns></returns> public static Location GetLocation(string ip = "") { if (string.IsNullOrWhiteSpace(ip)) { ip = Req.GetIP(); } if (data == null) { using (var fs = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read)) { data = new byte[fs.Length]; fs.Read(data, 0, data.Length); } var buffer = new byte[8]; Array.Copy(data, 0, buffer, 0, 8); firstStartIpOffset = ((buffer[0] + (buffer[1] * 0x100)) + ((buffer[2] * 0x100) * 0x100)) + (((buffer[3] * 0x100) * 0x100) * 0x100); lastStartIpOffset = ((buffer[4] + (buffer[5] * 0x100)) + ((buffer[6] * 0x100) * 0x100)) + (((buffer[7] * 0x100) * 0x100) * 0x100); ipCount = Convert.ToInt64((((lastStartIpOffset - firstStartIpOffset)) / 7.0)); if (ipCount <= 1L) { throw new ArgumentException("ip FileDataError"); } } if (!regex.Match(ip).Success) { throw new ArgumentException("IP格式错误"); } var Location = new Location { IP = ip }; var intIP = IpToInt(ip); if ((intIP >= IpToInt("127.0.0.1") && (intIP <= IpToInt("127.255.255.255")))) { Location.Area = "本机/局域网地址"; Location.Address = ""; } else { if ((((intIP >= IpToInt("0.0.0.0")) && (intIP <= IpToInt("2.255.255.255"))) || ((intIP >= IpToInt("64.0.0.0")) && (intIP <= IpToInt("126.255.255.255")))) || ((intIP >= IpToInt("58.0.0.0")) && (intIP <= IpToInt("60.255.255.255")))) { Location.Area = "网络保留地址"; Location.Address = ""; } } var right = ipCount; var left = 0L; var startIp = 0L; var endIpOff = 0L; var endIp = 0L; var countryFlag = 0; while (left < (right - 1L)) { var middle = (right + left) / 2L; startIp = GetStartIp(middle, out endIpOff); if (intIP == startIp) { left = middle; break; } if (intIP > startIp) { left = middle; } else { right = middle; } } startIp = GetStartIp(left, out endIpOff); endIp = GetEndIp(endIpOff, out countryFlag); if ((startIp <= intIP) && (endIp >= intIP)) { string local; Location.Area = GetCountry(endIpOff, countryFlag, out local); Location.Address = local; } else { Location.Area = "未知"; Location.Address = ""; } Location.Address = Regex.Replace(Location.Address, "CZ88.NET", "", RegexOptions.IgnoreCase); return(Location); }
/// <summary> /// 记住用户最后一次访问地址 /// </summary> public static void RememberUrl() { Cookies.Set(WebSystemConfigs.ConfigEntity.Cookies_CallBack_Url, Req.GetUrl()); }