getting closer

This commit is contained in:
Ben 2024-09-17 16:03:58 +02:00
parent 5b53c1af03
commit 9d5abd8955
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
9 changed files with 95 additions and 50 deletions

View File

@ -119,7 +119,8 @@ namespace Core
{
return http.OnClient(client =>
{
var response = Time.Wait(client.PutAsync(GetUrl() + route, new StringContent(body)));
var response = Time.Wait(client.PutAsync(GetUrl() + route,
new StringContent(body, MediaTypeHeaderValue.Parse("application/json"))));
return Time.Wait(response.Content.ReadAsStringAsync());
}, $"HTTP-PUT-STR: {route}");
}

View File

@ -6,18 +6,16 @@ namespace BittorrentPlugin
public class BittorrentContainerRecipe : ContainerRecipeFactory
{
public override string AppName => "bittorrent";
public override string Image => "thatbenbierens/bittorrentdriver:init";
public override string Image => "thatbenbierens/bittorrentdriver:init6";
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);
AddInternalPortAndVar("TRACKERPORT", TrackerPortTag);
AddInternalPortAndVar("PEERPORT", PeerPortTag);
AddExposedPortAndVar("APIPORT", ApiPortTag);
}
}

View File

@ -37,7 +37,7 @@ namespace BittorrentPlugin
var torrent = endpoint.HttpPostJson("create", new CreateTorrentRequest
{
Size = Convert.ToInt32(size.SizeInBytes),
TrackerUrl = trackerUrl.ToString()
TrackerUrl = $"{trackerUrl}/announce"
});
return torrent;
@ -46,22 +46,27 @@ namespace BittorrentPlugin
public string StartDaemon()
{
var endpoint = GetEndpoint();
return endpoint.HttpPutString("daemon", BittorrentContainerRecipe.PeerPort.ToString());
var peerPortAddress = container.GetInternalAddress(BittorrentContainerRecipe.PeerPortTag);
return endpoint.HttpPutString("daemon", peerPortAddress.Port.ToString());
}
public string DownloadTorrent(string torrent)
{
var endpoint = GetEndpoint();
return endpoint.HttpPostString<string>("download", torrent);
return endpoint.HttpPostJson("download", new DownloadTorrentRequest
{
TorrentBase64 = torrent
});
}
public string StartAsTracker()
{
TrackerAddress = container.GetAddress(tools.GetLog(), BittorrentContainerRecipe.TrackerPortTag);
TrackerAddress = container.GetInternalAddress(BittorrentContainerRecipe.TrackerPortTag);
var endpoint = GetEndpoint();
return endpoint.HttpPutString("tracker", BittorrentContainerRecipe.TrackerPort.ToString());
var trackerAddress = container.GetInternalAddress(BittorrentContainerRecipe.TrackerPortTag);
return endpoint.HttpPutString("tracker", trackerAddress.Port.ToString());
}
public Address TrackerAddress { get; private set; } = new Address("", 0);
@ -72,6 +77,11 @@ namespace BittorrentPlugin
public string TrackerUrl { get; set; } = string.Empty;
}
public class DownloadTorrentRequest
{
public string TorrentBase64 { get; set; } = string.Empty;
}
private IEndpoint GetEndpoint()
{
var address = container.GetAddress(tools.GetLog(), BittorrentContainerRecipe.ApiPortTag);

View File

@ -4,7 +4,7 @@ namespace BittorrentPlugin
{
public static class CoreInterfaceExtensions
{
public static IBittorrentNode StartNode(this CoreInterface ci)
public static IBittorrentNode StartBittorrentNode(this CoreInterface ci)
{
return Plugin(ci).StartNode();
}

View File

@ -71,7 +71,17 @@ namespace CodexTests.BasicTests
[Test]
public void BittorrentPluginTest()
{
var tracker = Ci.StartBittorrentNode();
var msg = tracker.StartAsTracker();
var seeder = Ci.StartBittorrentNode();
var torrent = seeder.CreateTorrent(10.MB(), tracker);
msg = seeder.StartDaemon();
var leecher = Ci.StartBittorrentNode();
msg = leecher.DownloadTorrent(torrent);
var yay = 0;
}
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>4d58719c-20df-4407-bfb4-0f65a324a118</UserSecretsId>

View File

@ -20,29 +20,54 @@ namespace BittorrentDriver.Controllers
[HttpPut("tracker")]
public string StartTracker([FromBody] int port)
{
Log("Starting tracker...");
return tracker.Start(port);
return Try(() =>
{
Log("Starting tracker...");
return tracker.Start(port);
});
}
[HttpPut("daemon")]
public string StartDaemon([FromBody] int peerPort)
{
Log("Starting daemon...");
return transmission.StartDaemon(peerPort);
return Try(() =>
{
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);
return Try(() =>
{
Log("Creating torrent file...");
return transmission.CreateNew(input.Size, input.TrackerUrl);
});
}
[HttpPost("download")]
public string DownloadTorrent([FromBody] string torrentBase64)
public string DownloadTorrent([FromBody] DownloadTorrentInput input)
{
Log("Downloading torrent...");
return transmission.Download(torrentBase64);
return Try(() =>
{
Log("Downloading torrent...");
return transmission.Download(input.TorrentBase64);
});
}
private string Try(Func<string> value)
{
try
{
return value();
}
catch (Exception exc)
{
log.Error(exc.ToString());
return exc.ToString();
}
}
private void Log(string v)
@ -56,4 +81,9 @@ namespace BittorrentDriver.Controllers
public int Size { get; set; }
public string TrackerUrl { get; set; } = string.Empty;
}
public class DownloadTorrentInput
{
public string TorrentBase64 { get; set; } = string.Empty;
}
}

View File

@ -1,37 +1,33 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
# Variables
ARG IMAGE=ubuntu:24.10
ARG APP_HOME=/app
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Tools/BittorrentDriver/", "Tools/BittorrentDriver/"]
COPY ["Framework", "Framework/"]
RUN dotnet restore "./Tools/BittorrentDriver/BittorrentDriver.csproj"
COPY . .
WORKDIR "/src/Tools/BittorrentDriver"
RUN dotnet build "./BittorrentDriver.csproj" -c $BUILD_CONFIGURATION -o /app/build
# Build
FROM ${IMAGE} AS builder
ARG APP_HOME
RUN apt-get update
RUN apt-get install dotnet-sdk-8.0 -y
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./BittorrentDriver.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
WORKDIR ${APP_HOME}
COPY ./Tools/BittorrentDriver ./Tools/BittorrentDriver
COPY ./Framework ./Framework
RUN dotnet restore Tools/BittorrentDriver
RUN dotnet publish Tools/BittorrentDriver -c Release -o out
FROM base AS final
# Create
FROM ${IMAGE}
ARG DEBIAN_FRONTEND=noninteractive
ARG APP_HOME
ENV APP_HOME=${APP_HOME}
USER root
# Set up npm and bittorrent-tracker
RUN apt-get update
RUN apt-get install npm software-properties-common -y
RUN apt-get install npm aspnetcore-runtime-8.0 -y
RUN npm install -g bittorrent-tracker
# Set up transmission
RUN apt-get install transmission-cli transmission-common transmission-daemon -y
USER app
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BittorrentDriver.dll"]
WORKDIR ${APP_HOME}
COPY --from=builder ${APP_HOME}/out .
CMD dotnet ${APP_HOME}/BittorrentDriver.dll

View File

@ -1,4 +1,4 @@
# Run from Tools folder:
# Run from repo root folder:
docker build -t thatbenbierens/bittorrentdriver:init -f .\BittorrentDriver\Dockerfile .
docker build -t thatbenbierens/bittorrentdriver:init -f .\Tools\BittorrentDriver\Dockerfile .
docker push thatbenbierens/bittorrentdriver:init