From f37b4734edeebedd783441fe7b0c5fc875c96192 Mon Sep 17 00:00:00 2001 From: Albie Spriddell Date: Wed, 29 Nov 2023 15:34:17 +0000 Subject: [PATCH] use cloudflare country code where possible --- .../Controllers/ConnectionStatusController.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/DragonFruit.OnionFruit.Web/Controllers/ConnectionStatusController.cs b/DragonFruit.OnionFruit.Web/Controllers/ConnectionStatusController.cs index 87ae25f..e214a7b 100644 --- a/DragonFruit.OnionFruit.Web/Controllers/ConnectionStatusController.cs +++ b/DragonFruit.OnionFruit.Web/Controllers/ConnectionStatusController.cs @@ -55,13 +55,13 @@ public async Task GetConnectionStatus() } // if not on redis, use cloudflare to check if tor and libloc to get network info - var (country, asInfo) = await _libloc.PerformAsync(a => GetConnectionInfo(a, ipAddress)).ConfigureAwait(false); - var isTor = HttpContext.Request.Headers.TryGetValue("CF-IPCountry", out var cloudflareCountry) && cloudflareCountry == "T1"; + var cloudflareCountryDetected = HttpContext.Request.Headers.TryGetValue("CF-IPCountry", out var cloudflareCountry); + var (country, asInfo) = await _libloc.PerformAsync(a => GetConnectionInfo(a, ipAddress, cloudflareCountryDetected ? cloudflareCountry.ToString() : null)).ConfigureAwait(false); - return new ConnectionStatusResponse(ipAddress.ToString(), isTor, country?.Code ?? "XX", country?.Name ?? "Unknown Country", asInfo?.Number, asInfo?.Name); + return new ConnectionStatusResponse(ipAddress.ToString(), cloudflareCountryDetected && cloudflareCountry == "T1", country?.Code ?? "XX", country?.Name ?? "Unknown Country", asInfo?.Number, asInfo?.Name); } - private (IDatabaseCountry country, IDatabaseAS asInfo) GetConnectionInfo(ILocationDatabase x, IPAddress address) + private (IDatabaseCountry country, IDatabaseAS asInfo) GetConnectionInfo(ILocationDatabase x, IPAddress address, [CanBeNull] string countryCodeOverride) { IDatabaseAS asInfo = null; IDatabaseCountry country = null; @@ -71,7 +71,7 @@ public async Task GetConnectionStatus() if (addr != null) { asInfo = x.AS.GetAS((int)addr.ASN); - country = x.Countries.GetCountry(addr.CountryCode); + country = x.Countries.GetCountry(countryCodeOverride ?? addr.CountryCode); } return (country, asInfo);