Пример #1
0
 public static TestTorrentManagerInfo Create(
     int?pieceLength            = null,
     long?size                  = null,
     InfoHashes infoHashes      = null,
     string name                = null,
     IList <ITorrentFile> files = null)
 {
     return(Create <TestTorrentManagerInfo> (pieceLength, size, infoHashes, name, files));
 }
        public MagnetLink(InfoHashes infoHashes, string?name = null, IList <string>?announceUrls = null, IEnumerable <string>?webSeeds = null, long?size = null)
        {
            InfoHashes = infoHashes ?? throw new ArgumentNullException(nameof(infoHashes));

            Name         = name;
            AnnounceUrls = new List <string> (announceUrls ?? Array.Empty <string> ()).AsReadOnly();
            Webseeds     = new List <string> (webSeeds ?? Array.Empty <string> ()).AsReadOnly();
            Size         = size;
        }
Пример #3
0
 public static T Create <T> (
     int?pieceLength            = null,
     long?size                  = null,
     InfoHashes infoHashes      = null,
     string name                = null,
     IList <ITorrentFile> files = null)
     where T : TestTorrentManagerInfo, new ()
 {
     return(new T {
         TorrentInfo = new TestTorrentInfo {
             Files = files ?? Array.Empty <ITorrentFile> (),
             Name = name ?? "name",
             PieceLength = pieceLength ?? (4 * 16 * 1024),
             Size = size ?? (4 * 16 * 1024) * 32,
             InfoHashes = new InfoHashes(new InfoHash(new byte[20]), null)
         }
     });
 }
 public MagnetLink(InfoHash infoHash, string?name = null, IList <string>?announceUrls = null, IEnumerable <string>?webSeeds = null, long?size = null)
     : this(InfoHashes.FromInfoHash(infoHash), name, announceUrls, webSeeds, size)
 {
 }
        /// <summary>
        /// Parses a magnet link from the given Uri. The uri should be in the form magnet:?xt=urn:btih:
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static MagnetLink FromUri(Uri uri)
        {
            InfoHashes?infoHashes   = null;
            string?    name         = null;
            var        announceUrls = new List <string> ();
            var        webSeeds     = new List <string> ();
            long?      size         = null;

            if (uri.Scheme != "magnet")
            {
                throw new FormatException("Magnet links must start with 'magnet:'.");
            }

            string[] parameters = uri.Query.Substring(1).Split('&');
            for (int i = 0; i < parameters.Length; i++)
            {
                string[] keyval = parameters[i].Split('=');
                if (keyval.Length != 2)
                {
                    // Skip anything we don't understand. Urls could theoretically contain many
                    // unknown parameters.
                    continue;
                }
                switch (keyval[0].Substring(0, 2))
                {
                case "xt":    //exact topic
                    string val = keyval[1].Substring(9);
                    switch (keyval[1].Substring(0, 9))
                    {
                    case "urn:sha1:":        //base32 hash
                    case "urn:btih:":
                        if (infoHashes?.V1 != null)
                        {
                            throw new FormatException("More than one v1 infohash in magnet link is not allowed.");
                        }

                        if (val.Length == 32)
                        {
                            infoHashes = new InfoHashes(InfoHash.FromBase32(val), infoHashes?.V2);
                        }
                        else if (val.Length == 40)
                        {
                            infoHashes = new InfoHashes(InfoHash.FromHex(val), infoHashes?.V2);
                        }
                        else
                        {
                            throw new FormatException("Infohash must be base32 or hex encoded.");
                        }
                        break;

                    case "urn:btmh:":
                        if (infoHashes?.V2 != null)
                        {
                            throw new FormatException("More than one v2 multihash in magnet link is not allowed.");
                        }

                        // BEP52: Support v2 magnet links
                        infoHashes = new InfoHashes(infoHashes?.V1, InfoHash.FromMultiHash(val));
                        break;
                    }
                    break;

                case "tr":    //address tracker
                    announceUrls.Add(keyval[1].UrlDecodeUTF8());
                    break;

                case "as":    //Acceptable Source
                    webSeeds.Add(keyval[1].UrlDecodeUTF8());
                    break;

                case "dn":    //display name
                    name = keyval[1].UrlDecodeUTF8();
                    break;

                case "xl":    //exact length
                    size = long.Parse(keyval[1]);
                    break;

                //case "xs":// eXact Source - P2P link.
                //case "kt"://keyword topic
                //case "mt"://manifest topic
                // Unused
                //break;
                default:
                    // Unknown/unsupported
                    break;
                }
            }

            if (infoHashes == null)
            {
                throw new FormatException("The magnet link did not contain a valid 'xt' parameter referencing the infohash");
            }

            return(new MagnetLink(infoHashes, name, announceUrls, webSeeds, size));
        }