Fixed GeoIP again

last service gone offline
This commit is contained in:
MaxXor 2015-01-13 08:32:45 +01:00
parent 82839f6c87
commit dcbabeb893
1 changed files with 37 additions and 9 deletions

View File

@ -1,6 +1,6 @@
using System.IO;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;
namespace Core
{
@ -12,11 +12,29 @@ namespace Core
public string Region { get; private set; }
public string City { get; private set; }
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;
}
public GeoIP()
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://freegeoip.net/xml/");
WANIP = GetWANIP();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("http://api.hackertarget.com/geoip/?q={0}", WANIP));
request.Proxy = null;
request.Timeout = 5000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
@ -27,14 +45,24 @@ namespace Core
dataStream.Close();
response.Close();
XmlDocument doc = new XmlDocument();
doc.LoadXml(responseString);
string[] resp = responseString.Split('\n');
WANIP = doc.SelectSingleNode("Response//IP").InnerXml.ToString();
Country = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//CountryName").InnerXml.ToString())) ? doc.SelectSingleNode("Response//CountryName").InnerXml.ToString() : "Unknown";
CountryCode = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//CountryCode").InnerXml.ToString())) ? doc.SelectSingleNode("Response//CountryCode").InnerXml.ToString() : "-";
Region = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//RegionName").InnerXml.ToString())) ? doc.SelectSingleNode("Response//RegionName").InnerXml.ToString() : "Unknown";
City = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//City").InnerXml.ToString())) ? doc.SelectSingleNode("Response//City").InnerXml.ToString() : "Unknown";
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);
}
}
}
catch
{