diff --git a/App.config b/App.config index ee8d2ae..8c62091 100644 --- a/App.config +++ b/App.config @@ -2,7 +2,7 @@ - + diff --git a/IPMonitor.csproj b/IPMonitor.csproj index 79dc059..fd955d1 100644 --- a/IPMonitor.csproj +++ b/IPMonitor.csproj @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + false AnyCPU diff --git a/IPMonitor.v12.suo b/IPMonitor.v12.suo index b67d609..5c42ff7 100644 Binary files a/IPMonitor.v12.suo and b/IPMonitor.v12.suo differ diff --git a/Program.cs b/Program.cs index 91690ff..a4d20ab 100644 --- a/Program.cs +++ b/Program.cs @@ -13,54 +13,133 @@ namespace IPMonitor { class Program { + //public static String GetPublicIPAddress() + //{ + // try + // { + // String address = ""; + // WebRequest request = WebRequest.Create("http://checkip.dyndns.org/"); + // using (WebResponse response = request.GetResponse()) + // using (StreamReader stream = new StreamReader(response.GetResponseStream())) + // { + // address = stream.ReadToEnd(); + // } + + // int first = address.IndexOf("Address: ") + 9; + // int last = address.LastIndexOf(""); + // address = address.Substring(first, last - first); + + // return address; + // } + // 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 GetPublicIPAddress() { - String address = ""; - WebRequest request = WebRequest.Create("http://checkip.dyndns.org/"); - using (WebResponse response = request.GetResponse()) - using (StreamReader stream = new StreamReader(response.GetResponseStream())) - { - address = stream.ReadToEnd(); - } + int MAX_RETRIES=5; + int TIMEOUT_BETWEEN_ATTEMPTS=30000; + try + { + String address = null; + String request="http://checkip.dyndns.org/"; + + for(int index=0;index"); + address = address.Substring(first, last - first); - int first = address.IndexOf("Address: ") + 9; - int last = address.LastIndexOf(""); - address = address.Substring(first, last - first); - - return address; + return address; + } + 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 void UpdateIPAddress(String ipAddress) { - - - String strPathFileName="ipaddress.txt"; - if(!File.Exists(strPathFileName)) + try { - WriteFile(strPathFileName, ipAddress); - SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress)); - } - else - { - String currentIPAddress=ReadFile(strPathFileName); - if(null!=currentIPAddress && !currentIPAddress.Equals(ipAddress)) + String strPathFileName="ipaddress.txt"; + if(!File.Exists(strPathFileName)) { - WriteFile(strPathFileName,ipAddress); + WriteFile(strPathFileName, ipAddress); SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress)); } + else + { + String currentIPAddress=ReadFile(strPathFileName); + if(null!=currentIPAddress && !currentIPAddress.Equals(ipAddress)) + { + WriteFile(strPathFileName,ipAddress); + SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress)); + } + 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 void WriteFile(String strPathFileName,String ipAddress) { - 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(); + 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) @@ -106,16 +185,17 @@ namespace IPMonitor Profiler profiler=new Profiler(); profiler.Start(); + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::Start] {0}",Directory.GetCurrentDirectory())); while(true) { String ipAddress=GetPublicIPAddress(); UpdateIPAddress(ipAddress); - try{Thread.Sleep(interval_ms);}catch{;} if(profiler.Split()>=notify_ms) { SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress)); profiler.Start(); } + try{Thread.Sleep(interval_ms);}catch{;} } } catch(Exception exception) diff --git a/bin/Debug/IPMonitor.exe b/bin/Debug/IPMonitor.exe index 1dc6c13..21afd64 100644 Binary files a/bin/Debug/IPMonitor.exe and b/bin/Debug/IPMonitor.exe differ diff --git a/bin/Debug/IPMonitor.exe.config b/bin/Debug/IPMonitor.exe.config index ee8d2ae..8c62091 100644 --- a/bin/Debug/IPMonitor.exe.config +++ b/bin/Debug/IPMonitor.exe.config @@ -2,7 +2,7 @@ - + diff --git a/bin/Debug/IPMonitor.pdb b/bin/Debug/IPMonitor.pdb index 70502bf..87056e4 100644 Binary files a/bin/Debug/IPMonitor.pdb and b/bin/Debug/IPMonitor.pdb differ diff --git a/bin/Debug/IPMonitor.vshost.exe b/bin/Debug/IPMonitor.vshost.exe index c0dfecc..f443805 100644 Binary files a/bin/Debug/IPMonitor.vshost.exe and b/bin/Debug/IPMonitor.vshost.exe differ diff --git a/bin/Release/IPMonitor.exe b/bin/Release/IPMonitor.exe index 1dc6c13..21afd64 100644 Binary files a/bin/Release/IPMonitor.exe and b/bin/Release/IPMonitor.exe differ diff --git a/bin/Release/IPMonitor.exe.config b/bin/Release/IPMonitor.exe.config index ee8d2ae..8c62091 100644 --- a/bin/Release/IPMonitor.exe.config +++ b/bin/Release/IPMonitor.exe.config @@ -2,7 +2,7 @@ - + diff --git a/bin/Release/IPMonitor.pdb b/bin/Release/IPMonitor.pdb index 70502bf..87056e4 100644 Binary files a/bin/Release/IPMonitor.pdb and b/bin/Release/IPMonitor.pdb differ diff --git a/bin/Release/IPMonitor.vshost.exe b/bin/Release/IPMonitor.vshost.exe index c0dfecc..f443805 100644 Binary files a/bin/Release/IPMonitor.vshost.exe and b/bin/Release/IPMonitor.vshost.exe differ diff --git a/bin/Release/ipmonitor.log b/bin/Release/ipmonitor.log index 680bcce..16b853a 100644 --- a/bin/Release/ipmonitor.log +++ b/bin/Release/ipmonitor.log @@ -1 +1,5 @@ -[LOCAL][Thread=1][TRACE.DEBUG][3/13/2024 6:14:56 PM] [IPMonitor.Program::WriteFile(strPathFileName,ipAddress)]Creating address file:ipaddress.txt +[LOCAL][Thread=1][TRACE.DEBUG][3/14/2024 7:08:13 AM] [IPMonitor.Program::Main(args)][IPMonitor::Start] C:\boneyard\IPMonitor\bin\Release +[LOCAL][Thread=1][TRACE.DEBUG][3/14/2024 10:18:48 AM] [IPMonitor.Program::GetHttpRequest(strRequest)][IPMonitor:GetHttpRequest] failed with System.Net.WebException: The remote server returned an error: (502) Bad Gateway. + at System.Net.HttpWebRequest.GetResponse() + at IPMonitor.Program.GetHttpRequest(String strRequest) in c:\boneyard\IPMonitor\Program.cs:line 82 +[LOCAL][Thread=1][TRACE.DEBUG][3/14/2024 10:18:48 AM] [IPMonitor.Program::GetPublicIPAddress()][IPMonitor::GetPublicIPAddress] Request failed http://checkip.dyndns.org/. Will retry after 30000 (ms) diff --git a/obj/Debug/IPMonitor.csproj.FileListAbsolute.txt b/obj/Debug/IPMonitor.csproj.FileListAbsolute.txt index 88e7acc..2d4dc05 100644 --- a/obj/Debug/IPMonitor.csproj.FileListAbsolute.txt +++ b/obj/Debug/IPMonitor.csproj.FileListAbsolute.txt @@ -5,5 +5,7 @@ C:\Visual Studio\IPMonitor\obj\Debug\IPMonitor.csprojResolveAssemblyReference.ca C:\Visual Studio\IPMonitor\obj\Debug\IPMonitor.exe C:\Visual Studio\IPMonitor\obj\Debug\IPMonitor.pdb C:\boneyard\IPMonitor\bin\Debug\IPMonitor.exe.config +C:\boneyard\IPMonitor\bin\Debug\IPMonitor.exe +C:\boneyard\IPMonitor\bin\Debug\IPMonitor.pdb C:\boneyard\IPMonitor\obj\Debug\IPMonitor.exe C:\boneyard\IPMonitor\obj\Debug\IPMonitor.pdb diff --git a/obj/Debug/IPMonitor.csprojResolveAssemblyReference.cache b/obj/Debug/IPMonitor.csprojResolveAssemblyReference.cache deleted file mode 100644 index d7696cb..0000000 Binary files a/obj/Debug/IPMonitor.csprojResolveAssemblyReference.cache and /dev/null differ diff --git a/obj/Debug/IPMonitor.exe b/obj/Debug/IPMonitor.exe index 1dc6c13..21afd64 100644 Binary files a/obj/Debug/IPMonitor.exe and b/obj/Debug/IPMonitor.exe differ diff --git a/obj/Debug/IPMonitor.pdb b/obj/Debug/IPMonitor.pdb index 70502bf..87056e4 100644 Binary files a/obj/Debug/IPMonitor.pdb and b/obj/Debug/IPMonitor.pdb differ