Files
opengenesislinkserver/addons/SysApi/SysApiConnector.cs
T
OpenGenesis (EULA) e81076f33c feat(g1): global SYS.API alerts over signed NexBus to all simulator regions
Adds a CSRF-protected admin-only alert command with severity presets and 5-second rate limit; signs a NexBus event and fans out via native IDialogModule.SendGeneralAlert across all hosted regions, including EventSim when configured. Default remains opt-in. NexVerse CI, .NET10 full build, SYS.API HTTP/HMAC tests and security contracts passed. Cross-platform builds/ROBUST boot passed; release-artifact upload remains blocked by GitHub storage quota. No production deployment.
2026-11-22 21:53:26 +01:00

102 lines
5.0 KiB
C#

// SPDX-License-Identifier: MPL-2.0
using log4net;
using Nini.Config;
using OpenSim.Framework.Servers.HttpServer;
using OpenSim.Server.Base;
using OpenSim.Server.Handlers.Base;
using OpenSim.Services.Interfaces;
namespace OpenGenesisLINK.SysApi;
/// <summary>
/// Side-by-side ROBUST service plugin. Does not replace NexVerseWorldApiConnector.
/// Port 8851 is an INTERNAL HTTP listener; HTTPS 8850 requires an ACME-backed
/// loopback reverse proxy, with no plaintext public listener.
/// </summary>
public sealed class SysApiConnector : ServiceConnector
{
private static readonly ILog Log = LogManager.GetLogger(typeof(SysApiConnector));
private static SysApiHost? _host;
private static readonly object Gate = new();
public SysApiConnector(IConfigSource config, IHttpServer existingRobustListener, string configName)
: base(config, existingRobustListener, configName)
{
var cfg = config.Configs["SysApi"];
if (cfg?.GetBoolean("Enabled", false) != true)
{
Log.Info("[SYS.API] Disabled by configuration.");
return;
}
try
{
int port = cfg.GetInt("InternalPort", 8851);
if (port is < 1025 or > 65535)
throw new InvalidOperationException("Invalid SysApi internal port");
string publicOrigin = cfg.GetString("PublicOrigin", "").Trim();
string secretDirectory = cfg.GetString("SecretDirectory", "/etc/oglwebinterface").Trim();
if (string.IsNullOrWhiteSpace(secretDirectory) || !Path.IsPathRooted(secretDirectory))
throw new InvalidOperationException("SysApi secret directory must be absolute");
lock (Gate)
{
if (_host != null) return;
var accounts = LoadService<IUserAccountService>(config, "UserAccountService");
var authentication = LoadService<IAuthenticationService>(config, "AuthenticationService");
if (accounts is null || authentication is null)
throw new InvalidOperationException("SYS.API requires local ROBUST account and authentication services");
var token = PortalSecretStore.OpenOrCreate(secretDirectory);
ISysApiReadModel? readModel = null;
try { readModel = new MySqlSysApiReadModel(config); }
catch (Exception e)
{
// Core dashboard stays available if an installation uses
// another DB provider or its read model is misconfigured.
Log.WarnFormat("[SYS.API] Database read model unavailable ({0}); JSON data endpoints disabled.",
e.GetType().Name);
}
// Global alerts use the pre-existing HMAC-signed NexBus. Do not
// expose arbitrary console execution or write to any simulator INI.
ISysApiAlertPublisher? alertPublisher = null;
if (cfg.GetBoolean("AlertsEnabled", false))
{
try
{
var bus = config.Configs["NexBus"];
if (bus?.GetBoolean("Enabled", false) != true ||
string.IsNullOrWhiteSpace(bus.GetString("Peers", "")))
throw new InvalidOperationException("Distributed NexBus with simulator peers is required");
int robustPort = config.Configs["Const"]?.GetInt("PublicPort", 0) ?? 0;
alertPublisher = new NexBusSysApiAlertPublisher(
robustPort,
bus.GetString("InboundPath", "/internal/nexbus/v1/events"),
bus.GetString("SharedKey", ""));
}
catch (Exception ex)
{
// A missing transport must not disable the administrative API.
Log.WarnFormat("[SYS.API ALERT] Disabled: NexBus relay unavailable ({0})",
ex.GetType().Name);
}
}
var host = new SysApiHost(port, publicOrigin, accounts, authentication, token,
readModel, alertPublisher);
host.Start();
_host = host;
Log.InfoFormat("[SYS.API] Foundation active on loopback 127.0.0.1:{0}; public TLS proxy port 8850 requires ACME certificate.", port);
}
}
catch (Exception e)
{
// Never prevent ROBUST/legacy World API startup for an optional SYS.API failure.
Log.Error("[SYS.API] Optional module initialization failed; ROBUST and legacy World API remain available.", e);
}
}
private static T? LoadService<T>(IConfigSource config, string section) where T : class
{
string id = config.Configs[section]?.GetString("LocalServiceModule", "") ?? "";
return string.IsNullOrWhiteSpace(id) ? null :
ServerUtils.LoadPlugin<T>(id, new object[] {config});
}
}