2024-09-17 13:44:06 +02:00
|
|
|
|
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),
|
2024-09-17 16:03:58 +02:00
|
|
|
|
TrackerUrl = $"{trackerUrl}/announce"
|
2024-09-17 13:44:06 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return torrent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string StartDaemon()
|
|
|
|
|
{
|
|
|
|
|
var endpoint = GetEndpoint();
|
2024-09-17 16:03:58 +02:00
|
|
|
|
var peerPortAddress = container.GetInternalAddress(BittorrentContainerRecipe.PeerPortTag);
|
|
|
|
|
return endpoint.HttpPutString("daemon", peerPortAddress.Port.ToString());
|
2024-09-17 13:44:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string DownloadTorrent(string torrent)
|
|
|
|
|
{
|
|
|
|
|
var endpoint = GetEndpoint();
|
|
|
|
|
|
2024-09-17 16:03:58 +02:00
|
|
|
|
return endpoint.HttpPostJson("download", new DownloadTorrentRequest
|
|
|
|
|
{
|
|
|
|
|
TorrentBase64 = torrent
|
|
|
|
|
});
|
2024-09-17 13:44:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string StartAsTracker()
|
|
|
|
|
{
|
2024-09-17 16:03:58 +02:00
|
|
|
|
TrackerAddress = container.GetInternalAddress(BittorrentContainerRecipe.TrackerPortTag);
|
2024-09-17 13:44:06 +02:00
|
|
|
|
var endpoint = GetEndpoint();
|
|
|
|
|
|
2024-09-17 16:03:58 +02:00
|
|
|
|
var trackerAddress = container.GetInternalAddress(BittorrentContainerRecipe.TrackerPortTag);
|
|
|
|
|
return endpoint.HttpPutString("tracker", trackerAddress.Port.ToString());
|
2024-09-17 13:44:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Address TrackerAddress { get; private set; } = new Address("", 0);
|
|
|
|
|
|
|
|
|
|
public class CreateTorrentRequest
|
|
|
|
|
{
|
|
|
|
|
public int Size { get; set; }
|
|
|
|
|
public string TrackerUrl { get; set; } = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 16:03:58 +02:00
|
|
|
|
public class DownloadTorrentRequest
|
|
|
|
|
{
|
|
|
|
|
public string TorrentBase64 { get; set; } = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 13:44:06 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|