diff --git a/Torch.Server/Commands/WhitelistCommands.cs b/Torch.Server/Commands/WhitelistCommands.cs new file mode 100644 index 0000000..de8ccf5 --- /dev/null +++ b/Torch.Server/Commands/WhitelistCommands.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Torch.Commands; + +namespace Torch.Server.Commands +{ + [Category("whitelist")] + public class WhitelistCommands : CommandModule + { + private TorchConfig Config => (TorchConfig)Context.Torch.Config; + + [Command("on", "Enables the whitelist.")] + public void On() + { + if (!Config.EnableWhitelist) + { + Config.EnableWhitelist = true; + Context.Respond("Whitelist enabled."); + Config.Save(); + } + else + Context.Respond("Whitelist is already enabled."); + } + + [Command("off", "Disables the whitelist")] + public void Off() + { + if (Config.EnableWhitelist) + { + Config.EnableWhitelist = false; + Context.Respond("Whitelist disabled."); + Config.Save(); + } + else + Context.Respond("Whitelist is already disabled."); + } + + [Command("add", "Add a Steam ID to the whitelist.")] + public void Add(ulong steamId) + { + if (Config.Whitelist.Add(steamId)) + { + Context.Respond($"Added {steamId} to the whitelist."); + Config.Save(); + } + else + Context.Respond($"{steamId} is already whitelisted."); + } + + [Command("remove", "Remove a Steam ID from the whitelist.")] + public void Remove(ulong steamId) + { + if (Config.Whitelist.Remove(steamId)) + { + Context.Respond($"Removed {steamId} from the whitelist."); + Config.Save(); + } + else + Context.Respond($"{steamId} is not whitelisted."); + } + } +} diff --git a/Torch.Server/Initializer.cs b/Torch.Server/Initializer.cs index 1a36e4c..431ad79 100644 --- a/Torch.Server/Initializer.cs +++ b/Torch.Server/Initializer.cs @@ -86,7 +86,15 @@ quit"; public void Run() { _server = new TorchServer(_config); - var init = Task.Run(() => _server.Init()); + var init = Task.Run(() => _server.Init()).ContinueWith(x => + { + if (!x.IsFaulted) + return; + + Log.Error("Error initializing server."); + foreach (var e in x.Exception.InnerExceptions) + Log.Error(e.InnerException ?? e); + }); if (!_config.NoGui) { if (_config.Autostart) diff --git a/Torch.Server/Managers/InstanceManager.cs b/Torch.Server/Managers/InstanceManager.cs index f6be324..9798f27 100644 --- a/Torch.Server/Managers/InstanceManager.cs +++ b/Torch.Server/Managers/InstanceManager.cs @@ -134,7 +134,7 @@ namespace Torch.Server.Managers foreach (var mod in checkpoint.Mods) sb.AppendLine(mod.PublishedFileId.ToString()); - DedicatedConfig.Mods = checkpoint.Mods.Select(x => x.PublishedFileId).ToList(); //sb.ToString(); + DedicatedConfig.Mods = checkpoint.Mods.Select(x => x.PublishedFileId).ToList(); Log.Debug("Loaded mod list from world"); diff --git a/Torch.Server/Managers/MultiplayerManagerDedicated.cs b/Torch.Server/Managers/MultiplayerManagerDedicated.cs index 80d9592..824895d 100644 --- a/Torch.Server/Managers/MultiplayerManagerDedicated.cs +++ b/Torch.Server/Managers/MultiplayerManagerDedicated.cs @@ -152,7 +152,13 @@ namespace Torch.Server.Managers _log.Info($"Connection attempt by {steamId} from {ip}"); // TODO implement IP bans - if (Torch.CurrentSession.KeenSession.OnlineMode == MyOnlineModeEnum.OFFLINE && !Torch.CurrentSession.KeenSession.IsUserAdmin(steamId)) + var config = (TorchConfig)Torch.Config; + if (config.EnableWhitelist && !config.Whitelist.Contains(steamId)) + { + _log.Warn($"Rejecting user {steamId} because they are not whitelisted in Torch.cfg."); + UserRejected(steamId, JoinResult.NotInGroup); + } + else if (Torch.CurrentSession.KeenSession.OnlineMode == MyOnlineModeEnum.OFFLINE && !Torch.CurrentSession.KeenSession.IsUserAdmin(steamId)) { _log.Warn($"Rejecting user {steamId}, world is set to offline and user is not admin."); UserRejected(steamId, JoinResult.TicketCanceled); diff --git a/Torch.Server/Torch.Server.csproj b/Torch.Server/Torch.Server.csproj index 18431c5..d1ac3a0 100644 --- a/Torch.Server/Torch.Server.csproj +++ b/Torch.Server/Torch.Server.csproj @@ -210,6 +210,7 @@ Properties\AssemblyVersion.cs + diff --git a/Torch.Server/TorchConfig.cs b/Torch.Server/TorchConfig.cs index bb1c8ad..7037a1a 100644 --- a/Torch.Server/TorchConfig.cs +++ b/Torch.Server/TorchConfig.cs @@ -60,6 +60,9 @@ namespace Torch.Server /// public List Plugins { get; set; } = new List(); + public bool EnableWhitelist { get; set; } = false; + public HashSet Whitelist { get; set; } = new HashSet(); + internal Point WindowSize { get; set; } = new Point(800, 600); internal Point WindowPosition { get; set; } = new Point(); [XmlIgnore] diff --git a/Torch.Server/TorchServer.cs b/Torch.Server/TorchServer.cs index fe67436..7fe26bf 100644 --- a/Torch.Server/TorchServer.cs +++ b/Torch.Server/TorchServer.cs @@ -17,6 +17,8 @@ using Sandbox.Game.World; using Torch.API; using Torch.API.Managers; using Torch.API.Session; +using Torch.Commands; +using Torch.Server.Commands; using Torch.Server.Managers; using Torch.Utils; using VRage; @@ -135,6 +137,7 @@ namespace Torch.Server base.Stop(); Log.Info("Server stopped."); + Config.Save(); State = ServerState.Stopped; IsRunning = false; CanRun = true; @@ -175,6 +178,9 @@ namespace Torch.Server _watchdog?.Dispose(); _watchdog = null; } + + if (newState == TorchSessionState.Loaded) + CurrentSession.Managers.GetManager().RegisterCommandModule(typeof(WhitelistCommands)); } /// diff --git a/Torch/Plugins/PluginManager.cs b/Torch/Plugins/PluginManager.cs index 8c4b252..0db5f45 100644 --- a/Torch/Plugins/PluginManager.cs +++ b/Torch/Plugins/PluginManager.cs @@ -125,7 +125,10 @@ namespace Torch.Managers LoadPluginFromFolder(path); } - _plugins.ForEach(x => x.Value.Init(Torch)); + foreach (var plugin in _plugins.Values) + { + plugin.Init(Torch); + } _log.Info($"Loaded {_plugins.Count} plugins."); PluginsLoaded?.Invoke(_plugins.Values.AsReadOnly()); }