From 09f2905f20bb4e8966be6f878fd596bf2cc7488c Mon Sep 17 00:00:00 2001 From: "L.J. Lim" Date: Wed, 21 Dec 2016 23:41:43 +0800 Subject: [PATCH 1/3] GetUserIP() tries to return an IPv4 Fixes a failure with site ban by IP --- Zero-K.info/Global.asax.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Zero-K.info/Global.asax.cs b/Zero-K.info/Global.asax.cs index 5632c73d2c..a0086ebf37 100644 --- a/Zero-K.info/Global.asax.cs +++ b/Zero-K.info/Global.asax.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Net; using System.Web; using System.Web.Mvc; using System.Web.Optimization; @@ -102,8 +103,11 @@ protected void Application_Start() private string GetUserIP() { - var ip = Context.Request.ServerVariables["REMOTE_ADDR"]; - return ip; + string hostname = Context.Request.ServerVariables["REMOTE_HOST"]; + IPAddress[] ipv4s = Array.FindAll(Dns.GetHostEntry(string.Empty).AddressList, + a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork); + if (ipv4s.Length > 0) return ipv4s[0].ToString(); + return Context.Request.ServerVariables["REMOTE_ADDR"]; } private void MvcApplication_Error(object sender, EventArgs e) From e6af1e33892dcbe0033109df084b3d450dc94f46 Mon Sep 17 00:00:00 2001 From: "L.J. Lim" Date: Mon, 2 Jan 2017 11:55:37 +0800 Subject: [PATCH 2/3] Fix IP check --- Zero-K.info/Global.asax.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Zero-K.info/Global.asax.cs b/Zero-K.info/Global.asax.cs index a0086ebf37..ce97ae3902 100644 --- a/Zero-K.info/Global.asax.cs +++ b/Zero-K.info/Global.asax.cs @@ -104,7 +104,7 @@ protected void Application_Start() private string GetUserIP() { string hostname = Context.Request.ServerVariables["REMOTE_HOST"]; - IPAddress[] ipv4s = Array.FindAll(Dns.GetHostEntry(string.Empty).AddressList, + IPAddress[] ipv4s = Array.FindAll(Dns.GetHostEntry(hostname).AddressList, a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork); if (ipv4s.Length > 0) return ipv4s[0].ToString(); return Context.Request.ServerVariables["REMOTE_ADDR"]; From f15b14b784bb11736d85a43fbf8af268b2508ca5 Mon Sep 17 00:00:00 2001 From: "L.J. Lim" Date: Fri, 13 Jan 2017 17:51:43 +0800 Subject: [PATCH 3/3] Forum controller uses enhanced IP check as well --- Zero-K.info/Controllers/ForumController.cs | 2 +- Zero-K.info/Global.asax.cs | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Zero-K.info/Controllers/ForumController.cs b/Zero-K.info/Controllers/ForumController.cs index 148301e7a2..a31a97a150 100644 --- a/Zero-K.info/Controllers/ForumController.cs +++ b/Zero-K.info/Controllers/ForumController.cs @@ -244,7 +244,7 @@ public ActionResult SubmitPost( forumPostID == null && string.IsNullOrWhiteSpace(title)) return Content("Cannot post new thread with blank title"); if (string.IsNullOrWhiteSpace(text)) return Content("Please type some text :)"); - var penalty = Punishment.GetActivePunishment(Global.AccountID, Request.ServerVariables["REMOTE_ADDR"], 0, x => x.BanForum); + var penalty = Punishment.GetActivePunishment(Global.AccountID, MvcApplication.GetUserIP(Request), 0, x => x.BanForum); if (penalty != null) { return diff --git a/Zero-K.info/Global.asax.cs b/Zero-K.info/Global.asax.cs index ce97ae3902..33e96aac55 100644 --- a/Zero-K.info/Global.asax.cs +++ b/Zero-K.info/Global.asax.cs @@ -103,11 +103,16 @@ protected void Application_Start() private string GetUserIP() { - string hostname = Context.Request.ServerVariables["REMOTE_HOST"]; + return GetUserIP(new HttpRequestWrapper(Context.Request)); + } + + public static string GetUserIP(HttpRequestBase request) + { + string hostname = request.ServerVariables["REMOTE_HOST"]; IPAddress[] ipv4s = Array.FindAll(Dns.GetHostEntry(hostname).AddressList, a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork); if (ipv4s.Length > 0) return ipv4s[0].ToString(); - return Context.Request.ServerVariables["REMOTE_ADDR"]; + return request.ServerVariables["REMOTE_ADDR"]; } private void MvcApplication_Error(object sender, EventArgs e)