Ejemplo n.º 1
0
        void LoadInternal(BEncodedDictionary torrentInformation)
        {
            Check.TorrentInformation(torrentInformation);
            AnnounceUrls = new List <IList <string> > ().AsReadOnly();

            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 = new List <IList <string> > {
                        new List <string> {
                            keypair.Value.ToString()
                        }.AsReadOnly()
                    }.AsReadOnly();
                    break;

                case ("creation date"):
                    try {
                        try {
                            CreationDate = UnixEpoch.AddSeconds(long.Parse(keypair.Value.ToString()));
                        } catch (Exception e) {
                            if (e is ArgumentOutOfRangeException)
                            {
                                CreationDate = UnixEpoch.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($"Could not parse {keypair.Value} into a number", e);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    break;

                case ("nodes"):
                    if (keypair.Value is BEncodedList list)
                    {
                        Nodes = list;
                    }
                    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 ("created by"):
                    CreatedBy = keypair.Value.ToString();
                    break;

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

                case ("info"):
                    using (SHA1 s = HashAlgoFactory.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;
                    }

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

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

                            Toolbox.Randomize(tier);

                            var resultTier = new List <string> ();
                            for (int k = 0; k < tier.Count; k++)
                            {
                                resultTier.Add(tier[k]);
                            }

                            if (resultTier.Count != 0)
                            {
                                result.Add(tier.AsReadOnly());
                            }
                        }
                        else
                        {
                            throw new BEncodingException(
                                      $"Non-BEncodedList found in announce-list (found {announces[j].GetType ()})");
                        }
                    }
                    if (result.Count > 0)
                    {
                        AnnounceUrls = result.AsReadOnly();
                    }
                    break;

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

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

                default:
                    break;
                }
            }
        }