Добавьте файлы проекта.

This commit is contained in:
2025-05-12 01:36:26 +03:00
parent f0541bd88a
commit f29596104e
3 changed files with 173 additions and 0 deletions

25
MagnetExtractor.sln Normal file
View File

@@ -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

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BencodeNET" Version="5.0.0" />
</ItemGroup>
</Project>

134
MagnetExtractor/Program.cs Normal file
View File

@@ -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 <path_to_torrent_file>");
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<BDictionary>(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<string> GetTrackers(BDictionary torrent)
{
var trackers = new List<string>();
// Извлекаем трекеры из 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<string> 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}");
}
}
}
}