Esempio n. 1
0
        static void Main(string[] args)
        {
            var peerId = "-YD0010-DEV CLIENT..";

            // Load a sample torrent file
            var fileContents = File.ReadAllBytes(@"insert-sample-torrent-path-here.torrent");

            // Get an object dictionary of the file contents
            var objects = BEncode.DecodeObject(new DataIndex(fileContents));

            // Parse the contents of the BEncode decoding
            var torrent = new Torrent(objects);

            Tracker.AnnounceRequest(peerId, torrent);

            TestEncoding(objects, fileContents);
            TestPieceHashing(torrent);

            System.Console.ReadLine();
        }
Esempio n. 2
0
        /// <summary>
        /// Make a request to the announce URL.
        /// </summary>
        /// <param name="peerId">This client's peer ID.</param>
        /// <param name="torrent">The torrent information.</param>
        public static void AnnounceRequest(string peerId, Torrent torrent)
        {
            var builder = new UriBuilder(torrent.Announce);
            var query = HttpUtility.ParseQueryString(builder.Query);

            query["peer_id"] = peerId;
            query["port"] = "26644";
            query["uploaded"] = "0";
            query["downloaded"] = "0";
            query["left"] = torrent.TorrentInfo.FileInfos.Sum(i => i.Length).ToString();
            query["compact"] = "1";
            query["event"] = "started";

            // Add info hash manually since we had to manually encode it
            var info_hash = Encoding.UTF8.GetString(WebUtility.UrlEncodeToBytes(torrent.InfoHash, 0, 20));
            builder.Query = string.Format("{0}&info_hash={1}", query.ToString(), info_hash);

            var announceRequest = new WebClient();
            announceRequest.Proxy = null;
            var result = announceRequest.DownloadData(builder.Uri);
            var announceResults = BEncode.DecodeObject(new DataIndex(result));
        }
Esempio n. 3
0
        private static void TestPieceHashing(Torrent torrent)
        {
            // Load the contents of the downloaded file
            var dlContents = File.ReadAllBytes(@"insert-sample-downloaded-file-here");

            // Compute the hash of the real downloaded file
            var sha1 = SHA1.Create();
            var sha1Hash = sha1.ComputeHash(dlContents.Take((int) torrent.TorrentInfo.PieceLength).ToArray());

            if (sha1Hash.SequenceEqual(torrent.TorrentInfo.Pieces.First()))
            {
                // If this evaluates to true, the following has been verified:
                // - The BEncode decoding worked correctly.
                // - The piece length was read in accurately.
                // - The pieces list was read in correctly.
                // - The SHA1 computation we just computed is the same as the first set of bytes in the pieces section of the torrent file.
                System.Console.WriteLine("Success: SHA1 hash of the first piece corresponds to the piece hash found in the torrent file.");
            }
            else
            {
                throw new Exception("Failure: SHA1 hash of the first piece did not match the piece hash found in the torrent file.");
            }
        }