Esempio n. 1
0
        private async Task <Zone> GetHostedZone(IAuthorizedSyntax context, string recordName)
        {
            var page       = 0;
            var allZones   = new List <Zone>();
            var totalCount = int.MaxValue;

            while (allZones.Count < totalCount)
            {
                page++;
                var zonesResp = await context.Zones.List().PerPage(50).Page(page).ParseAsync(_hc).ConfigureAwait(false);

                if (!zonesResp.Success || zonesResp.ResultInfo.Count == 0)
                {
                    break;
                }
                totalCount = zonesResp.ResultInfo.TotalCount;
                allZones.AddRange(zonesResp.Unpack());
            }

            if (allZones.Count == 0)
            {
                _log.Error("No zones could be found using the Cloudflare API. " +
                           "Maybe you entered a wrong API Token?");
                throw new Exception();
            }
            var bestZone = FindBestMatch(allZones.ToDictionary(x => x.Name), recordName);

            if (bestZone == null)
            {
                _log.Error($"No zone could be found that matches with record {recordName}. " +
                           $"Maybe the API Token does not allow access to your domain?");
                throw new Exception();
            }
            return(bestZone);
        }
Esempio n. 2
0
        private async Task DeleteRecord(string recordName, string token, IAuthorizedSyntax context, Zone zone)
        {
            var dns     = context.Zone(zone).Dns;
            var records = await dns
                          .List()
                          .OfType(DnsRecordType.TXT)
                          .WithName(recordName)
                          .WithContent(token)
                          .Match(MatchType.All)
                          .CallAsync(_hc)
                          .ConfigureAwait(false);

            var record = records.FirstOrDefault();

            if (record == null)
            {
                _log.Warning($"The record {recordName} that should be deleted does not exist at Cloudflare.");
                return;
            }

            try
            {
                _ = await dns.Delete(record.Id)
                    .CallAsync(_hc)
                    .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _log.Warning($"Unable to delete record from Cloudflare: {ex.Message}");
            }
        }
Esempio n. 3
0
        private async Task <Zone> GetHostedZone(IAuthorizedSyntax context, string recordName)
        {
            var prs        = _domainParser;
            var domainName = $"{prs.GetRegisterableDomain(recordName)}";
            var zonesResp  = await context.Zones.List()
                             .WithName(domainName)
                             .ParseAsync(_hc)
                             .ConfigureAwait(false);

            if (!zonesResp.Success || (zonesResp.Result?.Count ?? 0) < 1)
            {
                _log.Error("Zone {domainName} could not be found using the Cloudflare API." +
                           " Maybe you entered a wrong API Token or domain or the API Token does" +
                           " not allow access to this domain?", domainName);
                throw new Exception();
            }
            return(zonesResp.Unpack().First());
        }
Esempio n. 4
0
        private async Task <Zone> GetHostedZone(IAuthorizedSyntax context, string recordName)
        {
            var prs        = _domainParser;
            var domainName = $"{prs.GetDomain(recordName)}.{prs.GetTLD(recordName)}";
            var zonesResp  = await context.Zones.List()
                             .WithName(domainName)
                             .ParseAsync(_hc)
                             .ConfigureAwait(false);

            if (!zonesResp.Success || (zonesResp.Result?.Count ?? 0) < 1)
            {
                _log.Error(
                    "Zone {domainName} could not be found using the Cloudflare API. Maybe you entered a wrong API Token or domain or the API Token does not allow access to this domain?",
                    domainName);
                // maybe throwing would be better
                // this is how the Azure DNS Validator works
                return(null);
            }

            return(zonesResp.Unpack().First());
        }
Esempio n. 5
0
        private async Task DeleteRecord(string recordName, string token, IAuthorizedSyntax context, Zone zone)
        {
            var dns     = context.Zone(zone).Dns;
            var records = await dns
                          .List()
                          .OfType(DnsRecordType.TXT)
                          .WithName(recordName)
                          .WithContent(token)
                          .Match(MatchType.All)
                          .CallAsync(_hc)
                          .ConfigureAwait(false);

            var record = records.FirstOrDefault();

            if (record != null)
            {
                await dns.Delete(record.Id)
                .CallAsync(_hc)
                .ConfigureAwait(false);
            }
        }
Esempio n. 6
0
        private async Task DeleteRecord(string recordName, string token, IAuthorizedSyntax context, Zone zone)
        {
            var dns     = context.Zone(zone).Dns;
            var records = await dns
                          .List()
                          .OfType(DnsRecordType.TXT)
                          .WithName(recordName)
                          .WithContent(token)
                          .Match(MatchType.All)
                          .CallAsync(_hc)
                          .ConfigureAwait(false);

            var record = records.FirstOrDefault();

            if (record == null)
            {
                throw new Exception($"The record {recordName} that should be deleted does not exist at Cloudflare.");
            }
            await dns.Delete(record.Id)
            .CallAsync(_hc)
            .ConfigureAwait(false);
        }
Esempio n. 7
0
        private async Task <Zone> GetHostedZone(IAuthorizedSyntax context, string recordName)
        {
            var zonesResp = await context.Zones.List()
                            .ParseAsync(_hc)
                            .ConfigureAwait(false);

            if (!zonesResp.Success || (zonesResp.Result?.Count ?? 0) < 1)
            {
                _log.Error("No zones could be found using the Cloudflare API. " +
                           "Maybe you entered a wrong API Token?");
                throw new Exception();
            }
            var allZones = zonesResp.Unpack();
            var bestZone = FindBestMatch(allZones.ToDictionary(x => x.Name), recordName);

            if (bestZone == null)
            {
                _log.Error($"No zone could be found that matches with record {recordName}. " +
                           $"Maybe the API Token does not allow access to your domain?");
                throw new Exception();
            }
            return(bestZone);
        }