SafeDispatch/SafeMobileLIB_DLL/IP_Helpers.cs

83 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SafeMobileLib
{
public class IP_Helpers
{
public static string GetInterface(string IPstr)
{
string strInterface = "";
try
{
string[] arrStr = IPstr.Split('.');
int lastPartOfIP = Convert.ToInt32(arrStr[3]);
arrStr[3] = (lastPartOfIP + 1).ToString();
strInterface = arrStr[0] + "." + arrStr[1] + "." + arrStr[2] + "." + arrStr[3];
}
catch (Exception ex)
{
Console.WriteLine("Error GEtting interface");
Console.WriteLine(ex.ToString());
}
return strInterface;
}
public static string GetReverse_Interface(string IPstr)
{
string strInterface = "";
try
{
string[] arrStr = IPstr.Split('.');
int lastPartOfIP = Convert.ToInt32(arrStr[3]);
arrStr[3] = (lastPartOfIP - 1).ToString();
strInterface = arrStr[0] + "." + arrStr[1] + "." + arrStr[2] + "." + arrStr[3];
}
catch (Exception ex)
{
Console.WriteLine("Error GEtting interface");
Console.WriteLine(ex.ToString());
}
return strInterface;
}
public static async Task<string> GetPublicIPAsync()
{
return await Task.Run(() => GetPublicIP());
}
public static string GetPublicIP()
{
string a4 = "";
try
{
string url = "http://checkip.dyndns.org";
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
string response = sr.ReadToEnd().Trim();
string[] a = response.Split(':');
string a2 = a[1].Substring(1);
string[] a3 = a2.Split('<');
a4 = a3[0];
if(a4 == "")
a4 = new WebClient().DownloadString(@"http://icanhazip.com").Trim();
}
catch { a4 = new WebClient().DownloadString(@"http://icanhazip.com").Trim(); }
return a4;
}
}
}