Add sane whitelist, fix bad server init logging

This commit is contained in:
John Gross
2018-01-30 23:40:38 -08:00
parent b3d9a64632
commit e42a231553
8 changed files with 96 additions and 4 deletions

View File

@@ -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.");
}
}
}

View File

@@ -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)

View File

@@ -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");

View File

@@ -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);

View File

@@ -210,6 +210,7 @@
<Compile Include="..\Versioning\AssemblyVersion.cs">
<Link>Properties\AssemblyVersion.cs</Link>
</Compile>
<Compile Include="Commands\WhitelistCommands.cs" />
<Compile Include="FlowDocumentTarget.cs" />
<Compile Include="ListBoxExtensions.cs" />
<Compile Include="Managers\EntityControlManager.cs" />

View File

@@ -60,6 +60,9 @@ namespace Torch.Server
/// <inheritdoc />
public List<string> Plugins { get; set; } = new List<string>();
public bool EnableWhitelist { get; set; } = false;
public HashSet<ulong> Whitelist { get; set; } = new HashSet<ulong>();
internal Point WindowSize { get; set; } = new Point(800, 600);
internal Point WindowPosition { get; set; } = new Point();
[XmlIgnore]

View File

@@ -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<CommandManager>().RegisterCommandModule(typeof(WhitelistCommands));
}
/// <inheritdoc />

View File

@@ -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());
}