Files
opengenesislinkserver/addons/OSWebInterface/OSWebInterfaceModule.cs
T

115 lines
4.8 KiB
C#

// SPDX-License-Identifier: MPL-2.0
using System.Collections.Concurrent;
using log4net;
using Mono.Addins;
using Nini.Config;
using System.Collections.Generic;
using OpenMetaverse;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
[assembly: Addin("OSWebInterface", Namespace = "OpenGenesisLINK", Version = "0.1.0")]
[assembly: AddinDependency("::OpenSim.Region.Framework", OpenSim.VersionInfo.VersionNumber)]
namespace OpenGenesisLINK.Addons.OSWebInterface;
/// <summary>
/// Opt-in, loopback-only simulator administration. The DLL is independent of OpenSim.dll.
/// A separate TLS reverse proxy is REQUIRED before using it from another host.
/// </summary>
[Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "OSWebInterface")]
public sealed class OSWebInterfaceModule : ISharedRegionModule
{
private static readonly ILog Log = LogManager.GetLogger(typeof(OSWebInterfaceModule));
private readonly ConcurrentDictionary<UUID, Scene> _scenes = new();
private AdminHttpServer? _server;
private bool _enabled;
private int _port;
private string _storage = "";
public string Name => "OSWebInterface";
public Type? ReplaceableInterface => null;
public void Initialise(IConfigSource source)
{
IConfig? c = source?.Configs["OSWebInterface"];
_enabled = c?.GetBoolean("Enabled", false) ?? false;
if (!_enabled) return;
_port = c?.GetInt("Port", 9847) ?? 9847;
int retention = Math.Clamp(c?.GetInt("BackupRetention", 5) ?? 5, 1, 20);
string folder = c?.GetString("DataDirectory", "os-webinterface-data") ?? "os-webinterface-data";
if (_port < 1025 || _port > 65535 || Path.IsPathRooted(folder) ||
folder.Contains("..", StringComparison.Ordinal) ||
folder.Contains(':', StringComparison.Ordinal))
{
Log.Error("[OS WEBINTERFACE] Unsafe port or DataDirectory; module disabled.");
_enabled = false;
return;
}
_storage = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, folder));
try
{
Directory.CreateDirectory(_storage);
var services = new List<(string Name, Uri Uri)>();
foreach (var item in new (string Section, string Option)[]
{
("AssetService", "AssetServerURI"),
("InventoryService", "InventoryServerURI"),
("GridService", "GridServerURI"),
("UserAccountService", "UserAccountServerURI"),
("AuthenticationService", "AuthenticationServerURI"),
("PresenceService", "PresenceServerURI"),
("FriendsService", "FriendsServerURI"),
("Hypergrid", "GatekeeperURI")
})
{
string configured = source?.Configs[item.Section]?.GetString(item.Option, "") ?? "";
if (Uri.TryCreate(configured, UriKind.Absolute, out Uri? endpoint) &&
(endpoint.Scheme == Uri.UriSchemeHttp || endpoint.Scheme == Uri.UriSchemeHttps))
services.Add((item.Section, endpoint));
}
IConfig? node = source?.Configs["NexVerseNodeAgent"];
bool liveAllowed = c?.GetBoolean("AllowLiveUpdates", false) == true &&
node?.GetBoolean("Enabled", false) == true &&
node.GetBoolean("LiveUpdateEnabled", false);
string nodeId = node?.GetString("NodeId", Environment.MachineName) ?? Environment.MachineName;
string liveUrl = node?.GetString("LiveUpdateServiceUrl", "http://127.0.0.1:19090") ?? "";
string liveToken = Environment.GetEnvironmentVariable("OGL_NODE_UPDATER_TOKEN") ?? "";
var bridge = new LiveUpdateBridge(liveAllowed, nodeId, liveUrl, liveToken);
string publicOrigin = c?.GetString("PublicOrigin", "")?.Trim() ?? "";
_server = new AdminHttpServer(_scenes, _storage, _port, retention, services, bridge, publicOrigin);
_server.Start();
Log.Info($"[OS WEBINTERFACE] Enabled on http://127.0.0.1:{_port}/ (loopback only). Configure an HTTPS reverse proxy for remote access.");
}
catch (Exception e)
{
Log.Error("[OS WEBINTERFACE] Failed to start; admin web disabled", e);
_server?.Dispose();
_server = null;
_enabled = false;
}
}
public void AddRegion(Scene scene)
{
if (_enabled) _scenes[scene.RegionInfo.RegionID] = scene;
}
public void PostInitialise() { }
public void RegionLoaded(Scene scene) { }
public void RemoveRegion(Scene scene)
{
_scenes.TryRemove(scene.RegionInfo.RegionID, out _);
}
public void Close()
{
_enabled = false;
_server?.Dispose();
_server = null;
_scenes.Clear();
}
}