Skip to content

Commit d95e228

Browse files
committed
Added encryption and authentication capabilities to all internal send overloads
1 parent 9c506ea commit d95e228

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using BitStream = MLAPI.Serialization.BitStream;
2121
using System.Runtime.CompilerServices;
2222
using System.Security.Cryptography.X509Certificates;
23+
using System.Text;
2324

2425
namespace MLAPI
2526
{
@@ -796,7 +797,7 @@ internal void SendConnectionRequest()
796797
writer.WriteByteArray(NetworkConfig.ConnectionData);
797798
}
798799

799-
InternalMessageHandler.Send(ServerClientId, MLAPIConstants.MLAPI_CONNECTION_REQUEST, "MLAPI_INTERNAL", stream, new InternalSecuritySendOptions(NetworkConfig.EnableEncryption, false), true);
800+
InternalMessageHandler.Send(ServerClientId, MLAPIConstants.MLAPI_CONNECTION_REQUEST, "MLAPI_INTERNAL", stream, new InternalSecuritySendOptions(true, false), true);
800801
}
801802
}
802803

@@ -1017,7 +1018,7 @@ internal void OnClientDisconnectFromServer(uint clientId)
10171018
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
10181019
{
10191020
writer.WriteUInt32Packed(clientId);
1020-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_CLIENT_DISCONNECT, "MLAPI_INTERNAL", clientId, stream);
1021+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_CLIENT_DISCONNECT, "MLAPI_INTERNAL", clientId, stream, new InternalSecuritySendOptions(false, false));
10211022
}
10221023
}
10231024
}
@@ -1033,7 +1034,7 @@ private void SyncTime()
10331034
writer.WriteSinglePacked(NetworkTime);
10341035
int timestamp = NetworkConfig.NetworkTransport.GetNetworkTimestamp();
10351036
writer.WriteInt32Packed(timestamp);
1036-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_TIME_SYNC, "MLAPI_TIME_SYNC", stream);
1037+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_TIME_SYNC, "MLAPI_TIME_SYNC", stream, new InternalSecuritySendOptions(false, false));
10371038
}
10381039
}
10391040
}
@@ -1113,7 +1114,7 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
11131114
}
11141115
}
11151116

1116-
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_CONNECTION_APPROVED, "MLAPI_INTERNAL", stream, new InternalSecuritySendOptions(NetworkConfig.EnableEncryption, false), true);
1117+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_CONNECTION_APPROVED, "MLAPI_INTERNAL", stream, new InternalSecuritySendOptions(true, false), true);
11171118

11181119
if (OnClientConnectedCallback != null)
11191120
OnClientConnectedCallback.Invoke(clientId);

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Send.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ namespace MLAPI.Internal
88
{
99
internal static partial class InternalMessageHandler
1010
{
11-
internal static void Send(uint clientId, byte messageType, string channelName, Stream messageStream, InternalSecuritySendOptions options, bool skipQueue = false)
11+
internal static void Send(uint clientId, byte messageType, string channelName, Stream messageStream, InternalSecuritySendOptions securityOptions, bool skipQueue = false)
1212
{
1313
if (NetworkingManager.singleton.isServer && clientId == NetworkingManager.singleton.ServerClientId) return;
1414
using (PooledBitStream stream = PooledBitStream.Get())
1515
{
1616
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
1717
{
18-
writer.WriteBool(options.encrypted);
19-
writer.WriteBool(options.authenticated);
20-
if (options.encrypted && netManager.NetworkConfig.EnableEncryption)
18+
writer.WriteBool(securityOptions.encrypted);
19+
writer.WriteBool(securityOptions.authenticated);
20+
if (securityOptions.encrypted && netManager.NetworkConfig.EnableEncryption)
2121
{
2222
writer.WritePadBits();
2323
using (RijndaelManaged rijndael = new RijndaelManaged())
@@ -38,7 +38,7 @@ internal static void Send(uint clientId, byte messageType, string channelName, S
3838
}
3939
}
4040
}
41-
else if (options.authenticated && netManager.NetworkConfig.EnableEncryption)
41+
else if (securityOptions.authenticated && netManager.NetworkConfig.EnableEncryption)
4242
{
4343
writer.WritePadBits();
4444

@@ -66,8 +66,17 @@ internal static void Send(uint clientId, byte messageType, string channelName, S
6666
}
6767
}
6868

69-
internal static void Send(byte messageType, string channelName, Stream messageStream)
69+
internal static void Send(byte messageType, string channelName, Stream messageStream, InternalSecuritySendOptions securityOptions)
7070
{
71+
if (netManager.NetworkConfig.EnableEncryption && (securityOptions.authenticated || securityOptions.encrypted))
72+
{
73+
for (int i = 0; i < netManager.ConnectedClientsList.Count; i++)
74+
{
75+
Send(netManager.ConnectedClientsList[i].ClientId, messageType, channelName, messageStream, securityOptions);
76+
}
77+
return;
78+
}
79+
7180
using (PooledBitStream stream = PooledBitStream.Get())
7281
{
7382
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
@@ -87,8 +96,18 @@ internal static void Send(byte messageType, string channelName, Stream messageSt
8796
}
8897
}
8998

90-
internal static void Send(byte messageType, string channelName, uint clientIdToIgnore, Stream messageStream)
99+
internal static void Send(byte messageType, string channelName, uint clientIdToIgnore, Stream messageStream, InternalSecuritySendOptions securityOptions)
91100
{
101+
if (netManager.NetworkConfig.EnableEncryption && (securityOptions.authenticated || securityOptions.encrypted))
102+
{
103+
for (int i = 0; i < netManager.ConnectedClientsList.Count; i++)
104+
{
105+
if (netManager.ConnectedClientsList[i].ClientId == clientIdToIgnore) continue;
106+
Send(netManager.ConnectedClientsList[i].ClientId, messageType, channelName, messageStream, securityOptions);
107+
}
108+
return;
109+
}
110+
92111
using (PooledBitStream stream = PooledBitStream.Get())
93112
{
94113
using (PooledBitWriter writer = PooledBitWriter.Get(stream))

MLAPI/NetworkingManagerComponents/Core/NetworkPoolManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static NetworkedObject SpawnPoolObject(string poolName, Vector3 position,
8181
writer.WriteSinglePacked(rotation.eulerAngles.y);
8282
writer.WriteSinglePacked(rotation.eulerAngles.z);
8383

84-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_SPAWN_POOL_OBJECT, "MLAPI_INTERNAL", stream);
84+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_SPAWN_POOL_OBJECT, "MLAPI_INTERNAL", stream, new InternalSecuritySendOptions(false, false));
8585
}
8686
}
8787
return netObject;
@@ -105,7 +105,7 @@ public static void DestroyPoolObject(NetworkedObject netObject)
105105
{
106106
writer.WriteUInt32Packed(netObject.NetworkId);
107107

108-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_DESTROY_POOL_OBJECT, "MLAPI_INTERNAL", stream);
108+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_DESTROY_POOL_OBJECT, "MLAPI_INTERNAL", stream, new InternalSecuritySendOptions(false, false));
109109
}
110110
}
111111
}

MLAPI/NetworkingManagerComponents/Core/NetworkSceneManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static void SwitchScene(string sceneName)
6767
{
6868
writer.WriteUInt32Packed(sceneNameToIndex[sceneName]);
6969

70-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_SWITCH_SCENE, "MLAPI_INTERNAL", stream);
70+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_SWITCH_SCENE, "MLAPI_INTERNAL", stream, new InternalSecuritySendOptions(false, false));
7171
}
7272
}
7373
}

MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal static void RemoveOwnership(uint netId)
8181
writer.WriteUInt32Packed(netId);
8282
writer.WriteUInt32Packed(netObject.OwnerClientId);
8383

84-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_CHANGE_OWNER, "MLAPI_INTERNAL", stream);
84+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_CHANGE_OWNER, "MLAPI_INTERNAL", stream, new InternalSecuritySendOptions(false, false));
8585
}
8686
}
8787
}
@@ -109,7 +109,7 @@ internal static void ChangeOwnership(uint netId, uint clientId)
109109
writer.WriteUInt32Packed(netId);
110110
writer.WriteUInt32Packed(clientId);
111111

112-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_CHANGE_OWNER, "MLAPI_INTERNAL", stream);
112+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_CHANGE_OWNER, "MLAPI_INTERNAL", stream, new InternalSecuritySendOptions(false, false));
113113
}
114114
}
115115
}
@@ -376,7 +376,7 @@ internal static void OnDestroyObject(uint networkId, bool destroyGameObject)
376376
{
377377
writer.WriteUInt32Packed(networkId);
378378

379-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_DESTROY_OBJECT, "MLAPI_INTERNAL", stream);
379+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_DESTROY_OBJECT, "MLAPI_INTERNAL", stream, new InternalSecuritySendOptions(false, false));
380380
}
381381
}
382382
}

0 commit comments

Comments
 (0)