string GetPublicIpAddress(IpSupport protocol) { string errorMesssage; var publicIpAddress = Utility.GetPublicIpAddress(protocol, httpClient, out errorMesssage); // Abort if we get an error, keeping the current address in settings if (publicIpAddress == null) { AppendStatusTextThreadSafe($"Error getting public {protocol.ToString()}: {errorMesssage}"); return(null); } if ((protocol == IpSupport.IPv4 && txtPublicIpv4.Text != publicIpAddress) || (protocol == IpSupport.IPv6 && txtPublicIpv6.Text != publicIpAddress)) { DisplayPublicIpAddressThreadSafe(protocol, publicIpAddress); } return(publicIpAddress); }
// Ref: https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record public DnsUpdateResponse UpdateDns(IpSupport protocol, string zoneIdentifier, string dnsRecordIdentifier, string dnsRecordName, string content, bool proxied) { var recordType = protocol == IpSupport.IPv4 ? "A" : "AAAA"; var dnsUpdateRequest = new DnsUpdateRequest() { type = recordType, name = dnsRecordName, content = content, proxied = proxied }; HttpResponseMessage response; HttpRequestMessage req = GetRequestMessage(HttpMethod.Put, $"zones/{zoneIdentifier}/dns_records/{dnsRecordIdentifier}"); req.Content = new StringContent(JsonConvert.SerializeObject(dnsUpdateRequest), Encoding.UTF8, "application/json"); response = Client.SendAsync(req).Result; var result = response.Content.ReadAsStringAsync().Result; ValidateCloudflareResult(response, result, $"update {protocol.ToString()} DNS"); var ret = JsonConvert.DeserializeObject <DnsUpdateResponse>(result); return(ret); }
private bool UpdateCloudflareDns(IpSupport protocol) { var publicIpAddress = GetPublicIpAddress(protocol); if (publicIpAddress == null) { AppendStatusTextThreadSafe($"Error detecting public {protocol.ToString()} address"); return(false); } var oldPublicIpAddress = protocol == IpSupport.IPv4 ? settings.PublicIpv4Address : settings.PublicIpv6Address; if (publicIpAddress != oldPublicIpAddress) { if (protocol == IpSupport.IPv4) { settings.PublicIpv4Address = publicIpAddress; } else { settings.PublicIpv6Address = publicIpAddress; } settings.Save(); if (oldPublicIpAddress != null) { AppendStatusTextThreadSafe($"Public {protocol.ToString()} changed from {oldPublicIpAddress} to {publicIpAddress}"); } DisplayPublicIpAddressThreadSafe(protocol, publicIpAddress); // loop through DNS entries and update the ones selected that have a different IP List <Dns.Result> entriesToUpdate = null; try { entriesToUpdate = cfClient.GetAllDnsRecordsByZone().Where(d => settings.SelectedDomains .Any(s => s.ZoneName == d.zone_name && s.DnsName == d.name && d.content != publicIpAddress)).ToList(); } catch (Exception ex) { AppendStatusTextThreadSafe($"Error getting DNS records"); AppendStatusTextThreadSafe(ex.Message); tc.TrackException(ex); } if (entriesToUpdate == null) { return(false); } foreach (var entry in entriesToUpdate) { try { cfClient.UpdateDns(protocol, entry.zone_id, entry.id, entry.name, publicIpAddress, entry.proxied); txtOutput.Invoke((MethodInvoker) delegate { AppendStatusTextThreadSafe($"Updated name [{entry.name}] in zone [{entry.zone_name}] to {publicIpAddress}"); }); } catch (Exception ex) { AppendStatusTextThreadSafe($"Error updating [{entry.name}] in zone [{entry.zone_name}] to {publicIpAddress}"); AppendStatusTextThreadSafe(ex.Message); tc.TrackException(ex); } } return(true); } return(false); }