2015-07-03 00:10:01 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-07-03 00:10:01 -04:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.Commands;
|
|
|
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
|
using SharedLibraryCore.Exceptions;
|
|
|
|
|
using SharedLibraryCore.Objects;
|
|
|
|
|
using SharedLibraryCore.Services;
|
2018-04-08 14:48:40 -04:00
|
|
|
|
using IW4MAdmin.Application.API;
|
2018-03-14 01:36:25 -04:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using WebfrontCore;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2018-03-14 01:36:25 -04:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2017-06-19 13:58:01 -04:00
|
|
|
|
|
2018-04-08 14:48:40 -04:00
|
|
|
|
namespace IW4MAdmin.Application
|
2015-07-03 00:10:01 -04:00
|
|
|
|
{
|
2018-02-21 20:29:23 -05:00
|
|
|
|
public class ApplicationManager : IManager
|
2015-07-03 00:10:01 -04:00
|
|
|
|
{
|
2017-10-04 19:01:04 -04:00
|
|
|
|
private List<Server> _servers;
|
|
|
|
|
public List<Server> Servers => _servers.OrderByDescending(s => s.ClientNum).ToList();
|
2018-04-02 01:25:06 -04:00
|
|
|
|
public Dictionary<int, Player> PrivilegedClients { get; set; }
|
2017-06-19 13:58:01 -04:00
|
|
|
|
public ILogger Logger { get; private set; }
|
|
|
|
|
public bool Running { get; private set; }
|
2018-03-09 03:01:12 -05:00
|
|
|
|
public EventHandler<Event> ServerEventOccurred { get; private set; }
|
2017-06-19 13:58:01 -04:00
|
|
|
|
|
|
|
|
|
static ApplicationManager Instance;
|
|
|
|
|
List<AsyncStatus> TaskStatuses;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
List<Command> Commands;
|
2017-06-19 13:58:01 -04:00
|
|
|
|
List<MessageToken> MessageTokens;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
ClientService ClientSvc;
|
|
|
|
|
AliasService AliasSvc;
|
|
|
|
|
PenaltyService PenaltySvc;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
BaseConfigurationHandler<ApplicationConfiguration> ConfigHandler;
|
2018-04-08 14:48:40 -04:00
|
|
|
|
EventApi Api;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
#if FTP_LOG
|
2018-02-08 02:23:45 -05:00
|
|
|
|
const int UPDATE_FREQUENCY = 700;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
#else
|
2018-03-18 22:25:11 -04:00
|
|
|
|
const int UPDATE_FREQUENCY = 450;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
2017-06-19 13:58:01 -04:00
|
|
|
|
private ApplicationManager()
|
2015-07-03 00:10:01 -04:00
|
|
|
|
{
|
2018-03-25 00:32:54 -04:00
|
|
|
|
Logger = new Logger($@"{Utilities.OperatingDirectory}IW4MAdmin.log");
|
2017-10-04 19:01:04 -04:00
|
|
|
|
_servers = new List<Server>();
|
2017-05-26 18:49:27 -04:00
|
|
|
|
Commands = new List<Command>();
|
2017-06-19 13:58:01 -04:00
|
|
|
|
TaskStatuses = new List<AsyncStatus>();
|
|
|
|
|
MessageTokens = new List<MessageToken>();
|
2017-11-25 20:29:58 -05:00
|
|
|
|
ClientSvc = new ClientService();
|
|
|
|
|
AliasSvc = new AliasService();
|
|
|
|
|
PenaltySvc = new PenaltyService();
|
2018-04-02 01:25:06 -04:00
|
|
|
|
PrivilegedClients = new Dictionary<int, Player>();
|
2018-04-08 14:48:40 -04:00
|
|
|
|
Api = new EventApi();
|
|
|
|
|
ServerEventOccurred += Api.OnServerEvent;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
ConfigHandler = new BaseConfigurationHandler<ApplicationConfiguration>("IW4MAdminSettings");
|
2018-04-05 00:38:45 -04:00
|
|
|
|
Console.CancelKeyPress += new ConsoleCancelEventHandler(OnCancelKey);
|
2018-03-14 01:36:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 00:38:45 -04:00
|
|
|
|
private void OnCancelKey(object sender, ConsoleCancelEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-09-29 22:42:24 -04:00
|
|
|
|
public IList<Server> GetServers()
|
2015-07-03 00:10:01 -04:00
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
return Servers;
|
2015-07-03 00:10:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-29 22:42:24 -04:00
|
|
|
|
public IList<Command> GetCommands()
|
2015-08-17 16:38:42 -04:00
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
return Commands;
|
2015-08-17 16:38:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:58:01 -04:00
|
|
|
|
public static ApplicationManager GetInstance()
|
2015-08-22 02:04:30 -04:00
|
|
|
|
{
|
2017-06-19 13:58:01 -04:00
|
|
|
|
return Instance ?? (Instance = new ApplicationManager());
|
2015-08-22 02:04:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
public async Task Init()
|
2015-07-06 13:13:42 -04:00
|
|
|
|
{
|
2018-03-06 02:22:19 -05:00
|
|
|
|
#region DATABASE
|
2018-03-27 00:54:20 -04:00
|
|
|
|
var ipList = (await ClientSvc.Find(c => c.Level > Player.Permission.Trusted))
|
2018-04-04 15:38:34 -04:00
|
|
|
|
.Select(c => new
|
|
|
|
|
{
|
|
|
|
|
c.Password,
|
|
|
|
|
c.PasswordSalt,
|
|
|
|
|
c.ClientId,
|
|
|
|
|
c.Level,
|
|
|
|
|
c.Name
|
|
|
|
|
});
|
2018-03-27 00:54:20 -04:00
|
|
|
|
|
|
|
|
|
foreach (var a in ipList)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-04-04 15:38:34 -04:00
|
|
|
|
PrivilegedClients.Add(a.ClientId, new Player()
|
2018-04-02 01:25:06 -04:00
|
|
|
|
{
|
2018-04-04 15:38:34 -04:00
|
|
|
|
Name = a.Name,
|
2018-04-02 01:25:06 -04:00
|
|
|
|
ClientId = a.ClientId,
|
2018-04-04 15:38:34 -04:00
|
|
|
|
Level = a.Level,
|
|
|
|
|
PasswordSalt = a.PasswordSalt,
|
|
|
|
|
Password = a.Password
|
2018-04-02 01:25:06 -04:00
|
|
|
|
});
|
2018-03-27 00:54:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-29 22:42:24 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
2018-03-18 22:25:11 -04:00
|
|
|
|
#region CONFIG
|
|
|
|
|
var config = ConfigHandler.Configuration();
|
|
|
|
|
if (config?.Servers == null)
|
|
|
|
|
{
|
|
|
|
|
var newConfig = (ApplicationConfiguration)ConfigHandler.Configuration().Generate();
|
|
|
|
|
ConfigHandler.Set(newConfig);
|
|
|
|
|
|
|
|
|
|
newConfig.AutoMessagePeriod = config.AutoMessagePeriod;
|
|
|
|
|
newConfig.AutoMessages = config.AutoMessages;
|
|
|
|
|
newConfig.GlobalRules = config.GlobalRules;
|
|
|
|
|
newConfig.Maps = config.Maps;
|
|
|
|
|
newConfig.Servers = ConfigurationGenerator.GenerateServerConfig(new List<ServerConfiguration>());
|
|
|
|
|
config = newConfig;
|
|
|
|
|
await ConfigHandler.Save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (config.Servers.Count == 0)
|
|
|
|
|
throw new ServerException("A server configuration in IW4MAdminSettings.json is invalid");
|
|
|
|
|
|
|
|
|
|
|
2017-08-09 00:35:23 -04:00
|
|
|
|
#region PLUGINS
|
2018-04-08 02:44:42 -04:00
|
|
|
|
SharedLibraryCore.Plugins.PluginImporter.Load(this);
|
2017-08-09 00:35:23 -04:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
foreach (var Plugin in SharedLibraryCore.Plugins.PluginImporter.ActivePlugins)
|
2017-08-09 00:35:23 -04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-03-06 02:22:19 -05:00
|
|
|
|
await Plugin.OnLoadAsync(this);
|
2017-08-09 00:35:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.WriteError($"An error occured loading plugin {Plugin.Name}");
|
|
|
|
|
Logger.WriteDebug($"Exception: {e.Message}");
|
|
|
|
|
Logger.WriteDebug($"Stack Trace: {e.StackTrace}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2018-03-18 22:25:11 -04:00
|
|
|
|
foreach (var Conf in config.Servers)
|
2018-03-14 01:36:25 -04:00
|
|
|
|
{
|
2018-03-13 17:30:22 -04:00
|
|
|
|
try
|
2015-07-03 00:10:01 -04:00
|
|
|
|
{
|
2018-03-13 17:30:22 -04:00
|
|
|
|
var ServerInstance = new IW4MServer(this, Conf);
|
|
|
|
|
await ServerInstance.Initialize();
|
|
|
|
|
|
|
|
|
|
lock (_servers)
|
|
|
|
|
{
|
|
|
|
|
_servers.Add(ServerInstance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.WriteVerbose($"Now monitoring {ServerInstance.Hostname}");
|
|
|
|
|
|
|
|
|
|
// this way we can keep track of execution time and see if problems arise.
|
|
|
|
|
var Status = new AsyncStatus(ServerInstance, UPDATE_FREQUENCY);
|
|
|
|
|
lock (TaskStatuses)
|
2015-07-06 13:13:42 -04:00
|
|
|
|
{
|
2018-03-13 17:30:22 -04:00
|
|
|
|
TaskStatuses.Add(Status);
|
2015-07-06 13:13:42 -04:00
|
|
|
|
}
|
2018-03-13 17:30:22 -04:00
|
|
|
|
}
|
2015-08-28 00:39:36 -04:00
|
|
|
|
|
2018-03-13 17:30:22 -04:00
|
|
|
|
catch (ServerException e)
|
|
|
|
|
{
|
2018-03-14 01:36:25 -04:00
|
|
|
|
Logger.WriteError($"Not monitoring server {Conf.IPAddress}:{Conf.Port} due to uncorrectable errors");
|
2018-03-13 17:30:22 -04:00
|
|
|
|
if (e.GetType() == typeof(DvarException))
|
|
|
|
|
Logger.WriteDebug($"Could not get the dvar value for {(e as DvarException).Data["dvar_name"]} (ensure the server has a map loaded)");
|
|
|
|
|
else if (e.GetType() == typeof(NetworkException))
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2018-03-13 17:30:22 -04:00
|
|
|
|
Logger.WriteDebug(e.Message);
|
2018-04-02 23:11:19 -04:00
|
|
|
|
//Logger.WriteDebug($"Internal Exception: {e.Data["internal_exception"]}");
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2018-03-13 20:12:24 -04:00
|
|
|
|
|
|
|
|
|
// throw the exception to the main method to stop before instantly exiting
|
|
|
|
|
throw e;
|
2018-03-13 17:30:22 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-03 00:10:01 -04:00
|
|
|
|
}
|
2017-06-19 13:58:01 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region COMMANDS
|
2017-11-25 20:29:58 -05:00
|
|
|
|
if (ClientSvc.GetOwners().Result.Count == 0)
|
2017-11-15 16:04:13 -05:00
|
|
|
|
Commands.Add(new COwner());
|
|
|
|
|
|
|
|
|
|
Commands.Add(new CQuit());
|
|
|
|
|
Commands.Add(new CKick());
|
|
|
|
|
Commands.Add(new CSay());
|
|
|
|
|
Commands.Add(new CTempBan());
|
|
|
|
|
Commands.Add(new CBan());
|
|
|
|
|
Commands.Add(new CWhoAmI());
|
|
|
|
|
Commands.Add(new CList());
|
|
|
|
|
Commands.Add(new CHelp());
|
|
|
|
|
Commands.Add(new CFastRestart());
|
|
|
|
|
Commands.Add(new CMapRotate());
|
|
|
|
|
Commands.Add(new CSetLevel());
|
|
|
|
|
Commands.Add(new CUsage());
|
|
|
|
|
Commands.Add(new CUptime());
|
|
|
|
|
Commands.Add(new CWarn());
|
|
|
|
|
Commands.Add(new CWarnClear());
|
|
|
|
|
Commands.Add(new CUnban());
|
|
|
|
|
Commands.Add(new CListAdmins());
|
|
|
|
|
Commands.Add(new CLoadMap());
|
|
|
|
|
Commands.Add(new CFindPlayer());
|
|
|
|
|
Commands.Add(new CListRules());
|
|
|
|
|
Commands.Add(new CPrivateMessage());
|
|
|
|
|
Commands.Add(new CFlag());
|
|
|
|
|
Commands.Add(new CReport());
|
|
|
|
|
Commands.Add(new CListReports());
|
|
|
|
|
Commands.Add(new CListBanInfo());
|
|
|
|
|
Commands.Add(new CListAlias());
|
|
|
|
|
Commands.Add(new CExecuteRCON());
|
|
|
|
|
Commands.Add(new CPlugins());
|
|
|
|
|
Commands.Add(new CIP());
|
2017-11-18 01:59:37 -05:00
|
|
|
|
Commands.Add(new CMask());
|
2018-02-10 01:26:38 -05:00
|
|
|
|
Commands.Add(new CPruneAdmins());
|
2018-04-02 01:25:06 -04:00
|
|
|
|
Commands.Add(new CKillServer());
|
2018-04-04 15:38:34 -04:00
|
|
|
|
Commands.Add(new CSetPassword());
|
2017-06-19 13:58:01 -04:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
foreach (Command C in SharedLibraryCore.Plugins.PluginImporter.ActiveCommands)
|
2017-06-19 13:58:01 -04:00
|
|
|
|
Commands.Add(C);
|
|
|
|
|
#endregion
|
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
Running = true;
|
2015-07-03 00:10:01 -04:00
|
|
|
|
}
|
2017-08-09 00:35:23 -04:00
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
public void Start()
|
2015-07-03 00:10:01 -04:00
|
|
|
|
{
|
2018-02-10 01:26:38 -05:00
|
|
|
|
while (Running || TaskStatuses.Count > 0)
|
2015-07-03 00:10:01 -04:00
|
|
|
|
{
|
2017-08-09 00:35:23 -04:00
|
|
|
|
for (int i = 0; i < TaskStatuses.Count; i++)
|
2015-07-03 00:10:01 -04:00
|
|
|
|
{
|
2017-08-09 00:35:23 -04:00
|
|
|
|
var Status = TaskStatuses[i];
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
|
|
|
|
// task is read to be rerun
|
|
|
|
|
if (Status.RequestedTask == null || Status.RequestedTask.Status == TaskStatus.RanToCompletion)
|
|
|
|
|
{
|
2018-02-15 23:01:28 -05:00
|
|
|
|
// remove the task when we want to quit and last run has finished
|
|
|
|
|
if (!Running)
|
|
|
|
|
{
|
|
|
|
|
TaskStatuses.RemoveAt(i);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// normal operation
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Status.Update(new Task<bool>(() => { return (Status.Dependant as Server).ProcessUpdatesAsync(Status.GetToken()).Result; }));
|
2018-04-02 01:25:06 -04:00
|
|
|
|
if (Status.RunAverage > 1000 + UPDATE_FREQUENCY && !(Status.Dependant as Server).Throttled)
|
2018-02-15 23:01:28 -05:00
|
|
|
|
Logger.WriteWarning($"Update task average execution is longer than desired for {(Status.Dependant as Server)} [{Status.RunAverage}ms]");
|
|
|
|
|
}
|
2017-05-28 16:47:21 -04:00
|
|
|
|
}
|
2018-02-10 23:33:42 -05:00
|
|
|
|
|
|
|
|
|
if (Status.RequestedTask.Status == TaskStatus.Faulted)
|
|
|
|
|
{
|
|
|
|
|
Logger.WriteWarning($"Update task for {(Status.Dependant as Server)} faulted, restarting");
|
|
|
|
|
Status.Abort();
|
|
|
|
|
}
|
2015-07-03 00:10:01 -04:00
|
|
|
|
}
|
2017-05-28 16:47:21 -04:00
|
|
|
|
|
2017-09-27 16:07:43 -04:00
|
|
|
|
Thread.Sleep(UPDATE_FREQUENCY);
|
2015-07-03 00:10:01 -04:00
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
#if !DEBUG
|
|
|
|
|
foreach (var S in Servers)
|
2018-02-27 22:27:23 -05:00
|
|
|
|
S.Broadcast("^1IW4MAdmin going offline!").Wait();
|
2017-05-26 18:49:27 -04:00
|
|
|
|
#endif
|
2017-10-04 19:01:04 -04:00
|
|
|
|
_servers.Clear();
|
2015-07-03 00:10:01 -04:00
|
|
|
|
}
|
2015-07-06 15:51:08 -04:00
|
|
|
|
|
2017-08-09 00:35:23 -04:00
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
public void Stop()
|
2015-07-06 15:51:08 -04:00
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
Running = false;
|
2015-07-06 15:51:08 -04:00
|
|
|
|
}
|
2017-05-27 18:08:04 -04:00
|
|
|
|
|
2017-06-19 13:58:01 -04:00
|
|
|
|
public ILogger GetLogger()
|
2017-05-27 19:29:20 -04:00
|
|
|
|
{
|
|
|
|
|
return Logger;
|
|
|
|
|
}
|
2017-05-31 01:31:56 -04:00
|
|
|
|
|
2017-06-19 13:58:01 -04:00
|
|
|
|
public IList<MessageToken> GetMessageTokens()
|
2017-05-31 01:31:56 -04:00
|
|
|
|
{
|
|
|
|
|
return MessageTokens;
|
|
|
|
|
}
|
2017-06-12 13:50:00 -04:00
|
|
|
|
|
|
|
|
|
public IList<Player> GetActiveClients()
|
|
|
|
|
{
|
|
|
|
|
var ActiveClients = new List<Player>();
|
|
|
|
|
|
2017-10-04 19:01:04 -04:00
|
|
|
|
foreach (var server in _servers)
|
2017-06-12 13:50:00 -04:00
|
|
|
|
ActiveClients.AddRange(server.Players.Where(p => p != null));
|
|
|
|
|
|
|
|
|
|
return ActiveClients;
|
|
|
|
|
}
|
2017-08-17 19:28:08 -04:00
|
|
|
|
|
2017-11-25 20:29:58 -05:00
|
|
|
|
public ClientService GetClientService() => ClientSvc;
|
|
|
|
|
public AliasService GetAliasService() => AliasSvc;
|
|
|
|
|
public PenaltyService GetPenaltyService() => PenaltySvc;
|
2018-03-14 01:36:25 -04:00
|
|
|
|
|
2018-03-18 22:25:11 -04:00
|
|
|
|
public IConfigurationHandler<ApplicationConfiguration> GetApplicationSettings() => ConfigHandler;
|
2018-04-05 00:38:45 -04:00
|
|
|
|
|
|
|
|
|
public IDictionary<int, Player> GetPrivilegedClients() => PrivilegedClients;
|
2018-04-08 14:48:40 -04:00
|
|
|
|
|
|
|
|
|
public IEventApi GetEventApi() => Api;
|
2018-04-08 17:50:58 -04:00
|
|
|
|
|
|
|
|
|
public bool ShutdownRequested() => !Running;
|
2015-07-03 00:10:01 -04:00
|
|
|
|
}
|
|
|
|
|
}
|