From 11386b93b468022d2f7a40b853fcd422b0633aff Mon Sep 17 00:00:00 2001 From: Redus <92763949+Redusofficial@users.noreply.github.com> Date: Sat, 13 Apr 2024 03:43:13 +0100 Subject: [PATCH 1/6] GetServerLocation() Changes --- Bloxstrap/Integrations/ActivityWatcher.cs | 8 ++++-- Bloxstrap/Models/IPInfoResponse.cs | 35 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 Bloxstrap/Models/IPInfoResponse.cs diff --git a/Bloxstrap/Integrations/ActivityWatcher.cs b/Bloxstrap/Integrations/ActivityWatcher.cs index 142eba8..09692ba 100644 --- a/Bloxstrap/Integrations/ActivityWatcher.cs +++ b/Bloxstrap/Integrations/ActivityWatcher.cs @@ -284,9 +284,11 @@ try { - locationCity = await App.HttpClient.GetStringAsync($"https://ipinfo.io/{ActivityMachineAddress}/city"); - locationRegion = await App.HttpClient.GetStringAsync($"https://ipinfo.io/{ActivityMachineAddress}/region"); - locationCountry = await App.HttpClient.GetStringAsync($"https://ipinfo.io/{ActivityMachineAddress}/country"); + locationInformation = await Http.GetJson($"https://ipinfo.io/{ActivityMachineAddress}/json"); + + locationCity = locationInformation.IP; + locationRegion = locationInformation.Region; + locationCountry = locationInformation.Country; } catch (Exception ex) { diff --git a/Bloxstrap/Models/IPInfoResponse.cs b/Bloxstrap/Models/IPInfoResponse.cs new file mode 100644 index 0000000..dab396e --- /dev/null +++ b/Bloxstrap/Models/IPInfoResponse.cs @@ -0,0 +1,35 @@ +namespace Bloxstrap.Models +{ + public class IPInfoResponse + { + [JsonPropertyName("city")] + public string City { get; set; } + + [JsonPropertyName("country")] + public string Country { get; set; } + + [JsonPropertyName("region")] + public string Region { get; set; } + + [JsonPropertyName("ip")] + public string IP { get; set; } + + [JsonPropertyName("postal")] + public string Postal { get; set; } + + [JsonPropertyName("timezone")] + public string TimeZone { get; set; } + + [JsonPropertyName("readme")] + public string ReadME { get; set; } + + [JsonPropertyName("org")] + public string Org { get; set; } + + [JsonPropertyName("loc")] + public string Loc { get; set; } + + [JsonPropertyName("anycast")] + public string AnyCast { get; set; } + } +} From f81cedbf2aca3f73489447550723e1ffd9b76f8a Mon Sep 17 00:00:00 2001 From: Redus <92763949+Redusofficial@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:35:08 +0100 Subject: [PATCH 2/6] Requested Changes --- Bloxstrap/Integrations/ActivityWatcher.cs | 4 +++- Bloxstrap/Models/IPInfoResponse.cs | 27 +++-------------------- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/Bloxstrap/Integrations/ActivityWatcher.cs b/Bloxstrap/Integrations/ActivityWatcher.cs index 09692ba..8339343 100644 --- a/Bloxstrap/Integrations/ActivityWatcher.cs +++ b/Bloxstrap/Integrations/ActivityWatcher.cs @@ -282,9 +282,11 @@ string location, locationCity, locationRegion, locationCountry = ""; + IPInfoResponse locationInformation + try { - locationInformation = await Http.GetJson($"https://ipinfo.io/{ActivityMachineAddress}/json"); + IPInfoResponse locationInformation = await Http.GetJson($"https://ipinfo.io/{ActivityMachineAddress}/json"); locationCity = locationInformation.IP; locationRegion = locationInformation.Region; diff --git a/Bloxstrap/Models/IPInfoResponse.cs b/Bloxstrap/Models/IPInfoResponse.cs index dab396e..a0a62c0 100644 --- a/Bloxstrap/Models/IPInfoResponse.cs +++ b/Bloxstrap/Models/IPInfoResponse.cs @@ -3,33 +3,12 @@ namespace Bloxstrap.Models public class IPInfoResponse { [JsonPropertyName("city")] - public string City { get; set; } + public string? City { get; set; } [JsonPropertyName("country")] - public string Country { get; set; } + public string? Country { get; set; } [JsonPropertyName("region")] - public string Region { get; set; } - - [JsonPropertyName("ip")] - public string IP { get; set; } - - [JsonPropertyName("postal")] - public string Postal { get; set; } - - [JsonPropertyName("timezone")] - public string TimeZone { get; set; } - - [JsonPropertyName("readme")] - public string ReadME { get; set; } - - [JsonPropertyName("org")] - public string Org { get; set; } - - [JsonPropertyName("loc")] - public string Loc { get; set; } - - [JsonPropertyName("anycast")] - public string AnyCast { get; set; } + public string? Region { get; set; } } } From 03be7d749cc212091afa9a71b38c163d54a8ca10 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 14 Apr 2024 22:08:46 +0100 Subject: [PATCH 3/6] Forgot a semicolon --- Bloxstrap/Integrations/ActivityWatcher.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bloxstrap/Integrations/ActivityWatcher.cs b/Bloxstrap/Integrations/ActivityWatcher.cs index 8339343..ced99cf 100644 --- a/Bloxstrap/Integrations/ActivityWatcher.cs +++ b/Bloxstrap/Integrations/ActivityWatcher.cs @@ -282,7 +282,7 @@ string location, locationCity, locationRegion, locationCountry = ""; - IPInfoResponse locationInformation + IPInfoResponse locationInformation; try { From 44d8f9f743c838180805f60493836dc8e8e034f0 Mon Sep 17 00:00:00 2001 From: Redus <92763949+Redusofficial@users.noreply.github.com> Date: Sun, 14 Apr 2024 22:09:48 +0100 Subject: [PATCH 4/6] Forgot a semicolon --- Bloxstrap/Integrations/ActivityWatcher.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bloxstrap/Integrations/ActivityWatcher.cs b/Bloxstrap/Integrations/ActivityWatcher.cs index 8339343..ced99cf 100644 --- a/Bloxstrap/Integrations/ActivityWatcher.cs +++ b/Bloxstrap/Integrations/ActivityWatcher.cs @@ -282,7 +282,7 @@ string location, locationCity, locationRegion, locationCountry = ""; - IPInfoResponse locationInformation + IPInfoResponse locationInformation; try { From 38edb685740c3f435d4d020350c28b202f662df9 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 15 Apr 2024 00:43:44 +0100 Subject: [PATCH 5/6] Build error fix and added back ip property --- Bloxstrap/Integrations/ActivityWatcher.cs | 15 ++++++++------- Bloxstrap/Models/IPInfoResponse.cs | 8 ++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Bloxstrap/Integrations/ActivityWatcher.cs b/Bloxstrap/Integrations/ActivityWatcher.cs index ced99cf..3c4696c 100644 --- a/Bloxstrap/Integrations/ActivityWatcher.cs +++ b/Bloxstrap/Integrations/ActivityWatcher.cs @@ -280,17 +280,18 @@ if (GeolocationCache.ContainsKey(ActivityMachineAddress)) return GeolocationCache[ActivityMachineAddress]; - string location, locationCity, locationRegion, locationCountry = ""; - - IPInfoResponse locationInformation; + string location = "", locationCity = "", locationRegion = "", locationCountry = ""; try { - IPInfoResponse locationInformation = await Http.GetJson($"https://ipinfo.io/{ActivityMachineAddress}/json"); + var locationInformation = await Http.GetJson($"https://ipinfo.io/{ActivityMachineAddress}/json"); - locationCity = locationInformation.IP; - locationRegion = locationInformation.Region; - locationCountry = locationInformation.Country; + if (locationInformation is not null) + { + locationCity = locationInformation.IP; + locationRegion = locationInformation.Region; + locationCountry = locationInformation.Country; + } } catch (Exception ex) { diff --git a/Bloxstrap/Models/IPInfoResponse.cs b/Bloxstrap/Models/IPInfoResponse.cs index a0a62c0..6deaf26 100644 --- a/Bloxstrap/Models/IPInfoResponse.cs +++ b/Bloxstrap/Models/IPInfoResponse.cs @@ -2,13 +2,13 @@ namespace Bloxstrap.Models { public class IPInfoResponse { - [JsonPropertyName("city")] - public string? City { get; set; } + [JsonPropertyName("ip")] + public string IP { get; set; } = null!; [JsonPropertyName("country")] - public string? Country { get; set; } + public string Country { get; set; } = null!; [JsonPropertyName("region")] - public string? Region { get; set; } + public string Region { get; set; } = null!; } } From cda660cc3e7f6206ff05720a4e307f1affd7d24b Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Sun, 12 May 2024 21:20:00 +0100 Subject: [PATCH 6/6] Activity tracker, more efficient, fix city not IP --- Bloxstrap/Integrations/ActivityWatcher.cs | 45 +++++++++-------------- Bloxstrap/Models/IPInfoResponse.cs | 4 +- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/Bloxstrap/Integrations/ActivityWatcher.cs b/Bloxstrap/Integrations/ActivityWatcher.cs index 3c4696c..ac0bea0 100644 --- a/Bloxstrap/Integrations/ActivityWatcher.cs +++ b/Bloxstrap/Integrations/ActivityWatcher.cs @@ -280,18 +280,27 @@ if (GeolocationCache.ContainsKey(ActivityMachineAddress)) return GeolocationCache[ActivityMachineAddress]; - string location = "", locationCity = "", locationRegion = "", locationCountry = ""; - try { - var locationInformation = await Http.GetJson($"https://ipinfo.io/{ActivityMachineAddress}/json"); + string location = ""; + var ipInfo = await Http.GetJson($"https://ipinfo.io/{ActivityMachineAddress}/json"); - if (locationInformation is not null) - { - locationCity = locationInformation.IP; - locationRegion = locationInformation.Region; - locationCountry = locationInformation.Country; - } + if (ipInfo is null) + return $"N/A ({Resources.Strings.ActivityTracker_LookupFailed})"; + + if (string.IsNullOrEmpty(ipInfo.Country)) + location = "N/A"; + else if (ipInfo.City == ipInfo.Region) + location = $"{ipInfo.Region}, {ipInfo.Country}"; + else + location = $"{ipInfo.City}, {ipInfo.Region}, {ipInfo.Country}"; + + if (!ActivityInGame) + return $"N/A ({Resources.Strings.ActivityTracker_LeftGame})"; + + GeolocationCache[ActivityMachineAddress] = location; + + return location; } catch (Exception ex) { @@ -300,24 +309,6 @@ return $"N/A ({Resources.Strings.ActivityTracker_LookupFailed})"; } - - locationCity = locationCity.ReplaceLineEndings(""); - locationRegion = locationRegion.ReplaceLineEndings(""); - locationCountry = locationCountry.ReplaceLineEndings(""); - - if (string.IsNullOrEmpty(locationCountry)) - location = "N/A"; - else if (locationCity == locationRegion) - location = $"{locationRegion}, {locationCountry}"; - else - location = $"{locationCity}, {locationRegion}, {locationCountry}"; - - if (!ActivityInGame) - return $"N/A ({Resources.Strings.ActivityTracker_LeftGame})"; - - GeolocationCache[ActivityMachineAddress] = location; - - return location; } public void Dispose() diff --git a/Bloxstrap/Models/IPInfoResponse.cs b/Bloxstrap/Models/IPInfoResponse.cs index 6deaf26..f8d3bc0 100644 --- a/Bloxstrap/Models/IPInfoResponse.cs +++ b/Bloxstrap/Models/IPInfoResponse.cs @@ -2,8 +2,8 @@ namespace Bloxstrap.Models { public class IPInfoResponse { - [JsonPropertyName("ip")] - public string IP { get; set; } = null!; + [JsonPropertyName("city")] + public string City { get; set; } = null!; [JsonPropertyName("country")] public string Country { get; set; } = null!;