Initial commit

This commit is contained in:
Stone_Red
2025-12-19 18:16:08 +01:00
commit e647916f78
25 changed files with 953 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
# 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.
# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["RemoteExec.Server/RemoteExec.Server.csproj", "RemoteExec.Server/"]
RUN dotnet restore "./RemoteExec.Server/RemoteExec.Server.csproj"
COPY . .
WORKDIR "/src/RemoteExec.Server"
RUN dotnet build "./RemoteExec.Server.csproj" -c $BUILD_CONFIGURATION -o /app/build
# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./RemoteExec.Server.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "RemoteExec.Server.dll"]
@@ -0,0 +1,113 @@
using Microsoft.AspNetCore.SignalR;
using RemoteExec.Shared;
using System.Reflection;
using System.Runtime.Loader;
using System.Text.Json;
namespace RemoteExec.Server.Hubs;
public class RemoteExecutionHub : Hub
{
private readonly Dictionary<string, AssemblyLoadContext> connections = [];
public override Task OnConnectedAsync()
{
connections.Add(Context.ConnectionId, new RemoteJobAssemblyLoadContext($"RemoteJob_{Guid.NewGuid()}", true));
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception? exception)
{
if (connections.TryGetValue(Context.ConnectionId, out RemoteJobAssemblyLoadContext assemblyLoadContext))
{
assemblyLoadContext.Unload();
_ = connections.Remove(Context.ConnectionId);
}
return base.OnDisconnectedAsync(exception);
}
public async Task<RemoteExecutionResult> Execute(RemoteExecutionRequest req)
{
try
{
AssemblyLoadContext alc = new AssemblyLoadContext(
name: $"RemoteJob_{Guid.NewGuid()}",
isCollectible: true);
using MemoryStream ms = new MemoryStream(req.AssemblyBytes);
Assembly asm = alc.LoadFromStream(ms);
Type type = asm.GetType(req.TypeName, throwOnError: true)!;
Type?[] argTypes = req.ArgumentTypes
.Select(Type.GetType)
.ToArray()!;
MethodInfo? method = type.GetMethod(
req.MethodName,
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
binder: null,
argTypes,
modifiers: null);
if (method == null)
{
throw new MissingMethodException(req.TypeName, req.MethodName);
}
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length != req.Arguments.Length)
{
throw new ArgumentException("Argument count mismatch");
}
object?[] invokeArgs = new object?[req.Arguments.Length];
for (int i = 0; i < invokeArgs.Length; i++)
{
Type targetType = parameters[i].ParameterType;
object arg = req.Arguments[i];
if (arg is JsonElement je)
{
// Deserialize the JSON element into the expected CLR type
invokeArgs[i] = JsonSerializer.Deserialize(je.GetRawText(), targetType);
}
else if (arg == null)
{
invokeArgs[i] = null;
}
else if (!targetType.IsInstanceOfType(arg))
{
// Fallback for simple primitive conversions
invokeArgs[i] = Convert.ChangeType(arg, targetType);
}
else
{
invokeArgs[i] = arg;
}
}
object? result = method.Invoke(null, invokeArgs);
alc.Unload();
return new RemoteExecutionResult
{
Result = result
};
}
catch (Exception ex)
{
return new RemoteExecutionResult
{
Exception = ex.ToString()
};
}
}
}
+27
View File
@@ -0,0 +1,27 @@
using RemoteExec.Server.Hubs;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSignalR(options => options.MaximumReceiveMessageSize = null);
builder.Services.AddOpenApi();
WebApplication app = builder.Build();
app.MapHub<RemoteExecutionHub>("/remote");
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
_ = app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
@@ -0,0 +1,31 @@
{
"profiles": {
"http": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5202"
},
"https": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7109;http://localhost:5202"
},
"Container (Dockerfile)": {
"commandName": "Docker",
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": {
"ASPNETCORE_HTTPS_PORTS": "8081",
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true,
"useSSL": true
}
},
"$schema": "https://json.schemastore.org/launchsettings.json"
}
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>288f55bd-4ce1-4c6a-867f-a12ef417c7cf</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RemoteExec.Shared\RemoteExec.Shared.csproj" />
</ItemGroup>
</Project>
+6
View File
@@ -0,0 +1,6 @@
@RemoteExec.Server_HostAddress = http://localhost:5202
GET {{RemoteExec.Server_HostAddress}}/weatherforecast/
Accept: application/json
###
@@ -0,0 +1,18 @@
using System.Reflection;
using System.Runtime.Loader;
namespace RemoteExec.Server;
public class RemoteJobAssemblyLoadContext(string name) : AssemblyLoadContext(name, true)
{
public event EventHandler<RequestAssemblyEventArgs>? RequestAssembly;
protected override Assembly? Load(AssemblyName assemblyName)
{
RequestAssemblyEventArgs requestAssemblyEventArgs = new RequestAssemblyEventArgs(assemblyName);
RequestAssembly?.Invoke(this, requestAssemblyEventArgs);
return requestAssemblyEventArgs.GetAssemblyAsync().ConfigureAwait(false).GetAwaiter().GetResult();
}
}
@@ -0,0 +1,24 @@
using System.Reflection;
namespace RemoteExec.Server;
public class RequestAssemblyEventArgs(AssemblyName assemblyName) : EventArgs
{
private readonly TaskCompletionSource taskCompletionSource = new TaskCompletionSource();
private Assembly? assembly = null;
public AssemblyName Assembly { get; } = assemblyName;
public async Task<Assembly?> GetAssemblyAsync()
{
await taskCompletionSource.Task;
return assembly;
}
public void SetAssembly(Assembly assembly)
{
this.assembly = assembly;
taskCompletionSource.SetResult();
}
}
+12
View File
@@ -0,0 +1,12 @@
namespace RemoteExec.Server;
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
+9
View File
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}