Ejemplo n.º 1
0
        async void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            await ClientEngine.MainLoop;

            try {
                if (Engine.ConnectionManager.ShouldBanPeer(e.Peer))
                {
                    e.Connection.Dispose();
                    return;
                }

                if (!e.Connection.IsIncoming)
                {
                    var id = new PeerId(e.Peer, e.Connection, new MutableBitField(e.TorrentManager.Bitfield.Length).SetAll(false));
                    id.LastMessageSent.Restart();
                    id.LastMessageReceived.Restart();

                    Engine.ConnectionManager.ProcessNewOutgoingConnection(e.TorrentManager, id);
                    return;
                }

                logger.Info(e.Connection, "ConnectionReceived");

                var supportedEncryptions = EncryptionTypes.GetSupportedEncryption(e.Peer.AllowedEncryption, Engine.Settings.AllowedEncryption);
                EncryptorFactory.EncryptorResult result = await EncryptorFactory.CheckIncomingConnectionAsync(e.Connection, supportedEncryptions, SKeys);

                if (!await HandleHandshake(e.Peer, e.Connection, result.Handshake, result.Decryptor, result.Encryptor))
                {
                    e.Connection.Dispose();
                }
            } catch {
                e.Connection.Dispose();
            }
        }
Ejemplo n.º 2
0
 public async Task InitiateTransfer(CustomConnection connection, EncryptionTypes allowedEncryption)
 {
     EncryptorFactory.EncryptorResult result;
     if (connection.IsIncoming)
     {
         result = await EncryptorFactory.CheckIncomingConnectionAsync(connection, allowedEncryption, rig.Engine.Settings, HandshakeMessage.HandshakeLength, new [] { rig.Manager.InfoHash });
     }
     else
     {
         result = await EncryptorFactory.CheckOutgoingConnectionAsync(connection, allowedEncryption, rig.Engine.Settings, rig.Manager.InfoHash);
     }
     decryptor = result.Decryptor;
     encryptor = result.Encryptor;
     TestHandshake(result.InitialData, connection);
 }
Ejemplo n.º 3
0
        async void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            await ClientEngine.MainLoop;

            try
            {
                if (Engine.ConnectionManager.ShouldBanPeer(e.Peer))
                {
                    e.Connection.Dispose();
                    return;
                }

                if (!e.Connection.IsIncoming)
                {
                    var id = new PeerId(e.Peer, e.TorrentManager, e.Connection);
                    id.LastMessageSent.Restart();
                    id.LastMessageReceived.Restart();

                    Engine.ConnectionManager.ProcessNewOutgoingConnection(id);
                    return;
                }

                Logger.Log(e.Connection, "ListenManager - ConnectionReceived");

                var skeys = new List <InfoHash>();
                for (int i = 0; i < Engine.Torrents.Count; i++)
                {
                    skeys.Add(Engine.Torrents[i].InfoHash);
                }

                var result = await EncryptorFactory.CheckIncomingConnectionAsync(e.Connection, e.Peer.AllowedEncryption, Engine.Settings, HandshakeMessage.HandshakeLength, skeys.ToArray());

                var handshake = new HandshakeMessage();
                handshake.Decode(result.InitialData, 0, result.InitialData.Length);
                if (!await HandleHandshake(e.Peer, e.Connection, handshake, result.Decryptor, result.Encryptor))
                {
                    e.Connection.Dispose();
                }
            }
            catch
            {
                e.Connection.Dispose();
            }
        }
Ejemplo n.º 4
0
        public async Task Setup(bool metadataMode, bool multiFile = false, bool metadataOnly = false)
        {
            pair = new ConnectionPair().WithTimeout();
            rig  = multiFile ? TestRig.CreateMultiFile(32768, metadataMode) : TestRig.CreateSingleFile(1024 * 1024 * 1024, 32768, metadataMode);
            rig.RecreateManager().Wait();

            rig.Manager.HashChecked = true;
            await rig.Manager.StartAsync(metadataOnly);

            rig.AddConnection(pair.Outgoing);

            var    connection = pair.Incoming;
            PeerId id         = new PeerId(new Peer("", connection.Uri), connection, rig.Manager.Bitfield?.Clone().SetAll(false));

            var result = await EncryptorFactory.CheckIncomingConnectionAsync(id.Connection, id.Peer.AllowedEncryption, new[] { rig.Manager.InfoHash });

            decryptor = id.Decryptor = result.Decryptor;
            encryptor = id.Encryptor = result.Encryptor;
        }
Ejemplo n.º 5
0
        async void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            await ClientEngine.MainLoop;

            try
            {
                if (Engine.ConnectionManager.ShouldBanPeer(e.Peer))
                {
                    e.Connection.Dispose();
                    return;
                }

                if (!e.Connection.IsIncoming)
                {
                    var id = new PeerId(e.Peer, e.Connection, e.TorrentManager.Bitfield?.Clone().SetAll(false));
                    id.LastMessageSent.Restart();
                    id.LastMessageReceived.Restart();

                    Engine.ConnectionManager.ProcessNewOutgoingConnection(e.TorrentManager, id);
                    return;
                }

                Logger.Log(e.Connection, "ListenManager - ConnectionReceived");

                var skeys = new List <InfoHash>();
                for (int i = 0; i < Engine.Torrents.Count; i++)
                {
                    skeys.Add(Engine.Torrents[i].InfoHash);
                }

                var connection = ConnectionConverter.Convert(e.Connection);
                var result     = await EncryptorFactory.CheckIncomingConnectionAsync(connection, e.Peer.AllowedEncryption, Engine.Settings, skeys.ToArray());

                if (!await HandleHandshake(e.Peer, connection, result.Handshake, result.Decryptor, result.Encryptor))
                {
                    connection.Dispose();
                }
            }
            catch
            {
                e.Connection.Dispose();
            }
        }
Ejemplo n.º 6
0
        public async Task Setup(bool metadataMode, string metadataPath)
        {
            pair             = new ConnectionPair(55432);
            rig              = TestRig.CreateSingleFile(1024 * 1024 * 1024, 32768, metadataMode);
            rig.MetadataPath = metadataPath;
            rig.RecreateManager().Wait();

            rig.Manager.HashChecked = true;
            await rig.Manager.StartAsync();

            rig.AddConnection(pair.Outgoing);

            var    connection = pair.Incoming;
            PeerId id         = new PeerId(new Peer("", connection.Uri), rig.Manager, connection);

            var result = await EncryptorFactory.CheckIncomingConnectionAsync(id.Connection, id.Peer.AllowedEncryption, rig.Engine.Settings, HandshakeMessage.HandshakeLength, new InfoHash[] { id.TorrentManager.InfoHash });

            decryptor = id.Decryptor = result.Decryptor;
            encryptor = id.Encryptor = result.Encryptor;
        }
Ejemplo n.º 7
0
        public async Task Setup(bool metadataMode, bool multiFile = false, bool metadataOnly = false)
        {
            pair = new ConnectionPair().WithTimeout();
            rig  = multiFile ? TestRig.CreateMultiFile(32768, metadataMode) : TestRig.CreateSingleFile(Piece.BlockSize * 27, Piece.BlockSize * 2, metadataMode);
            rig.RecreateManager().Wait();

            // Mark the torrent as hash check complete with no data downloaded
            if (rig.Manager.HasMetadata)
            {
                rig.Manager.LoadFastResume(new FastResume(rig.Manager.InfoHash, rig.Manager.Bitfield.Clone().SetAll(false), rig.Manager.Bitfield.Clone().SetAll(false)));
            }
            await rig.Manager.StartAsync(metadataOnly);

            rig.AddConnection(pair.Outgoing);

            var    connection = pair.Incoming;
            PeerId id         = new PeerId(new Peer("", connection.Uri), connection, rig.Manager.Bitfield?.Clone().SetAll(false));

            var result = await EncryptorFactory.CheckIncomingConnectionAsync(id.Connection, id.Peer.AllowedEncryption, new[] { rig.Manager.InfoHash });

            decryptor = id.Decryptor = result.Decryptor;
            encryptor = id.Encryptor = result.Encryptor;
        }