Skip to content

Commit

Permalink
feat: adding option to allow ssl errors. Useful when testing with sel…
Browse files Browse the repository at this point in the history
…f signed cert
  • Loading branch information
James-Frowen committed Apr 9, 2024
1 parent 1391d80 commit d956089
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions source/Runtime/Client/SimpleWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public enum ClientState
/// </summary>
public abstract class SimpleWebClient
{
public static SimpleWebClient Create(int maxMessageSize, int maxMessagesPerTick, TcpConfig tcpConfig)
public static SimpleWebClient Create(int maxMessageSize, int maxMessagesPerTick, TcpConfig tcpConfig, bool allowSSLErrors = false)
{
#if UNITY_WEBGL && !UNITY_EDITOR
return new WebSocketClientWebGl(maxMessageSize, maxMessagesPerTick);
#else
return new WebSocketClientStandAlone(maxMessageSize, maxMessagesPerTick, tcpConfig);
return new WebSocketClientStandAlone(maxMessageSize, maxMessagesPerTick, tcpConfig, allowSSLErrors);
#endif
}

Expand Down
26 changes: 21 additions & 5 deletions source/Runtime/Client/StandAlone/ClientSslHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Codice.CM.Common;
using System;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
Expand All @@ -8,6 +9,13 @@ namespace JamesFrowen.SimpleWeb
{
internal class ClientSslHelper
{
private readonly bool allowErrors;

public ClientSslHelper(bool allowErrors)
{
this.allowErrors = allowErrors;
}

internal bool TryCreateStream(Connection conn, Uri uri)
{
NetworkStream stream = conn.client.GetStream();
Expand Down Expand Up @@ -36,12 +44,20 @@ Stream CreateStream(NetworkStream stream, Uri uri)
return sslStream;
}

static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// Do not allow this client to communicate with unauthenticated servers.

// only accept if no errors
return sslPolicyErrors == SslPolicyErrors.None;
if (sslPolicyErrors == SslPolicyErrors.None)
return true;

if (allowErrors)
{
Log.Error($"Cert had Errors {sslPolicyErrors}, but allowErrors is true");
return true;
}

// Do not allow this client to communicate with unauthenticated servers.
return false;
}
}
}
4 changes: 2 additions & 2 deletions source/Runtime/Client/StandAlone/WebSocketClientStandAlone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class WebSocketClientStandAlone : SimpleWebClient
readonly TcpConfig tcpConfig;
Connection conn;

internal WebSocketClientStandAlone(int maxMessageSize, int maxMessagesPerTick, TcpConfig tcpConfig) : base(maxMessageSize, maxMessagesPerTick)
internal WebSocketClientStandAlone(int maxMessageSize, int maxMessagesPerTick, TcpConfig tcpConfig, bool allowSSLErrors) : base(maxMessageSize, maxMessagesPerTick)
{
#if UNITY_WEBGL && !UNITY_EDITOR
throw new NotSupportedException();
#else
sslHelper = new ClientSslHelper();
sslHelper = new ClientSslHelper(allowSSLErrors);
handshake = new ClientHandshake();
this.tcpConfig = tcpConfig;
#endif
Expand Down

0 comments on commit d956089

Please sign in to comment.