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
|
|
|
|
|
2017-06-19 13:58:01 -04:00
|
|
|
|
using SharedLibrary;
|
|
|
|
|
using SharedLibrary.Interfaces;
|
|
|
|
|
using SharedLibrary.Commands;
|
|
|
|
|
using SharedLibrary.Helpers;
|
2017-06-19 16:46:31 -04:00
|
|
|
|
using SharedLibrary.Exceptions;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
using SharedLibrary.Objects;
|
|
|
|
|
using SharedLibrary.Services;
|
2018-03-09 03:01:12 -05:00
|
|
|
|
using WebfrontCore.Application.API;
|
2018-03-14 01:36:25 -04:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using WebfrontCore;
|
|
|
|
|
using SharedLibrary.Configuration;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2017-06-19 13:58:01 -04:00
|
|
|
|
|
2015-07-03 00:10:01 -04:00
|
|
|
|
namespace IW4MAdmin
|
|
|
|
|
{
|
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-03-06 02:22:19 -05:00
|
|
|
|
public List<int> AdministratorIPs { 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;
|
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-14 01:36:25 -04:00
|
|
|
|
Logger = new Logger($@"{Utilities.OperatingDirectory}Logs{Path.DirectorySeparatorChar}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-03-06 02:22:19 -05:00
|
|
|
|
AdministratorIPs = new List<int>();
|
2018-03-09 03:01:12 -05:00
|
|
|
|
ServerEventOccurred += EventAPI.OnServerEventOccurred;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
ConfigHandler = new BaseConfigurationHandler<ApplicationConfiguration>("IW4MAdminSettings");
|
2018-03-14 01:36:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
AdministratorIPs = (await ClientSvc.Find(c => c.Level > Player.Permission.Trusted))
|
|
|
|
|
.Select(c => c.IPAddress)
|
|
|
|
|
.ToList();
|
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
|
|
|
|
|
SharedLibrary.Plugins.PluginImporter.Load(this);
|
|
|
|
|
|
|
|
|
|
foreach (var Plugin in SharedLibrary.Plugins.PluginImporter.ActivePlugins)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
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-03-06 02:22:19 -05:00
|
|
|
|
Commands.Add(new CRestartServer());
|
2017-06-19 13:58:01 -04:00
|
|
|
|
|
|
|
|
|
foreach (Command C in SharedLibrary.Plugins.PluginImporter.ActiveCommands)
|
|
|
|
|
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; }));
|
|
|
|
|
if (Status.RunAverage > 1000 + UPDATE_FREQUENCY)
|
|
|
|
|
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();
|
2018-02-21 20:29:23 -05:00
|
|
|
|
//WebSvc.WebScheduler.Stop();
|
|
|
|
|
//WebSvc.SchedulerThread.Join();
|
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;
|
2015-07-03 00:10:01 -04:00
|
|
|
|
}
|
|
|
|
|
}
|