Skip to content

Commit e610067

Browse files
authored
chore: secure connections (#2209)
Interfaces with UTP 2.0 to allow secure connection. Adds UnityTransport APIs to set secrets.
1 parent 64c4331 commit e610067

File tree

6 files changed

+338
-25
lines changed

6 files changed

+338
-25
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,23 +1019,37 @@ public bool StartServer()
10191019

10201020
Initialize(true);
10211021

1022-
// If we failed to start then shutdown and notify user that the transport failed to start
1023-
if (NetworkConfig.NetworkTransport.StartServer())
1022+
IsServer = true;
1023+
IsClient = false;
1024+
IsListening = true;
1025+
1026+
try
10241027
{
1025-
IsServer = true;
1026-
IsClient = false;
1027-
IsListening = true;
1028+
// If we failed to start then shutdown and notify user that the transport failed to start
1029+
if (NetworkConfig.NetworkTransport.StartServer())
1030+
{
1031+
SpawnManager.ServerSpawnSceneObjectsOnStartSweep();
10281032

1029-
SpawnManager.ServerSpawnSceneObjectsOnStartSweep();
1033+
OnServerStarted?.Invoke();
1034+
return true;
1035+
}
1036+
else
1037+
{
1038+
IsServer = false;
1039+
IsClient = false;
1040+
IsListening = false;
10301041

1031-
OnServerStarted?.Invoke();
1032-
return true;
1042+
Debug.LogError($"Server is shutting down due to network transport start failure of {NetworkConfig.NetworkTransport.GetType().Name}!");
1043+
OnTransportFailure?.Invoke();
1044+
Shutdown();
1045+
}
10331046
}
1034-
else
1047+
catch (Exception)
10351048
{
1036-
Debug.LogError($"Server is shutting down due to network transport start failure of {NetworkConfig.NetworkTransport.GetType().Name}!");
1037-
OnTransportFailure?.Invoke();
1038-
Shutdown();
1049+
IsServer = false;
1050+
IsClient = false;
1051+
IsListening = false;
1052+
throw;
10391053
}
10401054

10411055
return false;
@@ -1093,23 +1107,38 @@ public bool StartHost()
10931107

10941108
Initialize(true);
10951109

1096-
// If we failed to start then shutdown and notify user that the transport failed to start
1097-
if (!NetworkConfig.NetworkTransport.StartServer())
1110+
IsServer = true;
1111+
IsClient = true;
1112+
IsListening = true;
1113+
1114+
try
10981115
{
1099-
Debug.LogError($"Server is shutting down due to network transport start failure of {NetworkConfig.NetworkTransport.GetType().Name}!");
1100-
OnTransportFailure?.Invoke();
1101-
Shutdown();
1102-
return false;
1116+
// If we failed to start then shutdown and notify user that the transport failed to start
1117+
if (!NetworkConfig.NetworkTransport.StartServer())
1118+
{
1119+
Debug.LogError($"Server is shutting down due to network transport start failure of {NetworkConfig.NetworkTransport.GetType().Name}!");
1120+
OnTransportFailure?.Invoke();
1121+
Shutdown();
1122+
1123+
IsServer = false;
1124+
IsClient = false;
1125+
IsListening = false;
1126+
1127+
return false;
1128+
}
1129+
}
1130+
catch (Exception)
1131+
{
1132+
IsServer = false;
1133+
IsClient = false;
1134+
IsListening = false;
1135+
throw;
11031136
}
11041137

11051138
MessagingSystem.ClientConnected(ServerClientId);
11061139
LocalClientId = ServerClientId;
11071140
NetworkMetrics.SetConnectionId(LocalClientId);
11081141

1109-
IsServer = true;
1110-
IsClient = true;
1111-
IsListening = true;
1112-
11131142
if (NetworkConfig.ConnectionApproval && ConnectionApprovalCallback != null)
11141143
{
11151144
var response = new ConnectionApprovalResponse();
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using System.IO;
3+
using UnityEngine;
4+
5+
namespace Unity.Netcode.Transports.UTP
6+
{
7+
/// <summary>
8+
/// Component to add to a NetworkManager if you want the certificates to be loaded from files.
9+
/// Mostly helpful to ease development and testing, especially with self-signed certificates
10+
///
11+
/// Shipping code should make the calls to
12+
/// - SetServerSecrets
13+
/// - SetClientSecrets
14+
/// directly, instead of relying on this.
15+
/// </summary>
16+
public class SecretsLoaderHelper : MonoBehaviour
17+
{
18+
internal struct ServerSecrets
19+
{
20+
public string ServerPrivate;
21+
public string ServerCertificate;
22+
};
23+
24+
internal struct ClientSecrets
25+
{
26+
public string ServerCommonName;
27+
public string ClientCertificate;
28+
};
29+
30+
private void Awake()
31+
{
32+
var serverSecrets = new ServerSecrets();
33+
34+
try
35+
{
36+
serverSecrets.ServerCertificate = ServerCertificate;
37+
}
38+
catch (Exception exception)
39+
{
40+
Debug.Log(exception);
41+
}
42+
43+
try
44+
{
45+
serverSecrets.ServerPrivate = ServerPrivate;
46+
}
47+
catch (Exception exception)
48+
{
49+
Debug.Log(exception);
50+
}
51+
52+
var clientSecrets = new ClientSecrets();
53+
try
54+
{
55+
clientSecrets.ClientCertificate = ClientCA;
56+
}
57+
catch (Exception exception)
58+
{
59+
Debug.Log(exception);
60+
}
61+
62+
try
63+
{
64+
clientSecrets.ServerCommonName = ServerCommonName;
65+
}
66+
catch (Exception exception)
67+
{
68+
Debug.Log(exception);
69+
}
70+
71+
var unityTransportComponent = GetComponent<UnityTransport>();
72+
73+
if (unityTransportComponent == null)
74+
{
75+
Debug.LogError($"You need to select the UnityTransport protocol, in the NetworkManager, in order for the SecretsLoaderHelper component to be useful.");
76+
return;
77+
}
78+
79+
unityTransportComponent.SetServerSecrets(serverSecrets.ServerCertificate, serverSecrets.ServerPrivate);
80+
unityTransportComponent.SetClientSecrets(clientSecrets.ServerCommonName, clientSecrets.ClientCertificate);
81+
}
82+
83+
[Tooltip("Hostname")]
84+
[SerializeField]
85+
private string m_ServerCommonName = "localhost";
86+
public string ServerCommonName
87+
{
88+
get => m_ServerCommonName;
89+
set => m_ServerCommonName = value;
90+
}
91+
92+
[Tooltip("Client CA filepath. Useful with self-signed certificates")]
93+
[SerializeField]
94+
private string m_ClientCAFilePath = "Assets/Secure/myGameClientCA.pem";
95+
public string ClientCAFilePath
96+
{
97+
get => m_ClientCAFilePath;
98+
set => m_ClientCAFilePath = value;
99+
}
100+
101+
[Tooltip("Client CA Override. Only useful for development with self-signed certificates. Certificate content, for platforms that lack file access (WebGL)")]
102+
[SerializeField]
103+
private string m_ClientCAOverride = "";
104+
public string ClientCAOverride
105+
{
106+
get => m_ClientCAOverride;
107+
set => m_ClientCAOverride = value;
108+
}
109+
110+
[Tooltip("Server Certificate filepath")]
111+
[SerializeField]
112+
private string m_ServerCertificateFilePath = "Assets/Secure/myGameServerCertificate.pem";
113+
public string ServerCertificateFilePath
114+
{
115+
get => m_ServerCertificateFilePath;
116+
set => m_ServerCertificateFilePath = value;
117+
}
118+
[Tooltip("Server Private Keyfilepath")]
119+
[SerializeField]
120+
private string m_ServerPrivateFilePath = "Assets/Secure/myGameServerPrivate.pem";
121+
public string ServerPrivateFilePath
122+
{
123+
get => m_ServerPrivateFilePath;
124+
set => m_ServerPrivate = value;
125+
}
126+
127+
private string m_ClientCA;
128+
public string ClientCA
129+
{
130+
get
131+
{
132+
if (m_ClientCAOverride != "")
133+
{
134+
return m_ClientCAOverride;
135+
}
136+
return ReadFile(m_ClientCAFilePath, "Client Certificate");
137+
}
138+
set => m_ClientCA = value;
139+
}
140+
private string m_ServerCertificate;
141+
public string ServerCertificate
142+
{
143+
get => ReadFile(m_ServerCertificateFilePath, "Server Certificate");
144+
set => m_ServerCertificate = value;
145+
}
146+
private string m_ServerPrivate;
147+
public string ServerPrivate
148+
{
149+
get => ReadFile(m_ServerPrivateFilePath, "Server Key");
150+
set => m_ServerPrivate = value;
151+
}
152+
153+
private static string ReadFile(string path, string label)
154+
{
155+
var reader = new StreamReader(path);
156+
string fileContent = reader.ReadToEnd();
157+
Debug.Log((fileContent.Length > 1) ? ("Successfully loaded " + fileContent.Length + " byte(s) from " + label) : ("Could not read " + label + " file"));
158+
return fileContent;
159+
}
160+
}
161+
}

com.unity.netcode.gameobjects/Runtime/Transports/UTP/SecretsLoaderHelper.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)