Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented WebIPRetriever.

  • Loading branch information...
commit 17274c76dc1833ebf7695980eb91d7eb00236f92 1 parent d396a4e
@seancallaway seancallaway authored
Showing with 64 additions and 0 deletions.
  1. +3 −0  DynZE.csproj
  2. +61 −0 WebIPRetriever.cs
View
3  DynZE.csproj
@@ -47,6 +47,7 @@
<Compile Include="AppConfig.cs" />
<Compile Include="CommandUpdater.cs" />
<Compile Include="Configurator.cs" />
+ <Compile Include="DynZEService.cs" />
<Compile Include="IIPPersistor.cs" />
<Compile Include="IIPRetriever.cs" />
<Compile Include="IIPUpdater.cs" />
@@ -54,6 +55,7 @@
<Compile Include="Install\ProjectInstaller.cs">
<SubType>Component</SubType>
</Compile>
+ <Compile Include="IPPersistor.cs" />
<Compile Include="IService.cs" />
<Compile Include="Log\ConsoleLog.cs" />
<Compile Include="Log\SystemLog.cs" />
@@ -68,6 +70,7 @@
</Compile>
<Compile Include="ServiceController.cs" />
<Compile Include="StringConstants.cs" />
+ <Compile Include="WebIPRetriever.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
View
61 WebIPRetriever.cs
@@ -0,0 +1,61 @@
+using System;
+using System.IO;
+using System.Net;
+using DynZE.Log;
+
+namespace DynZE
+{
+ class WebIPRetriever : IIPRetriever
+ {
+ ILog _log;
+
+ public WebIPRetriever(ILog log)
+ {
+ _log = log;
+ }
+
+ public string getIP()
+ {
+ try
+ {
+ // Create the HTTP request
+ var request = HttpWebRequest.Create("http://checkip.dyndns.org/");
+ request.Method = "GET";
+
+ var response = request.GetResponse();
+
+ using (var reader = new StreamReader(response.GetResponseStream()))
+ {
+ var answer = reader.ReadToEnd();
+ return extractIP(answer);
+ }
+ }
+ catch (Exception ex)
+ {
+ _log.write(LogLevel.Warn, "Could not retrieve current IP from web. {0}", ex);
+ return null;
+ }
+ }
+
+ private string extractIP(string answer)
+ {
+ var pattern = "Current IP Address: ";
+ var index = answer.IndexOf(pattern);
+
+ if (index == -1)
+ {
+ return null;
+ }
+
+ var start = index + pattern.Length;
+ var end = answer.IndexOf("<", start);
+
+ if (end == -1)
+ {
+ return null;
+ }
+
+ return answer.Substring(start, end - start);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.