public static ResultMetadata ComposeFrom(HostInformation host, AddressInformation address) { return(new ResultMetadata() { OrgName = host?.OrgName, CountryCode = host?.CountryCode, Country = address?.Country, Region = address?.Region, City = address?.City, ASName = address?.ASName, ASBlock = address?.ASBlock, ASType = address?.ASType, ISP = address?.ISP }); }
public async Task FetchIPLocationAsync([Option("f", "path of the file to parse")] string path, [Option("o", "output file name")] string fileName) { var results = new Dictionary <string, AddressInformation>(); if (File.Exists(fileName + ".json")) { results = JsonSerializer.Deserialize <Dictionary <string, AddressInformation> >(File.ReadAllText(fileName + ".json")); } foreach (var kvp in results) { if (kvp.Value == null) { results.Remove(kvp.Key); } } var fs = File.OpenRead(path); var traces = await JsonSerializer.DeserializeAsync <List <ParsingResult> >(fs); int remaining = traces.Count + traces.SelectMany(x => x.Hops).Count(); int total = remaining; foreach (var trace in traces) { Console.WriteLine($"({remaining}/{total})"); try { Console.WriteLine("LOCATE " + trace.Address); var resp = await SendIPLocationRequestAsync(trace.Address); using var doc = await JsonDocument.ParseAsync(await resp.Content.ReadAsStreamAsync()); var dest = AddressInformation.Read(doc); results[trace.Address] = dest; Console.WriteLine(); } catch (Exception) { Console.WriteLine("Failed to locate destination IP address - " + trace.Address); } remaining -= 1; foreach (var hop in trace.Hops) { Console.WriteLine($"({remaining}/{total})"); if (hop.Address == "*" || results.ContainsKey(hop.Address)) { Console.WriteLine("Address is *, or already saved - skipping " + hop.Address); Console.WriteLine(); remaining -= 1; continue; } try { Console.WriteLine("LOCATE " + hop.Address); var resp = await SendIPLocationRequestAsync(hop.Address); using var doc = await JsonDocument.ParseAsync(await resp.Content.ReadAsStreamAsync()); var hopInfo = AddressInformation.Read(doc); results[hop.Address] = hopInfo; Console.WriteLine(); } catch (Exception ex) { Console.WriteLine("Failed to locate hop address - " + ex.Message); results[hop.Host] = null; } remaining -= 1; } } File.WriteAllText(fileName + ".json", JsonSerializer.Serialize(results)); }