Example #1
0
        public async Task WebTldRuleProviderTest()
        {
            var tldRuleProvider = new WebTldRuleProvider();
            var rules           = await tldRuleProvider.BuildAsync();

            Assert.IsNotNull(rules);
        }
        public async Task RuleDivisionExample()
        {
            var provider = new WebTldRuleProvider();
            var rules    = await provider.BuildAsync();

            //ICANN and Private rules
            var domainParser = new DomainParser(rules);
            var domainInfo   = await domainParser.ParseAsync("sub.test.co.uk");

            Assert.Equal("test.co.uk", domainInfo.RegistrableDomain);
            domainInfo = await domainParser.ParseAsync("www.myblog.blogspot.co.uk");

            Assert.Equal("myblog.blogspot.co.uk", domainInfo.RegistrableDomain);

            //ICANN only rules
            var icannRules = rules.Where(x => x.Division == TldRuleDivision.ICANN);

            domainParser = new DomainParser(icannRules);
            domainInfo   = await domainParser.ParseAsync("sub.test.co.uk");

            Assert.Equal("test.co.uk", domainInfo.RegistrableDomain);
            domainInfo = await domainParser.ParseAsync("www.myblog.blogspot.co.uk");

            Assert.Equal("blogspot.co.uk", domainInfo.RegistrableDomain); //Now TLD returned
        }
Example #3
0
        public When_resolving_name_servers()
        {
            var webTldRuleProvider = new WebTldRuleProvider();
            var domainParser       = new DomainParser(webTldRuleProvider);
            var log = new LogService(true);

            _dnsClient = new LookupClientProvider(domainParser, log);
        }
Example #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(svcs =>
            {
                var provider  = new WebTldRuleProvider();
                var rules     = provider.BuildAsync().GetAwaiter().GetResult();
                var structure = new DomainDataStructure("*", new TldRule("*"));
                structure.AddRules(rules);
                return(structure);
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }
Example #5
0
        public Task Initialize(Options opts)
        {
            var envApiUser  = ConfigManager.GetEnvVarRequired(EnvApiUser);
            var envUsername = ConfigManager.GetEnvVarOrDefault(EnvUsername) ?? envApiUser;
            var envApiKey   = ConfigManager.GetEnvVarRequired(EnvApiKey);

            _dnsClient = InitializeDnsClient(envUsername, envApiUser, envApiKey, opts.Sandbox);

            var cacheProvider      = new FileCacheProvider(cacheTimeToLive: new TimeSpan(10, 0, 0));
            var webTldRuleProvider = new WebTldRuleProvider(cacheProvider: cacheProvider);

            _domainParser = new DomainParser(webTldRuleProvider);

            _ttlToUse = opts.Ttl ?? ConfigManager.DefaultTtlInSeconds;

            return(Task.CompletedTask);
        }
Example #6
0
        public static void LoadFromWeb()
        {
            var webTldRuleProvider = new WebTldRuleProvider(cacheProvider: new FileCacheProvider(cacheTimeToLive: new TimeSpan(10, 0, 0)));

            var domainParser = new DomainParser(webTldRuleProvider);

            for (var i = 0; i < 1000; i++)
            {
                var isValid = webTldRuleProvider.CacheProvider.IsCacheValid();
                if (!isValid)
                {
                    webTldRuleProvider.BuildAsync().GetAwaiter(); //Reload data
                }

                var domainInfo = domainParser.Get($"sub{i}.test.co.uk");

                Console.WriteLine("RegistrableDomain:{0}", domainInfo.RegistrableDomain);
                Console.WriteLine("SubDomain:{0}", domainInfo.SubDomain);
            }
        }
        public async Task CheckCachedRules()
        {
            string tmpFile = Path.Combine(Path.GetTempPath(), "UnitTestPublicSuffixList.dat");

            if (File.Exists(tmpFile))
            {
                File.Delete(tmpFile);
            }

            ITldRuleProvider provider = new WebTldRuleProvider(fileName: tmpFile);
            var rules = await provider.BuildAsync();

            Assert.True(File.Exists(tmpFile));
            Assert.NotNull(rules);
            var ruleList = rules.ToList();

            Assert.True(ruleList.Count > 100); //Expecting lots of rules

            //Spot checks (If test fails here, verify that rules still exist on:
            //https://publicsuffix.org/list/public_suffix_list.dat
            var spotChecks = new string[] { "com", "*.bd", "blogspot.com" };
            var lookup     = ruleList.ToDictionary(x => x.Name, x => x.Name);

            Assert.True(spotChecks.All(x => lookup.ContainsKey(x)));

            //Verify cache
            var fileDateBefore = File.GetLastWriteTimeUtc(tmpFile);
            var cachedRules    = await provider.BuildAsync();

            var fileDateAfter = File.GetLastWriteTimeUtc(tmpFile);

            Assert.Equal(ruleList.Count, cachedRules.Count());
            Assert.Equal(fileDateBefore, fileDateAfter);
            Assert.True(cachedRules.All(x => lookup.ContainsKey(x.Name)));

            //Cleanup
            if (File.Exists(tmpFile))
            {
                File.Delete(tmpFile);
            }
        }
        public async Task CachedProviderExample()
        {
            //Cache Public Suffix data to file, refresh after TTL expires
            //All params are optional (in which cases it uses same values as below as defaults)
            var cachedFile       = Path.Combine(Path.GetTempPath(), "public_suffix_list.dat");
            var plublicSuffixUrl = @"https://publicsuffix.org/list/public_suffix_list.dat";
            var timeToLive       = TimeSpan.FromDays(1);

            var provider = new WebTldRuleProvider(cachedFile, plublicSuffixUrl, timeToLive);
            var rules    = await provider.BuildAsync();

            Assert.NotEmpty(rules);
            Assert.False(provider.MustRefresh());

            //Lets manually force refresh of cache
            File.Delete(cachedFile);
            Assert.True(provider.MustRefresh());
            await provider.Refresh();

            Assert.False(provider.MustRefresh());
        }
Example #9
0
        public OrganisationDomainProvider()
        {
            WebTldRuleProvider tldRuleProvider = new WebTldRuleProvider(timeToLive: TimeSpan.FromDays(7));

            _domainParser = new DomainParser(tldRuleProvider);
        }
Example #10
0
 static DomainParser()
 {
     _webTldRuleProvider = new WebTldRuleProvider(
         cacheProvider: new FileCacheProvider(cacheTimeToLive: TimeSpan.FromDays(7)));
     _domainParser = new Nager.PublicSuffix.DomainParser(_webTldRuleProvider);
 }