Ejemplo n.º 1
0
        public void Announce(bool compact)
        {
            var fakeHttpTracker = new FakeHttpTracker(compact, 5001);

            fakeHttpTracker.Start();

            var trackerClient = new HttpTracker(
                new NullLogger <HttpTracker>(),
                new LocalTcpConnectionOptions
            {
                Port          = 5000,
                BindAddress   = IPAddress.Loopback,
                PublicAddress = IPAddress.Loopback,
            },
                new Uri("http://localhost:5001/announce"));

            var response = trackerClient.Announce(_request).Result;
            var peers    = response.Peers.Cast <TcpTransportStream>().ToArray();

            Assert.That(peers, Has.Length.EqualTo(2));

            var peer1 = peers.Single(x => x.RemoteEndPoint.Port == 5001);

            Assert.That(peer1.RemoteEndPoint.Address, Is.EqualTo(IPAddress.Parse("192.168.0.1")));

            var peer2 = peers.Single(x => x.RemoteEndPoint.Port == 5002);

            Assert.That(peer2.RemoteEndPoint.Address, Is.EqualTo(IPAddress.Parse("192.168.0.2")));

            fakeHttpTracker.Stop();
        }
Ejemplo n.º 2
0
        public List <Peer> RequestPeersFromTracker(string addr)
        {
            Tracker tracker;

            if (addr.StartsWith("udp"))
            {
                tracker = new UdpTracker(addr, torrent);
            }
            else if (addr.StartsWith("http"))
            {
                tracker = new HttpTracker(addr, torrent);
            }
            else
            {
                Console.WriteLine("Unimplemented tracker protocol: " + addr); return(null);
            }

            if (tracker.Handshake())
            {
                Console.WriteLine("Handshake ok with " + addr);
                trackers.Add(tracker);
            }
            else
            {
                Console.WriteLine("Handshake failed with " + addr);
            }

            return(tracker.Scrape());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TransferManager" /> class.
        /// </summary>
        /// <param name="listeningPort">The port.</param>
        /// <param name="torrentInfo">The torrent information.</param>
        /// <param name="throttlingManager">The throttling manager.</param>
        /// <param name="persistenceManager">The persistence manager.</param>
        public TransferManager(int listeningPort, TorrentInfo torrentInfo, ThrottlingManager throttlingManager, PersistenceManager persistenceManager)
        {
            listeningPort.MustBeGreaterThanOrEqualTo(IPEndPoint.MinPort);
            listeningPort.MustBeLessThanOrEqualTo(IPEndPoint.MaxPort);
            torrentInfo.CannotBeNull();
            throttlingManager.CannotBeNull();
            persistenceManager.CannotBeNull();

            Tracker tracker = null;

            this.PeerId = "-AB1100-" + "0123456789ABCDEF".Random(24);

            Debug.WriteLine($"creating torrent manager for torrent {torrentInfo.InfoHash}");
            Debug.WriteLine($"local peer id {this.PeerId}");

            this.TorrentInfo = torrentInfo;

            this.throttlingManager = throttlingManager;

            this.persistenceManager = persistenceManager;

            // initialize trackers
            foreach (var trackerUri in torrentInfo.AnnounceList)
            {
                if (trackerUri.Scheme == "http" ||
                    trackerUri.Scheme == "https")
                {
                    tracker = new HttpTracker(trackerUri, this.PeerId, torrentInfo.InfoHash, listeningPort);
                }
                else if (trackerUri.Scheme == "udp")
                {
                    tracker = new UdpTracker(trackerUri, this.PeerId, torrentInfo.InfoHash, listeningPort);
                }

                if (tracker != null)
                {
                    tracker.TrackingEvent       = TrackingEvent.Started;
                    tracker.Announcing         += this.Tracker_Announcing;
                    tracker.Announced          += this.Tracker_Announced;
                    tracker.TrackingFailed     += this.Tracker_TrackingFailed;
                    tracker.BytesLeftToDownload = this.TorrentInfo.Length - this.Downloaded;
                    tracker.WantedPeerCount     = 30; // we can handle 30 peers at a time

                    this.trackers.Add(trackerUri, tracker);
                }
                else
                {
                    // unsupported tracker protocol
                    Debug.WriteLine($"unsupported tracker protocol {trackerUri.Scheme}");
                }
            }
        }