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);
            id.Connection = e.Connection;

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

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

                ClientEngine.MainLoop.QueueWait(delegate
                {
                    for (var 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); });
            }
        }
        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.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();
            }
        }
        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); });
            }
        }
Exemple #4
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();
            }
        }
        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();
            }
        }
        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();
            }
        }
        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); });
            }
        }
        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); });
            }
        }
        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); });
            }
        }