Example #1
0
 public bool Equals(byte[] other)
 {
     return(other == null || other.Length != 20 ? false : Toolbox.ByteMatch(Hash, other));
 }
Example #2
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;
                }
            }
        }
 public string ToHex()
 {
     return(Toolbox.ToHex(Hash));
 }
Example #4
0
 public bool Equals(byte[] other)
 {
     return(other != null && other.Length == 20 && Toolbox.ByteMatch(Hash, other));
 }
Example #5
0
        protected void LoadInternal(BEncodedDictionary torrentInformation)
        {
            Check.TorrentInformation(torrentInformation);
            this.originalDictionary = torrentInformation;
            this.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;
                        }
                        this.announceUrls.Add(new RawTrackerTier());
                        this.announceUrls[0].Add(keypair.Value.ToString());
                        break;

                    case ("creation date"):
                        try
                        {
                            try
                            {
                                this.creationDate = this.creationDate.AddSeconds(long.Parse(keypair.Value.ToString()));
                            }
                            catch (Exception e)
                            {
                                if (e is ArgumentOutOfRangeException)
                                {
                                    this.creationDate = this.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"):
                        if (keypair.Value.ToString().Length != 0)
                        {
                            this.nodes = (BEncodedList)keypair.Value;
                        }
                        break;

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

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

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

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

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

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

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

                    case ("info"):
                        using (SHA1 s = HashAlgoFactory.Create <SHA1>())
                            this.infoHash = new InfoHash(s.ComputeHash(keypair.Value.Encode()));
                        this.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)
                                {
                                    this.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)
                        {
                            this.getRightHttpSeeds.Add(((BEncodedString)keypair.Value).Text);
                        }
                        else if (keypair.Value is BEncodedList)
                        {
                            foreach (BEncodedString str in (BEncodedList)keypair.Value)
                            {
                                this.GetRightHttpSeeds.Add(str.Text);
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                if (e is BEncodingException)
                {
                    throw;
                }
                else
                {
                    throw new BEncodingException("", e);
                }
            }
        }
Example #6
0
 void RaiseHashed(TorrentCreatorEventArgs e)
 => Toolbox.RaiseAsyncEvent(Hashed, this, e);