From 5b53c1af03a512df7c9cb0cd03cdbda7dde5456d Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 17 Sep 2024 13:44:06 +0200 Subject: [PATCH] All lined up --- Framework/Core/Endpoint.cs | 10 +++ .../BittorrentContainerRecipe.cs | 24 ++++++ .../BittorrentPlugin/BittorrentNode.cs | 82 +++++++++++++++++++ .../BittorrentPlugin/BittorrentPlugin.cs | 53 +----------- .../CoreInterfaceExtensions.cs | 4 +- Tests/CodexTests/BasicTests/ExampleTests.cs | 2 +- .../Controllers/TorrentController.cs | 9 ++ Tools/BittorrentDriver/Dockerfile | 7 +- Tools/BittorrentDriver/Transmission.cs | 16 +++- 9 files changed, 151 insertions(+), 56 deletions(-) create mode 100644 ProjectPlugins/BittorrentPlugin/BittorrentContainerRecipe.cs create mode 100644 ProjectPlugins/BittorrentPlugin/BittorrentNode.cs diff --git a/Framework/Core/Endpoint.cs b/Framework/Core/Endpoint.cs index 28b0c61..b156eeb 100644 --- a/Framework/Core/Endpoint.cs +++ b/Framework/Core/Endpoint.cs @@ -16,6 +16,7 @@ namespace Core TResponse HttpPostString(string route, string body); string HttpPostStream(string route, Stream stream); Stream HttpGetStream(string route); + string HttpPutString(string route, string body); T Deserialize(string json); } @@ -114,6 +115,15 @@ namespace Core }, $"HTTP-GET-STREAM: {route}"); } + public string HttpPutString(string route, string body) + { + return http.OnClient(client => + { + var response = Time.Wait(client.PutAsync(GetUrl() + route, new StringContent(body))); + return Time.Wait(response.Content.ReadAsStringAsync()); + }, $"HTTP-PUT-STR: {route}"); + } + public T Deserialize(string json) { var errors = new List(); diff --git a/ProjectPlugins/BittorrentPlugin/BittorrentContainerRecipe.cs b/ProjectPlugins/BittorrentPlugin/BittorrentContainerRecipe.cs new file mode 100644 index 0000000..86ec3d3 --- /dev/null +++ b/ProjectPlugins/BittorrentPlugin/BittorrentContainerRecipe.cs @@ -0,0 +1,24 @@ +using KubernetesWorkflow; +using KubernetesWorkflow.Recipe; + +namespace BittorrentPlugin +{ + public class BittorrentContainerRecipe : ContainerRecipeFactory + { + public override string AppName => "bittorrent"; + public override string Image => "thatbenbierens/bittorrentdriver:init"; + + public static string ApiPortTag = "API_PORT"; + public static string TrackerPortTag = "TRACKER_PORT"; + public static string PeerPortTag = "PEER_PORT"; + public static int TrackerPort = 31010; + public static int PeerPort = 31012; + + protected override void Initialize(StartupConfig config) + { + AddExposedPort(TrackerPort, TrackerPortTag); + AddExposedPort(PeerPort, PeerPortTag); + AddExposedPortAndVar("APIPORT", ApiPortTag); + } + } +} diff --git a/ProjectPlugins/BittorrentPlugin/BittorrentNode.cs b/ProjectPlugins/BittorrentPlugin/BittorrentNode.cs new file mode 100644 index 0000000..eeb98b5 --- /dev/null +++ b/ProjectPlugins/BittorrentPlugin/BittorrentNode.cs @@ -0,0 +1,82 @@ +using Core; +using KubernetesWorkflow.Types; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Utils; + +namespace BittorrentPlugin +{ + public interface IBittorrentNode + { + string StartAsTracker(); + string CreateTorrent(ByteSize size, IBittorrentNode tracker); + string StartDaemon(); + string DownloadTorrent(string torrent); + } + + public class BittorrentNode : IBittorrentNode + { + private readonly IPluginTools tools; + private readonly RunningContainer container; + + public BittorrentNode(IPluginTools tools, RunningContainer container) + { + this.tools = tools; + this.container = container; + } + + public string CreateTorrent(ByteSize size, IBittorrentNode tracker) + { + var trackerUrl = ((BittorrentNode)tracker).TrackerAddress; + var endpoint = GetEndpoint(); + + var torrent = endpoint.HttpPostJson("create", new CreateTorrentRequest + { + Size = Convert.ToInt32(size.SizeInBytes), + TrackerUrl = trackerUrl.ToString() + }); + + return torrent; + } + + public string StartDaemon() + { + var endpoint = GetEndpoint(); + return endpoint.HttpPutString("daemon", BittorrentContainerRecipe.PeerPort.ToString()); + } + + public string DownloadTorrent(string torrent) + { + var endpoint = GetEndpoint(); + + return endpoint.HttpPostString("download", torrent); + } + + public string StartAsTracker() + { + TrackerAddress = container.GetAddress(tools.GetLog(), BittorrentContainerRecipe.TrackerPortTag); + var endpoint = GetEndpoint(); + + return endpoint.HttpPutString("tracker", BittorrentContainerRecipe.TrackerPort.ToString()); + } + + public Address TrackerAddress { get; private set; } = new Address("", 0); + + public class CreateTorrentRequest + { + public int Size { get; set; } + public string TrackerUrl { get; set; } = string.Empty; + } + + private IEndpoint GetEndpoint() + { + var address = container.GetAddress(tools.GetLog(), BittorrentContainerRecipe.ApiPortTag); + var http = tools.CreateHttp(address.ToString(), c => { }); + return http.CreateEndpoint(address, "/torrent/", container.Name); + } + } +} diff --git a/ProjectPlugins/BittorrentPlugin/BittorrentPlugin.cs b/ProjectPlugins/BittorrentPlugin/BittorrentPlugin.cs index 15d3c04..7491254 100644 --- a/ProjectPlugins/BittorrentPlugin/BittorrentPlugin.cs +++ b/ProjectPlugins/BittorrentPlugin/BittorrentPlugin.cs @@ -22,58 +22,13 @@ namespace BittorrentPlugin { } - public void Run() + public IBittorrentNode StartNode() { var flow = tools.CreateWorkflow(); - var trackerPod = flow.Start(1, new TrackerContainerRecipe(), new StartupConfig()).WaitForOnline(); - var trackerContainer = trackerPod.Containers.Single(); + var pod = flow.Start(1, new BittorrentContainerRecipe(), new StartupConfig()).WaitForOnline(); + var container = pod.Containers.Single(); - //var msg = flow.ExecuteCommand(trackerContainer, "apt-get", "update"); - //msg = flow.ExecuteCommand(trackerContainer, "apt-get", "install", "npm", "-y"); - //var msg = flow.ExecuteCommand(trackerContainer, "npm", "install", "-g", "bittorrent-tracker"); - //msg = flow.ExecuteCommand(trackerContainer, "bittorrent-tracker", "--port", "30800", "&"); - - var clientPod = flow.Start(1, new BittorrentContainerRecipe(), new StartupConfig()).WaitForOnline(); - var clientContainer = clientPod.Containers.Single(); - - var msg = flow.ExecuteCommand(clientContainer, "echo", "1234567890987654321", - ">", "/root/datafile.txt"); - - var trackerAddress = trackerContainer.GetAddress(tools.GetLog(), TrackerContainerRecipe.HttpPort); - if (trackerAddress == null) throw new Exception(); - var trackerAddressStr = trackerAddress.ToString(); - - msg = flow.ExecuteCommand(clientContainer, "transmission-create", - "-o", "/root/outfile.torrent", - "-t", trackerAddressStr, - "/root/datafile.txt"); - - msg = flow.ExecuteCommand(clientContainer, "cat", "/root/outfile.torrent"); - - var a = 0; - } - } - - public class TrackerContainerRecipe : ContainerRecipeFactory - { - public override string AppName => "bittorrenttracker"; - public override string Image => "thatbenbierens/bittorrent-tracker:init"; - - public static string HttpPort = "http"; - - protected override void Initialize(StartupConfig config) - { - AddExposedPort(30800, HttpPort); - } - } - - public class BittorrentContainerRecipe : ContainerRecipeFactory - { - public override string AppName => "bittorrentclient"; - public override string Image => "thatbenbierens/bittorrent-client:init"; - - protected override void Initialize(StartupConfig config) - { + return new BittorrentNode(tools, container); } } } diff --git a/ProjectPlugins/BittorrentPlugin/CoreInterfaceExtensions.cs b/ProjectPlugins/BittorrentPlugin/CoreInterfaceExtensions.cs index c73dd77..82bd9d0 100644 --- a/ProjectPlugins/BittorrentPlugin/CoreInterfaceExtensions.cs +++ b/ProjectPlugins/BittorrentPlugin/CoreInterfaceExtensions.cs @@ -4,9 +4,9 @@ namespace BittorrentPlugin { public static class CoreInterfaceExtensions { - public static void RunThing(this CoreInterface ci) + public static IBittorrentNode StartNode(this CoreInterface ci) { - Plugin(ci).Run(); + return Plugin(ci).StartNode(); } private static BittorrentPlugin Plugin(CoreInterface ci) diff --git a/Tests/CodexTests/BasicTests/ExampleTests.cs b/Tests/CodexTests/BasicTests/ExampleTests.cs index 959a66e..3d1cd7c 100644 --- a/Tests/CodexTests/BasicTests/ExampleTests.cs +++ b/Tests/CodexTests/BasicTests/ExampleTests.cs @@ -71,7 +71,7 @@ namespace CodexTests.BasicTests [Test] public void BittorrentPluginTest() { - Ci.RunThing(); + } } } diff --git a/Tools/BittorrentDriver/Controllers/TorrentController.cs b/Tools/BittorrentDriver/Controllers/TorrentController.cs index 15f847f..c69fea3 100644 --- a/Tools/BittorrentDriver/Controllers/TorrentController.cs +++ b/Tools/BittorrentDriver/Controllers/TorrentController.cs @@ -24,15 +24,24 @@ namespace BittorrentDriver.Controllers return tracker.Start(port); } + [HttpPut("daemon")] + public string StartDaemon([FromBody] int peerPort) + { + Log("Starting daemon..."); + return transmission.StartDaemon(peerPort); + } + [HttpPost("create")] public string CreateTorrent([FromBody] CreateTorrentInput input) { + Log("Creating torrent file..."); return transmission.CreateNew(input.Size, input.TrackerUrl); } [HttpPost("download")] public string DownloadTorrent([FromBody] string torrentBase64) { + Log("Downloading torrent..."); return transmission.Download(torrentBase64); } diff --git a/Tools/BittorrentDriver/Dockerfile b/Tools/BittorrentDriver/Dockerfile index 225aa14..63b6017 100644 --- a/Tools/BittorrentDriver/Dockerfile +++ b/Tools/BittorrentDriver/Dockerfile @@ -9,10 +9,11 @@ EXPOSE 8081 FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src -COPY ["BittorrentDriver/BittorrentDriver.csproj", "BittorrentDriver/"] -RUN dotnet restore "./BittorrentDriver/BittorrentDriver.csproj" +COPY ["Tools/BittorrentDriver/", "Tools/BittorrentDriver/"] +COPY ["Framework", "Framework/"] +RUN dotnet restore "./Tools/BittorrentDriver/BittorrentDriver.csproj" COPY . . -WORKDIR "/src/BittorrentDriver" +WORKDIR "/src/Tools/BittorrentDriver" RUN dotnet build "./BittorrentDriver.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish diff --git a/Tools/BittorrentDriver/Transmission.cs b/Tools/BittorrentDriver/Transmission.cs index e6bcb56..45fe0bb 100644 --- a/Tools/BittorrentDriver/Transmission.cs +++ b/Tools/BittorrentDriver/Transmission.cs @@ -32,6 +32,18 @@ namespace BittorrentDriver return base64; } + public string StartDaemon(int peerPort) + { + var info = new ProcessStartInfo + { + FileName = "transmission-daemon", + Arguments = $"--peerport={peerPort} --download-dir={dataDir}" + }; + RunToComplete(info); + + return "OK"; + } + public string Download(string torrentBase64) { var torrentFile = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid().ToString() + ".torrent"); @@ -85,7 +97,9 @@ namespace BittorrentDriver try { var fileManager = new FileManager(log, dataDir); - return fileManager.GenerateFile(size.Bytes()); + var file = fileManager.GenerateFile(size.Bytes()); + log.Log("Generated file: " + file.Filename); + return file; } catch (Exception ex) {