Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace ClassIsland.ManagementServer.Server.Abstractions.Plugin;

public interface IServerPlugin
{
string Identifier { get; }

string Name { get; }

string Description { get; }

string Version { get; }

string MinClientVersion { get; }

string? MaxClientVersion { get; }

Task OnLoadAsync();

Task OnUnloadAsync();

Task<PluginMessageResponse> HandleMessageAsync(Guid clientId, string messageType, byte[] payload);

bool IsCompatible(string clientPluginVersion);
}

public class PluginMessageResponse
{
public int RetCode { get; set; }
public string MessageType { get; set; } = string.Empty;
public byte[] Payload { get; set; } = Array.Empty<byte>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace ClassIsland.ManagementServer.Server.Abstractions.Plugin;

public static class PluginVersionHelper
{
public static bool IsVersionCompatible(string clientVersion, string minVersion, string? maxVersion)
{
if (!Version.TryParse(clientVersion, out var parsedClientVersion))
{
return false;
}

if (!Version.TryParse(minVersion, out var parsedMinVersion))
{
return false;
}

if (parsedClientVersion < parsedMinVersion)
{
return false;
}

if (maxVersion != null && Version.TryParse(maxVersion, out var parsedMaxVersion))
{
if (parsedClientVersion > parsedMaxVersion)
{
return false;
}
}

return true;
}

public static Version? TryParseVersion(string versionString)
{
return Version.TryParse(versionString, out var version) ? version : null;
}
}
138 changes: 138 additions & 0 deletions ClassIsland.ManagementServer.Server/Controllers/PluginController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using ClassIsland.ManagementServer.Server.Authorization;
using ClassIsland.ManagementServer.Server.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ClassIsland.ManagementServer.Server.Controllers;

[ApiController]
[Authorize(Roles = Roles.Admin)]
[Route("api/v1/plugins/")]
public class PluginController(PluginManagerService pluginManagerService, PluginLoaderService pluginLoaderService) : ControllerBase
{
private PluginManagerService PluginManagerService { get; } = pluginManagerService;
private PluginLoaderService PluginLoaderService { get; } = pluginLoaderService;

[HttpGet]
public IActionResult GetPlugins()
{
var plugins = PluginManagerService.GetAllPlugins().Select(p => new
{
p.Identifier,
p.Name,
p.Description,
p.Version,
p.MinClientVersion,
p.MaxClientVersion
});
return Ok(plugins);
}

[HttpGet("{identifier}")]
public IActionResult GetPlugin(string identifier)
{
var plugin = PluginManagerService.Plugins.GetValueOrDefault(identifier);
if (plugin == null)
{
return NotFound(new { message = $"Plugin '{identifier}' not found" });
}

return Ok(new
{
plugin.Identifier,
plugin.Name,
plugin.Description,
plugin.Version,
plugin.MinClientVersion,
plugin.MaxClientVersion
});
}

[HttpPost("load")]
public async Task<IActionResult> LoadPlugin([FromBody] LoadPluginRequest request)
{
var plugins = await PluginLoaderService.LoadPluginFromAssemblyAsync(request.AssemblyPath);
if (plugins.Count > 0)
{
return Ok(new
{
message = $"Loaded {plugins.Count} plugin(s)",
plugins = plugins.Select(p => new { p.Identifier, p.Name, p.Version })
});
}
return BadRequest(new { message = "No plugins found in the specified assembly" });
}

[HttpPost("load-directory")]
public async Task<IActionResult> LoadPluginsFromDirectory([FromBody] LoadPluginDirectoryRequest request)
{
await PluginLoaderService.LoadPluginsFromDirectoryAsync(request.Directory);
return Ok(new { message = $"Loaded plugins from directory: {request.Directory}" });
}

[HttpDelete("{identifier}")]
public async Task<IActionResult> UnloadPlugin(string identifier, [FromQuery] int timeoutSeconds = 30)
{
var result = await PluginLoaderService.UnloadPluginAsync(identifier, TimeSpan.FromSeconds(timeoutSeconds));
if (result)
{
return Ok(new { message = $"Plugin '{identifier}' unloaded successfully" });
}
return BadRequest(new { message = $"Failed to unload plugin '{identifier}'" });
}

[HttpPost("{identifier}/hot-reload")]
public async Task<IActionResult> HotReloadPlugin(string identifier, [FromBody] HotReloadRequest request, [FromQuery] int timeoutSeconds = 30)
{
var result = await PluginLoaderService.HotReloadPluginAsync(identifier, request.NewAssemblyPath, TimeSpan.FromSeconds(timeoutSeconds));
if (result)
{
return Ok(new { message = $"Plugin '{identifier}' hot reloaded successfully" });
}
return BadRequest(new { message = $"Failed to hot reload plugin '{identifier}'" });
}

[HttpPost("{identifier}/notify-enable")]
public async Task<IActionResult> NotifyPluginEnable(string identifier)
{
if (!PluginManagerService.Plugins.ContainsKey(identifier))
{
return NotFound(new { message = $"Plugin '{identifier}' not found" });
}

await PluginManagerService.NotifyClientsPluginEnableAsync(identifier);
return Ok(new { message = $"Clients notified to enable plugin '{identifier}'" });
}

[HttpGet("{identifier}/pending-unload")]
public IActionResult GetPendingUnload(string identifier)
{
if (!PluginManagerService.PendingUnloadConfirmations.TryGetValue(identifier, out var confirmations))
{
return NotFound(new { message = $"No pending unload for plugin '{identifier}'" });
}

return Ok(new
{
PluginIdentifier = identifier,
TotalClients = confirmations.Count,
ConfirmedClients = confirmations.Count(c => c.Value),
PendingClients = confirmations.Where(c => !c.Value).Select(c => c.Key.ToString())
});
}
}

public class LoadPluginRequest
{
public string AssemblyPath { get; set; } = string.Empty;
}

public class LoadPluginDirectoryRequest
{
public string Directory { get; set; } = string.Empty;
}

public class HotReloadRequest
{
public string NewAssemblyPath { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using ClassIsland.ManagementServer.Server.Abstractions.Plugin;
using ClassIsland.Shared.Protobuf.Client;
using ClassIsland.Shared.Protobuf.Enum;
using ClassIsland.Shared.Protobuf.Server;
using ClassIsland.Shared.Protobuf.Service;
using Google.Protobuf;
using Grpc.Core;

namespace ClassIsland.ManagementServer.Server.Services.Grpc;

public class PluginGrpcService(
ILogger<PluginGrpcService> logger,
PluginManagerService pluginManagerService,
CyreneMspConnectionService connectionService) : PluginService.PluginServiceBase
{
private ILogger<PluginGrpcService> Logger { get; } = logger;
private PluginManagerService PluginManagerService { get; } = pluginManagerService;
private CyreneMspConnectionService ConnectionService { get; } = connectionService;

public override Task<PluginRegisterRsp> RegisterPlugins(PluginRegisterReq request, ServerCallContext context)
{
var clientPlugins = request.Plugins.Select(p =>
(p.Identifier, p.Version, p.IsPureLocal)).ToList();

var (compatible, incompatible) = PluginManagerService.CheckPluginCompatibility(clientPlugins);

var response = new PluginRegisterRsp();
response.CompatiblePlugins.AddRange(compatible);
response.IncompatiblePlugins.AddRange(incompatible);

Logger.LogInformation("插件兼容性检查: {} 兼容, {} 不兼容",
compatible.Count, incompatible.Count);

return Task.FromResult(response);
}

public override Task<PluginListRsp> GetServerPlugins(GetServerPluginsReq request, ServerCallContext context)
{
var response = new PluginListRsp();

foreach (var plugin in PluginManagerService.GetAllPlugins())
{
response.Plugins.Add(new ServerPluginInfo
{
Identifier = plugin.Identifier,
Version = plugin.Version,
Name = plugin.Name,
Description = plugin.Description
});
}

Logger.LogInformation("获取到 {} 个服务端插件", response.Plugins.Count);

return Task.FromResult(response);
}

public override async Task<PluginClientToServerRsp> SendPluginMessage(PluginClientToServerReq request, ServerCallContext context)
{
if (!TryGetClientUid(context, out var clientUid))
{
return CreateErrorResponse(request.PluginIdentifier, (int)Retcode.InvalidRequest);
}

var result = await PluginManagerService.HandlePluginMessageAsync(
clientUid,
request.PluginIdentifier,
request.MessageType,
request.Payload.ToByteArray());

return new PluginClientToServerRsp
{
RetCode = result.RetCode,
PluginIdentifier = request.PluginIdentifier,
MessageType = result.MessageType,
Payload = ByteString.CopyFrom(result.Payload)
};
}

public override Task<PluginClientToServerRsp> AcknowledgePluginDisable(PluginDisableAck request, ServerCallContext context)
{
if (!TryGetClientUid(context, out var clientUid))
{
return Task.FromResult(CreateErrorResponse(request.PluginIdentifier, (int)Retcode.InvalidRequest));
}

PluginManagerService.HandlePluginDisableAck(clientUid, request.PluginIdentifier, request.Success);

return Task.FromResult(new PluginClientToServerRsp
{
RetCode = (int)Retcode.Success,
PluginIdentifier = request.PluginIdentifier,
MessageType = "ack"
});
}

/// <summary>
/// Handle client acknowledgement of plugin enable
/// </summary>
public override Task<PluginClientToServerRsp> AcknowledgePluginEnable(PluginEnableAck request, ServerCallContext context)
{
if (!TryGetClientUid(context, out var clientUid))
{
return Task.FromResult(CreateErrorResponse(request.PluginIdentifier, (int)Retcode.InvalidRequest));
}

PluginManagerService.HandlePluginEnableAck(clientUid, request.PluginIdentifier, request.Success);

return Task.FromResult(new PluginClientToServerRsp
{
RetCode = (int)Retcode.Success,
PluginIdentifier = request.PluginIdentifier,
MessageType = "ack"
});
}

/// <summary>
/// Try to extract client UID from request headers
/// </summary>
private static bool TryGetClientUid(ServerCallContext context, out Guid clientUid)
{
return Guid.TryParse(context.RequestHeaders.GetValue("cuid"), out clientUid);
}

/// <summary>
/// Create a standard error response
/// </summary>
private static PluginClientToServerRsp CreateErrorResponse(string pluginIdentifier, int retCode)
{
return new PluginClientToServerRsp
{
RetCode = retCode,
PluginIdentifier = pluginIdentifier,
MessageType = "error"
};
}
}
Loading