/// <summary> /// Save Settings. /// </summary> internal static void SaveSettings() { // Core settings. Properties.Settings.Default.ServiceFilePath = ServiceFilePath; Properties.Settings.Default.LogFilePath = LogFilePath; Properties.Settings.Default.CurrentExternalIp = CurrentExternalIp.ToString(); Properties.Settings.Default.WatchInterval = WatchInterval; // Save settings. Properties.Settings.Default.Save(); }
/// <summary> /// Process Ip. /// </summary> /// <param name="source"></param> /// <param name="e"></param> private static void ProcessIp(object source, ElapsedEventArgs e) { try { // Send any unsent mail. for (var i = 0; i < MailMessages.Count; i++) { var message = MailMessages[i]; if (MailModule.Send(MailModule.SmtpHost, MailModule.SmtpPort, message, MailModule.EnableSsl).Count == 0) { MailMessages.Remove(message); } } // Create a web client to gather data. using (var wc = new WebClient()) { // Call out to the Service Host for the external ip being used by the current network. WebResponse = Support.GetJsonContent <WebResponse>(wc.DownloadString(MailModule.ServiceHost)); // If the response is null or empty send an email to the admin(s) to check for errors. if (WebResponse == null) { File.AppendAllText(LogFilePath + $@"{ DateTime.Now:(yyyy-MM-dd)}.log", $@"{DateTime.Now} - An error occured: The web response from {MailModule.ServiceHost} was '{null}'.{Environment.NewLine}{Environment.NewLine}"); // Prepare mail message to send. var message = new MailMessage(MailModule.EmailFrom, MailModule.EmailTo, "What Is My Ip - Error!", $"External IP Web Response was '{null}'."); // Send mail. // Flag service to resend if an error occured when sending mail. ResendToAddresses = MailModule.Send(MailModule.SmtpHost, MailModule.SmtpPort, message, MailModule.EnableSsl); // Queue previously unsent mail. if (ResendToAddresses.Count > 0) { MailMessages.Add(message); } else { // Write to logs. File.AppendAllText(LogFilePath + $@"{DateTime.Now:(yyyy-MM-dd)}.log", $@"{DateTime.Now} - Email sent out to: {MailModule.EmailTo}{Environment.NewLine}"); } foreach (var address in ResendToAddresses) { File.AppendAllText(LogFilePath + $@"{DateTime.Now:(yyyy-MM-dd)}.log", $@"{DateTime.Now} - Email failed to send out to: {address}{Environment.NewLine}"); } // Clear Addresses. ResendToAddresses.Clear(); } } // Check for a response. if (WebResponse != null) { // If current ip is null, which should not happen unless the registry entry is not set properly, // then parse the web response and update the registry. if (CurrentExternalIp == null) { CurrentExternalIp = NewExternalIpAddress = IPAddress.Parse(WebResponse.IpAddress); // Push to registry. UpdateCurrentExternalIpRegistryEntry(); // TODO: Saving in not working. //SaveSettings(); } else { NewExternalIpAddress = IPAddress.Parse(WebResponse.IpAddress); } // Process new ip, if changed. if (!CurrentExternalIp.Equals(NewExternalIpAddress)) { CurrentExternalIp = NewExternalIpAddress; // Push to registry. UpdateCurrentExternalIpRegistryEntry(); // TODO: Saving in not working. //SaveSettings(); if (ModulesController.IsIisEnabled) { // Update IIS FTP firewall settings. IISModule.SetFtpExternalFirewallIp(NewExternalIpAddress); } // Prepare mail message to send. var message = new MailMessage(MailModule.EmailFrom, MailModule.EmailTo, "What Is My Ip - IP Address Change!", $"External IP changed to: {WebResponse.IpAddress}"); // Send Mail. // Flag service to resend if an error occured when sending mail. ResendToAddresses = MailModule.Send(MailModule.SmtpHost, MailModule.SmtpPort, message, MailModule.EnableSsl); // Queue previously unsent mail. if (ResendToAddresses.Count > 0) { MailMessages.Add(message); } else { // Write to logs. File.AppendAllText(LogFilePath + $@"{DateTime.Now:(yyyy-MM-dd)}.log", $@"{DateTime.Now} - Email sent out to: {MailModule.EmailTo}{Environment.NewLine}"); } foreach (var address in ResendToAddresses) { File.AppendAllText(LogFilePath + $@"{DateTime.Now:(yyyy-MM-dd)}.log", $@"{DateTime.Now} - Email failed to send out to: {address}{Environment.NewLine}"); } // Clear Addresses. ResendToAddresses.Clear(); } WebResponse = null; } } catch (Exception ex) { // Output exception details. if (!string.IsNullOrWhiteSpace(LogFilePath)) { File.AppendAllText(LogFilePath + $@"{ DateTime.Now:(yyyy-MM-dd)}.log", $@"{DateTime.Now} - An error occured: {ex}{Environment.NewLine}{Environment.NewLine}"); } else { throw; } } }