Refactor.
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<appSettings>
|
<appSettings>
|
||||||
<add key="poll_time_ms" value="300000"/>
|
<add key="poll_time_ms" value="300000"/>
|
||||||
<add key="notify_time_ms" value="4320000"/>
|
<add key="notify_time_ms" value="10800000"/>
|
||||||
<add key="sms_smtpaddress" value="smtp.gmail.com"/>
|
<add key="sms_smtpaddress" value="smtp.gmail.com"/>
|
||||||
<add key="sms_smsusername" value="skessler1964@gmail.com"/>
|
<add key="sms_smsusername" value="skessler1964@gmail.com"/>
|
||||||
<add key="sms_smspassword" value="xjfo isnf gmyi zovr"/>
|
<add key="sms_smspassword" value="xjfo isnf gmyi zovr"/>
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
|
<Prefer32Bit>false</Prefer32Bit>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
|||||||
Binary file not shown.
98
Program.cs
98
Program.cs
@@ -13,27 +13,89 @@ namespace IPMonitor
|
|||||||
{
|
{
|
||||||
class Program
|
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("</body>");
|
||||||
|
// 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()
|
public static String GetPublicIPAddress()
|
||||||
{
|
{
|
||||||
String address = "";
|
int MAX_RETRIES=5;
|
||||||
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
|
int TIMEOUT_BETWEEN_ATTEMPTS=30000;
|
||||||
using (WebResponse response = request.GetResponse())
|
try
|
||||||
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
|
|
||||||
{
|
{
|
||||||
address = stream.ReadToEnd();
|
String address = null;
|
||||||
}
|
String request="http://checkip.dyndns.org/";
|
||||||
|
|
||||||
|
for(int index=0;index<MAX_RETRIES && null==address;index++)
|
||||||
|
{
|
||||||
|
address = GetHttpRequest(request);
|
||||||
|
if(null==address)
|
||||||
|
{
|
||||||
|
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::GetPublicIPAddress] Request failed {0}. Will retry after {1} (ms) ",request,TIMEOUT_BETWEEN_ATTEMPTS));
|
||||||
|
try{Thread.Sleep(TIMEOUT_BETWEEN_ATTEMPTS);}catch{;}
|
||||||
|
}
|
||||||
|
}
|
||||||
int first = address.IndexOf("Address: ") + 9;
|
int first = address.IndexOf("Address: ") + 9;
|
||||||
int last = address.LastIndexOf("</body>");
|
int last = address.LastIndexOf("</body>");
|
||||||
address = address.Substring(first, last - first);
|
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)
|
public static void UpdateIPAddress(String ipAddress)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
String strPathFileName="ipaddress.txt";
|
String strPathFileName="ipaddress.txt";
|
||||||
if(!File.Exists(strPathFileName))
|
if(!File.Exists(strPathFileName))
|
||||||
{
|
{
|
||||||
@@ -48,10 +110,21 @@ namespace IPMonitor
|
|||||||
WriteFile(strPathFileName,ipAddress);
|
WriteFile(strPathFileName,ipAddress);
|
||||||
SendSMSEmail(String.Format("IPMonitor IPAddress {0}",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)
|
public static void WriteFile(String strPathFileName,String ipAddress)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if(File.Exists(strPathFileName))File.Delete(strPathFileName);
|
if(File.Exists(strPathFileName))File.Delete(strPathFileName);
|
||||||
FileStream fileStream=new FileStream(strPathFileName,FileMode.Create,FileAccess.Write,FileShare.Read);
|
FileStream fileStream=new FileStream(strPathFileName,FileMode.Create,FileAccess.Write,FileShare.Read);
|
||||||
@@ -62,6 +135,12 @@ namespace IPMonitor
|
|||||||
streamWriter.Close();
|
streamWriter.Close();
|
||||||
streamWriter.Dispose();
|
streamWriter.Dispose();
|
||||||
}
|
}
|
||||||
|
catch(Exception exception)
|
||||||
|
{
|
||||||
|
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::WriteFile] Exception:{0}",exception.ToString()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static String ReadFile(String strPathFileName)
|
public static String ReadFile(String strPathFileName)
|
||||||
{
|
{
|
||||||
@@ -106,16 +185,17 @@ namespace IPMonitor
|
|||||||
|
|
||||||
Profiler profiler=new Profiler();
|
Profiler profiler=new Profiler();
|
||||||
profiler.Start();
|
profiler.Start();
|
||||||
|
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::Start] {0}",Directory.GetCurrentDirectory()));
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
String ipAddress=GetPublicIPAddress();
|
String ipAddress=GetPublicIPAddress();
|
||||||
UpdateIPAddress(ipAddress);
|
UpdateIPAddress(ipAddress);
|
||||||
try{Thread.Sleep(interval_ms);}catch{;}
|
|
||||||
if(profiler.Split()>=notify_ms)
|
if(profiler.Split()>=notify_ms)
|
||||||
{
|
{
|
||||||
SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress));
|
SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress));
|
||||||
profiler.Start();
|
profiler.Start();
|
||||||
}
|
}
|
||||||
|
try{Thread.Sleep(interval_ms);}catch{;}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(Exception exception)
|
catch(Exception exception)
|
||||||
|
|||||||
Binary file not shown.
@@ -2,7 +2,7 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<appSettings>
|
<appSettings>
|
||||||
<add key="poll_time_ms" value="300000"/>
|
<add key="poll_time_ms" value="300000"/>
|
||||||
<add key="notify_time_ms" value="4320000"/>
|
<add key="notify_time_ms" value="10800000"/>
|
||||||
<add key="sms_smtpaddress" value="smtp.gmail.com"/>
|
<add key="sms_smtpaddress" value="smtp.gmail.com"/>
|
||||||
<add key="sms_smsusername" value="skessler1964@gmail.com"/>
|
<add key="sms_smsusername" value="skessler1964@gmail.com"/>
|
||||||
<add key="sms_smspassword" value="xjfo isnf gmyi zovr"/>
|
<add key="sms_smspassword" value="xjfo isnf gmyi zovr"/>
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -2,7 +2,7 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<appSettings>
|
<appSettings>
|
||||||
<add key="poll_time_ms" value="300000"/>
|
<add key="poll_time_ms" value="300000"/>
|
||||||
<add key="notify_time_ms" value="4320000"/>
|
<add key="notify_time_ms" value="10800000"/>
|
||||||
<add key="sms_smtpaddress" value="smtp.gmail.com"/>
|
<add key="sms_smtpaddress" value="smtp.gmail.com"/>
|
||||||
<add key="sms_smsusername" value="skessler1964@gmail.com"/>
|
<add key="sms_smsusername" value="skessler1964@gmail.com"/>
|
||||||
<add key="sms_smspassword" value="xjfo isnf gmyi zovr"/>
|
<add key="sms_smspassword" value="xjfo isnf gmyi zovr"/>
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -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)
|
||||||
|
|||||||
@@ -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.exe
|
||||||
C:\Visual Studio\IPMonitor\obj\Debug\IPMonitor.pdb
|
C:\Visual Studio\IPMonitor\obj\Debug\IPMonitor.pdb
|
||||||
C:\boneyard\IPMonitor\bin\Debug\IPMonitor.exe.config
|
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.exe
|
||||||
C:\boneyard\IPMonitor\obj\Debug\IPMonitor.pdb
|
C:\boneyard\IPMonitor\obj\Debug\IPMonitor.pdb
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user