示例#1
0
        protected void LoadInternal(BEncodedDictionary torrentInformation)
        {
            Check.TorrentInformation(torrentInformation);
            _originalDictionary = torrentInformation;
            TorrentPath         = "";

            try
            {
                foreach (var 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);
                            }
                            if (e is FormatException)
                            {
                                throw new BEncodingException(
                                          string.Format("Could not parse {0} into a number", keypair.Value), e);
                            }
                            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"):
                        InfoHash = new InfoHash(SHA1Helper.ComputeHash(keypair.Value.Encode()));
                        ProcessInfo(((BEncodedDictionary)keypair.Value));
                        break;

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

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

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

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

                                Toolbox.Randomize(tier);

                                var collection = new RawTrackerTier();
                                for (var 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;
                }
                throw new BEncodingException("", e);
            }
        }
示例#2
0
        private void ParseMagnetLink(string url)
        {
            var 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
            }
            var parameters = splitStr[1].Split('&', ';');

            foreach (var keyval in parameters.Select(t => t.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.");
                    }

                    var val = keyval[1].Substring(9);
                    if (keyval[1].Substring(0, 9) == "urn:sha1:" || keyval[1].Substring(0, 9) == "urn:btih:")
                    {
                        switch (val.Length)
                        {
                        case 32:
                            InfoHash = InfoHash.FromBase32(val);
                            break;

                        case 40:
                            InfoHash = InfoHash.FromHex(val);
                            break;

                        default:
                            throw new FormatException("Infohash must be base32 or hex encoded.");
                        }
                    }
                    break;

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

                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;
                }
            }
        }
示例#3
0
 public override int GetHashCode()
 {
     return(InfoHash.GetHashCode());
 }