Skip to content

Commit

Permalink
[core] Support V1 and V2 encrypted connections
Browse files Browse the repository at this point in the history
Both the V1 hash and the truncated V2 hash should be addded
as skeys.
  • Loading branch information
alanmcgovern committed Jul 8, 2022
1 parent 9902cf5 commit 88789cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -407,7 +407,7 @@ internal async ReusableTask<bool> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@


using System;
using System.Collections.Generic;
using System.Linq;

using MonoTorrent.Client.Listeners;
Expand Down Expand Up @@ -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<InfoHash> (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<InfoHash> (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)
Expand Down

0 comments on commit 88789cd

Please sign in to comment.