示例#1
0
 private void CheckInfoFiles(BencodedList files)
 {
     if (files == null)
     {
         throw new TorrentException("Invalid metadata, \'files\' not of expected type.");
     }
 }
示例#2
0
        /// <summary>
        ///     Creates a .torrent file with the given data.
        /// </summary>
        /// <param name="name">The name of the file.</param>
        /// <param name="inputFiles">A List'string containing all the files to be added to the .torrent file.</param>
        /// <param name="filesDir">The root directory of all the files in the .torrent file.</param>
        /// <param name="announce">The announce URL of the tracker, in which the .torrent file will be uploaded to.</param>
        /// <param name="announceList">A list of announce URLs.</param>
        /// <param name="pieceLength">The number of bytes in each piece.</param>
        /// <param name="pieces">A list of strings consisting of the concatenation of all 20-byte SHA1 hash values.</param>
        /// <returns>A string representig the content of the .torrent file.</returns>
        public static string Create(string name, List <FileEntry> inputFiles, string filesDir, string announce,
                                    List <string> announceList, int pieceLength, List <byte[]> pieces)
        {
            var clientVersion = Global.Instance.Version;
            var res           = new BencodedDictionary();

            res.Add("announce", new BencodedString(announce));
            var alist = new BencodedList();

            announceList.ForEach(a => alist.Add(new BencodedList {
                new BencodedString(a)
            }));
            res.Add("announce-list", alist);
            res.Add("created by", new BencodedString("rtTorrent/" + clientVersion));
            res.Add("creation date", new BencodedInteger(GetUnixTime()));
            res.Add("encoding", new BencodedString("UTF-8"));

            var info = new BencodedDictionary();

            info.Add("piece length", new BencodedInteger(pieceLength));

            var piecesString = new StringBuilder();

            pieces.ForEach(piece => piece.ForEach(b => piecesString.Append((char)b)));

            info.Add("pieces", new BencodedString(piecesString.ToString()));

            //"1" - the client MUST publish its presence to get other peers ONLY via the trackers explicitly described in the metainfo file
            //"0" - the client may obtain peer from other means, e.g. PEX peer exchange, dht. Here, "private" may be read as "no external peer source".
            //info.Add("private", new BencodedInteger(0));

            if (inputFiles.Count == 1)
            {
                info.Add("name", new BencodedString(name));
                info.Add("length", new BencodedInteger(inputFiles[0].Length));
                //MD5 hash of the file should be added here. The BitTorrent protocol specifies it as NOT needed.
            }
            else
            {
                info.Add("name", new BencodedString(filesDir));
                var files = new BencodedList();

                foreach (var inputFile in inputFiles)
                {
                    var aFile = new BencodedDictionary();
                    aFile.Add("length", new BencodedInteger(inputFile.Length));

                    var filePath = new BencodedList();
                    inputFile.Name.Split(Path.DirectorySeparatorChar).ForEach(
                        dir => filePath.Add(new BencodedString(dir)));
                    aFile.Add("path", filePath);
                    files.Add(aFile);
                }
                info.Add("files", files);
            }
            res.Add("info", info);
            return(res.ToString());
        }
示例#3
0
        private List <string> DecodeAnnounceList(BencodedList announceList)
        {
            var list = new List <string>();

            foreach (BencodedList urllist in announceList)
            {
                list.Add((BencodedString)urllist.First());
            }
            return(list);
        }
示例#4
0
 private List<string> DecodeAnnounceList(BencodedList announceList)
 {
     var list = new List<string>();
     foreach (BencodedList urllist in announceList)
     {
         list.Add((BencodedString) urllist.First());
     }
     return list;
 }
示例#5
0
 private void CheckInfoFiles(BencodedList files)
 {
     if (files == null)
         throw new TorrentException(string.Format("Invalid metadata, 'files' not of expected type."));
 }
示例#6
0
        /// <summary>
        /// Creates a .torrent file with the given data.
        /// </summary>
        /// <param name="name">The name of the file.</param>
        /// <param name="inputFiles">A List'string containing all the files to be added to the .torrent file.</param>
        /// <param name="filesDir">The root directory of all the files in the .torrent file.</param>
        /// <param name="announce">The announce URL of the tracker, in which the .torrent file will be uploaded to.</param>
        /// <param name="announceList">A list of announce URLs.</param>
        /// <param name="pieceLength">The number of bytes in each piece.</param>
        /// <param name="pieces">A list of strings consisting of the concatenation of all 20-byte SHA1 hash values.</param>
        /// <returns>A string representig the content of the .torrent file.</returns>
        public static string Create(string name, List<FileEntry> inputFiles, string filesDir, string announce,
                                    List<string> announceList, int pieceLength, List<byte[]> pieces)
        {
            Contract.Requires(name != null);
            Contract.Requires(inputFiles != null);
            Contract.Requires(inputFiles.Count >= 1);
            Contract.Requires(announce != null);
            Contract.Requires(pieces != null);

            var clientVersion = Global.Instance.Version;
            var res = new BencodedDictionary();
            res.Add("announce", new BencodedString(announce));
            var alist = new BencodedList();
            announceList.ForEach(a => alist.Add(new BencodedList {new BencodedString(a)}));
            res.Add("announce-list", alist);
            res.Add("created by", new BencodedString("rtTorrent/" + clientVersion));
            res.Add("creation date", new BencodedInteger(GetUnixTime()));
            res.Add("encoding", new BencodedString("UTF-8"));

            var info = new BencodedDictionary();

            info.Add("piece length", new BencodedInteger(pieceLength));

            var piecesString = new StringBuilder();
            pieces.ForEach(piece => piece.ForEach(b => piecesString.Append((char) b)));

            info.Add("pieces", new BencodedString(piecesString.ToString()));

            //"1" - the client MUST publish its presence to get other peers ONLY via the trackers explicitly described in the metainfo file
            //"0" - the client may obtain peer from other means, e.g. PEX peer exchange, dht. Here, "private" may be read as "no external peer source".
            //info.Add("private", new BencodedInteger(0));

            if (inputFiles.Count == 1)
            {
                info.Add("name", new BencodedString(name));
                info.Add("length", new BencodedInteger(inputFiles[0].Length));
                //MD5 hash of the file should be added here. The BitTorrent protocol specifies it as NOT needed.
            }
            else
            {
                info.Add("name", new BencodedString(filesDir));
                var files = new BencodedList();

                foreach (FileEntry inputFile in inputFiles)
                {
                    var aFile = new BencodedDictionary();
                    aFile.Add("length", new BencodedInteger(inputFile.Length));

                    var filePath = new BencodedList();
                    inputFile.Name.Split(Path.DirectorySeparatorChar).ForEach(
                        dir => filePath.Add(new BencodedString(dir)));
                    aFile.Add("path", filePath);
                    files.Add(aFile);
                }
                info.Add("files", files);
            }
            res.Add("info", info);
            return res.ToString();
        }