From 49340c2bef3a6ee334cb985431dd2ccbfc7c749e Mon Sep 17 00:00:00 2001 From: James Frowen Date: Fri, 9 Jun 2023 18:40:17 +0100 Subject: [PATCH] fix: converting endpoint to ipv4 --- source/Runtime/Common/Connection.cs | 21 +++++++++++++-------- source/Runtime/Common/Request.cs | 7 +++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/source/Runtime/Common/Connection.cs b/source/Runtime/Common/Connection.cs index cd5bcf5..fe2593d 100644 --- a/source/Runtime/Common/Connection.cs +++ b/source/Runtime/Common/Connection.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.IO; using System.Linq; +using System.Net; using System.Net.Sockets; using System.Threading; @@ -10,8 +11,7 @@ namespace JamesFrowen.SimpleWeb internal sealed class Connection : IDisposable { public const int IdNotSet = -1; - - readonly object disposedLock = new object(); + private readonly object disposedLock = new object(); public TcpClient client; @@ -36,8 +36,7 @@ internal sealed class Connection : IDisposable public ConcurrentQueue sendQueue = new ConcurrentQueue(); public Action onDispose; - - volatile bool hasDisposed; + private volatile bool hasDisposed; public Connection(TcpClient client, Action onDispose) { @@ -111,17 +110,23 @@ public override string ToString() /// Gets the address based on the and RemoteEndPoint /// Called after ServerHandShake is accepted /// - /// internal string CalculateAddress() { - if (request.Headers.TryGetValue("X-Forwarded-For", out var forwardFor)) + if (request.Headers.TryGetValue("X-Forwarded-For", out string forwardFor)) { - var actualClientIP = forwardFor.ToString().Split(',').First(); + string actualClientIP = forwardFor.ToString().Split(',').First(); // Remove the port number from the address return actualClientIP.Split(':').First(); } + else + { + IPEndPoint ipEndPoint = (IPEndPoint)client.Client.RemoteEndPoint; + IPAddress ipAddress = ipEndPoint.Address; + if (ipAddress.IsIPv4MappedToIPv6) + ipAddress = ipAddress.MapToIPv4(); - return client.Client.RemoteEndPoint.ToString(); + return ipAddress.ToString(); + } } } } diff --git a/source/Runtime/Common/Request.cs b/source/Runtime/Common/Request.cs index 5883140..9a4086a 100644 --- a/source/Runtime/Common/Request.cs +++ b/source/Runtime/Common/Request.cs @@ -10,17 +10,16 @@ namespace JamesFrowen.SimpleWeb public class Request { private static readonly char[] lineSplitChars = new char[] { '\r', '\n' }; + private static readonly char[] headerSplitChars = new char[] { ':' }; public string RequestLine; public Dictionary Headers = new Dictionary(); public Request(string message) { string[] all = message.Split(lineSplitChars, StringSplitOptions.RemoveEmptyEntries); - // we need to add GET back in because ServerHandshake doesn't include it - RequestLine = "GET" + all[0]; + RequestLine = all.First(); Headers = all.Skip(1) - .Select(header => header.Split(':')) - .Where(split => split.Length == 2) + .Select(header => header.Split(headerSplitChars, 2, StringSplitOptions.RemoveEmptyEntries)) .ToDictionary(split => split[0].Trim(), split => split[1].Trim()); } }