Skip to content

Commit

Permalink
feat: Websockets now give client address, fix #1121 (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach committed Sep 28, 2019
1 parent 2cd36c8 commit c9f317d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -16,7 +17,7 @@ public interface IWebSocketServerFactory
/// <param name="stream">The network stream</param>
/// <param name="token">The optional cancellation token</param>
/// <returns>Http data read from the stream</returns>
Task<WebSocketHttpContext> ReadHttpHeaderFromStreamAsync(Stream stream, CancellationToken token = default(CancellationToken));
Task<WebSocketHttpContext> ReadHttpHeaderFromStreamAsync(TcpClient client, Stream stream, CancellationToken token = default(CancellationToken));

/// <summary>
/// Accept web socket with default options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ internal class WebSocketImplementation : WebSocket

Queue<ArraySegment<byte>> _messageQueue = new Queue<ArraySegment<byte>>();
SemaphoreSlim _sendSemaphore = new SemaphoreSlim(1, 1);
public WebSocketHttpContext Context { get; set; }


internal WebSocketImplementation(Guid guid, Func<MemoryStream> recycledStreamFactory, Stream stream, TimeSpan keepAliveInterval, string secWebSocketExtensions, bool includeExceptionInCloseResponse, bool isClient, string subProtocol)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;

namespace Ninja.WebSockets
{
Expand Down Expand Up @@ -30,19 +31,25 @@ public class WebSocketHttpContext
/// </summary>
public Stream Stream { get; private set; }

/// <summary>
/// The tcp connection we are using
/// </summary>
public TcpClient Client { get; private set; }

/// <summary>
/// Initialises a new instance of the WebSocketHttpContext class
/// </summary>
/// <param name="isWebSocketRequest">True if this is a valid WebSocket request</param>
/// <param name="httpHeader">The raw http header extracted from the stream</param>
/// <param name="path">The Path extracted from the http header</param>
/// <param name="stream">The stream AFTER the header has already been read</param>
public WebSocketHttpContext(bool isWebSocketRequest, IList<string> webSocketRequestedProtocols, string httpHeader, string path, Stream stream)
public WebSocketHttpContext(bool isWebSocketRequest, IList<string> webSocketRequestedProtocols, string httpHeader, string path, TcpClient client, Stream stream)
{
IsWebSocketRequest = isWebSocketRequest;
WebSocketRequestedProtocols = webSocketRequestedProtocols;
HttpHeader = httpHeader;
Path = path;
Client = client;
Stream = stream;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,13 +65,13 @@ public WebSocketServerFactory(Func<MemoryStream> bufferFactory)
/// <param name="stream">The network stream</param>
/// <param name="token">The optional cancellation token</param>
/// <returns>Http data read from the stream</returns>
public async Task<WebSocketHttpContext> ReadHttpHeaderFromStreamAsync(Stream stream, CancellationToken token = default(CancellationToken))
public async Task<WebSocketHttpContext> 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<string> subProtocols = HttpHelper.GetSubProtocols(header);
return new WebSocketHttpContext(isWebSocketRequest, subProtocols, header, path, stream);
return new WebSocketHttpContext(isWebSocketRequest, subProtocols, header, path, client, stream);
}

/// <summary>
Expand Down Expand Up @@ -100,7 +101,10 @@ public async Task<WebSocket> AcceptWebSocketAsync(WebSocketHttpContext context,
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)
Expand Down
7 changes: 5 additions & 2 deletions Assets/Mirror/Runtime/Transport/Websocket/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using Ninja.WebSockets;
using Ninja.WebSockets.Internal;
using UnityEngine;

namespace Mirror.Websocket
Expand Down Expand Up @@ -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" };
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit c9f317d

Please sign in to comment.