All lined up

This commit is contained in:
Ben 2024-09-17 13:44:06 +02:00
parent 125ee5d22e
commit 5b53c1af03
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
9 changed files with 151 additions and 56 deletions

View File

@ -16,6 +16,7 @@ namespace Core
TResponse HttpPostString<TResponse>(string route, string body);
string HttpPostStream(string route, Stream stream);
Stream HttpGetStream(string route);
string HttpPutString(string route, string body);
T Deserialize<T>(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<T>(string json)
{
var errors = new List<string>();

View File

@ -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);
}
}
}

View File

@ -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<string>("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);
}
}
}

View File

@ -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);
}
}
}

View File

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

View File

@ -71,7 +71,7 @@ namespace CodexTests.BasicTests
[Test]
public void BittorrentPluginTest()
{
Ci.RunThing();
}
}
}

View File

@ -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);
}

View File

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

View File

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