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); } } }; }
public void Setup() { list = new BanList(); list.Add(new AddressRange(IPAddress.Parse("0.0.0.1"), IPAddress.Parse("0.0.0.10"))); list.Add(new AddressRange(IPAddress.Parse("255.255.255.0"), IPAddress.Parse("255.255.255.255"))); }
void UpdateBanLists() { foreach (var i in externalBanList) { try { Console.WriteLine("simpletorrent: Retrieving an external ban list..."); Console.WriteLine("simpletorrent: {0}", i); var count = 0; HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(i); wr.Timeout = 10000; var res = wr.GetResponse(); var stream = new StreamReader( new System.IO.Compression.GZipStream( res.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress, true)); var colon = new char[] { ':' }; var dash = new char[] { '-' }; BanList banList = new BanList(); while (stream.Peek() != -1) { var line = stream.ReadLine().Trim(); /* # List distributed by iblocklist.com China Internet Information Center (CNNIC):1.2.4.0-1.2.4.255 China Internet Information Center (CNNIC):1.2.8.0-1.2.8.255 */ if (line.Length > 0 && !line.StartsWith("#")) { try { var ips = line.Split(colon, 2)[1].Split(dash, 2); banList.Add(new AddressRange(IPAddress.Parse(ips[0]), IPAddress.Parse(ips[1]))); count++; if (count % 50000 == 0) { Console.WriteLine("simpletorrent: {0} entries...", count); } } catch { } } } res.Dispose(); lock (externalBanLists) { if (!externalBanLists.ContainsKey(i)) { externalBanLists.Add(i, banList); } else { externalBanLists[i] = banList; } } Console.WriteLine("simpletorrent: Successuflly loaded {0} entries.", count); } catch (Exception ex) { Console.WriteLine("simpletorrent: Error retrieving list ({0})...", ex.Message); } } }