cs-codex-dist-tests/ProjectPlugins/BittorrentPlugin/BittorrentNode.cs

147 lines
4.6 KiB
C#
Raw Permalink Normal View History

2024-09-17 11:44:06 +00:00
using Core;
using KubernetesWorkflow.Types;
2024-09-18 09:54:23 +00:00
using Newtonsoft.Json;
2024-09-17 11:44:06 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
2024-09-18 09:54:23 +00:00
using System.Net;
2024-09-17 11:44:06 +00:00
using System.Text;
using System.Threading.Tasks;
using Utils;
namespace BittorrentPlugin
{
public interface IBittorrentNode
{
string StartAsTracker();
2024-09-18 09:54:23 +00:00
string AddTracker(IBittorrentNode tracker, string localFile);
string PutFile(string base64);
string GetTrackerStats();
CreateTorrentResult CreateTorrent(ByteSize size, IBittorrentNode tracker);
2024-09-17 11:44:06 +00:00
string StartDaemon();
2024-09-18 09:54:23 +00:00
string DownloadTorrent(string LocalFile);
2024-09-17 11:44:06 +00:00
}
public class BittorrentNode : IBittorrentNode
{
private readonly IPluginTools tools;
private readonly RunningContainer container;
2024-09-18 09:54:23 +00:00
private readonly PodInfo podInfo;
2024-09-17 11:44:06 +00:00
public BittorrentNode(IPluginTools tools, RunningContainer container)
{
this.tools = tools;
this.container = container;
2024-09-18 09:54:23 +00:00
podInfo = tools.CreateWorkflow().GetPodInfo(container);
2024-09-17 11:44:06 +00:00
}
2024-09-18 09:54:23 +00:00
public string StartAsTracker()
2024-09-17 11:44:06 +00:00
{
2024-09-18 09:54:23 +00:00
//TrackerAddress = container.GetInternalAddress(BittorrentContainerRecipe.TrackerPortTag);
2024-09-17 11:44:06 +00:00
var endpoint = GetEndpoint();
2024-09-18 09:54:23 +00:00
return endpoint.HttpPutString("starttracker", GetTrackerAddress().Port.ToString());
}
2024-09-17 11:44:06 +00:00
2024-09-18 09:54:23 +00:00
public string AddTracker(IBittorrentNode tracker, string localFile)
{
var endpoint = GetEndpoint();
var trackerUrl = ((BittorrentNode)tracker).GetTrackerAddress();
return endpoint.HttpPostJson("addtracker", new AddTrackerRequest
2024-09-17 11:44:06 +00:00
{
2024-09-18 09:54:23 +00:00
LocalFile = localFile,
2024-09-17 14:03:58 +00:00
TrackerUrl = $"{trackerUrl}/announce"
2024-09-17 11:44:06 +00:00
});
2024-09-18 09:54:23 +00:00
}
2024-09-17 11:44:06 +00:00
2024-09-18 09:54:23 +00:00
public string PutFile(string base64)
{
var endpoint = GetEndpoint();
return endpoint.HttpPostJson("postfile", new PostFileRequest
{
Base64Content = base64
});
2024-09-17 11:44:06 +00:00
}
public string StartDaemon()
{
var endpoint = GetEndpoint();
2024-09-17 14:03:58 +00:00
var peerPortAddress = container.GetInternalAddress(BittorrentContainerRecipe.PeerPortTag);
return endpoint.HttpPutString("daemon", peerPortAddress.Port.ToString());
2024-09-17 11:44:06 +00:00
}
2024-09-18 09:54:23 +00:00
public CreateTorrentResult CreateTorrent(ByteSize size, IBittorrentNode tracker)
2024-09-17 11:44:06 +00:00
{
2024-09-18 09:54:23 +00:00
var trackerUrl = ((BittorrentNode)tracker).GetTrackerAddress();
2024-09-17 11:44:06 +00:00
var endpoint = GetEndpoint();
2024-09-18 09:54:23 +00:00
var json = endpoint.HttpPostJson("create", new CreateTorrentRequest
2024-09-17 14:03:58 +00:00
{
2024-09-18 09:54:23 +00:00
Size = Convert.ToInt32(size.SizeInBytes),
TrackerUrl = $"{trackerUrl}/announce"
2024-09-17 14:03:58 +00:00
});
2024-09-18 09:54:23 +00:00
return JsonConvert.DeserializeObject<CreateTorrentResult>(json)!;
2024-09-17 11:44:06 +00:00
}
2024-09-18 09:54:23 +00:00
public string DownloadTorrent(string localFile)
2024-09-17 11:44:06 +00:00
{
var endpoint = GetEndpoint();
2024-09-18 09:54:23 +00:00
return endpoint.HttpPostJson("download", new DownloadTorrentRequest
{
LocalFile = localFile
});
2024-09-17 11:44:06 +00:00
}
2024-09-18 09:54:23 +00:00
public string GetTrackerStats()
2024-09-17 11:44:06 +00:00
{
2024-09-18 10:21:45 +00:00
var endpoint = GetEndpoint();
return endpoint.HttpGetString("stats");
2024-09-17 11:44:06 +00:00
}
2024-09-18 09:54:23 +00:00
//public Address TrackerAddress { get; private set; } = new Address("", 0);
public Address GetTrackerAddress()
2024-09-17 14:03:58 +00:00
{
2024-09-18 09:54:23 +00:00
var address = container.GetInternalAddress(BittorrentContainerRecipe.TrackerPortTag);
return new Address("http://" + podInfo.Ip, address.Port);
2024-09-17 14:03:58 +00:00
}
2024-09-17 11:44:06 +00:00
private IEndpoint GetEndpoint()
{
2024-09-18 09:54:23 +00:00
var address = container.GetAddress(BittorrentContainerRecipe.ApiPortTag);
2024-09-17 11:44:06 +00:00
var http = tools.CreateHttp(address.ToString(), c => { });
return http.CreateEndpoint(address, "/torrent/", container.Name);
}
}
2024-09-18 09:54:23 +00:00
public class CreateTorrentRequest
{
public int Size { get; set; }
public string TrackerUrl { get; set; } = string.Empty;
}
public class CreateTorrentResult
{
public string LocalFilePath { get; set; } = string.Empty;
public string TorrentBase64 { get; set; } = string.Empty;
}
public class DownloadTorrentRequest
{
public string LocalFile { get; set; } = string.Empty;
}
public class AddTrackerRequest
{
public string TrackerUrl { get; set; } = string.Empty;
public string LocalFile { get; set; } = string.Empty;
}
public class PostFileRequest
{
public string Base64Content { get; set; } = string.Empty;
}
2024-09-17 11:44:06 +00:00
}