Ejemplo n.º 1
0
        public void InfoHashTest()
        {
            MagnetLink link = MagnetLink.Parse("magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C");

            Assert.AreEqual(InfoHash.FromBase32("YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C"), link.InfoHash, "#1");

            //base32
            InfoHash initial = new InfoHash(System.Text.Encoding.ASCII.GetBytes("foobafoobafoobafooba"));

            link = MagnetLink.Parse("magnet:?xt=urn:sha1:MZXW6YTBMZXW6YTBMZXW6YTBMZXW6YTB");
            Assert.AreEqual(initial, link.InfoHash, "#2");

            //base40 = hex
            InfoHash hash = Create();
            string   hex  = hash.ToHex();

            link = MagnetLink.Parse("magnet:?xt=urn:btih:" + hex);
            Assert.AreEqual(hash, link.InfoHash, "#3");
        }
Ejemplo n.º 2
0
        void ParseMagnetLink(string url)
        {
            string[] splitStr = url.Split('?');
            if (splitStr.Length == 0 || splitStr[0] != "magnet:")
            {
                throw new FormatException("The magnet link must start with 'magnet:?'.");
            }

            if (splitStr.Length == 1)
            {
                return;//no parametter
            }
            string[] parameters = splitStr[1].Split('&', ';');

            for (int i = 0; i < parameters.Length; i++)
            {
                string[] keyval = parameters[i].Split('=');
                if (keyval.Length != 2)
                {
                    throw new FormatException("A field-value pair of the magnet link contain more than one equal'.");
                }
                switch (keyval[0].Substring(0, 2))
                {
                case "xt":    //exact topic
                    if (InfoHash != null)
                    {
                        throw new FormatException("More than one infohash in magnet link is not allowed.");
                    }

                    string val = keyval[1].Substring(9);
                    switch (keyval[1].Substring(0, 9))
                    {
                    case "urn:sha1:":        //base32 hash
                    case "urn:btih:":
                        if (val.Length == 32)
                        {
                            InfoHash = InfoHash.FromBase32(val);
                        }
                        else if (val.Length == 40)
                        {
                            InfoHash = InfoHash.FromHex(val);
                        }
                        else
                        {
                            throw new FormatException("Infohash must be base32 or hex encoded.");
                        }
                        break;
                    }
                    break;

                case "tr":     //address tracker
                    var bytes = UriHelper.UrlDecode(keyval[1]);
                    AnnounceUrls.Add(Encoding.UTF8.GetString(bytes));
                    break;

                case "ws":    //webseed
                case "as":    //acceptable source
                    Webseeds.Add(keyval[1]);
                    break;

                case "dn":    //display name
                    var name = UriHelper.UrlDecode(keyval[1]);
                    Name = Encoding.UTF8.GetString(name);
                    break;

                case "xl":    //exact length
                case "xs":    // eXact Source - P2P link.
                case "kt":    //keyword topic
                case "mt":    //manifest topic
                    //not supported for moment
                    break;

                default:
                    //not supported
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        protected void LoadInternal(BEncodedDictionary torrentInformation)
        {
            Check.TorrentInformation(torrentInformation);
            originalDictionary = torrentInformation;
            torrentPath        = "";

            try
            {
                foreach (KeyValuePair <BEncodedString, BEncodedValue> keypair in torrentInformation)
                {
                    switch (keypair.Key.Text)
                    {
                    case ("announce"):
                        // Ignore this if we have an announce-list
                        if (torrentInformation.ContainsKey("announce-list"))
                        {
                            break;
                        }
                        announceUrls.Add(new RawTrackerTier());
                        announceUrls[0].Add(keypair.Value.ToString());
                        break;

                    case ("creation date"):
                        try
                        {
                            try
                            {
                                creationDate = creationDate.AddSeconds(long.Parse(keypair.Value.ToString()));
                            }
                            catch (Exception e)
                            {
                                if (e is ArgumentOutOfRangeException)
                                {
                                    creationDate = creationDate.AddMilliseconds(long.Parse(keypair.Value.ToString()));
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            if (e is ArgumentOutOfRangeException)
                            {
                                throw new BEncodingException("Argument out of range exception when adding seconds to creation date.", e);
                            }
                            else if (e is FormatException)
                            {
                                throw new BEncodingException(String.Format("Could not parse {0} into a number", keypair.Value), e);
                            }
                            else
                            {
                                throw;
                            }
                        }
                        break;

                    case ("nodes"):
                        nodes = (BEncodedList)keypair.Value;
                        break;

                    case ("comment.utf-8"):
                        if (keypair.Value.ToString().Length != 0)
                        {
                            comment = keypair.Value.ToString();           // Always take the UTF-8 version
                        }
                        break;                                            // even if there's an existing value

                    case ("comment"):
                        if (String.IsNullOrEmpty(comment))
                        {
                            comment = keypair.Value.ToString();
                        }
                        break;

                    case ("publisher-url.utf-8"):                         // Always take the UTF-8 version
                        publisherUrl = keypair.Value.ToString();          // even if there's an existing value
                        break;

                    case ("publisher-url"):
                        if (String.IsNullOrEmpty(publisherUrl))
                        {
                            publisherUrl = keypair.Value.ToString();
                        }
                        break;

                    case ("azureus_properties"):
                        azureusProperties = keypair.Value;
                        break;

                    case ("created by"):
                        createdBy = keypair.Value.ToString();
                        break;

                    case ("encoding"):
                        encoding = keypair.Value.ToString();
                        break;

                    case ("info"):
                        using (SHA1 s = HashAlgoFactory.Create <SHA1>())
                            infoHash = new InfoHash(s.ComputeHash(keypair.Value.Encode()));
                        ProcessInfo(((BEncodedDictionary)keypair.Value));
                        break;

                    case ("name"):                                                   // Handled elsewhere
                        break;

                    case ("announce-list"):
                        if (keypair.Value is BEncodedString)
                        {
                            break;
                        }
                        BEncodedList announces = (BEncodedList)keypair.Value;

                        for (int j = 0; j < announces.Count; j++)
                        {
                            if (announces[j] is BEncodedList)
                            {
                                BEncodedList  bencodedTier = (BEncodedList)announces[j];
                                List <string> tier         = new List <string>(bencodedTier.Count);

                                for (int k = 0; k < bencodedTier.Count; k++)
                                {
                                    tier.Add(bencodedTier[k].ToString());
                                }

                                Toolbox.Randomize <string>(tier);

                                RawTrackerTier collection = new RawTrackerTier();
                                for (int k = 0; k < tier.Count; k++)
                                {
                                    collection.Add(tier[k]);
                                }

                                if (collection.Count != 0)
                                {
                                    announceUrls.Add(collection);
                                }
                            }
                            else
                            {
                                throw new BEncodingException(String.Format("Non-BEncodedList found in announce-list (found {0})",
                                                                           announces[j].GetType()));
                            }
                        }
                        break;

                    case ("httpseeds"):
                        // This form of web-seeding is not supported.
                        break;

                    case ("url-list"):
                        if (keypair.Value is BEncodedString)
                        {
                            getRightHttpSeeds.Add(((BEncodedString)keypair.Value).Text);
                        }
                        else if (keypair.Value is BEncodedList)
                        {
                            foreach (BEncodedString str in (BEncodedList)keypair.Value)
                            {
                                GetRightHttpSeeds.Add(str.Text);
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                if (e is BEncodingException)
                {
                    throw;
                }
                else
                {
                    throw new BEncodingException("", e);
                }
            }
        }
Ejemplo n.º 4
0
 public void NullHex()
 {
     Assert.Throws <ArgumentNullException> (() => {
         InfoHash.FromHex(null);
     });
 }
Ejemplo n.º 5
0
 public override int GetHashCode()
 {
     return(InfoHash.GetHashCode());
 }
Ejemplo n.º 6
0
 public void InvalidBase32()
 {
     Assert.Throws <ArgumentException> (() => InfoHash.FromBase32("YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5"));
     Assert.Throws <ArgumentException> (() => InfoHash.FromBase32("YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5?"));
 }
Ejemplo n.º 7
0
 public void InvalidHex()
 {
     Assert.Throws <ArgumentException> (() => {
         InfoHash.FromHex("123123123123123123123");
     });
 }
Ejemplo n.º 8
0
 public void NullHex()
 {
     InfoHash.FromHex(null);
 }
Ejemplo n.º 9
0
 public void InvalidHex()
 {
     InfoHash.FromHex("123123123123123123123");
 }
Ejemplo n.º 10
0
 //[ExpectedException(typeof(ArgumentNullException))]
 public void NullHex()
 {
     Assert.Catch <ArgumentException>(() =>
                                      InfoHash.FromHex(null));
 }
Ejemplo n.º 11
0
 //[ExpectedException (typeof(ArgumentException))]
 public void InvalidHex()
 {
     Assert.Catch <ArgumentException>(() =>
                                      InfoHash.FromHex("123123123123123123123"));
 }
Ejemplo n.º 12
0
        public void ParseFromMagnetLin()
        {
            var infoHash = InfoHash.FromMagnetLink("magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xl=12345");

            Assert.IsNotNull(infoHash);
        }