예제 #1
0
        public async Task <string> GetHostZoneId()
        {
            var account = AwsCommon.GetEnvironmentAccounts()[environment];
            var request = new ListHostedZonesByNameRequest()
            {
                DNSName = account.DNS + "."
            };
            var response = await client.ListHostedZonesByNameAsync(request);

            return(response.HostedZones.Find(o => o.Name == account.DNS + ".").Id);
        }
예제 #2
0
        public async Task <string> GetHostedZoneIdByName(string hostedZoneName)
        {
            using (AmazonRoute53Client route53Client = GetAmazonRoute53Client())
            {
                ListHostedZonesByNameResponse zones = await route53Client.ListHostedZonesByNameAsync(new ListHostedZonesByNameRequest()
                {
                    DNSName = hostedZoneName
                });

                HostedZone matchingZone = zones?.HostedZones.FirstOrDefault(zone => zone.Name == hostedZoneName);
                return(matchingZone?.Id);
            }
        }
예제 #3
0
        private static string FindHostedZoneID(string domain)
        {
            string ret = null;
            var    listHostedZonesRequest = new ListHostedZonesByNameRequest
            {
                DNSName = domain
            };
            var listHostedZonesResponse = c.ListHostedZonesByNameAsync(listHostedZonesRequest).Result;

            if (listHostedZonesResponse.HostedZones.Count > 0)
            {
                ret = listHostedZonesResponse.HostedZones[0].Id;
            }
            return(ret);
        }
예제 #4
0
        private async static Task <string> GetHostedZoneIdByName(string hostedZoneName)
        {
            AmazonRoute53Config config = new AmazonRoute53Config()
            {
                RegionEndpoint = RegionEndpoint.GetBySystemName(Settings.AWSRegion) // TODO: inject
            };

            using (AmazonRoute53Client route53Client = new AmazonRoute53Client(Settings.AWSAccessKeyId, Settings.AWSAccessKeySecret, config))
            {
                ListHostedZonesByNameResponse zones = await route53Client.ListHostedZonesByNameAsync(new ListHostedZonesByNameRequest()
                {
                    DNSName = hostedZoneName
                });

                HostedZone matchingZone = zones?.HostedZones.FirstOrDefault(zone => zone.Name == hostedZoneName);
                return(matchingZone?.Id);
            }
        }
        /// <summary>
        /// This method does all of the work to update the DNS records with our current WAN IP. This is separated from the main
        /// method to make it easier in the future to perform batch updates (if needed).
        /// </summary>
        /// <param name="id">The AWS Access key ID</param>
        /// <param name="secret">The AWS Access key secret</param>
        /// <param name="domain">The domain name we are going to be updating records for (e.g. shawnlehner.com)</param>
        /// <param name="subdomain">The subdomain we would like to update (e.g. "house" if we were updating house.shawnlehner.com)</param>
        private static async Task PerformDnsUpdate(string id, string secret, string domain, string subdomain)
        {
            #region Lookup Current WAN IP

            // We use ipify.org to quickly/easily get our external WAN IP address which we can later use
            // for updating our DNS records.

            string ip = null;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.ipify.org");
            using (Stream s = (await request.GetResponseAsync()).GetResponseStream())
                using (StreamReader r = new StreamReader(s))
                {
                    ip = r.ReadToEnd();
                }

            #endregion

            // Combine our domain and subdomain to get the full record name
            string recordName = subdomain.Trim('.') + "." + domain.Trim('.');

            // Create our AWS API client for sending requests to the Route53 API
            AmazonRoute53Client client = new AmazonRoute53Client(id, secret, RegionEndpoint.USEast1);

            #region Lookup current state of the domain on Route53

            // Lookup the zone for our domain
            HostedZone zone = (await client.ListHostedZonesByNameAsync(new ListHostedZonesByNameRequest
            {
                DNSName = domain
            })).HostedZones.First();

            // Lookup our current records to see if we need to make an update
            ListResourceRecordSetsResponse recordSet = await client.ListResourceRecordSetsAsync(new ListResourceRecordSetsRequest
            {
                HostedZoneId    = zone.Id,
                MaxItems        = "1",
                StartRecordName = recordName
            });

            #endregion

            // Check to see if our IP is already up to date. No sense making a change if we don't need to.
            if (recordSet.ResourceRecordSets.Count > 0 &&
                recordSet.ResourceRecordSets[0].Name.Trim('.') == recordName &&
                recordSet.ResourceRecordSets[0].ResourceRecords[0].Value == ip)
            {
                return;
            }

            #region Request DNS record update with new IP

            // Our IP address is not up-to-date so we need to make a change request. We use UPSERT action which
            // will work whether or not a record already exists.
            ChangeResourceRecordSetsRequest changeRequest = new ChangeResourceRecordSetsRequest
            {
                HostedZoneId = zone.Id,
                ChangeBatch  = new ChangeBatch
                {
                    Changes = new List <Change>
                    {
                        new Change
                        {
                            ResourceRecordSet = new ResourceRecordSet
                            {
                                Name            = recordName,
                                TTL             = 60,
                                Type            = RRType.A,
                                ResourceRecords = new List <ResourceRecord> {
                                    new ResourceRecord {
                                        Value = ip
                                    }
                                }
                            },
                            Action = ChangeAction.UPSERT
                        }
                    }
                }
            };

            // Send our change request to the API
            ChangeResourceRecordSetsResponse response =
                await client.ChangeResourceRecordSetsAsync(changeRequest);

            // Check our response code to verify everything worked
            if (response.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new Exception("API request to update DNS record has failed.");
            }

            #endregion
        }