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 WhoisAsync([Option("f", "path of the file to parse")] string path, [Option("o", "output file name")] string fileName) { var results = new Dictionary <string, HostInformation>(); if (File.Exists(fileName + ".json")) { results = JsonSerializer.Deserialize <Dictionary <string, HostInformation> >(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("WHOIS " + trace.Host); var resp = await SendWhoisRequestAsync(trace.Host); using var doc = await JsonDocument.ParseAsync(await resp.Content.ReadAsStreamAsync()); var dest = HostInformation.Read(doc); results[trace.Host] = dest; Console.WriteLine(); } catch (Exception) { Console.WriteLine("Failed to WHOIS destination domain"); } remaining -= 1; foreach (var hop in trace.Hops) { Console.WriteLine($"({remaining}/{total})"); if (hop.Host == "*" || results.ContainsKey(hop.Host)) { Console.WriteLine("Host is *, or already saved - skipping " + hop.Host); Console.WriteLine(); remaining -= 1; continue; } try { Console.WriteLine("WHOIS " + hop.Host); var resp = await SendWhoisRequestAsync(hop.Host); using var doc = await JsonDocument.ParseAsync(await resp.Content.ReadAsStreamAsync()); var hopInfo = HostInformation.Read(doc); results[hop.Host] = hopInfo; Console.WriteLine(); } catch (Exception ex) { Console.WriteLine("Failed to WHOIS hop domain - " + ex.Message); results[hop.Host] = null; } remaining -= 1; } } File.WriteAllText(fileName + ".json", JsonSerializer.Serialize(results)); }