Skip to content

Commit

Permalink
fix: converting endpoint to ipv4
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Jun 9, 2023
1 parent 1656e0b commit 49340c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
21 changes: 13 additions & 8 deletions source/Runtime/Common/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;

Expand All @@ -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;

Expand All @@ -36,8 +36,7 @@ internal sealed class Connection : IDisposable
public ConcurrentQueue<ArrayBuffer> sendQueue = new ConcurrentQueue<ArrayBuffer>();

public Action<Connection> onDispose;

volatile bool hasDisposed;
private volatile bool hasDisposed;

public Connection(TcpClient client, Action<Connection> onDispose)
{
Expand Down Expand Up @@ -111,17 +110,23 @@ public override string ToString()
/// Gets the address based on the <see cref="request"/> and RemoteEndPoint
/// <para>Called after ServerHandShake is accepted</para>
/// </summary>
/// <exception cref="NotImplementedException"></exception>
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();
}
}
}
}
7 changes: 3 additions & 4 deletions source/Runtime/Common/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> Headers = new Dictionary<string, string>();

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());
}
}
Expand Down

0 comments on commit 49340c2

Please sign in to comment.