// 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;
///
/// 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.
///
[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 _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));
}
_server = new AdminHttpServer(_scenes, _storage, _port, retention, services);
_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();
}
}