Skip to content

Commit

Permalink
Finish refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aallbrig committed Jan 9, 2024
1 parent d1c141b commit 17c2ddc
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 38 deletions.
8 changes: 6 additions & 2 deletions Assets/Mirror/Transports/SSL/CertificateSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Security.Cryptography.X509Certificates;
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;

namespace Mirror
{
Expand Down Expand Up @@ -32,6 +31,11 @@ public X509Certificate2 Certificate
}
}

public string CertPassword()
{
return File.ReadAllText(PasswordFilePath);
}

private X509Certificate2 NewPasswordProtectedCertificate()
{
if (!ValidateCertificatePath(CertificatePath))
Expand All @@ -44,7 +48,7 @@ private X509Certificate2 NewPasswordProtectedCertificate()
Debug.LogError("Password file path is invalid (" + PasswordFilePath + "). Unable to create certificate.");
return null;
}
string password = File.ReadAllText(PasswordFilePath);
string password = CertPassword();
return new X509Certificate2(CertificatePath, password);
}

Expand Down
26 changes: 0 additions & 26 deletions Assets/Mirror/Transports/SSL/SSLSettings.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using UnityEditor;
using UnityEngine;

namespace Mirror
Expand All @@ -17,26 +12,5 @@ public class SSLSettings

[Tooltip("Protocol to use for ssl (default: TLS 1.2)")]
public SslProtocols SSLProtocol = SslProtocols.Tls12;

public Stream CreateStream(NetworkStream stream, X509Certificate2 certificate)
{
if (!SSLEnabled)
{
Debug.LogError("SSL is not enabled. Unable to create stream.");
return null;
}

SslStream sslStream = new(stream, true, AcceptClient);
sslStream.AuthenticateAsServer(certificate, false, SSLProtocol, false);

return sslStream;
}

// Always accept client
private bool AcceptClient(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
}
}
52 changes: 51 additions & 1 deletion Assets/Mirror/Transports/SSL/TransportSecurity.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
using System;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using Mirror.SimpleWeb;
using UnityEngine;

namespace Mirror
{
public class TransportSecurity : MonoBehaviour
public class TransportSecurity : MonoBehaviour, ICreateStream
{
public SSLSettings sslSettings;
public CertificateSettings certificateSettings;
public SSLSettings GetSslSettings()
{
return sslSettings;
}
public bool TryCreateStream(IConnection conn)
{
NetworkStream stream = conn.Client.GetStream();
if (sslSettings.SSLEnabled)
{
try
{
conn.Stream = CreateStream(stream);
return true;
}
catch (Exception e)
{
Debug.LogError($"[SWT-ServerSslHelper]: Create SSLStream Failed: {e.Message}");
return false;
}
}

conn.Stream = stream;
return true;
}
public Stream CreateStream(NetworkStream stream)
{
if (!sslSettings.SSLEnabled)
{
Debug.LogError("SSL is not enabled. Unable to create stream.");
return null;
}

SslStream sslStream = new SslStream(stream, true, AcceptClient);
sslStream.AuthenticateAsServer(certificateSettings.Certificate, false, sslSettings.SSLProtocol, false);

return sslStream;
}

// Always accept client
private bool AcceptClient(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Security.Cryptography.X509Certificates;

namespace Mirror.SimpleWeb
{
public interface ICreateStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public SslConfig(bool enabled, string certPath, string certPassword, SslProtocol
this.sslProtocols = sslProtocols;
}
}
internal class ServerSslHelper: ICreateStream
public class ServerSslHelper: ICreateStream
{
readonly SslConfig config;
readonly X509Certificate2 certificate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class SimpleWebServer

public bool Active { get; private set; }

public SimpleWebServer(int maxMessagesPerTick, TcpConfig tcpConfig, int maxMessageSize, int handshakeMaxSize, SslConfig sslConfig)
public SimpleWebServer(int maxMessagesPerTick, TcpConfig tcpConfig, int maxMessageSize, int handshakeMaxSize, ICreateStream streamCreator)
{
this.maxMessagesPerTick = maxMessagesPerTick;
// use max because bufferpool is used for both messages and handshake
int max = Math.Max(maxMessageSize, handshakeMaxSize);
bufferPool = new BufferPool(5, 20, max);
server = new WebSocketServer(tcpConfig, maxMessageSize, handshakeMaxSize, bufferPool, new ServerSslHelper(sslConfig));
server = new WebSocketServer(tcpConfig, maxMessageSize, handshakeMaxSize, bufferPool, streamCreator);
}

public void Start(ushort port)
Expand Down
10 changes: 4 additions & 6 deletions Assets/Mirror/Transports/SimpleWeb/SimpleWebTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,14 @@ public override void ServerStart()
if (ServerActive())
Log.Warn("[SWT-ServerStart]: Server Already Started");

SslConfig config;
if (transportSecurity && transportSecurity.enabled)
{
config = transportSecurity.GetSslSettings();
}
server = new SimpleWebServer(serverMaxMsgsPerTick, TcpConfig, maxMessageSize, maxHandshakeSize, transportSecurity);
else
{
config = new SslConfig(false, "", "", System.Security.Authentication.SslProtocols.None);
SslConfig sslConfig = new SslConfig(false, "", "", System.Security.Authentication.SslProtocols.None);
ServerSslHelper serverSslHelper = new ServerSslHelper(sslConfig);
server = new SimpleWebServer(serverMaxMsgsPerTick, TcpConfig, maxMessageSize, maxHandshakeSize, serverSslHelper);
}
server = new SimpleWebServer(serverMaxMsgsPerTick, TcpConfig, maxMessageSize, maxHandshakeSize, config);

server.onConnect += OnServerConnected.Invoke;
server.onDisconnect += OnServerDisconnected.Invoke;
Expand Down

0 comments on commit 17c2ddc

Please sign in to comment.