public void Setup()
        {
            HashAlgoFactory.Register <SHA1, SHA1Fake>();

            creator   = new TestTorrentCreator();
            announces = new RawTrackerTiers();
            announces.Add(new RawTrackerTier(new string[] { "http://tier1.com/announce1", "http://tier1.com/announce2" }));
            announces.Add(new RawTrackerTier(new string[] { "http://tier2.com/announce1", "http://tier2.com/announce2" }));

            creator.Comment      = Comment;
            creator.CreatedBy    = CreatedBy;
            creator.PieceLength  = PieceLength;
            creator.Publisher    = Publisher;
            creator.PublisherUrl = PublisherUrl;
            creator.SetCustom(CustomKey, CustomValue);
            files = new List <TorrentFile>(new TorrentFile[] {
                new TorrentFile(Path.Combine(Path.Combine("Dir1", "SDir1"), "File1"), (int)(PieceLength * 2.30), 0, 1),
                new TorrentFile(Path.Combine(Path.Combine("Dir1", "SDir1"), "File2"), (int)(PieceLength * 36.5), 1, 3),
                new TorrentFile(Path.Combine(Path.Combine("Dir1", "SDir2"), "File3"), (int)(PieceLength * 3.17), 3, 12),
                new TorrentFile(Path.Combine(Path.Combine("Dir2", "SDir1"), "File4"), (int)(PieceLength * 1.22), 12, 15),
                new TorrentFile(Path.Combine(Path.Combine("Dir2", "SDir2"), "File5"), (int)(PieceLength * 6.94), 15, 15),
            });

            writer           = new TestWriter();
            writer.DontWrite = true;
        }
Пример #2
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);
                }
            }
        }
Пример #3
0
 public void FixtureTeardown()
 {
     HashAlgoFactory.Register <SHA1, SHA1CryptoServiceProvider>();
 }
Пример #4
0
 public void FixtureSetup()
 {
     HashAlgoFactory.Register <SHA1, SHA1Fake>();
 }
Пример #5
0
        byte [] CalcPiecesHash(List <TorrentFile> files, PieceWriter writer)
        {
            byte []     buffer        = null;
            int         bufferRead    = 0;
            long        fileRead      = 0;
            long        overallRead   = 0;
            long        overallTotal  = 0;
            MD5         md5Hasher     = null;
            SHA1        shaHasher     = null;
            List <byte> torrentHashes = null;

            shaHasher     = HashAlgoFactory.Create <SHA1> ();
            torrentHashes = new List <byte> ();
            overallTotal  = Toolbox.Accumulate <TorrentFile> (files, delegate(TorrentFile m) { return(m.Length); });

            long pieceLength = PieceLength;

            buffer = new byte [pieceLength];

            if (StoreMD5)
            {
                md5Hasher = HashAlgoFactory.Create <MD5> ();
            }

            try {
                foreach (TorrentFile file in files)
                {
                    fileRead = 0;
                    if (md5Hasher != null)
                    {
                        md5Hasher.Initialize();
                    }

                    while (fileRead < file.Length)
                    {
                        int toRead = (int)Math.Min(buffer.Length - bufferRead, file.Length - fileRead);
                        int read   = writer.Read(file, fileRead, buffer, bufferRead, toRead);
                        cts?.Cancel();

                        if (md5Hasher != null)
                        {
                            md5Hasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead);
                        }
                        shaHasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead);

                        bufferRead  += read;
                        fileRead    += read;
                        overallRead += read;

                        if (bufferRead == buffer.Length)
                        {
                            bufferRead = 0;
                            shaHasher.TransformFinalBlock(buffer, 0, 0);
                            torrentHashes.AddRange(shaHasher.Hash);
                            shaHasher.Initialize();
                        }
                        RaiseHashed(new TorrentCreatorEventArgs(file.Path, fileRead, file.Length, overallRead, overallTotal));
                    }
                    if (md5Hasher != null)
                    {
                        md5Hasher.TransformFinalBlock(buffer, 0, 0);
                        md5Hasher.Initialize();
                        file.MD5 = md5Hasher.Hash;
                    }
                }
                if (bufferRead > 0)
                {
                    shaHasher.TransformFinalBlock(buffer, 0, 0);
                    torrentHashes.AddRange(shaHasher.Hash);
                }
            } finally {
                if (shaHasher != null)
                {
                    shaHasher.Clear();
                }
                if (md5Hasher != null)
                {
                    md5Hasher.Clear();
                }
            }
            return(torrentHashes.ToArray());
        }