public static void SetUpTimer(bool retry) { if (!_alreadyStarted || retry) { // Load values FROM THE LATEST PREVIOUS VERSION inside the current settings. // Upgrading after saving will STILL get the PREVIOUS VERSION's settings // TODO: Detect if version changed before calling upgrade. var settingsIpv6 = Properties.Settings.Default.LastIPv6; Log.WriteLine("IPv6 from settings:" + settingsIpv6); var settingsIpv4 = Properties.Settings.Default.LastIPv4; Log.WriteLine("IPv4 from settings:" + settingsIpv4); if (!_alreadyStarted && string.IsNullOrEmpty(settingsIpv4) && string.IsNullOrEmpty(settingsIpv6)) { // No settings, try to upgrade. Properties.Settings.Default.Upgrade(); // Try again _alreadyStarted = true; SetUpTimer(true); } else { if (settingsIpv6 != null && System.Net.IPAddress.TryParse(settingsIpv6, out System.Net.IPAddress v6)) { CachedValues.SetCachedExternalIpAddressv6(v6); CachedValues.SetPreviousExternalIpAddressv6(v6); } if (settingsIpv4 != null && System.Net.IPAddress.TryParse(settingsIpv4, out System.Net.IPAddress v4)) { CachedValues.SetCachedExternalIpAddressv4(v4); CachedValues.SetPreviousExternalIpAddressv4(v4); } CheckAndSendEmail(); System.Timers.Timer aTimer = new System.Timers.Timer(60 * 60 * 1000); //one hour in milliseconds aTimer.Elapsed += OnTimedIpUpdaterEvent; _alreadyStarted = true; } } }
private static bool CheckIpChange(/*bool forced = false*/) { Debug.WriteLine("Called CheckIPChange()"); IEnumerable <IPAddress> cachedIps = CachedValues.GetCachedIPs(); System.Collections.Generic.HashSet <System.Net.IPAddress> newIps = GetNewPublicIPs(); if (newIps == null) { Log.WriteLine("IP Fetcher returned null! Woah! We don't have offline mode right now so something is very wrong!"); return(false); } var changed = !newIps.SetEquals(cachedIps); // Clear out cached IPs, otherwise we would return invalid IPs if we lose either (stack disabled, etc) if (changed) { changed = false; if (CachedValues.GetPreviousExternalIpAddressv6() != null) { Log.WriteLine("Previous IPv6: " + CachedValues.GetPreviousExternalIpAddressv6()); } if (CachedValues.GetPreviousExternalIpAddressv4() != null) { Log.WriteLine("Previous IPv4: " + CachedValues.GetPreviousExternalIpAddressv4()); } CachedValues.SetPreviousExternalIpAddressv6(CachedValues.GetCachedExternalIpAddressv6()); CachedValues.SetPreviousExternalIpAddressv4(CachedValues.GetCachedExternalIpAddressv4()); CachedValues.SetCachedExternalIpAddressv6(null); CachedValues.SetCachedExternalIpAddressv4(null); Log.WriteLine("Fetched IPs:" + string.Join(",", newIps)); foreach (System.Net.IPAddress address in newIps) { switch (address.AddressFamily) { case System.Net.Sockets.AddressFamily.InterNetworkV6: // we have IPv6 if (CachedValues.GetPreviousExternalIpAddressv6() != null) { if (!address.Equals(CachedValues.GetPreviousExternalIpAddressv6())) { changed = true; Log.WriteLine("IPv6 Changed (" + CachedValues.GetPreviousExternalIpAddressv6() + " => " + address + ")"); } } else { changed = true; } CachedValues.SetCachedExternalIpAddressv6(address); break; case System.Net.Sockets.AddressFamily.InterNetwork: // we have IPv4 if (CachedValues.GetPreviousExternalIpAddressv4() != null) { if (!address.Equals(CachedValues.GetPreviousExternalIpAddressv4())) { changed = true; Log.WriteLine("IPv4 Changed (" + CachedValues.GetPreviousExternalIpAddressv4() + " => " + address + ")"); } } else { changed = true; } CachedValues.SetCachedExternalIpAddressv4(address); break; default: // Do nothing. Security wants this. throw new NotSupportedException(); } } } if (changed) { Properties.Settings.Default.LastIPv4 = CachedValues.GetCachedExternalIpAddressv4()?.ToString(); Properties.Settings.Default.LastIPv6 = CachedValues.GetCachedExternalIpAddressv6()?.ToString(); Properties.Settings.Default.Save(); } return(changed); }