From a9f744024862db7eaf9888d66dba38f1466bc216 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 23 May 2015 11:04:01 +0200 Subject: [PATCH] Improvements to GeoIP guesser --- Logic/Cache/GuidCache.cs | 1 + Logic/Support/General.cs | 10 +++++---- .../LogEntries/ParticipationActionBase.cs | 1 + .../Support/LogEntries/PersonAddedLogEntry.cs | 6 +++++ Logic/Support/Persistence.cs | 1 + Site5/Global.asax | 4 ++-- Site5/Pages/v5/Public/Signup.aspx | 6 ++--- Site5/Pages/v5/Public/Signup.aspx.cs | 22 ++++++++++++++----- 8 files changed, 35 insertions(+), 16 deletions(-) diff --git a/Logic/Cache/GuidCache.cs b/Logic/Cache/GuidCache.cs index 9786ad06..a3a46739 100644 --- a/Logic/Cache/GuidCache.cs +++ b/Logic/Cache/GuidCache.cs @@ -47,6 +47,7 @@ namespace Swarmops.Logic.Cache _cache.Remove (guidString); } } + private static void ConditionalGarbageCollect() { diff --git a/Logic/Support/General.cs b/Logic/Support/General.cs index e6fccd18..4cbbb364 100644 --- a/Logic/Support/General.cs +++ b/Logic/Support/General.cs @@ -40,10 +40,10 @@ namespace Swarmops.Logic.Support public static string GetRemoteIPAddressChain() { string forwardedIps = - HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; + HttpContext.Current.Request.Headers ["X-Forwarded-For"]; - string directIp = - HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; + string directIp = + HttpContext.Current.Request.UserHostAddress; // may be IPv4 or IPv6 string result = directIp; @@ -59,7 +59,9 @@ namespace Swarmops.Logic.Support // ReSharper disable once InconsistentNaming -- IPAddress is the canonical writing public static string GetMostLikelyRemoteIPAddress() { - // All of this can be faked trivially by the client, so you can't rely on it for any kind of security. + // All of this can be faked trivially by the client, so we can't rely on it for any kind of security. + // While it is possible to set up a server environment that strips out such fakes, doing so should not + // be a requirement for running Swarmops. string forwardedIps = HttpContext.Current.Request.Headers["X-Forwarded-For"]; diff --git a/Logic/Support/LogEntries/ParticipationActionBase.cs b/Logic/Support/LogEntries/ParticipationActionBase.cs index 4ab3361d..ebf091ba 100644 --- a/Logic/Support/LogEntries/ParticipationActionBase.cs +++ b/Logic/Support/LogEntries/ParticipationActionBase.cs @@ -20,5 +20,6 @@ namespace Swarmops.Logic.Support.LogEntries /// public DateTime DateTime { get; set; } public int ActingPersonId { get; set; } + public string ActingIPAddress { get; set; } } } diff --git a/Logic/Support/LogEntries/PersonAddedLogEntry.cs b/Logic/Support/LogEntries/PersonAddedLogEntry.cs index b712f2f5..bae1d4fe 100644 --- a/Logic/Support/LogEntries/PersonAddedLogEntry.cs +++ b/Logic/Support/LogEntries/PersonAddedLogEntry.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using System.Web; using System.Linq; using System.Text; using System.Threading.Tasks; using Swarmops.Logic.Swarm; +using Swarmops.Logic.Support; namespace Swarmops.Logic.Support.LogEntries { @@ -21,6 +23,10 @@ namespace Swarmops.Logic.Support.LogEntries DateTime = System.DateTime.UtcNow; ParticipationId = participation.Identity; ActingPersonId = actingPerson.Identity; + if (HttpContext.Current != null) + { + ActingIPAddress = SupportFunctions.GetRemoteIPAddressChain(); + } } } } diff --git a/Logic/Support/Persistence.cs b/Logic/Support/Persistence.cs index c0e3c0e5..29b91973 100644 --- a/Logic/Support/Persistence.cs +++ b/Logic/Support/Persistence.cs @@ -24,6 +24,7 @@ namespace Swarmops.Logic.Support } } + public static Persistence Key { get { return new Persistence(); } diff --git a/Site5/Global.asax b/Site5/Global.asax index 321f0dce..ee9cf424 100644 --- a/Site5/Global.asax +++ b/Site5/Global.asax @@ -36,6 +36,8 @@ string[] rewriteCandidates = { + "{0}/Dashboard.aspx", // for dev.swarmops.com -- Dashboard overrides Default in root, if it exists + "{0}Dashboard.aspx", // for dev.swarmops.com/ -- Dashboard overrides Default in root, if it exists "{0}/Default.aspx", // for dev.swarmops.com "{0}Default.aspx", // for dev.swarmops.com/ "{0}.aspx", // for dev.swarmops.com/Security/Login @@ -60,8 +62,6 @@ { // Set default roles (none) - string[] userRoles = new string[0]; - // ----------- SET ROLES ----------- // Determine logged-on user (FormsAuthentication) diff --git a/Site5/Pages/v5/Public/Signup.aspx b/Site5/Pages/v5/Public/Signup.aspx index 0a1c8538..ced8101c 100644 --- a/Site5/Pages/v5/Public/Signup.aspx +++ b/Site5/Pages/v5/Public/Signup.aspx @@ -134,16 +134,14 @@ SwarmopsJS.ajaxCall("/Pages/v5/Public/Signup.aspx/GuessCountry", {}, function(result) { if (result.Success && result.DisplayMessage.length > 0) { $('#<%=this.DropCountries.ClientID%>').val(result.DisplayMessage); + UpdatePostalPrefix('XX', result.DisplayMessage); } else { // No dice for some reason - initialize postal codes with default lookup - // (the change-of-field in the success scenario triggers this in that case) UpdatePostalPrefix('XX', $('#<%= DropCountries.ClientID %>').val()); } }); - - $('#<%= TextPostal.ClientID %>').on('input', function () { if (CheckPostalCode()) { if ($('#<%= TextPostal.ClientID %>').val().length == postalCodeLength) { @@ -622,7 +620,7 @@

-
+
​
​
​
diff --git a/Site5/Pages/v5/Public/Signup.aspx.cs b/Site5/Pages/v5/Public/Signup.aspx.cs index 207bf922..e1ec973a 100644 --- a/Site5/Pages/v5/Public/Signup.aspx.cs +++ b/Site5/Pages/v5/Public/Signup.aspx.cs @@ -20,6 +20,7 @@ using Swarmops.Logic.Structure; using Swarmops.Logic.Support; using Swarmops.Logic.Support.LogEntries; using Swarmops.Logic.Swarm; +using Swarmops.Logic.Cache; namespace Swarmops.Frontend.Pages.Public { @@ -299,18 +300,27 @@ namespace Swarmops.Frontend.Pages.Public { string ipAddress = Logic.Support.SupportFunctions.GetMostLikelyRemoteIPAddress(); + NGeoIP.RawData rawData = (NGeoIP.RawData) GuidCache.Get(ipAddress); + Persistence.Key["DebugData"] = HttpContext.Current.Request.ToRaw(); - NGeoIP.Request request = new Request() + if (rawData == null) { - Format = Format.Json, - IP = ipAddress - }; - NGeoClient client = new NGeoClient (request); - NGeoIP.RawData rawData = client.Execute(); + NGeoIP.Request request = new Request() + { + Format = Format.Json, + IP = ipAddress + }; + NGeoClient client = new NGeoClient(request); + rawData = client.Execute(); + } if (!string.IsNullOrEmpty (rawData.CountryCode)) { + // Successful lookup + + GuidCache.Set(ipAddress, rawData); // store lookup results in cache for later + return new AjaxCallResult {Success = true, DisplayMessage = rawData.CountryCode}; } }