diff --git a/MagnetExtractor.sln b/MagnetExtractor.sln new file mode 100644 index 0000000..3e1d9a0 --- /dev/null +++ b/MagnetExtractor.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35931.197 d17.13 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagnetExtractor", "MagnetExtractor\MagnetExtractor.csproj", "{911B8CAF-FF6E-4433-98D9-5068BBE3F37F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {911B8CAF-FF6E-4433-98D9-5068BBE3F37F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {911B8CAF-FF6E-4433-98D9-5068BBE3F37F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {911B8CAF-FF6E-4433-98D9-5068BBE3F37F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {911B8CAF-FF6E-4433-98D9-5068BBE3F37F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {99C83323-E6A2-4DCC-BCF8-BF7BFB0A0D49} + EndGlobalSection +EndGlobal diff --git a/MagnetExtractor/MagnetExtractor.csproj b/MagnetExtractor/MagnetExtractor.csproj new file mode 100644 index 0000000..35159cc --- /dev/null +++ b/MagnetExtractor/MagnetExtractor.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/MagnetExtractor/Program.cs b/MagnetExtractor/Program.cs new file mode 100644 index 0000000..821b134 --- /dev/null +++ b/MagnetExtractor/Program.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using BencodeNET.Objects; +using BencodeNET.Parsing; +using BencodeNET.Torrents; + +namespace TorrentToMagnet +{ + class Program + { + static void Main(string[] args) + { + if (args.Length != 1) + { + Console.WriteLine("Usage: TorrentToMagnet "); + return; + } + + string torrentFilePath = args[0]; + + if (!File.Exists(torrentFilePath)) + { + Console.WriteLine("File not found."); + return; + } + + try + { + // Загружаем .torrent файл + var torrentData = File.ReadAllBytes(torrentFilePath); + var parser = new BencodeParser(); + var torrent = parser.Parse(torrentData); + + // Извлекаем информацию для magnet-ссылки + var infoHash = GetInfoHash(torrent); + var name = GetName(torrent); + var trackers = GetTrackers(torrent); + + // Формируем magnet-ссылку + string magnetLink = $"magnet:?xt=urn:btih:{infoHash}&dn={Uri.EscapeDataString(name)}"; + + // Добавляем трекеры в magnet-ссылку + foreach (var tracker in trackers) + { + magnetLink += $"&tr={Uri.EscapeDataString(tracker)}"; + } + + Console.WriteLine("Magnet Link:"); + Console.WriteLine(magnetLink); + + // Спрашиваем пользователя, хочет ли он сохранить трекеры в файл + Console.WriteLine("Do you want to save the trackers to a file? (yes/no)"); + string response = Console.ReadLine().Trim().ToLower(); + + if (response == "yes" || response == "y") + { + Console.WriteLine("Enter the file name to save the trackers:"); + string fileName = Console.ReadLine().Trim(); + + // Сохраняем трекеры в файл + SaveTrackersToFile(trackers, fileName); + } + } + catch (Exception ex) + { + Console.WriteLine("Error: " + ex.Message); + } + } + + static string GetInfoHash(BDictionary torrent) + { + var info = torrent["info"] as BDictionary; + var infoBytes = info.EncodeAsBytes(); + var sha1 = System.Security.Cryptography.SHA1.Create(); + var hashBytes = sha1.ComputeHash(infoBytes); + return BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); + } + + static string GetName(BDictionary torrent) + { + var info = torrent["info"] as BDictionary; + return info["name"].ToString(); + } + + static List GetTrackers(BDictionary torrent) + { + var trackers = new List(); + + // Извлекаем трекеры из announce + if (torrent.ContainsKey("announce")) + { + var announce = torrent["announce"].ToString(); + trackers.Add(announce); + } + + // Извлекаем трекеры из announce-list + if (torrent.ContainsKey("announce-list")) + { + var announceList = torrent["announce-list"] as BList; + foreach (var item in announceList) + { + if (item is BList subList) + { + foreach (var subItem in subList) + { + trackers.Add(subItem.ToString()); + } + } + else + { + trackers.Add(item.ToString()); + } + } + } + + return trackers; + } + + static void SaveTrackersToFile(List trackers, string filePath) + { + try + { + File.WriteAllLines(filePath, trackers); + Console.WriteLine($"Trackers saved to {filePath}"); + } + catch (Exception ex) + { + Console.WriteLine($"Error saving trackers to file: {ex.Message}"); + } + } + } +} \ No newline at end of file