Files
MoShiro/backend/src/MoShiro.Backend.Gateway/GatewayServiceCollectionExtensions.cs
T
justme 1ff847e45a
ci/woodpecker/push/main-amd64-build Pipeline was successful
feat: declare every protocol method's direction and enforce it in the router; add the zero-trust design [CI]
Methods, roles and binary channels are declared in one table per plane (node and peer). RpcRouter checks direction, kind and role when handlers are registered, when messages arrive (fleet_protocol_violation, disconnect after repeated violations) and before sending. NodeConnection is split by responsibility.
2026-10-07 16:17:02 +08:00

47 lines
2.1 KiB
C#

using Fleet.Core;
using Fleet.Rpc;
using Fleet.Server;
using Microsoft.Extensions.DependencyInjection;
using MoShiro.Backend.Application;
using MoShiro.Protocol;
namespace MoShiro.Backend.Gateway;
public static class GatewayServiceCollectionExtensions
{
/// <summary>
/// 在 Fleet.Server 之上接入 MoShiro:期望状态(TaskSet)、RawChunk 收件箱、<c>moshiro.*</c> 方法、注册挂钩和节点事件。
/// 需要事先注册 <see cref="IFleetStore"/> 和 <see cref="TimeProvider"/>,并在 <c>AddMoShiroApplication</c> 之后调用。
/// </summary>
public static IServiceCollection AddMoShiroGateway(this IServiceCollection services)
{
services.AddSingleton<IAgentGateway, FleetGateway>();
services.AddHostedService<ConnectionShutdown>();
services.AddFleetEnrollment();
services.AddFleetServer(options => options.NodeIdPrefix = AgentIds.Prefix)
.AddState<TaskSetStateProvider>()
.AddState<ProbeAgentsStateProvider>()
.AddInbox<RawChunkInbox>()
.WithMethods(MoShiroMethodTable.Table, node => NodeRoles.Of(node.Id.Value))
.MapMethods(MapMoShiroMethods);
return services;
}
private static void MapMoShiroMethods(RpcRouter<NodeCallContext> router, IServiceProvider services)
{
router
.OnNotify(
MoShiroMethods.Event,
MoShiroJsonContext.Default.AgentEventParams,
(context, parameters, ct) => services.GetRequiredService<AgentEventService>().OnEventAsync(context.Node.Id, parameters, ct))
.OnNotify(
MoShiroMethods.Host,
MoShiroJsonContext.Default.HostSnapshot,
(context, snapshot, ct) => services.GetRequiredService<HostStatusService>().OnHostAsync(context.Node.Id, snapshot, ct))
.OnNotify(
MoShiroMethods.TestStatus,
MoShiroJsonContext.Default.TestStatusParams,
(context, parameters, ct) => services.GetRequiredService<TestService>().OnStatusAsync(context.Node.Id, parameters, ct));
}
}