Ejemplo n.º 1
0
 private void Init(List <FileStream> streams)
 {
     if (streams == null || streams.Count == 0)
     {
         MultiLangMsg messages = new MultiLangMsg();
         throw new ArgumentNullException(messages.GetMessage("NoStreamsProvided"));
     }
     fileStreams        = streams;
     currentStreamIndex = 0;
     currentStream      = fileStreams[currentStreamIndex];
 }
Ejemplo n.º 2
0
        private bool Run(string[] args)
        {
            Torrent torrent = new Torrent(messages);

            try
            {
                for (int i = 0; i < args.Length; i++)
                {
                    switch (args[i])
                    {
                    case "-l":
                        torrent.PiecesLength = int.Parse(args[i + 1]);
                        i++;
                        Console.WriteLine("{0}: 2^{1}", messages.GetMessage("PieceLength"), torrent.PiecesLength);
                        break;

                    case "-p":
                        torrent.PrivateTorrent = true;
                        Console.WriteLine("{0}: {1}", messages.GetMessage("PrivateFlag"),
                                          torrent.PrivateTorrent ? messages.GetMessage("StrTrue") : messages.GetMessage("StrFalse"));
                        break;

                    case "-movieonly":
                        torrent.MovieOnly = true;
                        Console.WriteLine(messages.GetMessage("MovieOnly"));
                        break;

                    case "-a":
                        torrent.AnnounceURL = args[i + 1];
                        i++;
                        Console.WriteLine("{0}: {1}", messages.GetMessage("Tracker"), torrent.AnnounceURL);
                        break;

                    case "-o":
                        torrent.TorrentFileName = args[i + 1];
                        i++;
                        Console.WriteLine("{0}: {1}", messages.GetMessage("FileName"), torrent.TorrentFileName);
                        break;

                    default:
                        if (args[i][args[i].Length - 1].Equals('"'))
                        {
                            args[i] = args[i].Substring(0, args[i].Length - 1);
                        }
                        if (args[i][args[i].Length - 1].Equals('\\'))
                        {
                            args[i] = args[i].Substring(0, args[i].Length - 1);
                        }
                        if (Directory.Exists(args[i]))
                        {
                            torrent.DirectoryName = args[i];
                            Console.WriteLine("{0}: {1}", messages.GetMessage("Folder"), torrent.DirectoryName);
                        }
                        else if (File.Exists(args[i]))
                        {
                            torrent.FileName = args[i];
                            Console.WriteLine("{0}: {1}", messages.GetMessage("File"), torrent.FileName);
                        }
                        break;
                    }
                }
                if ((!torrent.DirectoryName.Equals("")) || (!torrent.FileName.Equals("")))
                {
                    torrent.MakeTorrentData();
                    torrent.SaveTorrentFile();
                    torrent.CheckTracker();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}: {1}", messages.GetMessage("ErrorMessage"), ex.Message);
            }
            return(torrent.TorrentPrepared);
        }
Ejemplo n.º 3
0
 private void Init(List<FileStream> streams)
 {
     if (streams == null || streams.Count == 0)
     {
         MultiLangMsg messages = new MultiLangMsg();
         throw new ArgumentNullException(messages.GetMessage("NoStreamsProvided"));
     }
     fileStreams = streams;
     currentStreamIndex = 0;
     currentStream = fileStreams[currentStreamIndex];
 }
Ejemplo n.º 4
0
        public void MakeTorrentData()
        {
            if (AnnounceURL.Equals(""))
            {
                return;
            }

            DirectoryInfo       directoryInfo = null;
            MultipleFilesStream filesMFS      = null;

            try
            {
                if (Directory.Exists(DirectoryName))
                {
                    directoryInfo          = new DirectoryInfo(DirectoryName);
                    torrentSaved.Info.Name = directoryInfo.Name;
                    if (TorrentFileName.Equals(""))
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + torrentSaved.Info.Name + ".torrent";
                    }
                    else
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + TorrentFileName;
                    }

                    if (MovieOnly)
                    {
                        filesFilter = new string[] { "*.mkv", "*.mp4", "*.avi" };
                    }
                    filesMFS = new MultipleFilesStream(directoryInfo, filesFilter);
                }
                else if (File.Exists(FileName))
                {
                    FileInfo fileInfo = new FileInfo(FileName);
                    directoryInfo          = new DirectoryInfo(fileInfo.DirectoryName);
                    torrentSaved.Info.Name = fileInfo.Name;
                    if (TorrentFileName.Equals(""))
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + torrentSaved.Info.Name + ".torrent";
                    }
                    else
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + TorrentFileName;
                    }
                    List <string> files = new List <string>();
                    files.Add(fileInfo.FullName);
                    filesMFS = new MultipleFilesStream(files);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw new ArgumentException(messages.GetMessage("FilesNotFound"));
            }

            if (PiecesLength <= 0)
            {
                torrentSaved.Info.PieceLength = GetPiecesLength(filesMFS.Length);
            }
            else
            {
                torrentSaved.Info.PieceLength = (long)Math.Pow(2, PiecesLength);
            }
            Console.WriteLine("{0}: {1}", messages.GetMessage("PieceLength"), torrentSaved.Info.PieceLength);

            Console.WriteLine("\n{0}: {1}", messages.GetMessage("FilesFound"), filesMFS.GetFilesInfo().Count());
            if (filesMFS.GetFilesInfo().Count() > 0)
            {
                int i = 0;
                torrentSaved.Info.torrentFiles = new TorrentFileStruct[filesMFS.GetFilesInfo().Count()];
                foreach (FileInfo fileInfo in filesMFS.GetFilesInfo())
                {
                    if (directoryInfo.FullName == fileInfo.DirectoryName)
                    {
                        torrentSaved.Info.torrentFiles[i].Path = fileInfo.Name;
                    }
                    else
                    {
                        torrentSaved.Info.torrentFiles[i].Path =
                            fileInfo.DirectoryName.Replace(directoryInfo.FullName + "\\", "") + "\\" + fileInfo.Name;
                    }
                    torrentSaved.Info.torrentFiles[i].Length = fileInfo.Length;
                    Console.WriteLine("{0} [{1}]", torrentSaved.Info.torrentFiles[i].Path, fileInfo.Length);
                    i++;
                }
            }

            using (MemoryStream piecesMemoryStream = new MemoryStream())
            {
                using (BinaryReader inputStreamReader = new BinaryReader(filesMFS))
                {
                    byte[] readBuffer = new Byte[torrentSaved.Info.PieceLength];
                    long   readCounter, totalReadCounter = 0, toRead = filesMFS.Length;
                    while ((readCounter = inputStreamReader.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        byte[] lastBuffer = new byte[readCounter];
                        if (readCounter < readBuffer.Length)
                        {
                            Array.Copy(readBuffer, 0, lastBuffer, 0, readCounter);
                        }
                        else
                        {
                            lastBuffer = readBuffer;
                        }
                        byte[] sha1Hash = (new SHA1CryptoServiceProvider()).ComputeHash(lastBuffer);
                        piecesMemoryStream.Write(sha1Hash, 0, sha1Hash.Length);
                        totalReadCounter += readCounter;

                        Console.Write("\r{0}... {1} / {2} [{3:p}]", messages.GetMessage("Progress"), totalReadCounter,
                                      toRead, (double)totalReadCounter / toRead);
                    }
                }
                torrentSaved.Info.Pieces = piecesMemoryStream.ToArray();
            }

            if (PrivateTorrent)
            {
                torrentSaved.Info.PrivateTorrent = 1;
            }

            torrentSaved.Announce = AnnounceURL;

            TorrentPrepared = true;
        }