private static void UpdateRecordIfNeeded(RecordUpdateRequest rur) { try { // Is the update request enabled? if (rur.Enabled) { // Yes, so validate first rur.Validate(); // Check if it is time to update... bool bUpdate = true; if (rur.LastUpdated.HasValue) { TimeSpan ts = DateTime.Now.Subtract(rur.LastUpdated.Value); bUpdate = ts.TotalMinutes >= rur.UpdateFrequencyMinutes; } // Update? if (bUpdate) { string pwd = null; string tok = null; if (rur.IsApiToken) { tok = rur.Password; } else { pwd = rur.Password; } DNSimple.DNSimpleRestClient c = new DNSimpleRestClient(rur.EmailAddress, pwd, tok); rur.RecordContent = GetPublicIP(); dynamic ret = c.UpdateRecord(rur.Domain, rur.RecordId, rur.RecordName, rur.RecordContent); rur.LastUpdated = DateTime.Now; } } } catch (Exception ex) { HandleException(ex); } }
public static Task <IEnumerable <DnsimpleRecord> > LoadDnsimpleRecords(Config config, string fqDomainName) { return(Task.Run(() => { var domainName = DeriveBaseDomainName(fqDomainName); Log.Trace("Loading DNS records for '{0}' (for '{1}').", domainName, fqDomainName); var token = UnprotectSecret(config.dnsimpleApiToken, config.randomString); var client = new DNSimpleRestClient(config.dnsimpleUsername, token: token); var records = client.ListRecords(domainName); int recordCount; try { recordCount = (int)records.Length; } catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) { // Length property does not exist: probably because of a failure. throw new Exception("Error when loading Dnsimple records: " + records.message); } Log.Debug("Loaded {0:N0} DNS record(s) for '{1}' (for '{2}').", recordCount, domainName, fqDomainName); return ((ExpandoObject[])records).Select(x => new DnsimpleRecord(x)).ToList().AsEnumerable(); })); }
public static Task UpdateDnsimpleRecord(Config config, KeyValuePair <string, DnsName> domain, IEnumerable <DnsimpleRecord> records, IEnumerable <IPAddress> addresses) { return(Task.Run(() => { var token = UnprotectSecret(config.dnsimpleApiToken, config.randomString); var client = new DNSimpleRestClient(config.dnsimpleUsername, token: token); var fqDomainName = domain.Key; var domainName = DeriveBaseDomainName(domain.Key); var subName = DeriveSubDomainName(domain.Key); // It is possible for a computer to have multiple IP addresses (much more common with IPv6, but with multiple NICs, quite possible with IPv4 as well). // So we maintain them in multiple records. // When a record changes, a new record is added and the old one removed (no in-place updates). Log.Trace("Checking addresses for '{0}'...", fqDomainName); foreach (var addrGrp in addresses.GroupBy(x => x.AddressFamily)) { var v4orv6 = addrGrp.Key == System.Net.Sockets.AddressFamily.InterNetwork ? "IPv4" : "IPv6"; var recordType = addrGrp.Key == System.Net.Sockets.AddressFamily.InterNetwork ? "A" : "AAAA"; var ttl = addrGrp.Key == System.Net.Sockets.AddressFamily.InterNetwork ? domain.Value.ttl_ipv4.GetValueOrDefault(_DefaultTTL) : domain.Value.ttl_ipv6.GetValueOrDefault(_DefaultTTL); Log.Trace("Checking {0} addresses to add for '{1}'...", v4orv6, fqDomainName); foreach (var address in addrGrp) { // Check address. var record = records.FirstOrDefault(x => x.record_type == recordType && x.ContentAsIpAddress().Equals(address) && String.Equals(x.name, subName, StringComparison.InvariantCultureIgnoreCase)); if (record == null) { // A new address. Log.Debug("Adding '{0}' record for '{1}' (domain '{2}'): {3}.", recordType, fqDomainName, domainName, address); client.AddRecord(domainName, subName, recordType, address.ToString(), ttl: ttl); Log.Info("New {0} address detected for '{1}' - added '{2}' OK.", v4orv6, fqDomainName, address); } else { // Address has not changed: no action required. Log.Trace("Address '{0}' is correct in DNS: unchanged.", address); } } // Now, check for addresses which need to be removed. Log.Trace("Checking for {0} addresses to remove for '{1}'...", v4orv6, fqDomainName); foreach (var record in records.Where(x => x.record_type == recordType && String.Equals(x.name, subName, StringComparison.InvariantCultureIgnoreCase))) { if (!addrGrp.Contains(record.ContentAsIpAddress())) { // Address has changed / been released and needs to be removed. Log.Debug("Deleting stale '{0}' record for '{1}' (domain '{2}'): {3}.", recordType, fqDomainName, domainName, record.ContentAsIpAddress()); client.DeleteRecord(domainName, record.id); Log.Info("Stale {0} address detected for '{1}' - removed '{2}' OK.", v4orv6, fqDomainName, record.ContentAsIpAddress()); } else { // Address remains: no action required. Log.Trace("Address '{0}' is correct in DNS: unchanged.", record.ContentAsIpAddress()); } } } })); }
public DnsimpleClient(string username, ApiToken token) { client = new DNSimpleRestClient(username, token); }
public DnsimpleClient(string username, string password) { client = new DNSimpleRestClient(username, password); }