public void AddPeers_PrivateTorrent() { // You can't manually add peers to private torrents var dict = (BEncodedDictionary)rig.TorrentDict["info"]; dict["private"] = (BEncodedString)"1"; Torrent t = Torrent.Load(rig.TorrentDict); TorrentManager manager = new TorrentManager(t, "path", new TorrentSettings()); Assert.ThrowsAsync <InvalidOperationException>(() => manager.AddPeerAsync(new Peer("id", new Uri("tcp:://whatever.com")))); }
private async void OnReceiveCallBack(IAsyncResult ar) { UdpClient u = (UdpClient)ar.AsyncState; IPEndPoint e = new IPEndPoint(IPAddress.Any, 0); try { byte[] receiveBytes = u.EndReceive(ar, ref e); string receiveString = Encoding.ASCII.GetString(receiveBytes); Regex exp = new Regex("BT-SEARCH \\* HTTP/1.1\\r\\nHost: 239.192.152.143:6771\\r\\nPort: (?<port>[^@]+)\\r\\nInfohash: (?<hash>[^@]+)\\r\\n\\r\\n\\r\\n"); Match match = exp.Match(receiveString); if (!match.Success) { return; } int portcheck = Convert.ToInt32(match.Groups["port"].Value); if (portcheck < 0 || portcheck > 65535) { return; } TorrentManager manager = null; InfoHash matchHash = InfoHash.FromHex(match.Groups["hash"].Value); for (int i = 0; manager == null && i < engine.Torrents.Count; i++) { if (engine.Torrents [i].InfoHash == matchHash) { manager = engine.Torrents [i]; } } if (manager == null) { return; } Uri uri = new Uri("ipv4://" + e.Address.ToString() + ':' + match.Groups["port"].Value); Peer peer = new Peer("", uri, EncryptionTypes.All); // Add new peer to matched Torrent if (!manager.HasMetadata || !manager.Torrent.IsPrivate) { await ClientEngine.MainLoop; int count = await manager.AddPeerAsync(peer) ? 1 : 0; manager.RaisePeersFound(new LocalPeersAdded(manager, count, 1)); } } catch { // Failed to receive data, ignore } finally { try { u.BeginReceive(OnReceiveCallBack, ar.AsyncState); } catch { // It's closed } } }