2014-07-08 12:58:53 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
2015-01-13 07:32:45 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
2015-01-13 18:29:11 +00:00
|
|
|
|
namespace xClient.Core
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
2015-01-13 18:43:55 +00:00
|
|
|
|
public class GeoIP
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
|
|
|
|
public string WANIP { get; private set; }
|
|
|
|
|
public string Country { get; private set; }
|
|
|
|
|
public string CountryCode { get; private set; }
|
|
|
|
|
public string Region { get; private set; }
|
|
|
|
|
public string City { get; private set; }
|
|
|
|
|
|
2015-01-13 07:32:45 +00:00
|
|
|
|
private string GetWANIP()
|
|
|
|
|
{
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://checkip.dyndns.org");
|
|
|
|
|
request.Proxy = null;
|
|
|
|
|
request.Timeout = 5000;
|
|
|
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
|
|
Stream dataStream = response.GetResponseStream();
|
|
|
|
|
StreamReader reader = new StreamReader(dataStream);
|
|
|
|
|
string responseString = reader.ReadToEnd();
|
|
|
|
|
reader.Close();
|
|
|
|
|
dataStream.Close();
|
|
|
|
|
response.Close();
|
|
|
|
|
|
|
|
|
|
responseString = (new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")).Match(responseString).Value;
|
|
|
|
|
return responseString;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public GeoIP()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-01-13 07:32:45 +00:00
|
|
|
|
WANIP = GetWANIP();
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("http://api.hackertarget.com/geoip/?q={0}", WANIP));
|
2014-07-08 12:58:53 +00:00
|
|
|
|
request.Proxy = null;
|
2014-12-02 20:10:27 +00:00
|
|
|
|
request.Timeout = 5000;
|
2014-07-08 12:58:53 +00:00
|
|
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
|
|
Stream dataStream = response.GetResponseStream();
|
|
|
|
|
StreamReader reader = new StreamReader(dataStream);
|
|
|
|
|
string responseString = reader.ReadToEnd();
|
|
|
|
|
reader.Close();
|
|
|
|
|
dataStream.Close();
|
|
|
|
|
response.Close();
|
|
|
|
|
|
2015-01-13 07:32:45 +00:00
|
|
|
|
string[] resp = responseString.Split('\n');
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
2015-01-13 07:32:45 +00:00
|
|
|
|
foreach (string line in resp)
|
|
|
|
|
{
|
|
|
|
|
if (line.StartsWith("Country: "))
|
|
|
|
|
{
|
|
|
|
|
Country = line.Replace("Country: ", string.Empty);
|
|
|
|
|
CountryCode = Country;
|
|
|
|
|
}
|
|
|
|
|
else if (line.StartsWith("State: "))
|
|
|
|
|
{
|
|
|
|
|
Region = line.Replace("State: ", string.Empty);
|
|
|
|
|
}
|
|
|
|
|
else if (line.StartsWith("City: "))
|
|
|
|
|
{
|
|
|
|
|
City = line.Replace("City: ", string.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
WANIP = "-";
|
|
|
|
|
Country = "Unknown";
|
|
|
|
|
CountryCode = "-";
|
|
|
|
|
Region = "Unknown";
|
|
|
|
|
City = "Unknown";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|