SafeDispatch/SafeMobileLIB_DLL/IP_Helpers.cs

83 lines
2.6 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
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
{
2024-06-07 13:20:57 +00:00
string[] arrStr = IPstr.Split(".".ToCharArray());
2024-02-22 16:43:59 +00:00
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
{
2024-06-07 13:20:57 +00:00
string[] arrStr = IPstr.Split(".".ToCharArray());
2024-02-22 16:43:59 +00:00
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();
2024-06-07 13:20:57 +00:00
string[] a = response.Split(":".ToCharArray());
2024-02-22 16:43:59 +00:00
string a2 = a[1].Substring(1);
2024-06-07 13:20:57 +00:00
string[] a3 = a2.Split("<".ToCharArray());
2024-02-22 16:43:59 +00:00
a4 = a3[0];
if(a4 == "")
a4 = new WebClient().DownloadString(@"http://icanhazip.com").Trim();
}
catch { a4 = new WebClient().DownloadString(@"http://icanhazip.com").Trim(); }
return a4;
}
}
}