Skip to content

Commit

Permalink
Websocket Sec-WebSocket-Key is invalid #100
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Dec 6, 2020
1 parent 8d9e26d commit 4a713ae
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Specify version format
version: "5.0.2.{build}"
version: "5.0.3.{build}"

# Image to use
image: Visual Studio 2019
Expand Down
2 changes: 1 addition & 1 deletion examples/WsChatClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override void OnWsConnecting(HttpRequest request)
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString())));
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(WsNonce));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
Expand Down
2 changes: 1 addition & 1 deletion examples/WssChatClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override void OnWsConnecting(HttpRequest request)
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString())));
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(WsNonce));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
Expand Down
2 changes: 1 addition & 1 deletion performance/WsEchoClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void OnWsConnecting(HttpRequest request)
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString())));
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(WsNonce));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
Expand Down
2 changes: 1 addition & 1 deletion performance/WsMulticastClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override void OnWsConnecting(HttpRequest request)
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString())));
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(WsNonce));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
Expand Down
2 changes: 1 addition & 1 deletion performance/WssEchoClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override void OnWsConnecting(HttpRequest request)
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString())));
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(WsNonce));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
Expand Down
2 changes: 1 addition & 1 deletion performance/WssMulticastClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void OnWsConnecting(HttpRequest request)
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString())));
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(WsNonce));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
Expand Down
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>5.0.2.0</Version>
<Version>5.0.3.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2021 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
21 changes: 17 additions & 4 deletions source/NetCoreServer/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class WebSocket : IWebSocket
{
private readonly IWebSocket _wsHandler;

public WebSocket(IWebSocket wsHandler) { _wsHandler = wsHandler; ClearWsBuffers(); }
public WebSocket(IWebSocket wsHandler) { _wsHandler = wsHandler; ClearWsBuffers(); InitWsNonce(); }

/// <summary>
/// Final frame
Expand Down Expand Up @@ -87,7 +87,7 @@ public bool PerformClientUpgrade(HttpResponse response, Guid id)
else if (string.Compare(key, "Sec-WebSocket-Accept", StringComparison.OrdinalIgnoreCase) == 0)
{
// Calculate the original WebSocket hash
string wskey = Convert.ToBase64String(Encoding.UTF8.GetBytes(id.ToString())) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
string wskey = Convert.ToBase64String(WsNonce) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
string wshash;
using (SHA1Managed sha1 = new SHA1Managed())
{
Expand Down Expand Up @@ -119,8 +119,7 @@ public bool PerformClientUpgrade(HttpResponse response, Guid id)

// WebSocket successfully handshaked!
WsHandshaked = true;
Random rnd = new Random();
rnd.NextBytes(WsSendMask);
WsRandom.NextBytes(WsSendMask);
_wsHandler.OnWsConnected(response);

return true;
Expand Down Expand Up @@ -500,6 +499,11 @@ public void ClearWsBuffers()
}
}

/// <summary>
/// Initialize WebSocket random nonce
/// </summary>
public void InitWsNonce() => WsRandom.NextBytes(WsNonce);

/// <summary>
/// Handshaked flag
/// </summary>
Expand Down Expand Up @@ -542,5 +546,14 @@ public void ClearWsBuffers()
/// Send mask
/// </summary>
internal readonly byte[] WsSendMask = new byte[4];

/// <summary>
/// WebSocket random generator
/// </summary>
internal readonly Random WsRandom = new Random();
/// <summary>
/// WebSocket random nonce of 16 bytes
/// </summary>
internal readonly byte[] WsNonce = new byte[16];
}
}
8 changes: 8 additions & 0 deletions source/NetCoreServer/WsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class WsClient : HttpClient, IWebSocket
/// <param name="endpoint">IP endpoint</param>
public WsClient(IPEndPoint endpoint) : base(endpoint) { WebSocket = new WebSocket(this); }

/// <summary>
/// WebSocket random nonce
/// </summary>
public byte[] WsNonce => WebSocket.WsNonce;

#region WebSocket connection methods

public override bool Connect() { _syncConnect = true; return base.Connect(); }
Expand Down Expand Up @@ -340,6 +345,9 @@ protected override void OnDisconnected()

// Clear WebSocket send/receive buffers
WebSocket.ClearWsBuffers();

// Initialize new WebSocket random nonce
WebSocket.InitWsNonce();
}

protected override void OnReceived(byte[] buffer, long offset, long size)
Expand Down
3 changes: 3 additions & 0 deletions source/NetCoreServer/WsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ protected override void OnDisconnected()

// Clear WebSocket send/receive buffers
WebSocket.ClearWsBuffers();

// Initialize new WebSocket random nonce
WebSocket.InitWsNonce();
}

protected override void OnReceived(byte[] buffer, long offset, long size)
Expand Down
8 changes: 8 additions & 0 deletions source/NetCoreServer/WssClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class WssClient : HttpsClient, IWebSocket
/// <param name="endpoint">IP endpoint</param>
public WssClient(SslContext context, IPEndPoint endpoint) : base(context, endpoint) { WebSocket = new WebSocket(this); }

/// <summary>
/// WebSocket random nonce
/// </summary>
public byte[] WsNonce => WebSocket.WsNonce;

#region WebSocket connection methods

public override bool Connect() { _syncConnect = true; return base.Connect(); }
Expand Down Expand Up @@ -343,6 +348,9 @@ protected override void OnDisconnected()

// Clear WebSocket send/receive buffers
WebSocket.ClearWsBuffers();

// Initialize new WebSocket random nonce
WebSocket.InitWsNonce();
}

protected override void OnReceived(byte[] buffer, long offset, long size)
Expand Down
3 changes: 3 additions & 0 deletions source/NetCoreServer/WssSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ protected override void OnDisconnected()

// Clear WebSocket send/receive buffers
WebSocket.ClearWsBuffers();

// Initialize new WebSocket random nonce
WebSocket.InitWsNonce();
}

protected override void OnReceived(byte[] buffer, long offset, long size)
Expand Down
2 changes: 1 addition & 1 deletion tests/WsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override void OnWsConnecting(HttpRequest request)
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString())));
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(WsNonce));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/WssTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override void OnWsConnecting(HttpRequest request)
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString())));
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(WsNonce));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
Expand Down

0 comments on commit 4a713ae

Please sign in to comment.