Beispiel #1
0
        private bool UpdateCloudflareDns(IpSupport protocol)
        {
            var publicIpAddress = GetPublicIpAddress(protocol);

            if (publicIpAddress == null)
            {
                AppendStatusTextThreadSafe($"Error detecting public {protocol} 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} changed from {oldPublicIpAddress} to {publicIpAddress}");
                }

                DisplayPublicIpAddressThreadSafe(protocol, publicIpAddress);

                var typesToUpdateForThisProtocol = new List <string> {
                    "SPF",
                    "TXT",
                    protocol == IpSupport.IPv4 ? "A" : "AAAA"
                };

                // Get requested entries to update
                List <Dns.Result> potentialEntriesToUpdate = null;
                try
                {
                    var allRecordsByZone = cfClient.GetAllDnsRecordsByZone();

                    potentialEntriesToUpdate = allRecordsByZone.Where(d => settings.SelectedDomains.Any(s =>
                                                                                                        s.ZoneName == d.zone_name &&
                                                                                                        s.DnsName == d.name &&
                                                                                                        s.Type == d.type) &&
                                                                      typesToUpdateForThisProtocol.Contains(d.type)).ToList();
                }
                catch (Exception ex)
                {
                    AppendStatusTextThreadSafe($"Error getting DNS records");
                    AppendStatusTextThreadSafe(ex.Message);
                }

                // TODO:determine which ones need updating

                if (potentialEntriesToUpdate == null || !potentialEntriesToUpdate.Any())
                {
                    return(false);
                }

                foreach (var entry in potentialEntriesToUpdate)
                {
                    string content;
                    if (entry.type == "SPF" || entry.type == "TXT")
                    {
                        content = UpdateContent(protocol, entry.content, publicIpAddress);
                    }
                    else
                    {
                        content = publicIpAddress;
                    }

                    if (entry.content == content)
                    {
                        continue;
                    }

                    try
                    {
                        cfClient.UpdateDns(protocol, entry.zone_id, entry.id, entry.type, entry.name, content, entry.proxied);
                        txtOutput.Invoke((MethodInvoker) delegate
                        {
                            AppendStatusTextThreadSafe($"Updated {entry.type} record [{entry.name}] in zone [{entry.zone_name}] to {content}");
                        });
                    }
                    catch (Exception ex)
                    {
                        AppendStatusTextThreadSafe($"Error updating [{entry.type}] record [{entry.name}] in zone [{entry.zone_name}] to {content}");
                        AppendStatusTextThreadSafe(ex.Message);
                    }
                }
                return(true);
            }

            return(false);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        void DoUpdate()
        {
            if (!PreflightSettingsCheck())
            {
                return;
            }

            var publicIpAddress = GetPublicIpAddress();

            if (publicIpAddress == null)
            {
                AppendStatusTextThreadSafe($"Error detecting public IP address");
                return;
            }

            var oldPublicIpAddress = settings.PublicIpAddress;

            if (publicIpAddress != oldPublicIpAddress)
            {
                settings.PublicIpAddress = publicIpAddress;
                settings.Save();

                if (oldPublicIpAddress != null)
                {
                    AppendStatusTextThreadSafe($"Public IP changed from {oldPublicIpAddress} to {publicIpAddress}");
                }

                DisplayPublicIpAddressThreadSafe(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);
                }

                if (entriesToUpdate == null)
                {
                    return;
                }

                foreach (var entry in entriesToUpdate)
                {
                    try
                    {
                        cfClient.UpdateDns(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);
                    }
                }

                // fetch and update listview with current status of records
                UpdateList();
            }
        }