Ejemplo n.º 1
0
        /// <summary>
        /// Updates the list of hosts in the domain.
        /// </summary>
        private async Task SetHosts(ParsedDomain domain, IReadOnlyList <NameCheapHostRecord> hosts)
        {
            var args = WithDefaultArgs("domains.dns.setHosts", new Dictionary <string, string>
            {
                ["SLD"] = domain.SLD,
                ["TLD"] = domain.TLD
            });

            var idx = 1;

            foreach (var host in hosts)
            {
                args["HostName" + idx]   = host.Name;
                args["RecordType" + idx] = host.Type;
                args["Address" + idx]    = host.Address;
                args["MXPref" + idx]     = host.MxPref.ToString();
                args["TTL" + idx]        = host.Ttl.ToString();

                idx++;
            }

            var request = new HttpRequestMessage
            {
                Method     = HttpMethod.Post,
                RequestUri = new Uri(API_URL),
                Content    = new FormUrlEncodedContent(args)
            };

            await InvokeApiAsync(request);
        }
Ejemplo n.º 2
0
        // Look for a resolver currently resolving this domain.
        // If there is one just wait for it and return, if there is none create a new one and wait for that one.
        private async Task <IP4Addr> AssignResolverAndResolve(ParsedDomain domain)
        {
            Task <IP4Addr> resolver;
            var            hasResolver = resolvers.TryGetValue(domain, out resolver);

            if (hasResolver)
            {
                if (resolver.Status == TaskStatus.RanToCompletion)
                {
                    try {
                        var reversed = new ParsedDomain(AsignReverserAndReverse(resolver.Result).Result);
                        if (domain.Equals(reversed))
                        {
                            // Reverse successful
                            return(resolver.Result);
                        }
                    } catch (DNSClientException) {
                        // Reverse unsucessful -> cache is out of date DNSClient.Reverse() did throw -> re-resolve
                    }
                    // Reverse unsucessful -> re-resolve this domain and update resolver.
                    resolvers.TryUpdate(domain, Resolve(domain), resolver);
                }
                else
                {
                    // Wait for the running task.
                    return(resolver.Result);
                }
            }

            resolver = resolvers.GetOrAdd(domain, Resolve(domain));
            return(resolver.Result);
        }
Ejemplo n.º 3
0
        private async Task <IP4Addr> Resolve(ParsedDomain domain)
        {
            if (domain.IsTopLevelDomain())
            {
                int     rootServerIndex = Interlocked.Increment(ref rootServerCounter) % dnsClient.GetRootServers().Count;
                IP4Addr rootServerIP    = dnsClient.GetRootServers()[rootServerIndex];
                return(dnsClient.Resolve(rootServerIP, domain.GetThisLevelSubdomain()).Result);
            }
            else
            {
                var upperLevelIP = await AssignResolverAndResolve(domain.GetUpperLevel());

                return(dnsClient.Resolve(upperLevelIP, domain.GetThisLevelSubdomain()).Result);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns the list of hosts for a domain.
        /// </summary>
        private async Task <List <NameCheapHostRecord> > GetHosts(ParsedDomain domain)
        {
            var xml = await InvokeGetApiAsync("domains.dns.getHosts", new Dictionary <string, string>
            {
                ["SLD"] = domain.SLD,
                ["TLD"] = domain.TLD
            });

            return(xml.Element(_ns + "CommandResponse")
                   .Descendants(_ns + "host")
                   .Select(x => new NameCheapHostRecord
            {
                Address = x.Attr <string>("Address"),
                Name = x.Attr <string>("Name"),
                Type = x.Attr <string>("Type"),
                HostId = x.Attr <int>("HostId"),
                MxPref = x.Attr <int>("MXPref"),
                Ttl = x.Attr <int>("TTL"),
            })
                   .ToList());
        }
Ejemplo n.º 5
0
            // Equals considers domains[0..index] range
            public override bool Equals(object obj)
            {
                Debug.Assert(index >= 0 && index < domains.Length);
                if (!(obj is ParsedDomain))
                {
                    return(false);
                }
                ParsedDomain other = (ParsedDomain)obj;

                if (index != other.index)
                {
                    return(false);
                }
                for (int i = index; i >= 0; --i)
                {
                    if (domains[i] != other.domains[i])
                    {
                        return(false);
                    }
                }
                return(true);
            }
Ejemplo n.º 6
0
        public async Task <IP4Addr> ResolveRecursive(string domain)
        {
            ParsedDomain parsedDomain = new ParsedDomain(domain);

            return(await AssignResolverAndResolve(parsedDomain));
        }