using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.Net; using System.Net.Http; using System.IO; using System.Diagnostics; using System.Threading; using System.Web; namespace IPMonitor { class Program { // Main public static void UpdateIPAddress(String ipAddress) { try { String strPathFileName="ipaddress.txt"; if(!File.Exists(strPathFileName)) { WriteFile(strPathFileName, ipAddress); ZoneEditResponses zoneEditResponses=UpdateZoneEditRecord(ipAddress); SendSMSEmail(String.Format("IPMonitor IPAddress {0}. ZoneEditUpdated={1}",ipAddress,zoneEditResponses.IsSuccess())); } else { String currentIPAddress=ReadFile(strPathFileName); if(null!=currentIPAddress && !currentIPAddress.Equals(ipAddress)) { WriteFile(strPathFileName,ipAddress); ZoneEditResponses zoneEditResponses=UpdateZoneEditRecord(ipAddress); SendSMSEmail(String.Format("IPMonitor IPAddress {0}. ZoneEditUpdated={1}",ipAddress,zoneEditResponses.IsSuccess())); } else if(null==currentIPAddress) { SendSMSEmail("IPMonitor "+ipAddress+ ". IPMonitor encountered an issue reading the IPAddress file."); } } } catch(Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::UpdateIPAddress] Exception:{0}",exception.ToString())); } } public static String GetPublicIPAddress() { int MAX_RETRIES=5; int TIMEOUT_BETWEEN_ATTEMPTS=30000; String request="http://checkip.dyndns.org/"; try { String address = null; for(int index=0;index"); address = address.Substring(first, last - first); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Request succeeded: {0} -> {1}",request,address)); return address; } catch(Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception parsing address: {0}",exception.ToString())); return null; } } catch(Exception exception) { String message = String.Format("[IPMonitor::GetPublicIPAddress] Exception:{0}",exception.ToString()); MDTrace.WriteLine(LogLevel.DEBUG,message); SendSMSEmail("IPMonitor encountered an issue retrieving."); return null; } } public static String GetHttpRequest(String strRequest) { try { WebRequest request = WebRequest.Create(strRequest); using (WebResponse response = request.GetResponse()) using (StreamReader stream = new StreamReader(response.GetResponseStream())) { return stream.ReadToEnd(); } } catch(Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor:GetHttpRequest] failed with {0}",exception.ToString())); return null; } } public static ZoneEditResponses UpdateZoneEditRecord(String ipAddress) { HttpClient client = new HttpClient(); String user=ConfigurationManager.AppSettings["zoneedit_user"]; String password=ConfigurationManager.AppSettings["zoneedit_password"]; String host=ConfigurationManager.AppSettings["zoneedit_host"]; String strRequest=null; StringBuilder sb=new StringBuilder(); HttpNetResponse httpNetResponse=null; sb = new StringBuilder(); sb.Append("https://"); sb.Append("dynamic.zoneedit.com/auth/dynamic.html"); sb.Append("?host=").Append(HttpUtility.UrlEncode(host)); sb.Append("&dnsto=").Append(HttpUtility.UrlEncode(ipAddress)); strRequest=sb.ToString(); httpNetResponse = HttpNetRequest.GetRequestNoEncodingV5C(strRequest,user,password); if(!httpNetResponse.Success) { return new ZoneEditResponses(); } MDTrace.WriteLine(LogLevel.DEBUG,String.Format("{0}",httpNetResponse.ResponseString)); return new ZoneEditResponses(httpNetResponse.ResponseString); } public static void WriteFile(String strPathFileName,String ipAddress) { try { if(File.Exists(strPathFileName))File.Delete(strPathFileName); FileStream fileStream=new FileStream(strPathFileName,FileMode.Create,FileAccess.Write,FileShare.Read); StreamWriter streamWriter=new StreamWriter(fileStream); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Creating address file:{0}",strPathFileName)); streamWriter.WriteLine(ipAddress); streamWriter.Flush(); streamWriter.Close(); streamWriter.Dispose(); } catch(Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::WriteFile] Exception:{0}",exception.ToString())); return; } } public static String ReadFile(String strPathFileName) { try { FileStream fileStream=new FileStream(strPathFileName,FileMode.Open,FileAccess.ReadWrite,FileShare.Read); StreamReader streamReader=new StreamReader(fileStream); String item=null; item=streamReader.ReadLine(); streamReader.Close(); streamReader.Dispose(); return item; } catch(Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::ReadFile] Exception:{0}",exception.ToString())); return null; } } public static void SendSMSEmail(string message) { String smsSMTPAddress = ConfigurationManager.AppSettings["sms_smtpaddress"]; String smsUserName = ConfigurationManager.AppSettings["sms_smsusername"]; String smsPassword = ConfigurationManager.AppSettings["sms_smspassword"]; String[] smsRecipients = ConfigurationManager.AppSettings["sms_smsrecipients"].Split(','); SMSClient.SendSMSEmail(message, smsUserName, smsRecipients, smsSMTPAddress, smsUserName, smsPassword); } static void Main(string[] args) { int interval_ms = int.Parse(ConfigurationManager.AppSettings["poll_time_ms"]); int notify_ms = int.Parse(ConfigurationManager.AppSettings["notify_time_ms"]); try { MDTrace.LogLevel = LogLevel.DEBUG; String strLogFile = "ipmonitor.log"; Utility.CopyFile(strLogFile,Utility.DateTimeToStringYYYYMMDDMMSSTT(DateTime.Now)+strLogFile); Utility.DeleteFile(strLogFile); Trace.Listeners.Add(new TextWriterTraceListener(strLogFile)); Profiler profiler=new Profiler(); profiler.Start(); MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::Start] {0}",Directory.GetCurrentDirectory())); while(true) { String ipAddress=GetPublicIPAddress(); UpdateIPAddress(ipAddress); if(profiler.Split()>=notify_ms) { SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress)); profiler.Start(); } try{Thread.Sleep(interval_ms);}catch{;} } } catch(Exception exception) { MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor] Exception:{0}",exception.ToString())); } } } }