diff --git a/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/IWebSocketServerFactory.cs b/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/IWebSocketServerFactory.cs index 6915463b5..98d7b3942 100644 --- a/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/IWebSocketServerFactory.cs +++ b/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/IWebSocketServerFactory.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Net.Sockets; using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; @@ -16,7 +17,7 @@ public interface IWebSocketServerFactory /// The network stream /// The optional cancellation token /// Http data read from the stream - Task ReadHttpHeaderFromStreamAsync(Stream stream, CancellationToken token = default(CancellationToken)); + Task ReadHttpHeaderFromStreamAsync(TcpClient client, Stream stream, CancellationToken token = default(CancellationToken)); /// /// Accept web socket with default options diff --git a/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/Internal/WebSocketImplementation.cs b/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/Internal/WebSocketImplementation.cs index a4be86337..33d85ec35 100644 --- a/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/Internal/WebSocketImplementation.cs +++ b/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/Internal/WebSocketImplementation.cs @@ -65,6 +65,8 @@ internal class WebSocketImplementation : WebSocket Queue> _messageQueue = new Queue>(); SemaphoreSlim _sendSemaphore = new SemaphoreSlim(1, 1); + public WebSocketHttpContext Context { get; set; } + internal WebSocketImplementation(Guid guid, Func recycledStreamFactory, Stream stream, TimeSpan keepAliveInterval, string secWebSocketExtensions, bool includeExceptionInCloseResponse, bool isClient, string subProtocol) { diff --git a/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/WebSocketHttpContext.cs b/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/WebSocketHttpContext.cs index f1c85dc0e..fd136e1e7 100644 --- a/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/WebSocketHttpContext.cs +++ b/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/WebSocketHttpContext.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.IO; +using System.Net.Sockets; namespace Ninja.WebSockets { @@ -30,6 +31,11 @@ public class WebSocketHttpContext /// public Stream Stream { get; private set; } + /// + /// The tcp connection we are using + /// + public TcpClient Client { get; private set; } + /// /// Initialises a new instance of the WebSocketHttpContext class /// @@ -37,12 +43,13 @@ public class WebSocketHttpContext /// The raw http header extracted from the stream /// The Path extracted from the http header /// The stream AFTER the header has already been read - public WebSocketHttpContext(bool isWebSocketRequest, IList webSocketRequestedProtocols, string httpHeader, string path, Stream stream) + public WebSocketHttpContext(bool isWebSocketRequest, IList webSocketRequestedProtocols, string httpHeader, string path, TcpClient client, Stream stream) { IsWebSocketRequest = isWebSocketRequest; WebSocketRequestedProtocols = webSocketRequestedProtocols; HttpHeader = httpHeader; Path = path; + Client = client; Stream = stream; } } diff --git a/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/WebSocketServerFactory.cs b/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/WebSocketServerFactory.cs index 4720538ea..972e8dbe7 100644 --- a/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/WebSocketServerFactory.cs +++ b/Assets/Mirror/Runtime/Transport/Websocket/Ninja.WebSockets/WebSocketServerFactory.cs @@ -23,6 +23,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Net.Sockets; using System.Net.WebSockets; using System.Text.RegularExpressions; using System.Threading; @@ -64,13 +65,13 @@ public WebSocketServerFactory(Func bufferFactory) /// The network stream /// The optional cancellation token /// Http data read from the stream - public async Task ReadHttpHeaderFromStreamAsync(Stream stream, CancellationToken token = default(CancellationToken)) + public async Task ReadHttpHeaderFromStreamAsync(TcpClient client, Stream stream, CancellationToken token = default(CancellationToken)) { string header = await HttpHelper.ReadHttpHeaderAsync(stream, token); string path = HttpHelper.GetPathFromHeader(header); bool isWebSocketRequest = HttpHelper.IsWebSocketUpgradeRequest(header); IList subProtocols = HttpHelper.GetSubProtocols(header); - return new WebSocketHttpContext(isWebSocketRequest, subProtocols, header, path, stream); + return new WebSocketHttpContext(isWebSocketRequest, subProtocols, header, path, client, stream); } /// @@ -100,7 +101,10 @@ public WebSocketServerFactory(Func bufferFactory) await PerformHandshakeAsync(guid, context.HttpHeader, options.SubProtocol, context.Stream, token); Events.Log.ServerHandshakeSuccess(guid); string secWebSocketExtensions = null; - return new WebSocketImplementation(guid, _bufferFactory, context.Stream, options.KeepAliveInterval, secWebSocketExtensions, options.IncludeExceptionInCloseResponse, false, options.SubProtocol); + return new WebSocketImplementation(guid, _bufferFactory, context.Stream, options.KeepAliveInterval, secWebSocketExtensions, options.IncludeExceptionInCloseResponse, false, options.SubProtocol) + { + Context = context + }; } static void CheckWebSocketVersion(string httpHeader) diff --git a/Assets/Mirror/Runtime/Transport/Websocket/Server.cs b/Assets/Mirror/Runtime/Transport/Websocket/Server.cs index c681f25ea..6f53a88d8 100644 --- a/Assets/Mirror/Runtime/Transport/Websocket/Server.cs +++ b/Assets/Mirror/Runtime/Transport/Websocket/Server.cs @@ -9,6 +9,7 @@ using System.Threading; using System.Threading.Tasks; using Ninja.WebSockets; +using Ninja.WebSockets.Internal; using UnityEngine; namespace Mirror.Websocket @@ -128,7 +129,7 @@ async Task ProcessTcpClient(TcpClient tcpClient, CancellationToken token) sslStream.AuthenticateAsServer(_sslConfig.Certificate, _sslConfig.ClientCertificateRequired, _sslConfig.EnabledSslProtocols, _sslConfig.CheckCertificateRevocation); stream = sslStream; } - WebSocketHttpContext context = await webSocketServerFactory.ReadHttpHeaderFromStreamAsync(stream, token); + WebSocketHttpContext context = await webSocketServerFactory.ReadHttpHeaderFromStreamAsync(tcpClient, stream, token); if (context.IsWebSocketRequest) { WebSocketServerOptions options = new WebSocketServerOptions() { KeepAliveInterval = TimeSpan.FromSeconds(30), SubProtocol = "binary" }; @@ -312,7 +313,9 @@ public string GetClientAddress(int connectionId) // find the connection if (clients.TryGetValue(connectionId, out WebSocket client)) { - return ""; + WebSocketImplementation wsClient = client as WebSocketImplementation; + return wsClient.Context.Client.Client.RemoteEndPoint.ToString(); + } return null; }