Esempio n. 1
0
        static LocalPeerListener()
        {
            var guid      = Guid.NewGuid();
            var guidBytes = guid.ToByteArray();

            cookie = HexHelper.BytesToHex(guidBytes, 0, 4);
        }
Esempio n. 2
0
 /// <summary>
 /// Returns the hexadecimal string for this peer ID.
 /// </summary>
 /// <returns>The hexadecimal string.</returns>
 public string ToHexString()
 {
     if (id != null)
     {
         return(HexHelper.BytesToHex(id));
     }
     else
     {
         return(string.Empty);
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Returns the text-representation of this info hash.
 /// </summary>
 /// <returns>The hash string.</returns>
 public override string ToString()
 {
     if (hash != null)
     {
         return(HexHelper.BytesToHex(hash));
     }
     else
     {
         return("<NONE>");
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Returns the text-representation of this peer ID.
        /// </summary>
        /// <returns>The peer client info with ID.</returns>
        public override string ToString()
        {
            if (id != null)
            {
                string idHex = HexHelper.BytesToHex(id);

                string clientName;
                string clientVersion;
                if (PeerHelper.TryGetPeerClientInfo(this, out clientName, out clientVersion))
                {
                    return(string.Format("[{0} (v{1}), RAW:{2}]", clientName, clientVersion, idHex));
                }
                else
                {
                    return(string.Format("[UNKNOWN, RAW:{0}]", idHex));
                }
            }
            else
            {
                return("<NONE>");
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Returns the text-representation of this piece hash.
 /// </summary>
 /// <returns>The hash in hexadecimals.</returns>
 public override string ToString()
 {
     return(HexHelper.BytesToHex(hash));
 }
Esempio n. 6
0
        private BEncoding.Dictionary Save()
        {
            var torrentInfo = new BEncoding.Dictionary();

            // Make sure that there is a piece size
            if (pieceSize == 0)
            {
                throw new InvalidOperationException("No piece size has been set yet.");
            }
            else if (!MathHelper.IsPowerOfTwo(pieceSize))
            {
                throw new InvalidOperationException("The piece size must be a power of two.");
            }
            else if (files == null || files.Length == 0)
            {
                throw new InvalidOperationException("No files have been defined yet.");
            }
            else if (totalSize == 0L)
            {
                throw new InvalidOperationException("The total size of the torrent cannot be 0.");
            }

            int pieceCount = (int)((totalSize - 1) / pieceSize) + 1;

            if (pieceCount != pieceHashes.Length)
            {
                throw new InvalidOperationException(string.Format("The calculated number of pieces is {0} while there are {1} piece hashes.", pieceCount, pieceHashes.Length));
            }

            if (announces != null && announces.Length > 0)
            {
                if (!string.IsNullOrEmpty(announces[0].Url))
                {
                    torrentInfo.Add("announce", announces[0].Url);
                }

                var trackerList = new BEncoding.List();
                for (int i = 0; i < announces.Length; i++)
                {
                    var urls = announces[i].Urls;
                    if (urls == null || urls.Length == 0)
                    {
                        continue;
                    }

                    var urlList = new BEncoding.List();
                    for (int j = 0; j < urls.Length; j++)
                    {
                        if (!string.IsNullOrEmpty(urls[j]))
                        {
                            urlList.Add(urls[j]);
                        }
                    }

                    if (urlList.Count > 0)
                    {
                        trackerList.Add(urlList);
                    }
                }

                if (trackerList.Count > 0)
                {
                    torrentInfo.Add("announce-list", trackerList);
                }
            }

            if (!string.IsNullOrEmpty(comment))
            {
                torrentInfo.Add("comment", comment);
            }
            if (!string.IsNullOrEmpty(createdBy))
            {
                torrentInfo.Add("created by", createdBy);
            }
            if (creationDate != DateTime.MinValue)
            {
                long creationDateTimestamp = TimeHelper.GetUnixTimestampFromDate(creationDate);
                torrentInfo.Add("creation date", creationDateTimestamp);
            }

            // Get the piece hash data
            byte[] pieceHashData = new byte[20 * pieceCount];
            for (int i = 0; i < pieceCount; i++)
            {
                Buffer.BlockCopy(pieceHashes[i].Hash, 0, pieceHashData, (i * 20), 20);
            }

            var info = new BEncoding.Dictionary();

            info.Add("piece length", pieceSize);
            info.Add("pieces", pieceHashData);
            info.Add("private", (isPrivate ? 1 : 0));

            if (!string.IsNullOrEmpty(source))
            {
                info.Add("source", source);
            }

            if (files != null)
            {
                if (files.Length == 1)
                {
                    var fileItem = files[0];
                    info.Add("length", fileItem.Size);

                    if (fileItem.MD5Hash != null && fileItem.MD5Hash.Length == 16)
                    {
                        string fileMD5HashHex = HexHelper.BytesToHex(fileItem.MD5Hash);
                        info.Add("md5sum", fileMD5HashHex);
                    }

                    info.Add("name", GetFileName(fileItem.Path));
                }
                else if (files.Length > 1)
                {
                    info.Add("name", name ?? "Unnamed");

                    var fileList = new BEncoding.List();
                    for (int i = 0; i < files.Length; i++)
                    {
                        var fileItem       = files[i];
                        var fileDictionary = new BEncoding.Dictionary();
                        fileDictionary.Add("length", fileItem.Size);

                        if (fileItem.MD5Hash != null && fileItem.MD5Hash.Length == 16)
                        {
                            string fileMD5HashHex = HexHelper.BytesToHex(fileItem.MD5Hash);
                            info.Add("md5sum", fileMD5HashHex);
                        }

                        string[] pathParts = fileItem.Path.Split(new char[] { '/' });
                        var      pathList  = new BEncoding.List(pathParts);
                        fileDictionary.Add("path", pathList);
                        fileList.Add(fileDictionary);
                    }

                    info.Add("files", fileList);
                }
            }

            infoHash = ComputeInfoHash(info);
            torrentInfo.Add("info", info);
            return(torrentInfo);
        }