Exemplo n.º 1
0
        private void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            if (engine.ConnectionManager.ShouldBanPeer(e.Peer))
            {
                e.Connection.Dispose();
                return;
            }
            PeerId id = new PeerId(e.Peer, e.TorrentManager);

            id.Connection = e.Connection;

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

            if (id.Connection.IsIncoming)
            {
                List <InfoHash> skeys = new List <InfoHash>();

                ClientEngine.MainLoop.QueueWait((MainLoopTask) delegate {
                    for (int i = 0; i < engine.Torrents.Count; i++)
                    {
                        skeys.Add(engine.Torrents[i].InfoHash);
                    }
                });

                EncryptorFactory.BeginCheckEncryption(id, HandshakeMessage.HandshakeLength, endCheckEncryptionCallback, id, skeys.ToArray());
            }
            else
            {
                ClientEngine.MainLoop.Queue(delegate { engine.ConnectionManager.ProcessFreshConnection(id); });
            }
        }
        internal 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
                EncryptorFactory.BeginCheckEncryption(id, 0, this.endCheckEncryptionCallback, id);

                id.TorrentManager.Peers.ConnectedPeers.Add(id);
                id.WhenConnected = DateTime.Now;
                // Baseline the time the last block was received
                id.LastBlockReceived = DateTime.Now;
            }
            catch (Exception ex)
            {
                logger.Error("Process fresh connection error: {0}", ex.Message);
                id.TorrentManager.RaiseConnectionAttemptFailed(
                    new PeerConnectionFailedEventArgs(id.TorrentManager, id.Peer, Direction.Outgoing, "ProcessFreshConnection: failed to encrypt"));

                id.Connection.Dispose();
                id.Connection = null;
            }
        }
Exemplo n.º 3
0
        private void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            if (Engine.ConnectionManager.ShouldBanPeer(e.Peer))
            {
                e.Connection.Dispose();
                return;
            }
            var id = new PeerId(e.Peer, e.TorrentManager)
            {
                Connection = e.Connection
            };

            //Debug.WriteLine("ListenManager - ConnectionReceived: {0}", id.Connection);

            if (id.Connection.IsIncoming)
            {
                var skeys = new List <InfoHash>();

                ClientEngine.MainLoop.QueueWait(delegate { skeys.AddRange(Engine.Torrents.Select(t => t.InfoHash)); });

                EncryptorFactory.BeginCheckEncryption(id, HandshakeMessage.HandshakeLength, _endCheckEncryptionCallback,
                                                      id, skeys.ToArray());
            }
            else
            {
                ClientEngine.MainLoop.Queue(delegate { Engine.ConnectionManager.ProcessFreshConnection(id); });
            }
        }
Exemplo n.º 4
0
        public void InitiateTransfer(CustomConnection connection)
        {
            PeerId id = new PeerId(new Peer("", connection.Uri), rig.Manager);

            id.Connection = connection;
            byte[] data;

            EncryptorFactory.EndCheckEncryption(EncryptorFactory.BeginCheckEncryption(id, 68, null, null, new InfoHash[] { id.TorrentManager.InfoHash }), out data);
            decryptor = id.Decryptor;
            encryptor = id.Encryptor;
            TestHandshake(data, connection);
        }
Exemplo n.º 5
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();

            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.EndCheckEncryption(EncryptorFactory.BeginCheckEncryption(id, 68, null, null, new InfoHash[] { id.TorrentManager.InfoHash }), out data);
            decryptor = id.Decryptor;
            encryptor = id.Encryptor;
        }
Exemplo n.º 6
0
        private void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            if (engine.ConnectionManager.ShouldBanPeer(e.Peer))
            {
                e.Connection.Dispose();
                return;
            }
            PeerId id = new PeerId(e.Peer, e.TorrentManager);

            id.Connection = e.Connection;

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

            if (id.Connection.IsIncoming)
            {
                EncryptorFactory.BeginCheckEncryption(id, HandshakeMessage.HandshakeLength, endCheckEncryptionCallback, id, engine.SKeys);
            }
            else
            {
                ClientEngine.MainLoop.Queue(delegate { engine.ConnectionManager.ProcessFreshConnection(id); });
            }
        }
Exemplo n.º 7
0
        private void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            if (engine.ConnectionManager.ShouldBanPeer(e.Peer))
            {
                e.Connection.Dispose();
                return;
            }
            PeerId id = new PeerId(e.Peer, e.TorrentManager);

            id.Connection = e.Connection;

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

            if (id.Connection.IsIncoming)
            {
                if (!_hasAcceptedConnections)
                {
                    var ep = ((System.Net.IPEndPoint)id.Connection.EndPoint);

                    System.Net.NetworkInformation.NetworkInterface[] networkInterfaces
                        = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

                    var found = false;

                    foreach (System.Net.NetworkInformation.NetworkInterface network
                             in networkInterfaces)
                    {
                        System.Net.NetworkInformation.IPInterfaceProperties properties
                            = network.GetIPProperties();

                        foreach (System.Net.NetworkInformation.IPAddressInformation address
                                 in properties.UnicastAddresses)
                        {
                            if (address.Address.AddressFamily != AddressFamily.InterNetwork &&
                                address.Address.AddressFamily != AddressFamily.InterNetworkV6)
                            {
                                continue;
                            }

                            if (System.Net.IPAddress.IsLoopback(address.Address))
                            {
                                continue;
                            }

                            if (address.Address.ToString() == ep.Address.ToString())
                            {
                                found = true;
                            }
                        }
                    }

                    if (!found)
                    {
                        _hasAcceptedConnections = true;
                    }
                }

                List <InfoHash> skeys = new List <InfoHash>();

                ClientEngine.MainLoop.QueueWait((MainLoopTask) delegate {
                    for (int i = 0; i < engine.Torrents.Count; i++)
                    {
                        skeys.Add(engine.Torrents[i].InfoHash);
                    }
                });

                EncryptorFactory.BeginCheckEncryption(id, HandshakeMessage.HandshakeLength, endCheckEncryptionCallback, id, skeys.ToArray());
            }
            else
            {
                ClientEngine.MainLoop.Queue(delegate { engine.ConnectionManager.ProcessFreshConnection(id); });
            }
        }