Ejemplo n.º 1
0
        internal async void ProcessFreshConnection(PeerId id)
        {
            // If we have too many open connections, close the connection
            if (OpenConnections > this.MaxOpenConnections)
            {
                CleanupSocket(id, "Too many connections");
                return;
            }

            try
            {
                id.ProcessingQueue = true;
                // Increase the count of the "open" connections
                var initialData = await EncryptorFactory.CheckEncryptionAsync(id, 0, new[] { id.TorrentManager.InfoHash });

                EndCheckEncryption(id, initialData);


                id.TorrentManager.Peers.ConnectedPeers.Add(id);
                id.WhenConnected.Restart();
                // Baseline the time the last block was received
                id.LastBlockReceived.Restart();
            }
            catch (Exception)
            {
                id.TorrentManager.RaiseConnectionAttemptFailed(
                    new PeerConnectionFailedEventArgs(id.TorrentManager, id.Peer, Direction.Outgoing, "ProcessFreshConnection: failed to encrypt"));

                id.Connection.Dispose();
                id.Connection = null;
            }
        }
Ejemplo n.º 2
0
        private async void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            await ClientEngine.MainLoop;

            try
            {
                if (Engine.ConnectionManager.ShouldBanPeer(e.Peer))
                {
                    e.Connection.Dispose();
                    return;
                }
                var id = new PeerId(e.Peer, e.TorrentManager);
                id.Connection = e.Connection;
                if (!e.Connection.IsIncoming)
                {
                    Engine.ConnectionManager.ProcessFreshConnection(id);
                    return;
                }

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

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

                var initialData = await EncryptorFactory.CheckEncryptionAsync(id, HandshakeMessage.HandshakeLength, skeys.ToArray());

                if (initialData != null && initialData.Length != HandshakeMessage.HandshakeLength)
                {
                    e.Connection.Dispose();
                    return;
                }

                HandshakeMessage handshake;
                if (initialData == null)
                {
                    handshake = await PeerIO.ReceiveHandshakeAsync(id.Connection, id.Decryptor);
                }
                else
                {
                    handshake = new HandshakeMessage();
                    handshake.Decode(initialData, 0, initialData.Length);
                }
                if (!await HandleHandshake(id, handshake))
                {
                    e.Connection.Dispose();
                }
            }
            catch
            {
                e.Connection.Dispose();
            }
        }
Ejemplo n.º 3
0
        public async Task InitiateTransfer(CustomConnection connection, EncryptionTypes allowedEncryption)
        {
            PeerId id = new PeerId(new Peer("", connection.Uri), rig.Manager);

            id.Peer.Encryption = allowedEncryption;
            id.Connection      = connection;

            var data = await EncryptorFactory.CheckEncryptionAsync(id, 68, new InfoHash[] { id.TorrentManager.InfoHash });

            decryptor = id.Decryptor;
            encryptor = id.Encryptor;
            TestHandshake(data, connection);
        }
Ejemplo n.º 4
0
        public void 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;
            rig.Manager.Start();
            rig.AddConnection(pair.Outgoing);

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

            id.Connection = connection;


            byte[] data = EncryptorFactory.CheckEncryptionAsync(id, 68, new InfoHash[] { id.TorrentManager.InfoHash }).Result;
            decryptor = id.Decryptor;
            encryptor = id.Encryptor;
        }