Beispiel #1
0
        public void SetUp()
        {
            SerilogConfig.Init();

            parser = new WhoisParser();
        }
Beispiel #2
0
        public void NetworksLocationPropertyCountsToTsv(string inputFilePath, string propertyName, string outputFilePath)
        {
            var parser = new WhoisParser(new SectionTokenizer(), new SectionParser());

            this.NetworksLocationPropertyCountsToTsv(parser, inputFilePath, propertyName, outputFilePath);
        }
Beispiel #3
0
        public void NetworksWithLocationsToTsv(string inputFilePath, string outputFilePath)
        {
            var parser = new WhoisParser(new SectionTokenizer(), new SectionParser());

            this.NetworksWithLocationsToTsv(parser, inputFilePath, outputFilePath);
        }
Beispiel #4
0
        protected void NetworksLocationPropertyCountsToTsv(WhoisParser parser, string inputFilePath, string propertyName, string outputFilePath)
        {
            var outputFolderPath = Path.GetDirectoryName(outputFilePath);

            if (!Directory.Exists(outputFolderPath))
            {
                Directory.CreateDirectory(outputFolderPath);
            }

            var locationExtraction = new NetworkLocationExtraction(parser);

            var stringsCount = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase);

            var normalizedLocationType = typeof(NormalizedLocation);
            var properties             = normalizedLocationType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            PropertyInfo targetProperty = null;

            foreach (var property in properties)
            {
                if (property.Name == propertyName)
                {
                    targetProperty = property;
                }
            }

            if (targetProperty == null)
            {
                throw new ArgumentNullException("targetProperty");
            }

            foreach (var network in locationExtraction.ExtractNetworksWithLocations(inputFilePath, inputFilePath))
            {
                if (network.Id != null)
                {
                    var rawPropertyValue = targetProperty.GetValue(network.Location);

                    if (rawPropertyValue != null)
                    {
                        var value = (string)rawPropertyValue;

                        int currentCount;

                        if (!stringsCount.TryGetValue(value, out currentCount))
                        {
                            currentCount = 0;
                        }

                        currentCount++;
                        stringsCount[value] = currentCount;
                    }
                }
                //// TODO: Else log
            }

            using (var outputFile = new StreamWriter(outputFilePath))
            {
                foreach (var entry in stringsCount)
                {
                    // No need to sanitize entry.Value since it's a number
                    outputFile.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}\t{1}", TsvUtils.ReplaceAndTrimIllegalCharacters(entry.Key, removeDoubleQuotes: true), entry.Value));
                }
            }
        }
Beispiel #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WhoisLookup"/> class.
 /// </summary>
 public WhoisLookup(WhoisOptions options)
 {
     Options      = options;
     whoisParser  = new WhoisParser();
     ServerLookup = new IanaServerLookup();
 }
Beispiel #6
0
        public void TestRWhoisRetrieveRecords()
        {
            var parser   = new WhoisParser(new SectionTokenizer(), new RWhoisSectionParser());
            var sections = parser.RetrieveSections("rwhois.sample.txt");

            var i = -1;

            foreach (var section in sections)
            {
                i++;

                if (i == 0)
                {
                    // network: NET-104-169-61-0-24
                    Assert.AreEqual("network", section.Type);
                    Assert.AreEqual("NET-104-169-61-0-24", section.Id);

                    var records = section.Records;

                    var keys         = new HashSet <string>(records.Keys);
                    var expectedKeys = new HashSet <string>()
                    {
                        "Auth-Area", "ID", "Network-Name", "IP-Network", "Org-Name;I", "Street-Address", "City", "State", "Postal-Code", "Country-Code", "Tech-Contact;I", "Admin-Contact;I", "Abuse-Contact;I", "Updated", "Updated-By", "Class-Name"
                    };

                    Assert.IsTrue(keys.SetEquals(expectedKeys), "The returned section keys are different than expected");

                    Assert.AreEqual("104.169.0.0/16", records["Auth-Area"].ToString(), "The Auth-Area record had an incorrect value");
                    Assert.AreEqual("NET-104-169-61-0-24", records["ID"].ToString(), "The ID record had an incorrect value");
                    Assert.AreEqual("104-169-61-0-24", records["Network-Name"].ToString(), "The Network-Name record had an incorrect value");
                    Assert.AreEqual("104.169.61.0/24", records["IP-Network"].ToString(), "The IP-Network record had an incorrect value");
                    Assert.AreEqual("FIOS-D Frontier Communications Everett/Redmond WA", records["Org-Name;I"].ToString(), "The Org-Name;I record had an incorrect value");
                    Assert.AreEqual("426 E. Casino Rd", records["Street-Address"].ToString(), "The Street-Address record had an incorrect value");
                    Assert.AreEqual("Everett", records["City"].ToString(), "The City record had an incorrect value");
                    Assert.AreEqual("WA", records["State"].ToString(), "The State record had an incorrect value");
                    Assert.AreEqual("98208", records["Postal-Code"].ToString(), "The Postal-Code record had an incorrect value");
                    Assert.AreEqual("US", records["Country-Code"].ToString(), "The Country-Code record had an incorrect value");
                    Assert.AreEqual("AM100-FRTR", records["Tech-Contact;I"].ToString(), "The Tech-Contact;I record had an incorrect value");
                    Assert.AreEqual("IPADMIN-FRTR", records["Admin-Contact;I"].ToString(), "The Admin-Contact;I record had an incorrect value");
                    Assert.AreEqual("ABUSE-FRTR", records["Abuse-Contact;I"].ToString(), "The Abuse-Contact;I record had an incorrect value");
                    Assert.AreEqual("20141006", records["Updated"].ToString(), "The Updated record had an incorrect value");
                    Assert.AreEqual("*****@*****.**", records["Updated-By"].ToString(), "The Updated-By record had an incorrect value");
                    Assert.AreEqual("network", records["Class-Name"].ToString(), "The Class-Name record had an incorrect value");
                }
                else if (i == 1)
                {
                    // network: NET-104-169-0-0-16
                    Assert.AreEqual("network", section.Type);
                    Assert.AreEqual("NET-104-169-0-0-16", section.Id);

                    var records = section.Records;

                    var keys         = new HashSet <string>(records.Keys);
                    var expectedKeys = new HashSet <string>()
                    {
                        "Auth-Area", "ID", "Network-Name", "IP-Network", "Org-Name;I", "Street-Address", "City", "State", "Postal-Code", "Country-Code", "Tech-Contact;I", "Admin-Contact;I", "Updated", "Updated-By", "Class-Name"
                    };

                    Assert.IsTrue(keys.SetEquals(expectedKeys), "The returned section keys are different than expected");

                    Assert.AreEqual("104.169.0.0/16", records["Auth-Area"].ToString(), "The Auth-Area record had an incorrect value");
                    Assert.AreEqual("NET-104-169-0-0-16", records["ID"].ToString(), "The ID record had an incorrect value");
                    Assert.AreEqual("104-169-0-0-16", records["Network-Name"].ToString(), "The Network-Name record had an incorrect value");
                    Assert.AreEqual("104.169.0.0/16", records["IP-Network"].ToString(), "The IP-Network record had an incorrect value");
                    Assert.AreEqual("Frontier Communications Solutions", records["Org-Name;I"].ToString(), "The Org-Name;I record had an incorrect value");
                    Assert.AreEqual("180 South Clinton Ave", records["Street-Address"].ToString(), "The Street-Address record had an incorrect value");
                    Assert.AreEqual("Rochester", records["City"].ToString(), "The City record had an incorrect value");
                    Assert.AreEqual("NY", records["State"].ToString(), "The State record had an incorrect value");
                    Assert.AreEqual("14646", records["Postal-Code"].ToString(), "The Postal-Code record had an incorrect value");
                    Assert.AreEqual("US", records["Country-Code"].ToString(), "The Country-Code record had an incorrect value");
                    Assert.AreEqual("ABUSE-FRTR", records["Tech-Contact;I"].ToString(), "The Tech-Contact;I record had an incorrect value");
                    Assert.AreEqual("IPADMIN-FRTR", records["Admin-Contact;I"].ToString(), "The Admin-Contact;I record had an incorrect value");
                    Assert.AreEqual("20140813", records["Updated"].ToString(), "The Updated record had an incorrect value");
                    Assert.AreEqual("*****@*****.**", records["Updated-By"].ToString(), "The Updated-By record had an incorrect value");
                    Assert.AreEqual("network", records["Class-Name"].ToString(), "The Class-Name record had an incorrect value");
                }
                else if (i == 2)
                {
                    // network: NET-104-169-0-0-16
                    Assert.AreEqual("network", section.Type);
                    Assert.AreEqual("NET-1851.169.244.0.0/16", section.Id);

                    var records = section.Records;

                    CollectionAssert.AreEquivalent(TextUtils.SplitTextToLines(text: "221 Old County Rd\r\nPembroke, ME 04666", removeEmptyEntries: true), TextUtils.SplitTextToLines(text: records["Address"].ToString(), removeEmptyEntries: true), "The Address record had an incorrect value");
                }
            }
        }