@@ -100,6 +100,7 @@ internal set
100
100
/// </summary>
101
101
public bool isListening { get ; internal set ; }
102
102
private byte [ ] messageBuffer ;
103
+ private byte [ ] encryptionBuffer ;
103
104
/// <summary>
104
105
/// Gets if we are connected as a client
105
106
/// </summary>
@@ -282,9 +283,12 @@ private object Init(bool server)
282
283
lastReceiveTickTime = 0f ;
283
284
eventOvershootCounter = 0f ;
284
285
connectionPendingClients . Clear ( ) ;
286
+ hailPendingClients . Clear ( ) ;
287
+ pendingClientAesKeys . Clear ( ) ;
285
288
ConnectedClients . Clear ( ) ;
286
289
ConnectedClientsList . Clear ( ) ;
287
290
messageBuffer = new byte [ NetworkConfig . MessageBufferSize ] ;
291
+ encryptionBuffer = new byte [ NetworkConfig . EncryptionBufferSize ] ;
288
292
#if ! DISABLE_CRYPTOGRAPHY
289
293
pendingKeyExchanges . Clear ( ) ;
290
294
#endif
@@ -523,6 +527,19 @@ public void StopServer()
523
527
NetworkConfig . NetworkTransport . DisconnectClient ( clientId ) ;
524
528
}
525
529
}
530
+
531
+ foreach ( uint clientId in hailPendingClients )
532
+ {
533
+ if ( ! disconnectedIds . Contains ( clientId ) )
534
+ {
535
+ disconnectedIds . Add ( clientId ) ;
536
+ if ( clientId == NetworkConfig . NetworkTransport . ServerClientId )
537
+ continue ;
538
+
539
+ NetworkConfig . NetworkTransport . DisconnectClient ( clientId ) ;
540
+ }
541
+ }
542
+
526
543
isServer = false ;
527
544
Shutdown ( ) ;
528
545
}
@@ -812,13 +829,21 @@ private IEnumerator ApprovalTimeout(uint clientId)
812
829
{
813
830
float timeStarted = NetworkTime ;
814
831
//We yield every frame incase a pending client disconnects and someone else gets its connection id
815
- while ( NetworkTime - timeStarted < NetworkConfig . ClientConnectionBufferTimeout && connectionPendingClients . Contains ( clientId ) )
832
+ while ( NetworkTime - timeStarted < NetworkConfig . ClientConnectionBufferTimeout && ( connectionPendingClients . Contains ( clientId ) || hailPendingClients . Contains ( clientId ) ) )
816
833
{
817
834
yield return null ;
818
835
}
819
- if ( connectionPendingClients . Contains ( clientId ) && ! ConnectedClients . ContainsKey ( clientId ) )
836
+
837
+ if ( connectionPendingClients . Contains ( clientId ) && ! ConnectedClients . ContainsKey ( clientId ) )
838
+ {
839
+ // Timeout
840
+ if ( LogHelper . CurrentLogLevel <= LogLevel . Developer ) LogHelper . LogInfo ( "Client " + clientId + " Handshake Timed Out" ) ;
841
+ DisconnectClient ( clientId ) ;
842
+ }
843
+
844
+ if ( hailPendingClients . Contains ( clientId ) && ! ConnectedClients . ContainsKey ( clientId ) )
820
845
{
821
- //Timeout
846
+ // Timeout
822
847
if ( LogHelper . CurrentLogLevel <= LogLevel . Developer ) LogHelper . LogInfo ( "Client " + clientId + " Handshake Timed Out" ) ;
823
848
DisconnectClient ( clientId ) ;
824
849
}
@@ -830,8 +855,7 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
830
855
{
831
856
using ( BitStream bitStream = new BitStream ( data ) )
832
857
{
833
- RijndaelManaged rijndael = null ;
834
- Stream stream = bitStream ;
858
+ BitStream stream = bitStream ;
835
859
try
836
860
{
837
861
if ( LogHelper . CurrentLogLevel <= LogLevel . Developer ) LogHelper . LogInfo ( "Unwrapping Data Header" ) ;
@@ -845,11 +869,20 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
845
869
{
846
870
headerReader . SkipPadBits ( ) ;
847
871
headerReader . ReadByteArray ( IVBuffer , 16 ) ;
848
- rijndael = new RijndaelManaged ( ) ;
849
- rijndael . Padding = PaddingMode . PKCS7 ;
850
- rijndael . Key = isServer ? ( ConnectedClients . ContainsKey ( clientId ) ? ConnectedClients [ clientId ] . AesKey : pendingClientAesKeys [ clientId ] ) : clientAesKey ;
851
- rijndael . IV = IVBuffer ;
852
- stream = new CryptoStream ( bitStream , rijndael . CreateDecryptor ( ) , CryptoStreamMode . Read ) ;
872
+ stream = new BitStream ( encryptionBuffer ) ;
873
+ using ( RijndaelManaged rijndael = new RijndaelManaged ( ) )
874
+ {
875
+ rijndael . Padding = PaddingMode . PKCS7 ;
876
+ rijndael . Key = isServer ? ( ConnectedClients . ContainsKey ( clientId ) ? ConnectedClients [ clientId ] . AesKey : pendingClientAesKeys [ clientId ] ) : clientAesKey ;
877
+ rijndael . IV = IVBuffer ;
878
+ using ( CryptoStream cryptoStream = new CryptoStream ( bitStream , rijndael . CreateDecryptor ( ) , CryptoStreamMode . Read ) )
879
+ {
880
+ int readByte = 0 ;
881
+ while ( ( readByte = cryptoStream . ReadByte ( ) ) != - 1 )
882
+ stream . WriteByte ( ( byte ) readByte ) ;
883
+ }
884
+ }
885
+
853
886
using ( PooledBitReader reader = PooledBitReader . Get ( stream ) )
854
887
{
855
888
messageType = reader . ReadByteDirect ( ) ;
@@ -860,8 +893,8 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
860
893
headerReader . SkipPadBits ( ) ;
861
894
using ( HMACSHA256 hmac = new HMACSHA256 ( isServer ? ConnectedClients [ clientId ] . AesKey : clientAesKey ) )
862
895
{
863
- // 1 is the size of the header. 32 is the size of the hmac
864
896
headerReader . ReadByteArray ( HMACBuffer , 32 ) ;
897
+ // 1 is the size of the header. 32 is the size of the hmac
865
898
byte [ ] hmacBytes = hmac . ComputeHash ( bitStream . GetBuffer ( ) , 1 + 32 , totalSize - ( 1 + 32 ) ) ;
866
899
for ( int i = 0 ; i < hmacBytes . Length ; i ++ )
867
900
{
@@ -872,6 +905,7 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
872
905
}
873
906
}
874
907
}
908
+
875
909
messageType = headerReader . ReadByteDirect ( ) ;
876
910
}
877
911
else
@@ -886,7 +920,7 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
886
920
887
921
//Client tried to send a network message that was not the connection request before he was accepted.
888
922
if ( isServer && ( NetworkConfig . EnableEncryption && hailPendingClients . Contains ( clientId ) && messageType != MLAPIConstants . MLAPI_CERTIFICATE_HAIL_RESPONSE ) ||
889
- ( connectionPendingClients . Contains ( clientId ) && messageType != MLAPIConstants . MLAPI_CONNECTION_REQUEST ) )
923
+ ( connectionPendingClients . Contains ( clientId ) && messageType != MLAPIConstants . MLAPI_CONNECTION_REQUEST ) )
890
924
{
891
925
if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "Message recieved from clientId " + clientId + " before it has been accepted" ) ;
892
926
return ;
@@ -967,7 +1001,6 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
967
1001
finally
968
1002
{
969
1003
if ( stream != bitStream ) stream . Dispose ( ) ;
970
- if ( rijndael != null ) rijndael . Clear ( ) ;
971
1004
}
972
1005
}
973
1006
}
@@ -983,9 +1016,15 @@ internal void DisconnectClient(uint clientId)
983
1016
if ( connectionPendingClients . Contains ( clientId ) )
984
1017
connectionPendingClients . Remove ( clientId ) ;
985
1018
1019
+ if ( hailPendingClients . Contains ( clientId ) )
1020
+ hailPendingClients . Remove ( clientId ) ;
1021
+
986
1022
if ( ConnectedClients . ContainsKey ( clientId ) )
987
1023
ConnectedClients . Remove ( clientId ) ;
988
1024
1025
+ if ( pendingClientAesKeys . ContainsKey ( clientId ) )
1026
+ pendingClientAesKeys . Remove ( clientId ) ;
1027
+
989
1028
for ( int i = ConnectedClientsList . Count - 1 ; i > - 1 ; i -- )
990
1029
{
991
1030
if ( ConnectedClientsList [ i ] . ClientId == clientId )
@@ -1004,6 +1043,13 @@ internal void OnClientDisconnectFromServer(uint clientId)
1004
1043
{
1005
1044
if ( connectionPendingClients . Contains ( clientId ) )
1006
1045
connectionPendingClients . Remove ( clientId ) ;
1046
+
1047
+ if ( hailPendingClients . Contains ( clientId ) )
1048
+ hailPendingClients . Remove ( clientId ) ;
1049
+
1050
+ if ( pendingClientAesKeys . ContainsKey ( clientId ) )
1051
+ pendingClientAesKeys . Remove ( clientId ) ;
1052
+
1007
1053
if ( ConnectedClients . ContainsKey ( clientId ) )
1008
1054
{
1009
1055
if ( NetworkConfig . HandleObjectSpawning )
@@ -1060,12 +1106,16 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
1060
1106
{
1061
1107
if ( approved )
1062
1108
{
1063
- //Inform new client it got approved
1109
+ // Inform new client it got approved
1064
1110
if ( connectionPendingClients . Contains ( clientId ) )
1065
1111
connectionPendingClients . Remove ( clientId ) ;
1066
1112
1113
+ if ( hailPendingClients . Contains ( clientId ) )
1114
+ hailPendingClients . Remove ( clientId ) ;
1115
+
1067
1116
byte [ ] aesKey = pendingClientAesKeys . ContainsKey ( clientId ) ? pendingClientAesKeys [ clientId ] : null ;
1068
- pendingClientAesKeys . Remove ( clientId ) ;
1117
+ if ( pendingClientAesKeys . ContainsKey ( clientId ) )
1118
+ pendingClientAesKeys . Remove ( clientId ) ;
1069
1119
NetworkedClient client = new NetworkedClient ( )
1070
1120
{
1071
1121
ClientId = clientId ,
@@ -1183,6 +1233,9 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
1183
1233
if ( connectionPendingClients . Contains ( clientId ) )
1184
1234
connectionPendingClients . Remove ( clientId ) ;
1185
1235
1236
+ if ( hailPendingClients . Contains ( clientId ) )
1237
+ hailPendingClients . Remove ( clientId ) ;
1238
+
1186
1239
#if ! DISABLE_CRYPTOGRAPHY
1187
1240
if ( pendingKeyExchanges . ContainsKey ( clientId ) )
1188
1241
pendingKeyExchanges . Remove ( clientId ) ;
0 commit comments