JsonResult GetIpv4(string inip, string ip) { App_Start.GeoipContext.InitData(); CityLocation ty = null; CityIpV4 ty1 = new CityIpV4(); IPAddress incomingIp = IPAddress.Parse(ip); DateTime strt = DateTime.UtcNow; var lst = App_Start.GeoipContext._cityv4.FindAll(t => t.network.StartsWith(ip)); if (lst.Count == 0) { var srch = ip.Remove(ip.LastIndexOf('.') + 1); lst = App_Start.GeoipContext._cityv4.FindAll(t => t.network.StartsWith(srch)); } if (lst.Count == 0) { var srch = ip.Split('.')[0] + "." + ip.Split('.')[1]; lst = App_Start.GeoipContext._cityv4.FindAll(t => t.network.StartsWith(srch)); } if (lst.Count == 0) { var srch = ip.Split('.')[0]; lst = App_Start.GeoipContext._cityv4.FindAll(t => t.network.StartsWith(srch)); } if (lst.Count == 0) { lst = App_Start.GeoipContext._cityv4; } foreach (var row in lst) { IPNetwork network = IPNetwork.Parse(row.network); if (IPNetwork.Contains(network, incomingIp)) { Console.WriteLine(row.postal_code); ty1 = row; ty = App_Start.GeoipContext._cityloc.Find(t => t.geoname_id == row.geoname_id); if (ty != null) { Console.WriteLine(ty.ToString()); } continue; } } DateTime end = DateTime.UtcNow; if (ty == null) { return(Json(new { Inip = inip, Message = "No data found" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { Inip = inip, Network = ty1, Address = ty, Start = strt.ToString(), End = end.ToString(), type = "IPv4" }, JsonRequestBehavior.AllowGet)); } }
public static void InitData() { string fileNamev4 = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/GeoLite2-City-CSV_20171107/GeoLite2-City-Blocks-IPv4.csv"); string fileNamev6 = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/GeoLite2-City-CSV_20171107/GeoLite2-City-Blocks-IPv6.csv"); string fileNamecloc = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/GeoLite2-City-CSV_20171107/GeoLite2-City-Locations-en.csv"); int idx = 0; if (_cityv4 == null || _cityv4.Count == 0) { _cityv4 = new List <CityIpV4>(); foreach (var line in File.ReadLines(fileNamev4)) { if (idx == 0) { idx++; continue; } _cityv4.Add(CityIpV4.FromCsv(line)); } //_cityv4 = File.ReadAllLines(fileNamev4) // .Skip(1) // .Select(v => CityIpV4.FromCsv(v)) // .ToList(); } idx = 0; if (_cityv6 == null || _cityv6.Count == 0) { _cityv6 = new List <CityIpV6>(); foreach (var line in File.ReadLines(fileNamev6)) { if (idx == 0) { idx++; continue; } _cityv6.Add(CityIpV6.FromCsv(line)); } //_cityv6 = File.ReadAllLines(fileNamev6) // .Skip(1) // .Select(v => CityIpV6.FromCsv(v)) // .ToList(); } idx = 0; if (_cityloc == null || _cityloc.Count == 0) { _cityloc = new List <CityLocation>(); foreach (var line in File.ReadLines(fileNamecloc)) { if (idx == 0) { idx++; continue; } _cityloc.Add(CityLocation.FromCsv(line)); } //_cityloc = File.ReadAllLines(fileNamecloc) // .Skip(1) // .Select(v => CityLocation.FromCsv(v)) // .ToList(); } }