public void Asn_ValidResponse() { using (var reader = new DatabaseReader(_asnDatabaseFile)) { var ipAddressStr = "1.128.0.0"; var response = reader.Asn(ipAddressStr); CheckAsn(response, ipAddressStr); Assert.True(reader.TryAsn(ipAddressStr, out response !)); CheckAsn(response, ipAddressStr); var ipAddress = IPAddress.Parse(ipAddressStr); response = reader.Asn(ipAddress); CheckAsn(response, ipAddressStr); Assert.True(reader.TryAsn(ipAddress, out response !)); CheckAsn(response, ipAddressStr); } }
public static (string location, string network) GetIPLocation(this IPAddress ip) { switch (ip.AddressFamily) { case AddressFamily.InterNetwork when ip.IsPrivateIP(): case AddressFamily.InterNetworkV6 when ip.IsPrivateIP(): return("内网", "内网IP"); case AddressFamily.InterNetworkV6 when ip.IsIPv4MappedToIPv6: ip = ip.MapToIPv4(); goto case AddressFamily.InterNetwork; case AddressFamily.InterNetwork: var parts = IPSearcher.MemorySearch(ip.ToString())?.Region.Split('|'); if (parts != null) { var asn = MaxmindAsnReader.Asn(ip); var network = parts[^ 1] == "0" ? asn.AutonomousSystemOrganization : parts[^ 1];
public static List <Logfileentries> logfilesentry(string dbcountrypath, string dbasnpath, string logpath) { List <Logfileentries> logentries = new List <Logfileentries>(); Regex IPaddress = new Regex(@"\b(?:\d{1,3}\.){3}\d{1,3}\b"); Regex Timestamp = new Regex(@"\d{2}\/\w{3}\/\d{4}:\d{2}:\d{2}:\d{2} (\+|\-)\d{4}"); Regex IPandTimestamp = new Regex(@"\b(?:\d{1,3}\.){3}\d{1,3}\b - - \[\d{2}\/\w{3}\/\d{4}:\d{2}:\d{2}:\d{2} (\+|\-)\d{4}\] "); DatabaseReader ipcountry = new DatabaseReader(dbcountrypath); DatabaseReader ipASN = new DatabaseReader(dbasnpath); string hostname = ""; foreach (var line in File.ReadLines(logpath)) { Logfileentries logfileentry = new Logfileentries(); Match iptimestamp = IPandTimestamp.Match(line); Match ipaddress = IPaddress.Match(iptimestamp.Value); Match timestamp = Timestamp.Match(iptimestamp.Value); //Console.WriteLine(ipaddress.Value); CountryResponse countryresponse = ipcountry.Country(ipaddress.Value); AsnResponse ASNresponse = ipASN.Asn(ipaddress.Value); try{ hostname = Dns.GetHostEntry(ipaddress.Value).HostName; } catch (SocketException) {} if (hostname == "") { hostname = "unknown domain"; } string[] logmethod = Regex.Split(line, IPandTimestamp.ToString()); logfileentry.Logipaddress = ipaddress.Value; logfileentry.Logtimestamp = timestamp.Value; logfileentry.Logmethod = logmethod[2]; logfileentry.Logcountry = countryresponse.Country.ToString(); logfileentry.LogASN = ASNresponse.AutonomousSystemOrganization.ToString(); logfileentry.LogDNS = hostname; logentries.Add(logfileentry); //Console.WriteLine(logfileentry.Logipaddress + "||" + logfileentry.Logtimestamp + "||" + logfileentry.Logmethod + "||" + logfileentry.Logcountry + "||" + logfileentry.LogASN + "||" + logfileentry.LogDNS); } return(logentries); }
public static AsnResponse GetAsnResponse(string ipAddress) => AsnReader.Asn(ipAddress);
public static (string location, string network) GetIPLocation(this IPAddress ip) { switch (ip.AddressFamily) { case AddressFamily.InterNetwork when ip.IsPrivateIP(): case AddressFamily.InterNetworkV6 when ip.IsPrivateIP(): return("内网", "内网IP"); case AddressFamily.InterNetworkV6 when ip.IsIPv4MappedToIPv6: ip = ip.MapToIPv4(); goto case AddressFamily.InterNetwork; case AddressFamily.InterNetwork: var parts = IPSearcher.MemorySearch(ip.ToString())?.Region.Split('|'); if (parts != null) { var asn = Policy <AsnResponse> .Handle <AddressNotFoundException>().Fallback(new AsnResponse()).Execute(() => MaxmindAsnReader.Asn(ip)); var network = parts[^ 1] == "0" ? asn.AutonomousSystemOrganization : parts[^ 1];
public static AsnResponse GetIPAsn(this string ip) { if (ip.IsPrivateIP()) { return(new AsnResponse()); } return(Policy <AsnResponse> .Handle <AddressNotFoundException>().Fallback(new AsnResponse()).Execute(() => MaxmindAsnReader.Asn(ip))); }