Ejemplo n.º 1
0
        void SetupBanlist()
        {
            banlist = new BanList();

            if (!File.Exists ("banlist"))
                return;

            // The banlist parser can parse a standard block list from peerguardian or similar services
            BanListParser parser = new BanListParser();
            IEnumerable<AddressRange> ranges = parser.Parse(File.OpenRead("banlist"));
            banlist.AddRange(ranges);

            // Add a few IPAddress by hand
            banlist.Add(IPAddress.Parse("12.21.12.21"));
            banlist.Add(IPAddress.Parse("11.22.33.44"));
            banlist.Add(IPAddress.Parse("44.55.66.77"));

            engine.ConnectionManager.BanPeer += delegate (object o, AttemptConnectionEventArgs e){
                IPAddress address;

                // The engine can raise this event simultaenously on multiple threads
                if (IPAddress.TryParse(e.Peer.ConnectionUri.Host, out address)) {
                    lock (banlist) {
                        // If the value of e.BanPeer is true when the event completes,
                        // the connection will be closed. Otherwise it will be allowed
                        e.BanPeer = banlist.IsBanned(address);
                    }
                }
            };
        }