/// <summary>
        /// Tests creating torrent.
        /// </summary>
        /// <remarks>MonoTorrent TorrentCreator needs file to be available (even
        /// though it could be empty) even if dedup writer is used.</remarks>
        public static void TestCreateTorrent(string db, string dataFile, string savePath, string savePath1)
        {
            var dedupWriter = new DedupDiskWriter(new DeduplicationService(new ChunkDbService(db, false)));
            var creator = new DedupTorrentCreator(dedupWriter);
            var ip = NetUtil.GetLocalIPByInterface("Local Area Connection");
            var tier = new RawTrackerTier {
                string.Format("http://{0}:25456/announce", ip.ToString()),
                "udp://tracker.publicbt.com:80/announce",
                "udp://tracker.openbittorrent.com:80/announce"
            };
            var filename = Path.GetFileName(dataFile);
            creator.GetrightHttpSeeds.Add(string.Format(
                "http://{0}:49645/FileServer/FileRange/{1}", ip.ToString(),
                filename));
            creator.Announces.Add(tier);
            var binaryTorrent = creator.Create(new TorrentFileSource(dataFile));
            var torrent = Torrent.Load(binaryTorrent);
            string infoHash = torrent.InfoHash.ToHex();
            File.WriteAllBytes(savePath, binaryTorrent.Encode());

            // Now read from the real file.
            var creator1 = new TorrentCreator();
            creator1.Announces.Add(tier);
            creator1.GetrightHttpSeeds.Add(string.Format(
                "http://{0}:49645/FileServer/FileRange/{1}", ip.ToString(),
                filename));
            var binary1 = creator1.Create(new TorrentFileSource(dataFile));
            string infoHash1 = Torrent.Load(binary1).InfoHash.ToHex();
            File.WriteAllBytes(savePath1, binary1.Encode());

            Assert.AreEqual(infoHash, infoHash1);
            logger.DebugFormat("InfoHash: {0}", infoHash);
        }
Example #2
0
        public MagnetLink(string url)
        {
            Check.Url(url);
            AnnounceUrls = new RawTrackerTier();

            ParseMagnetLink(url);
        }
Example #3
0
 private string GetMainTracker(Torrent torrent)
 {
     if (torrent.AnnounceUrls.Count > 0)
     {
         RawTrackerTier tier = torrent.AnnounceUrls[0];
         if (tier.Count > 0)
         {
             Uri      uri   = new Uri(tier[0]);
             string   host  = uri.Host;
             string[] parts = host.Split('.');
             if (parts.Length > 2)
             {
                 host = parts[parts.Length - 2] + "." + parts[parts.Length - 1];
             }
             return(host);
         }
         else
         {
             return("No Tiers");
         }
     }
     else
     {
         return("No Announce URL");
     }
 }
Example #4
0
        private void createButtonClicked(object sender, RoutedEventArgs e)
        {
            var sourcePath = pathTextBox.Text;

            if (string.IsNullOrEmpty(sourcePath))
            {
                MessageBox.Show("Please select a file or files to add.");
                return;
            }
            if (singleFileRadioButton.IsChecked.Value)
            {
                if (!File.Exists(sourcePath))
                {
                    MessageBox.Show("The selected file does not exist!");
                    return;
                }
            }
            if (entireFolderRadioButton.IsChecked.Value)
            {
                if (!Directory.Exists(sourcePath))
                {
                    MessageBox.Show("The selected folder does not exist!");
                    return;
                }
            }
            Creator = new TorrentCreator();
            var source = new TorrentFileSource(sourcePath, ignoreHiddenFilesCheckBox.IsChecked.Value);
            var tier   = new RawTrackerTier(trackerListBox.Items.Cast <string>());

            Creator.Announces.Add(tier);
            Creator.Comment     = commentTextBox.Text;
            Creator.Private     = privateTorrentCheckBox.IsChecked.Value;
            Creator.CreatedBy   = "Patchy BitTorrent Client";
            Creator.PieceLength = TorrentCreator.RecommendedPieceSize(source.Files);
            var dialog = new SaveFileDialog();

            dialog.Filter           = "Torrent Files (*.torrent)|*.torrent|All Files (*.*)|*.*";
            dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            FilePath = sourcePath;
            if (dialog.ShowDialog().Value)
            {
                // Create the torrent
                Path = dialog.FileName;
                if (!Path.EndsWith(".torrent"))
                {
                    Path += ".torrent";
                }
                pathGrid.IsEnabled = trackerGrid.IsEnabled = optionsGrid.IsEnabled = createButton.IsEnabled = false;
                Creator.Hashed    += Creator_Hashed;
                Creator.BeginCreate(source, CreationComplete, null);
            }
        }
Example #5
0
        public void UnsupportedTrackers()
        {
            RawTrackerTier tier = new RawTrackerTier {
                "fake://123.123.123.2:5665"
            };

            rig.Torrent.AnnounceUrls.Add(tier);
            TorrentManager manager = new TorrentManager(rig.Torrent, "", new TorrentSettings());

            foreach (MonoTorrent.Client.Tracker.TrackerTier t in manager.TrackerManager)
            {
                Assert.IsTrue(t.Trackers.Count > 0, "#1");
            }
        }
Example #6
0
        private const int PieceLength = 16384;//64 * 1024;

        public ITorrent CreateTorrent(string downloadFolder, string relativeFileSourcePath, string destinationFolder)
        {
            var creator = new MonoTorrent.Common.TorrentCreator();

            var config = Catalog.Factory.Resolve <IConfig>();

            creator.Announces.Clear();

            // Add one tier which contains a tracker
            var tier = new RawTrackerTier
            {
                string.Format(
                    "http://{0}:{1}/announce",
                    config[BitTorrentSettings.TrackerHost],
                    config[BitTorrentSettings.TrackerPort])
            };

            creator.Announces.Add(tier);

            creator.Announce = string.Format(
                "http://{0}:{1}/announce",
                config[BitTorrentSettings.TrackerHost],
                config[BitTorrentSettings.TrackerPort]);


            creator.CreatedBy = "Monotorrent Client/" + VersionInfo.ClientVersion;

            creator.Comment   = downloadFolder;
            creator.Publisher = relativeFileSourcePath;

            creator.PieceLength  = PieceLength;
            creator.PublisherUrl = string.Empty;

            //not allowing dht, peer exchange
            creator.Private = true;

            creator.Hashed += (o, e) => Console.WriteLine("{0} {1}", e.FileSize, e.CurrentFile.First());

            var fullSourcePath = Path.Combine(downloadFolder, relativeFileSourcePath);
            var fileSource     = new TorrentFileSource(fullSourcePath);

            //var randomName = Path.GetTempFileName();
            var destFile = Path.Combine(destinationFolder, Path.GetFileNameWithoutExtension(fullSourcePath) + ".torrent");

            creator.Create(fileSource, destFile);

            var torrentFile = new TorrentFile(destFile, relativeFileSourcePath);

            return(torrentFile);
        }
 public static TorrentManager CreateManager(this InfoHash hash, StorageFolder saveFolder)
 {
     return(new TorrentManager(hash, saveFolder, new TorrentSettings(4, 150, 0, 0)
     {
         UseDht = true,
         EnablePeerExchange = true
     }, RawTrackerTier.CreateTiers(new[]
     {
         "udp://tracker.yify-torrents.com:80",
         "udp://tracker.yify-torrents.com:80",
         "udp://tracker.openbittorrent.com:80",
         "udp://tracker.publicbt.org:80",
         "udp://tracker.coppersurfer.tk:6969",
         "udp://tracker.leechers-paradise.org:6969",
         "udp://open.demonii.com:1337",
         "udp://p4p.arenabg.ch:1337",
         "udp://p4p.arenabg.com:1337"
     })));
 }
Example #8
0
        /// <summary>
        /// Tests creating torrent.
        /// </summary>
        /// <remarks>MonoTorrent TorrentCreator needs file to be available (even
        /// though it could be empty) even if dedup writer is used.</remarks>
        public static void TestCreateTorrent(string db, string dataFile, string savePath, string savePath1)
        {
            var dedupWriter = new DedupDiskWriter(new DeduplicationService(new ChunkDbService(db, false)));
            var creator     = new DedupTorrentCreator(dedupWriter);
            var ip          = NetUtil.GetLocalIPByInterface("Local Area Connection");
            var tier        = new RawTrackerTier {
                string.Format("http://{0}:25456/announce", ip.ToString()),
                "udp://tracker.publicbt.com:80/announce",
                "udp://tracker.openbittorrent.com:80/announce"
            };
            var filename = Path.GetFileName(dataFile);

            creator.GetrightHttpSeeds.Add(string.Format(
                                              "http://{0}:49645/FileServer/FileRange/{1}", ip.ToString(),
                                              filename));
            creator.Announces.Add(tier);
            var    binaryTorrent = creator.Create(new TorrentFileSource(dataFile));
            var    torrent       = Torrent.Load(binaryTorrent);
            string infoHash      = torrent.InfoHash.ToHex();

            File.WriteAllBytes(savePath, binaryTorrent.Encode());

            // Now read from the real file.
            var creator1 = new TorrentCreator();

            creator1.Announces.Add(tier);
            creator1.GetrightHttpSeeds.Add(string.Format(
                                               "http://{0}:49645/FileServer/FileRange/{1}", ip.ToString(),
                                               filename));
            var    binary1   = creator1.Create(new TorrentFileSource(dataFile));
            string infoHash1 = Torrent.Load(binary1).InfoHash.ToHex();

            File.WriteAllBytes(savePath1, binary1.Encode());

            Assert.AreEqual(infoHash, infoHash1);
            logger.DebugFormat("InfoHash: {0}", infoHash);
        }
Example #9
0
 public void UnsupportedTrackers ()
 {
     RawTrackerTier tier = new RawTrackerTier {
         "fake://123.123.123.2:5665"
     };
     rig.Torrent.AnnounceUrls.Add (tier);
     TorrentManager manager = new TorrentManager (rig.Torrent, "", new TorrentSettings());
     foreach (MonoTorrent.Client.Tracker.TrackerTier t in manager.TrackerManager)
     {
         Assert.IsTrue (t.Trackers.Count > 0, "#1");
     }
 }
Example #10
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"):
#if NETSTANDARD1_5
                        using (SHA1 s = System.Security.Cryptography.SHA1.Create())
#else
                        using (SHA1 s = HashAlgoFactory.Create <SHA1>())
#endif
                            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);
                }
            }
        }
Example #11
0
 private void createButtonClicked(object sender, RoutedEventArgs e)
 {
     var sourcePath = pathTextBox.Text;
     if (string.IsNullOrEmpty(sourcePath))
     {
         MessageBox.Show("Please select a file or files to add.");
         return;
     }
     if (singleFileRadioButton.IsChecked.Value)
     {
         if (!File.Exists(sourcePath))
         {
             MessageBox.Show("The selected file does not exist!");
             return;
         }
     }
     if (entireFolderRadioButton.IsChecked.Value)
     {
         if (!Directory.Exists(sourcePath))
         {
             MessageBox.Show("The selected folder does not exist!");
             return;
         }
     }
     Creator = new TorrentCreator();
     var source = new TorrentFileSource(sourcePath, ignoreHiddenFilesCheckBox.IsChecked.Value);
     var tier = new RawTrackerTier(trackerListBox.Items.Cast<string>());
     Creator.Announces.Add(tier);
     Creator.Comment = commentTextBox.Text;
     Creator.Private = privateTorrentCheckBox.IsChecked.Value;
     Creator.CreatedBy = "Patchy BitTorrent Client";
     Creator.PieceLength = TorrentCreator.RecommendedPieceSize(source.Files);
     var dialog = new SaveFileDialog();
     dialog.Filter = "Torrent Files (*.torrent)|*.torrent|All Files (*.*)|*.*";
     dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
     FilePath = sourcePath;
     if (dialog.ShowDialog().Value)
     {
         // Create the torrent
         Path = dialog.FileName;
         if (!Path.EndsWith(".torrent"))
             Path += ".torrent";
         pathGrid.IsEnabled = trackerGrid.IsEnabled = optionsGrid.IsEnabled = createButton.IsEnabled = false;
         Creator.Hashed += Creator_Hashed;
         Creator.BeginCreate(source, CreationComplete, null);
     }
 }