From 88789cd7f3f172790177bb24187bf93aee9a7a92 Mon Sep 17 00:00:00 2001 From: Alan McGovern Date: Fri, 8 Jul 2022 14:40:07 +0100 Subject: [PATCH] [core] Support V1 and V2 encrypted connections Both the V1 hash and the truncated V2 hash should be addded as skeys. --- .../Managers/ConnectionManager.cs | 6 ++--- .../Managers/ListenManager.cs | 22 +++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/MonoTorrent.Client/MonoTorrent.Client/Managers/ConnectionManager.cs b/src/MonoTorrent.Client/MonoTorrent.Client/Managers/ConnectionManager.cs index 0534b56aa..c9062bcb2 100644 --- a/src/MonoTorrent.Client/MonoTorrent.Client/Managers/ConnectionManager.cs +++ b/src/MonoTorrent.Client/MonoTorrent.Client/Managers/ConnectionManager.cs @@ -188,9 +188,9 @@ internal async void ProcessNewOutgoingConnection (TorrentManager manager, PeerId try { // Create a handshake message to send to the peer - var handshake = new HandshakeMessage (manager.InfoHashes.V1OrV2, LocalPeerId, Constants.ProtocolStringV100); + var handshake = new HandshakeMessage (id.ExpectedInfoHash.Truncate (), LocalPeerId, Constants.ProtocolStringV100); var preferredEncryption = EncryptionTypes.GetPreferredEncryption (id.Peer.AllowedEncryption, Settings.AllowedEncryption); - EncryptorFactory.EncryptorResult result = await EncryptorFactory.CheckOutgoingConnectionAsync (id.Connection, preferredEncryption, manager.InfoHashes.V1OrV2.Truncate (), handshake, manager.Engine!.Factories); + EncryptorFactory.EncryptorResult result = await EncryptorFactory.CheckOutgoingConnectionAsync (id.Connection, preferredEncryption, id.ExpectedInfoHash.Truncate (), handshake, manager.Engine!.Factories); id.Decryptor = result.Decryptor; id.Encryptor = result.Encryptor; } catch { @@ -407,7 +407,7 @@ internal async ReusableTask IncomingConnectionAcceptedAsync (TorrentManage id.LastBlockReceived.Restart (); // Send our handshake now that we've decided to keep the connection - var handshake = new HandshakeMessage (manager.InfoHashes.V1OrV2, manager.Engine!.PeerId, Constants.ProtocolStringV100); + var handshake = new HandshakeMessage (id.ExpectedInfoHash.Truncate (), manager.Engine!.PeerId, Constants.ProtocolStringV100); await PeerIO.SendMessageAsync (id.Connection, id.Encryptor, handshake, manager.UploadLimiters, id.Monitor, manager.Monitor); manager.HandlePeerConnected (id); diff --git a/src/MonoTorrent.Client/MonoTorrent.Client/Managers/ListenManager.cs b/src/MonoTorrent.Client/MonoTorrent.Client/Managers/ListenManager.cs index 8069afbd2..5fefdeddb 100644 --- a/src/MonoTorrent.Client/MonoTorrent.Client/Managers/ListenManager.cs +++ b/src/MonoTorrent.Client/MonoTorrent.Client/Managers/ListenManager.cs @@ -28,6 +28,7 @@ using System; +using System.Collections.Generic; using System.Linq; using MonoTorrent.Client.Listeners; @@ -60,19 +61,22 @@ internal ListenManager (ClientEngine engine) public void Add (InfoHashes skey) { - var clone = new InfoHash[SKeys.Length + 1]; - Array.Copy (SKeys, clone, SKeys.Length); - clone[clone.Length - 1] = skey.V1OrV2.Truncate (); - SKeys = clone; + var clone = new List (SKeys); + if (skey.V1 != null) + clone.Add (skey.V1); + if (skey.V2 != null) + clone.Add (skey.V2.Truncate ()); + SKeys = clone.ToArray (); } public void Remove (InfoHashes skey) { - var clone = new InfoHash[SKeys.Length - 1]; - var index = Array.IndexOf (SKeys, skey.V1OrV2.Truncate ()); - Array.Copy (SKeys, clone, index); - Array.Copy (SKeys, index + 1, clone, index, clone.Length - index); - SKeys = clone; + var clone = new List (SKeys); + if (skey.V1 != null) + clone.Remove (skey.V1); + if (skey.V2 != null) + clone.Remove (skey.V2.Truncate ()); + SKeys = clone.ToArray (); } public void SetListener (IPeerConnectionListener listener)