2020-04-13 17:16:31 -04:00
|
|
|
|
using IW4MAdmin.Application.IO;
|
2020-02-17 11:05:31 -05:00
|
|
|
|
using IW4MAdmin.Application.Misc;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Dtos;
|
2018-04-16 16:31:14 -04:00
|
|
|
|
using SharedLibraryCore.Exceptions;
|
2019-05-29 17:55:35 -04:00
|
|
|
|
using SharedLibraryCore.Helpers;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2021-07-11 18:26:30 -04:00
|
|
|
|
using System.Net;
|
2022-02-01 15:27:16 -05:00
|
|
|
|
using System.Net.Sockets;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Serilog.Context;
|
2019-04-02 21:20:37 -04:00
|
|
|
|
using static SharedLibraryCore.Database.Models.EFClient;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Models;
|
|
|
|
|
using Data.Models.Server;
|
2022-06-11 12:34:00 -04:00
|
|
|
|
using IW4MAdmin.Application.Alerts;
|
2021-11-23 18:26:33 -05:00
|
|
|
|
using IW4MAdmin.Application.Commands;
|
2021-07-08 22:12:09 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-06-11 12:34:00 -04:00
|
|
|
|
using SharedLibraryCore.Alerts;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using static Data.Models.Client.EFClient;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2015-03-08 17:20:10 -04:00
|
|
|
|
namespace IW4MAdmin
|
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
public class IW4MServer : Server
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2020-01-26 19:06:50 -05:00
|
|
|
|
private static readonly SharedLibraryCore.Localization.TranslationLookup loc = Utilities.CurrentLocalization.LocalizationIndex;
|
2020-04-01 15:11:56 -04:00
|
|
|
|
public GameLogEventDetection LogEvent;
|
2020-01-26 19:06:50 -05:00
|
|
|
|
private readonly ITranslationLookup _translationLookup;
|
2022-03-23 09:43:57 -04:00
|
|
|
|
private readonly IMetaServiceV2 _metaService;
|
2019-08-10 10:08:26 -04:00
|
|
|
|
private const int REPORT_FLAG_COUNT = 4;
|
2020-12-13 21:33:37 -05:00
|
|
|
|
private long lastGameTime = 0;
|
2019-04-23 18:27:20 -04:00
|
|
|
|
|
2018-10-25 09:14:39 -04:00
|
|
|
|
public int Id { get; private set; }
|
2020-11-17 19:24:54 -05:00
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private readonly IClientNoticeMessageFormatter _messageFormatter;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
private readonly ILookupCache<EFServer> _serverCache;
|
2021-06-30 19:10:45 -04:00
|
|
|
|
private readonly CommandConfiguration _commandConfiguration;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
|
|
|
|
public IW4MServer(
|
|
|
|
|
ServerConfiguration serverConfiguration,
|
2021-06-30 19:10:45 -04:00
|
|
|
|
CommandConfiguration commandConfiguration,
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ITranslationLookup lookup,
|
2022-03-23 09:43:57 -04:00
|
|
|
|
IMetaServiceV2 metaService,
|
2020-11-17 19:24:54 -05:00
|
|
|
|
IServiceProvider serviceProvider,
|
2021-03-22 12:09:25 -04:00
|
|
|
|
IClientNoticeMessageFormatter messageFormatter,
|
|
|
|
|
ILookupCache<EFServer> serverCache) : base(serviceProvider.GetRequiredService<ILogger<Server>>(),
|
2022-01-26 11:32:16 -05:00
|
|
|
|
#pragma warning disable CS0612
|
2020-11-11 18:31:26 -05:00
|
|
|
|
serviceProvider.GetRequiredService<SharedLibraryCore.Interfaces.ILogger>(),
|
2022-01-26 11:32:16 -05:00
|
|
|
|
#pragma warning restore CS0612
|
2020-11-11 18:31:26 -05:00
|
|
|
|
serverConfiguration,
|
|
|
|
|
serviceProvider.GetRequiredService<IManager>(),
|
|
|
|
|
serviceProvider.GetRequiredService<IRConConnectionFactory>(),
|
2021-11-28 11:17:56 -05:00
|
|
|
|
serviceProvider.GetRequiredService<IGameLogReaderFactory>(), serviceProvider)
|
2018-06-30 21:55:16 -04:00
|
|
|
|
{
|
2020-01-26 19:06:50 -05:00
|
|
|
|
_translationLookup = lookup;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
_metaService = metaService;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_serviceProvider = serviceProvider;
|
2020-11-17 19:24:54 -05:00
|
|
|
|
_messageFormatter = messageFormatter;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
_serverCache = serverCache;
|
2021-06-30 19:10:45 -04:00
|
|
|
|
_commandConfiguration = commandConfiguration;
|
2018-06-30 21:55:16 -04:00
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
public override async Task<EFClient> OnClientConnected(EFClient clientFromLog)
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogDebug("Client slot #{clientNumber} now reserved", clientFromLog.ClientNumber);
|
2019-04-12 23:25:18 -04:00
|
|
|
|
|
2019-05-17 10:02:09 -04:00
|
|
|
|
EFClient client = await Manager.GetClientService().GetUnique(clientFromLog.NetworkId);
|
2015-03-08 17:20:10 -04:00
|
|
|
|
|
2019-05-17 10:02:09 -04:00
|
|
|
|
// first time client is connecting to server
|
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogDebug("Client {client} first time connecting", clientFromLog.ToString());
|
2019-05-17 10:02:09 -04:00
|
|
|
|
clientFromLog.CurrentServer = this;
|
|
|
|
|
client = await Manager.GetClientService().Create(clientFromLog);
|
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2021-06-03 11:51:03 -04:00
|
|
|
|
client.CopyAdditionalProperties(clientFromLog);
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// this is only a temporary version until the IPAddress is transmitted
|
2019-05-17 10:02:09 -04:00
|
|
|
|
client.CurrentAlias = new EFAlias()
|
|
|
|
|
{
|
|
|
|
|
Name = clientFromLog.Name,
|
|
|
|
|
IPAddress = clientFromLog.IPAddress
|
|
|
|
|
};
|
2018-08-07 14:43:09 -04:00
|
|
|
|
|
2019-05-17 10:02:09 -04:00
|
|
|
|
// Do the player specific stuff
|
|
|
|
|
client.ClientNumber = clientFromLog.ClientNumber;
|
|
|
|
|
client.Score = clientFromLog.Score;
|
|
|
|
|
client.Ping = clientFromLog.Ping;
|
2022-03-25 14:16:41 -04:00
|
|
|
|
client.Team = clientFromLog.Team;
|
2022-03-28 17:05:00 -04:00
|
|
|
|
client.TeamName = clientFromLog.TeamName;
|
2019-05-17 10:02:09 -04:00
|
|
|
|
client.CurrentServer = this;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
client.State = ClientState.Connecting;
|
2015-04-10 00:02:12 -04:00
|
|
|
|
|
2019-05-17 10:02:09 -04:00
|
|
|
|
Clients[client.ClientNumber] = client;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogDebug("End PreConnect for {client}", client.ToString());
|
2019-05-17 10:02:09 -04:00
|
|
|
|
var e = new GameEvent()
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2019-05-17 10:02:09 -04:00
|
|
|
|
Origin = client,
|
|
|
|
|
Owner = this,
|
|
|
|
|
Type = GameEvent.EventType.Connect
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
Manager.AddEvent(e);
|
2019-11-15 15:50:20 -05:00
|
|
|
|
return client;
|
2015-03-08 17:20:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
public override async Task OnClientDisconnected(EFClient client)
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2019-12-27 21:37:50 -05:00
|
|
|
|
if (!GetClientsAsList().Any(_client => _client.NetworkId == client.NetworkId))
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using (LogContext.PushProperty("Server", ToString()))
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogWarning("{client} disconnecting, but they are not connected", client.ToString());
|
|
|
|
|
}
|
2019-12-27 21:37:50 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-07 21:30:11 -05:00
|
|
|
|
#if DEBUG == true
|
2019-02-17 19:48:40 -05:00
|
|
|
|
if (client.ClientNumber >= 0)
|
2015-03-16 16:40:30 -04:00
|
|
|
|
{
|
2019-02-17 19:48:40 -05:00
|
|
|
|
#endif
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogDebug("Client {@client} disconnecting...", new { client=client.ToString(), client.State });
|
2020-02-06 22:05:50 -05:00
|
|
|
|
Clients[client.ClientNumber] = null;
|
|
|
|
|
await client.OnDisconnect();
|
2019-04-02 21:20:37 -04:00
|
|
|
|
|
2020-02-06 22:05:50 -05:00
|
|
|
|
var e = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Origin = client,
|
|
|
|
|
Owner = this,
|
|
|
|
|
Type = GameEvent.EventType.Disconnect
|
|
|
|
|
};
|
2015-03-08 17:20:10 -04:00
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
Manager.AddEvent(e);
|
2019-02-17 19:48:40 -05:00
|
|
|
|
#if DEBUG == true
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2015-03-08 17:20:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 02:32:30 -04:00
|
|
|
|
public override async Task ExecuteEvent(GameEvent E)
|
2015-07-06 13:13:42 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using (LogContext.PushProperty("Server", ToString()))
|
2018-09-23 20:45:54 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (E.IsBlocking)
|
2018-05-10 01:34:29 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
await E.Origin?.Lock();
|
2018-05-10 01:34:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
bool canExecuteCommand = true;
|
|
|
|
|
|
|
|
|
|
try
|
2015-08-20 15:23:13 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (!await ProcessEvent(E))
|
2019-11-15 15:50:20 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
return;
|
2019-11-15 15:50:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
Command C = null;
|
|
|
|
|
if (E.Type == GameEvent.EventType.Command)
|
2019-11-15 15:50:20 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-06-30 19:10:45 -04:00
|
|
|
|
C = await SharedLibraryCore.Commands.CommandProcessing.ValidateCommand(E, Manager.GetApplicationSettings().Configuration(), _commandConfiguration);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (CommandException e)
|
|
|
|
|
{
|
2021-01-17 22:58:18 -05:00
|
|
|
|
ServerLogger.LogWarning(e, "Error validating command from event {@event}",
|
|
|
|
|
new { E.Type, E.Data, E.Message, E.Subtype, E.IsRemote, E.CorrelationId });
|
2020-11-11 18:31:26 -05:00
|
|
|
|
E.FailReason = GameEvent.EventFailReason.Invalid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (C != null)
|
|
|
|
|
{
|
|
|
|
|
E.Extra = C;
|
|
|
|
|
}
|
2019-11-15 15:50:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
try
|
2019-11-15 15:50:20 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
var loginPlugin = Manager.Plugins.FirstOrDefault(_plugin => _plugin.Name == "Login");
|
|
|
|
|
|
|
|
|
|
if (loginPlugin != null)
|
|
|
|
|
{
|
|
|
|
|
await loginPlugin.OnEventAsync(E, this);
|
|
|
|
|
}
|
2019-11-15 15:50:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
catch (AuthorizationException e)
|
|
|
|
|
{
|
|
|
|
|
E.Origin.Tell($"{loc["COMMAND_NOTAUTHORIZED"]} - {e.Message}");
|
|
|
|
|
canExecuteCommand = false;
|
|
|
|
|
}
|
2020-04-20 11:45:58 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// hack: this prevents commands from getting executing that 'shouldn't' be
|
|
|
|
|
if (E.Type == GameEvent.EventType.Command && E.Extra is Command command &&
|
|
|
|
|
(canExecuteCommand || E.Origin?.Level == Permission.Console))
|
2020-04-20 11:45:58 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogInformation("Executing command {comamnd} for {client}", command.Name, E.Origin.ToString());
|
|
|
|
|
await command.ExecuteAsync(E);
|
2020-04-20 11:45:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
var pluginTasks = Manager.Plugins
|
|
|
|
|
.Where(_plugin => _plugin.Name != "Login")
|
|
|
|
|
.Select(async plugin => await CreatePluginTask(plugin, E));
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(pluginTasks);
|
2020-04-20 11:45:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
catch (Exception e)
|
2020-04-20 11:45:58 -04:00
|
|
|
|
{
|
2020-11-18 19:48:24 -05:00
|
|
|
|
ServerLogger.LogError(e, "Unexpected exception occurred processing event");
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (E.Origin != null && E.Type == GameEvent.EventType.Command)
|
|
|
|
|
{
|
|
|
|
|
E.Origin.Tell(_translationLookup["SERVER_ERROR_COMMAND_INGAME"]);
|
|
|
|
|
}
|
2020-04-20 11:45:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
finally
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (E.IsBlocking)
|
2019-11-15 15:50:20 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
E.Origin?.Unlock();
|
2019-11-15 15:50:20 -05:00
|
|
|
|
}
|
2020-05-16 12:54:01 -04:00
|
|
|
|
}
|
2015-08-20 13:52:30 -04:00
|
|
|
|
}
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
private async Task CreatePluginTask(IPlugin plugin, GameEvent gameEvent)
|
|
|
|
|
{
|
|
|
|
|
// we don't want to run the events on parser plugins
|
2022-02-13 22:38:40 -05:00
|
|
|
|
if (plugin is ScriptPlugin { IsParser: true })
|
2019-11-15 15:50:20 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
return;
|
2019-11-15 15:50:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using var tokenSource = new CancellationTokenSource();
|
|
|
|
|
tokenSource.CancelAfter(Utilities.DefaultCommandTimeout);
|
2019-11-15 15:50:20 -05:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-11-13 22:19:44 -05:00
|
|
|
|
await plugin.OnEventAsync(gameEvent, this).WithWaitCancellation(tokenSource.Token);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
2022-02-13 22:38:40 -05:00
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogWarning("Timed out executing event {EventType} for {Plugin}", gameEvent.Type,
|
|
|
|
|
plugin.Name);
|
|
|
|
|
}
|
2020-11-11 18:31:26 -05:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-11-20 20:01:25 -05:00
|
|
|
|
Console.WriteLine(loc["SERVER_PLUGIN_ERROR"].FormatExt(plugin.Name, ex.GetType().Name));
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogError(ex, "Could not execute {methodName} for plugin {plugin}",
|
|
|
|
|
nameof(plugin.OnEventAsync), plugin.Name);
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
2015-08-20 13:52:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Perform the server specific tasks when an event occurs
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="E"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-11-11 18:31:26 -05:00
|
|
|
|
protected override async Task<bool> ProcessEvent(GameEvent E)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using (LogContext.PushProperty("Server", ToString()))
|
|
|
|
|
using (LogContext.PushProperty("EventType", E.Type))
|
2019-04-23 18:27:20 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogDebug("processing event of type {type}", E.Type);
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
if (E.Type == GameEvent.EventType.Start)
|
|
|
|
|
{
|
|
|
|
|
var existingServer = (await _serverCache
|
|
|
|
|
.FirstAsync(server => server.Id == EndPoint));
|
|
|
|
|
|
|
|
|
|
var serverId = await GetIdForServer(E.Owner);
|
|
|
|
|
|
|
|
|
|
if (existingServer == null)
|
|
|
|
|
{
|
|
|
|
|
var server = new EFServer()
|
|
|
|
|
{
|
|
|
|
|
Port = Port,
|
|
|
|
|
EndPoint = ToString(),
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
GameName = (Reference.Game?)GameName,
|
|
|
|
|
HostName = Hostname
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await _serverCache.AddAsync(server);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.ConnectionLost)
|
2019-04-23 18:27:20 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
var exception = E.Extra as Exception;
|
|
|
|
|
ServerLogger.LogError(exception,
|
|
|
|
|
"Connection lost with {server}", ToString());
|
|
|
|
|
|
|
|
|
|
if (!Manager.GetApplicationSettings().Configuration().IgnoreServerConnectionLost)
|
2020-04-22 19:46:41 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
Console.WriteLine(loc["SERVER_ERROR_COMMUNICATION"].FormatExt($"{IP}:{Port}"));
|
2022-06-11 12:34:00 -04:00
|
|
|
|
|
|
|
|
|
var alert = Alert.AlertState.Build().OfType(E.Type.ToString())
|
|
|
|
|
.WithCategory(Alert.AlertCategory.Error)
|
|
|
|
|
.FromSource("System")
|
|
|
|
|
.WithMessage(loc["SERVER_ERROR_COMMUNICATION"].FormatExt($"{IP}:{Port}"))
|
|
|
|
|
.ExpiresIn(TimeSpan.FromDays(1));
|
|
|
|
|
|
|
|
|
|
Manager.AlertManager.AddAlert(alert);
|
2020-04-22 19:46:41 -04:00
|
|
|
|
}
|
2022-06-11 12:34:00 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
Throttled = true;
|
2019-04-25 14:00:54 -04:00
|
|
|
|
}
|
2019-04-23 18:27:20 -04:00
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.ConnectionRestored)
|
2019-11-15 15:50:20 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogInformation(
|
|
|
|
|
"Connection restored with {server}", ToString());
|
|
|
|
|
|
|
|
|
|
if (!Manager.GetApplicationSettings().Configuration().IgnoreServerConnectionLost)
|
2019-11-15 15:50:20 -05:00
|
|
|
|
{
|
2022-06-11 12:34:00 -04:00
|
|
|
|
Console.WriteLine(loc["MANAGER_CONNECTION_REST"].FormatExt($"{IP}:{Port}"));
|
|
|
|
|
|
|
|
|
|
var alert = Alert.AlertState.Build().OfType(E.Type.ToString())
|
|
|
|
|
.WithCategory(Alert.AlertCategory.Information)
|
|
|
|
|
.FromSource("System")
|
|
|
|
|
.WithMessage(loc["MANAGER_CONNECTION_REST"].FormatExt($"{IP}:{Port}"))
|
|
|
|
|
.ExpiresIn(TimeSpan.FromDays(1));
|
|
|
|
|
|
|
|
|
|
Manager.AlertManager.AddAlert(alert);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
2019-11-15 15:50:20 -05:00
|
|
|
|
|
2021-09-18 19:28:37 -04:00
|
|
|
|
if (!string.IsNullOrEmpty(CustomSayName))
|
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
await this.SetDvarAsync("sv_sayname", CustomSayName, Manager.CancellationToken);
|
2021-09-18 19:28:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
Throttled = false;
|
2019-11-15 15:50:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.ChangePermission)
|
2019-02-22 20:06:51 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
var newPermission = (Permission) E.Extra;
|
|
|
|
|
ServerLogger.LogInformation("{origin} is setting {target} to permission level {newPermission}",
|
|
|
|
|
E.Origin.ToString(), E.Target.ToString(), newPermission);
|
|
|
|
|
await Manager.GetClientService().UpdateLevel(newPermission, E.Target, E.Origin);
|
2019-02-22 20:06:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Connect)
|
2020-04-25 20:01:26 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (E.Origin.State != ClientState.Connected)
|
|
|
|
|
{
|
|
|
|
|
E.Origin.State = ClientState.Connected;
|
|
|
|
|
E.Origin.LastConnection = DateTime.UtcNow;
|
|
|
|
|
E.Origin.Connections += 1;
|
2019-04-02 21:20:37 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ChatHistory.Add(new ChatInfo()
|
|
|
|
|
{
|
|
|
|
|
Name = E.Origin.Name,
|
|
|
|
|
Message = "CONNECTED",
|
|
|
|
|
Time = DateTime.UtcNow
|
|
|
|
|
});
|
2019-04-05 22:15:17 -04:00
|
|
|
|
|
2022-03-23 09:43:57 -04:00
|
|
|
|
var clientTag = await _metaService.GetPersistentMetaByLookup(EFMeta.ClientTagV2,
|
|
|
|
|
EFMeta.ClientTagNameV2, E.Origin.ClientId, Manager.CancellationToken);
|
2021-01-24 12:47:19 -05:00
|
|
|
|
|
|
|
|
|
if (clientTag?.LinkedMeta != null)
|
|
|
|
|
{
|
|
|
|
|
E.Origin.Tag = clientTag.LinkedMeta.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 22:12:09 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var factory = _serviceProvider.GetRequiredService<IDatabaseContextFactory>();
|
2022-01-28 10:35:01 -05:00
|
|
|
|
await using var context = factory.CreateContext(enableTracking: false);
|
2021-07-08 22:12:09 -04:00
|
|
|
|
|
|
|
|
|
var messageCount = await context.InboxMessages
|
|
|
|
|
.CountAsync(msg => msg.DestinationClientId == E.Origin.ClientId && !msg.IsDelivered);
|
|
|
|
|
|
|
|
|
|
if (messageCount > 0)
|
|
|
|
|
{
|
|
|
|
|
E.Origin.Tell(_translationLookup["SERVER_JOIN_OFFLINE_MESSAGES"]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogError(ex, "Could not get offline message count for {Client}", E.Origin.ToString());
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-16 14:20:54 -04:00
|
|
|
|
await E.Origin.OnJoin(E.Origin.IPAddress, Manager.GetApplicationSettings().Configuration().EnableImplicitAccountLinking);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
2019-06-09 10:50:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.PreConnect)
|
2018-04-28 21:11:13 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogInformation("Detected PreConnect for {client} from {source}", E.Origin.ToString(), E.Source);
|
|
|
|
|
// we don't want to track bots in the database at all if ignore bots is requested
|
|
|
|
|
if (E.Origin.IsBot && Manager.GetApplicationSettings().Configuration().IgnoreBots)
|
2019-05-17 10:02:09 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
return false;
|
2019-05-17 10:02:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (E.Origin.CurrentServer == null)
|
2019-05-17 10:02:09 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogWarning("Preconnecting client {client} did not have a current server specified",
|
|
|
|
|
E.Origin.ToString());
|
|
|
|
|
E.Origin.CurrentServer = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var existingClient = GetClientsAsList().FirstOrDefault(_client => _client.Equals(E.Origin));
|
2019-05-17 10:02:09 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// they're already connected
|
|
|
|
|
if (existingClient != null && existingClient.ClientNumber == E.Origin.ClientNumber &&
|
|
|
|
|
!E.Origin.IsBot)
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogInformation("{client} is already connected, so we are ignoring their PreConnect",
|
|
|
|
|
E.Origin.ToString());
|
2019-05-17 10:02:09 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-04-28 21:11:13 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// this happens for some reason rarely where the client spots get out of order
|
|
|
|
|
// possible a connect/reconnect game event before we get to process it here
|
|
|
|
|
// it appears that new games decide to switch client slots between maps (even if the clients aren't disconnecting)
|
|
|
|
|
// bots can have duplicate names which causes conflicting GUIDs
|
2020-11-27 22:52:52 -05:00
|
|
|
|
if (existingClient != null && existingClient.ClientNumber != E.Origin.ClientNumber &&
|
2020-11-11 18:31:26 -05:00
|
|
|
|
!E.Origin.IsBot)
|
2018-11-07 21:30:11 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogWarning(
|
|
|
|
|
"client {client} is trying to connect in client slot {newClientSlot}, but they are already registered in client slot {oldClientSlot}, swapping...",
|
|
|
|
|
E.Origin.ToString(), E.Origin.ClientNumber, existingClient.ClientNumber);
|
|
|
|
|
// we need to remove them so the client spots can swap
|
|
|
|
|
await OnClientDisconnected(Clients[existingClient.ClientNumber]);
|
2018-11-07 21:30:11 -05:00
|
|
|
|
}
|
2018-04-28 21:11:13 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (Clients[E.Origin.ClientNumber] == null)
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogDebug("Begin PreConnect for {origin}", E.Origin.ToString());
|
|
|
|
|
// we can go ahead and put them in so that they don't get re added
|
|
|
|
|
Clients[E.Origin.ClientNumber] = E.Origin;
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-22 17:03:34 -04:00
|
|
|
|
E.Origin.GameName = (Reference.Game?)GameName;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
E.Origin = await OnClientConnected(E.Origin);
|
|
|
|
|
E.Target = E.Origin;
|
|
|
|
|
}
|
2018-06-30 21:55:16 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{loc["SERVER_ERROR_ADDPLAYER"]} {E.Origin}");
|
|
|
|
|
ServerLogger.LogError(ex, "Could not add player {player}", E.Origin.ToString());
|
|
|
|
|
Clients[E.Origin.ClientNumber] = null;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-06-25 19:01:47 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (E.Origin.Level > Permission.Moderator)
|
|
|
|
|
{
|
2021-11-23 18:26:33 -05:00
|
|
|
|
E.Origin.Tell(loc["SERVER_REPORT_COUNT_V2"].FormatExt(E.Owner.Reports.Count));
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for some reason there's still a client in the spot
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogWarning(
|
|
|
|
|
"{origin} is connecting but {existingClient} is currently in that client slot",
|
|
|
|
|
E.Origin.ToString(), Clients[E.Origin.ClientNumber].ToString());
|
|
|
|
|
}
|
2019-06-25 19:01:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Flag)
|
2018-10-02 13:39:08 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
DateTime? expires = null;
|
2018-10-02 13:39:08 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (E.Extra is TimeSpan ts)
|
|
|
|
|
{
|
|
|
|
|
expires = DateTime.UtcNow + ts;
|
|
|
|
|
}
|
2019-04-05 14:34:03 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// todo: maybe move this to a seperate function
|
|
|
|
|
var newPenalty = new EFPenalty()
|
|
|
|
|
{
|
|
|
|
|
Type = EFPenalty.PenaltyType.Flag,
|
|
|
|
|
Expires = expires,
|
|
|
|
|
Offender = E.Target,
|
|
|
|
|
Offense = E.Data,
|
|
|
|
|
Punisher = E.ImpersonationOrigin ?? E.Origin,
|
|
|
|
|
When = DateTime.UtcNow,
|
|
|
|
|
Link = E.Target.AliasLink
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-21 11:40:03 -04:00
|
|
|
|
await Manager.GetPenaltyService().Create(newPenalty);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
E.Target.SetLevel(Permission.Flagged, E.Origin);
|
|
|
|
|
}
|
2018-10-02 13:39:08 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Unflag)
|
2018-10-02 13:39:08 -04:00
|
|
|
|
{
|
2022-02-22 18:09:50 -05:00
|
|
|
|
var unflagPenalty = new EFPenalty
|
2020-11-11 18:31:26 -05:00
|
|
|
|
{
|
|
|
|
|
Type = EFPenalty.PenaltyType.Unflag,
|
|
|
|
|
Expires = DateTime.UtcNow,
|
|
|
|
|
Offender = E.Target,
|
|
|
|
|
Offense = E.Data,
|
|
|
|
|
Punisher = E.ImpersonationOrigin ?? E.Origin,
|
|
|
|
|
When = DateTime.UtcNow,
|
|
|
|
|
Link = E.Target.AliasLink
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
E.Target.SetLevel(Permission.User, E.Origin);
|
2022-02-22 18:09:50 -05:00
|
|
|
|
await Manager.GetPenaltyService().RemoveActivePenalties(E.Target.AliasLinkId, E.Target.NetworkId,
|
|
|
|
|
E.Target.CurrentAlias?.IPAddress);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
await Manager.GetPenaltyService().Create(unflagPenalty);
|
|
|
|
|
}
|
2019-05-08 21:34:17 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Report)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
Reports.Add(new Report()
|
|
|
|
|
{
|
|
|
|
|
Origin = E.Origin,
|
|
|
|
|
Target = E.Target,
|
2022-04-19 19:43:58 -04:00
|
|
|
|
Reason = E.Data,
|
|
|
|
|
ReportedOn = DateTime.UtcNow
|
2020-11-11 18:31:26 -05:00
|
|
|
|
});
|
2019-05-08 21:34:17 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
var newReport = new EFPenalty()
|
|
|
|
|
{
|
|
|
|
|
Type = EFPenalty.PenaltyType.Report,
|
|
|
|
|
Expires = DateTime.UtcNow,
|
|
|
|
|
Offender = E.Target,
|
|
|
|
|
Offense = E.Message,
|
|
|
|
|
Punisher = E.ImpersonationOrigin ?? E.Origin,
|
|
|
|
|
Active = true,
|
|
|
|
|
When = DateTime.UtcNow,
|
|
|
|
|
Link = E.Target.AliasLink
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await Manager.GetPenaltyService().Create(newReport);
|
|
|
|
|
|
2021-03-23 11:34:44 -04:00
|
|
|
|
var reportNum = await Manager.GetClientService().GetClientReportCount(E.Target.ClientId);
|
|
|
|
|
var canBeAutoFlagged = await Manager.GetClientService().CanBeAutoFlagged(E.Target.ClientId);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
2021-03-23 11:34:44 -04:00
|
|
|
|
if (!E.Target.IsPrivileged() && reportNum >= REPORT_FLAG_COUNT && canBeAutoFlagged)
|
2020-11-11 18:31:26 -05:00
|
|
|
|
{
|
|
|
|
|
E.Target.Flag(
|
|
|
|
|
Utilities.CurrentLocalization.LocalizationIndex["SERVER_AUTO_FLAG_REPORT"]
|
|
|
|
|
.FormatExt(reportNum), Utilities.IW4MAdminClient(E.Owner));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-10 10:08:26 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.TempBan)
|
|
|
|
|
{
|
|
|
|
|
await TempBan(E.Data, (TimeSpan) E.Extra, E.Target, E.ImpersonationOrigin ?? E.Origin);
|
|
|
|
|
}
|
2019-08-10 10:08:26 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Ban)
|
2019-08-10 10:08:26 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
bool isEvade = E.Extra != null ? (bool) E.Extra : false;
|
|
|
|
|
await Ban(E.Data, E.Target, E.ImpersonationOrigin ?? E.Origin, isEvade);
|
2019-08-10 10:08:26 -04:00
|
|
|
|
}
|
2018-10-02 13:39:08 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Unban)
|
|
|
|
|
{
|
|
|
|
|
await Unban(E.Data, E.Target, E.ImpersonationOrigin ?? E.Origin);
|
|
|
|
|
}
|
2018-10-02 13:39:08 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Kick)
|
|
|
|
|
{
|
2020-11-17 19:24:54 -05:00
|
|
|
|
await Kick(E.Data, E.Target, E.ImpersonationOrigin ?? E.Origin, E.Extra as EFPenalty);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
2018-10-02 13:39:08 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Warn)
|
|
|
|
|
{
|
|
|
|
|
await Warn(E.Data, E.Target, E.ImpersonationOrigin ?? E.Origin);
|
|
|
|
|
}
|
2018-10-02 13:39:08 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Disconnect)
|
|
|
|
|
{
|
|
|
|
|
ChatHistory.Add(new ChatInfo()
|
|
|
|
|
{
|
|
|
|
|
Name = E.Origin.Name,
|
|
|
|
|
Message = "DISCONNECTED",
|
|
|
|
|
Time = DateTime.UtcNow
|
|
|
|
|
});
|
2018-10-02 13:39:08 -04:00
|
|
|
|
|
2022-04-28 11:14:35 -04:00
|
|
|
|
await _metaService.SetPersistentMeta("LastMapPlayed", CurrentMap.Alias, E.Origin.ClientId);
|
|
|
|
|
await _metaService.SetPersistentMeta("LastServerPlayed", E.Owner.Hostname, E.Origin.ClientId);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
2018-10-03 22:20:49 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.PreDisconnect)
|
2019-04-02 21:20:37 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogInformation("Detected PreDisconnect for {client} from {source}",
|
|
|
|
|
E.Origin.ToString(), E.Source);
|
|
|
|
|
bool isPotentialFalseQuit = E.GameTime.HasValue && E.GameTime.Value == lastGameTime;
|
2019-02-22 20:06:51 -05:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (isPotentialFalseQuit)
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogDebug(
|
|
|
|
|
"Received PreDisconnect event for {origin}, but it occured at game time {gameTime}, which is the same last map change, so we're ignoring",
|
|
|
|
|
E.Origin.ToString(), E.GameTime);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-02-06 19:35:30 -05:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// predisconnect comes from minimal rcon polled players and minimal log players
|
|
|
|
|
// so we need to disconnect the "full" version of the client
|
|
|
|
|
var client = GetClientsAsList().FirstOrDefault(_client => _client.Equals(E.Origin));
|
2020-02-06 19:35:30 -05:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
|
|
|
|
// this can happen when the status picks up the connect before the log does
|
|
|
|
|
ServerLogger.LogInformation(
|
|
|
|
|
"Ignoring PreDisconnect for {origin} because they are no longer on the client list",
|
|
|
|
|
E.Origin.ToString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-11-07 21:30:11 -05:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (client.State != ClientState.Unknown)
|
|
|
|
|
{
|
|
|
|
|
await OnClientDisconnected(client);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-01-06 12:04:36 -05:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogWarning(
|
|
|
|
|
"Expected disconnecting client {client} to be in state {state}, but is in state {clientState}",
|
|
|
|
|
client.ToString(), ClientState.Connected.ToString(), client.State);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-11-07 21:30:11 -05:00
|
|
|
|
}
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Update)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogDebug("Begin Update for {origin}", E.Origin.ToString());
|
|
|
|
|
await OnClientUpdate(E.Origin);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Say)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (E.Data?.Length > 0)
|
2018-05-14 13:55:10 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
string message = E.Data;
|
|
|
|
|
if (E.Data.IsQuickMessage())
|
2018-05-14 13:55:10 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-07-09 17:50:33 -04:00
|
|
|
|
message = _serviceProvider.GetRequiredService<DefaultSettings>()
|
2020-11-11 18:31:26 -05:00
|
|
|
|
.QuickMessages
|
|
|
|
|
.First(_qm => _qm.Game == GameName)
|
|
|
|
|
.Messages[E.Data.Substring(1)];
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
message = E.Data.Substring(1);
|
|
|
|
|
}
|
2019-04-25 14:00:54 -04:00
|
|
|
|
}
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
2022-04-19 19:43:58 -04:00
|
|
|
|
ChatHistory.Add(new ChatInfo
|
2020-08-20 11:38:11 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
Name = E.Origin.Name,
|
|
|
|
|
Message = message,
|
|
|
|
|
Time = DateTime.UtcNow,
|
|
|
|
|
IsHidden = !string.IsNullOrEmpty(GamePassword)
|
|
|
|
|
});
|
2018-05-14 13:55:10 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.MapChange)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogInformation("New map loaded - {clientCount} active players", ClientNum);
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// iw4 doesn't log the game info
|
|
|
|
|
if (E.Extra == null)
|
2018-05-03 01:25:49 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
var dict = await this.GetInfoAsync(new TimeSpan(0, 0, 20));
|
|
|
|
|
|
|
|
|
|
if (dict == null)
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogWarning("Map change event response doesn't have any data");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Gametype = dict["gametype"];
|
|
|
|
|
Hostname = dict["hostname"];
|
|
|
|
|
|
|
|
|
|
string mapname = dict["mapname"] ?? CurrentMap.Name;
|
|
|
|
|
UpdateMap(mapname);
|
|
|
|
|
}
|
2018-05-03 01:25:49 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
var dict = (Dictionary<string, string>) E.Extra;
|
|
|
|
|
Gametype = dict["g_gametype"];
|
|
|
|
|
Hostname = dict["sv_hostname"];
|
|
|
|
|
MaxClients = int.Parse(dict["sv_maxclients"]);
|
2018-05-03 01:25:49 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
string mapname = dict["mapname"];
|
2019-11-18 15:02:35 -05:00
|
|
|
|
UpdateMap(mapname);
|
2018-05-03 01:25:49 -04:00
|
|
|
|
}
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
|
|
|
|
if (E.GameTime.HasValue)
|
|
|
|
|
{
|
|
|
|
|
lastGameTime = E.GameTime.Value;
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.MapEnd)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogInformation("Game ending...");
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (E.GameTime.HasValue)
|
|
|
|
|
{
|
|
|
|
|
lastGameTime = E.GameTime.Value;
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
2020-02-06 19:35:30 -05:00
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Tell)
|
2020-02-06 19:35:30 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
await Tell(E.Message, E.Target);
|
2020-02-06 19:35:30 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
else if (E.Type == GameEvent.EventType.Broadcast)
|
2020-02-06 19:35:30 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (!Utilities.IsDevelopment && E.Data != null) // hides broadcast when in development mode
|
|
|
|
|
{
|
|
|
|
|
await E.Owner.ExecuteCommandAsync(E.Data);
|
|
|
|
|
}
|
2020-02-06 19:35:30 -05:00
|
|
|
|
}
|
2022-03-12 14:38:33 -05:00
|
|
|
|
|
|
|
|
|
else if (E.Type == GameEvent.EventType.JoinTeam)
|
|
|
|
|
{
|
|
|
|
|
E.Origin.UpdateTeam(E.Extra as string);
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
lock (ChatHistory)
|
2018-06-30 21:55:16 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
while (ChatHistory.Count > Math.Ceiling(ClientNum / 2.0))
|
|
|
|
|
{
|
|
|
|
|
ChatHistory.RemoveAt(0);
|
|
|
|
|
}
|
2018-06-30 21:55:16 -04:00
|
|
|
|
}
|
2018-05-04 00:22:10 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
// the last client hasn't fully disconnected yet
|
|
|
|
|
// so there will still be at least 1 client left
|
|
|
|
|
if (ClientNum < 2)
|
2018-12-01 13:17:53 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ChatHistory.Clear();
|
2018-12-01 13:17:53 -05:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
return true;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-06 22:48:49 -04:00
|
|
|
|
private async Task OnClientUpdate(EFClient origin)
|
2018-11-07 21:30:11 -05:00
|
|
|
|
{
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var client = Manager.GetActiveClients().FirstOrDefault(c => c.NetworkId == origin.NetworkId);
|
2019-12-27 13:10:20 -05:00
|
|
|
|
|
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
2021-10-31 12:57:32 -04:00
|
|
|
|
ServerLogger.LogWarning("{Origin} expected to exist in client list for update, but they do not", origin.ToString());
|
2019-12-28 16:40:55 -05:00
|
|
|
|
return;
|
2019-12-27 13:10:20 -05:00
|
|
|
|
}
|
2018-11-07 21:30:11 -05:00
|
|
|
|
|
2019-11-25 13:05:12 -05:00
|
|
|
|
client.Ping = origin.Ping;
|
|
|
|
|
client.Score = origin.Score;
|
2018-11-07 21:30:11 -05:00
|
|
|
|
|
2019-11-25 13:05:12 -05:00
|
|
|
|
// update their IP if it hasn't been set yet
|
|
|
|
|
if (client.IPAddress == null &&
|
|
|
|
|
!client.IsBot &&
|
|
|
|
|
client.State == ClientState.Connected)
|
|
|
|
|
{
|
|
|
|
|
try
|
2018-11-07 21:30:11 -05:00
|
|
|
|
{
|
2021-08-16 14:20:54 -04:00
|
|
|
|
await client.OnJoin(origin.IPAddress, Manager.GetApplicationSettings().Configuration().EnableImplicitAccountLinking);
|
2019-11-25 13:05:12 -05:00
|
|
|
|
}
|
2019-04-08 13:29:48 -04:00
|
|
|
|
|
2019-11-25 13:05:12 -05:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using(LogContext.PushProperty("Server", ToString()))
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogError(e, "Could not execute on join for {origin}", origin.ToString());
|
|
|
|
|
}
|
2018-11-07 21:30:11 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-06 19:35:30 -05:00
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
else if (client.IPAddress != null && client.State == ClientState.Disconnecting ||
|
2020-05-16 21:55:18 -04:00
|
|
|
|
client.Level == Permission.Banned)
|
2020-02-06 19:35:30 -05:00
|
|
|
|
{
|
2021-10-31 12:57:32 -04:00
|
|
|
|
ServerLogger.LogWarning("{Client} state is Unknown (probably kicked), but they are still connected. trying to kick again...", origin.ToString());
|
2021-08-16 14:20:54 -04:00
|
|
|
|
await client.CanConnect(client.IPAddress, Manager.GetApplicationSettings().Configuration().EnableImplicitAccountLinking);
|
2020-02-06 19:35:30 -05:00
|
|
|
|
}
|
2018-11-07 21:30:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-27 18:07:54 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// lists the connecting and disconnecting clients via RCon response
|
2019-06-27 21:06:30 -04:00
|
|
|
|
/// array index 0 = connecting clients
|
2018-08-27 18:07:54 -04:00
|
|
|
|
/// array index 1 = disconnecting clients
|
2019-06-27 21:06:30 -04:00
|
|
|
|
/// array index 2 = updated clients
|
2018-08-27 18:07:54 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2022-06-08 10:10:31 -04:00
|
|
|
|
async Task<List<EFClient>[]> PollPlayersAsync(CancellationToken token)
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2018-11-05 22:01:29 -05:00
|
|
|
|
var currentClients = GetClientsAsList();
|
2022-06-08 10:10:31 -04:00
|
|
|
|
var statusResponse = await this.GetStatusAsync(token);
|
2022-06-01 12:25:11 -04:00
|
|
|
|
|
|
|
|
|
if (statusResponse is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 11:51:03 -04:00
|
|
|
|
var polledClients = statusResponse.Clients.AsEnumerable();
|
2019-04-02 21:20:37 -04:00
|
|
|
|
|
2018-12-30 19:13:13 -05:00
|
|
|
|
if (Manager.GetApplicationSettings().Configuration().IgnoreBots)
|
2018-10-13 19:49:08 -04:00
|
|
|
|
{
|
|
|
|
|
polledClients = polledClients.Where(c => !c.IsBot);
|
|
|
|
|
}
|
2018-08-27 18:07:54 -04:00
|
|
|
|
var disconnectingClients = currentClients.Except(polledClients);
|
2018-11-07 21:30:11 -05:00
|
|
|
|
var connectingClients = polledClients.Except(currentClients);
|
2018-11-25 21:00:36 -05:00
|
|
|
|
var updatedClients = polledClients.Except(connectingClients).Except(disconnectingClients);
|
2018-06-30 21:55:16 -04:00
|
|
|
|
|
2021-06-03 11:51:03 -04:00
|
|
|
|
UpdateMap(statusResponse.Map);
|
|
|
|
|
UpdateGametype(statusResponse.GameType);
|
|
|
|
|
UpdateHostname(statusResponse.Hostname);
|
|
|
|
|
UpdateMaxPlayers(statusResponse.MaxClients);
|
2019-11-18 15:02:35 -05:00
|
|
|
|
|
2021-06-03 11:51:03 -04:00
|
|
|
|
return new []
|
2018-11-07 21:30:11 -05:00
|
|
|
|
{
|
|
|
|
|
connectingClients.ToList(),
|
|
|
|
|
disconnectingClients.ToList(),
|
|
|
|
|
updatedClients.ToList()
|
|
|
|
|
};
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2021-08-26 18:35:05 -04:00
|
|
|
|
public override async Task<long> GetIdForServer(Server server = null)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2021-08-26 18:35:05 -04:00
|
|
|
|
server ??= this;
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
if ($"{server.IP}:{server.Port.ToString()}" == "66.150.121.184:28965")
|
|
|
|
|
{
|
|
|
|
|
return 886229536;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo: this is not stable and will need to be migrated again...
|
|
|
|
|
long id = HashCode.Combine(server.IP, server.Port);
|
|
|
|
|
id = id < 0 ? Math.Abs(id) : id;
|
|
|
|
|
|
|
|
|
|
var serverId = (await _serverCache
|
|
|
|
|
.FirstAsync(_server => _server.ServerId == server.EndPoint ||
|
|
|
|
|
_server.EndPoint == server.ToString() ||
|
|
|
|
|
_server.ServerId == id))?.ServerId;
|
|
|
|
|
|
|
|
|
|
return !serverId.HasValue ? id : serverId.Value;
|
|
|
|
|
}
|
2015-03-08 17:20:10 -04:00
|
|
|
|
|
2019-11-18 15:02:35 -05:00
|
|
|
|
private void UpdateMap(string mapname)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(mapname))
|
|
|
|
|
{
|
|
|
|
|
CurrentMap = Maps.Find(m => m.Name == mapname) ?? new Map()
|
|
|
|
|
{
|
|
|
|
|
Alias = mapname,
|
|
|
|
|
Name = mapname
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 10:30:02 -04:00
|
|
|
|
private void UpdateGametype(string gameType)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(gameType))
|
|
|
|
|
{
|
|
|
|
|
Gametype = gameType;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 11:51:03 -04:00
|
|
|
|
private void UpdateHostname(string hostname)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(hostname) || Hostname == hostname)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using(LogContext.PushProperty("Server", ToString()))
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogDebug("Updating hostname to {HostName}", hostname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Hostname = hostname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateMaxPlayers(int? maxPlayers)
|
|
|
|
|
{
|
|
|
|
|
if (maxPlayers == null || maxPlayers == MaxClients)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using(LogContext.PushProperty("Server", ToString()))
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogDebug("Updating max clients to {MaxPlayers}", maxPlayers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MaxClients = maxPlayers.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-24 11:02:53 -04:00
|
|
|
|
private async Task ShutdownInternal()
|
|
|
|
|
{
|
|
|
|
|
foreach (var client in GetClientsAsList())
|
|
|
|
|
{
|
|
|
|
|
await client.OnDisconnect();
|
|
|
|
|
|
|
|
|
|
var e = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.Disconnect,
|
|
|
|
|
Owner = this,
|
|
|
|
|
Origin = client
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
Manager.AddEvent(e);
|
2019-08-24 11:02:53 -04:00
|
|
|
|
|
|
|
|
|
await e.WaitAsync(Utilities.DefaultCommandTimeout, new CancellationTokenRegistration().Token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
private DateTime _lastMessageSent = DateTime.Now;
|
|
|
|
|
private DateTime _lastPlayerCount = DateTime.Now;
|
2015-03-08 17:20:10 -04:00
|
|
|
|
|
2022-06-08 10:10:31 -04:00
|
|
|
|
public override async Task<bool> ProcessUpdatesAsync(CancellationToken token)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2017-05-30 17:23:31 -04:00
|
|
|
|
try
|
2019-10-23 11:40:24 -04:00
|
|
|
|
{
|
2022-06-08 10:10:31 -04:00
|
|
|
|
if (token.IsCancellationRequested)
|
2018-04-29 16:44:04 -04:00
|
|
|
|
{
|
2019-08-24 11:02:53 -04:00
|
|
|
|
await ShutdownInternal();
|
2018-11-07 21:30:11 -05:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-06-12 08:28:08 -04:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-08 17:41:44 -04:00
|
|
|
|
if (Manager.GetApplicationSettings().Configuration().RConPollRate == int.MaxValue &&
|
|
|
|
|
Utilities.IsDevelopment)
|
2019-06-24 12:01:34 -04:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 10:10:31 -04:00
|
|
|
|
var polledClients = await PollPlayersAsync(token);
|
2018-08-27 18:07:54 -04:00
|
|
|
|
|
2022-06-01 12:25:11 -04:00
|
|
|
|
if (polledClients is null)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
foreach (var disconnectingClient in polledClients[1]
|
|
|
|
|
.Where(client => !client.IsZombieClient /* ignores "fake" zombie clients */))
|
2018-08-27 18:07:54 -04:00
|
|
|
|
{
|
2020-04-26 13:32:41 -04:00
|
|
|
|
disconnectingClient.CurrentServer = this;
|
2022-06-08 10:10:31 -04:00
|
|
|
|
var e = new GameEvent
|
2018-08-27 18:07:54 -04:00
|
|
|
|
{
|
2018-11-07 21:30:11 -05:00
|
|
|
|
Type = GameEvent.EventType.PreDisconnect,
|
2018-08-27 18:07:54 -04:00
|
|
|
|
Origin = disconnectingClient,
|
2020-05-04 17:50:02 -04:00
|
|
|
|
Owner = this,
|
|
|
|
|
Source = GameEvent.EventSource.Status
|
2018-08-27 18:07:54 -04:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
Manager.AddEvent(e);
|
2019-11-15 15:50:20 -05:00
|
|
|
|
await e.WaitAsync(Utilities.DefaultCommandTimeout, Manager.CancellationToken);
|
2018-08-27 18:07:54 -04:00
|
|
|
|
}
|
2019-09-26 17:08:49 -04:00
|
|
|
|
|
2018-08-27 18:07:54 -04:00
|
|
|
|
// this are our new connecting clients
|
2022-04-08 17:41:44 -04:00
|
|
|
|
foreach (var client in polledClients[0].Where(client =>
|
|
|
|
|
!string.IsNullOrEmpty(client.Name) && (client.Ping != 999 || client.IsBot)))
|
2018-08-27 18:07:54 -04:00
|
|
|
|
{
|
2020-04-26 13:32:41 -04:00
|
|
|
|
client.CurrentServer = this;
|
2022-04-22 17:03:34 -04:00
|
|
|
|
client.GameName = (Reference.Game?)GameName;
|
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
var e = new GameEvent
|
2018-09-02 17:59:27 -04:00
|
|
|
|
{
|
2018-11-07 21:30:11 -05:00
|
|
|
|
Type = GameEvent.EventType.PreConnect,
|
|
|
|
|
Origin = client,
|
2019-10-18 14:39:21 -04:00
|
|
|
|
Owner = this,
|
2020-05-04 17:50:02 -04:00
|
|
|
|
IsBlocking = true,
|
2020-09-21 16:30:42 -04:00
|
|
|
|
Extra = client.GetAdditionalProperty<string>("BotGuid"),
|
2022-04-22 17:03:34 -04:00
|
|
|
|
Source = GameEvent.EventSource.Status,
|
2018-11-07 21:30:11 -05:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
Manager.AddEvent(e);
|
2019-11-15 15:50:20 -05:00
|
|
|
|
await e.WaitAsync(Utilities.DefaultCommandTimeout, Manager.CancellationToken);
|
2019-09-26 17:08:49 -04:00
|
|
|
|
}
|
2018-09-02 17:59:27 -04:00
|
|
|
|
|
2018-11-07 21:30:11 -05:00
|
|
|
|
// these are the clients that have updated
|
|
|
|
|
foreach (var client in polledClients[2])
|
|
|
|
|
{
|
2020-04-26 13:32:41 -04:00
|
|
|
|
client.CurrentServer = this;
|
2022-04-08 17:41:44 -04:00
|
|
|
|
var gameEvent = new GameEvent
|
2018-08-27 18:07:54 -04:00
|
|
|
|
{
|
2018-11-07 21:30:11 -05:00
|
|
|
|
Type = GameEvent.EventType.Update,
|
2018-08-27 18:07:54 -04:00
|
|
|
|
Origin = client,
|
|
|
|
|
Owner = this
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
Manager.AddEvent(gameEvent);
|
2019-09-26 17:08:49 -04:00
|
|
|
|
}
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (Throttled)
|
2018-04-29 16:44:04 -04:00
|
|
|
|
{
|
2022-04-08 17:41:44 -04:00
|
|
|
|
var gameEvent = new GameEvent
|
2019-03-24 22:34:20 -04:00
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.ConnectionRestored,
|
|
|
|
|
Owner = this,
|
|
|
|
|
Origin = Utilities.IW4MAdminClient(this),
|
|
|
|
|
Target = Utilities.IW4MAdminClient(this)
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
Manager.AddEvent(gameEvent);
|
2017-08-09 00:35:23 -04:00
|
|
|
|
}
|
2018-11-07 21:30:11 -05:00
|
|
|
|
|
2018-04-29 16:44:04 -04:00
|
|
|
|
LastPoll = DateTime.Now;
|
2017-06-12 08:28:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
catch (NetworkException ex)
|
2017-06-12 08:28:08 -04:00
|
|
|
|
{
|
2022-04-08 17:41:44 -04:00
|
|
|
|
if (Throttled)
|
2017-08-09 00:35:23 -04:00
|
|
|
|
{
|
2022-04-08 17:41:44 -04:00
|
|
|
|
return true;
|
2017-08-09 00:35:23 -04:00
|
|
|
|
}
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
var gameEvent = new GameEvent
|
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.ConnectionLost,
|
|
|
|
|
Owner = this,
|
|
|
|
|
Origin = Utilities.IW4MAdminClient(this),
|
|
|
|
|
Target = Utilities.IW4MAdminClient(this),
|
|
|
|
|
Extra = ex,
|
|
|
|
|
Data = ConnectionErrors.ToString()
|
|
|
|
|
};
|
2022-03-29 18:18:41 -04:00
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
Manager.AddEvent(gameEvent);
|
2018-02-07 00:19:06 -05:00
|
|
|
|
return true;
|
2017-06-12 08:28:08 -04:00
|
|
|
|
}
|
2022-04-08 17:41:44 -04:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
RunServerCollection();
|
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
if (DateTime.Now - _lastMessageSent <=
|
|
|
|
|
TimeSpan.FromSeconds(Manager.GetApplicationSettings().Configuration().AutoMessagePeriod) ||
|
|
|
|
|
BroadcastMessages.Count <= 0 || ClientNum <= 0)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2022-04-08 17:41:44 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-05-05 16:36:26 -04:00
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
// send out broadcast messages
|
|
|
|
|
var messages =
|
|
|
|
|
(await this.ProcessMessageToken(Manager.GetMessageTokens(), BroadcastMessages[NextMessage])).Split(
|
|
|
|
|
Environment.NewLine);
|
|
|
|
|
await BroadcastAsync(messages, token: Manager.CancellationToken);
|
2018-05-05 16:36:26 -04:00
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
NextMessage = NextMessage == BroadcastMessages.Count - 1 ? 0 : NextMessage + 1;
|
|
|
|
|
_lastMessageSent = DateTime.Now;
|
2017-05-30 17:23:31 -04:00
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
return true;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2017-05-30 17:23:31 -04:00
|
|
|
|
|
2019-08-24 11:02:53 -04:00
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
|
{
|
|
|
|
|
await ShutdownInternal();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-28 01:22:18 -04:00
|
|
|
|
// this one is ok
|
2022-04-08 17:41:44 -04:00
|
|
|
|
catch (Exception e) when (e is ServerException || e is RConException)
|
2018-04-15 21:27:43 -04:00
|
|
|
|
{
|
2022-04-08 17:41:44 -04:00
|
|
|
|
using (LogContext.PushProperty("Server", ToString()))
|
2020-04-22 21:51:04 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogWarning(e, "Undesirable exception occured during processing updates");
|
2020-04-22 21:51:04 -04:00
|
|
|
|
}
|
2022-04-08 17:41:44 -04:00
|
|
|
|
|
2018-04-15 21:27:43 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
catch (Exception e)
|
2017-05-30 17:23:31 -04:00
|
|
|
|
{
|
2022-04-08 17:41:44 -04:00
|
|
|
|
using (LogContext.PushProperty("Server", ToString()))
|
2020-11-11 18:31:26 -05:00
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogError(e, "Unexpected exception occured during processing updates");
|
|
|
|
|
}
|
2022-04-08 17:41:44 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
Console.WriteLine(loc["SERVER_ERROR_EXCEPTION"].FormatExt($"[{IP}:{Port}]"));
|
2018-02-07 00:19:06 -05:00
|
|
|
|
return false;
|
2015-03-08 17:20:10 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-19 14:14:30 -04:00
|
|
|
|
|
2022-03-29 18:18:41 -04:00
|
|
|
|
private void RunServerCollection()
|
|
|
|
|
{
|
|
|
|
|
var appConfig = _serviceProvider.GetService<ApplicationConfiguration>();
|
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
if (DateTime.Now - _lastPlayerCount < appConfig?.ServerDataCollectionInterval)
|
2022-03-29 18:18:41 -04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var maxItems = Math.Ceiling(appConfig.MaxClientHistoryTime.TotalMinutes /
|
|
|
|
|
appConfig.ServerDataCollectionInterval.TotalMinutes);
|
|
|
|
|
|
|
|
|
|
while (ClientHistory.ClientCounts.Count > maxItems)
|
|
|
|
|
{
|
|
|
|
|
ClientHistory.ClientCounts.RemoveAt(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClientHistory.ClientCounts.Add(new ClientCountSnapshot
|
|
|
|
|
{
|
|
|
|
|
ClientCount = ClientNum,
|
|
|
|
|
ConnectionInterrupted = Throttled,
|
|
|
|
|
Time = DateTime.UtcNow,
|
|
|
|
|
Map = CurrentMap.Name
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-08 17:41:44 -04:00
|
|
|
|
_lastPlayerCount = DateTime.Now;
|
2022-03-29 18:18:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
public async Task Initialize()
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2021-07-11 18:26:30 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-02-01 15:27:16 -05:00
|
|
|
|
ResolvedIpEndPoint =
|
|
|
|
|
new IPEndPoint(
|
|
|
|
|
(await Dns.GetHostAddressesAsync(IP)).First(address =>
|
|
|
|
|
address.AddressFamily == AddressFamily.InterNetwork), Port);
|
2021-07-11 18:26:30 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ServerLogger.LogWarning(ex, "Could not resolve hostname or IP for RCon connection {IP}:{Port}", IP, Port);
|
|
|
|
|
ResolvedIpEndPoint = new IPEndPoint(IPAddress.Parse(IP), Port);
|
|
|
|
|
}
|
2022-02-01 19:27:03 -05:00
|
|
|
|
|
2019-02-02 21:19:24 -05:00
|
|
|
|
RconParser = Manager.AdditionalRConParsers
|
2022-02-01 19:27:03 -05:00
|
|
|
|
.FirstOrDefault(parser =>
|
|
|
|
|
parser.Version == ServerConfig.RConParserVersion ||
|
|
|
|
|
parser.Name == ServerConfig.RConParserVersion);
|
2019-02-01 20:49:25 -05:00
|
|
|
|
|
2019-02-02 21:19:24 -05:00
|
|
|
|
EventParser = Manager.AdditionalEventParsers
|
2022-02-01 19:27:03 -05:00
|
|
|
|
.FirstOrDefault(parser =>
|
|
|
|
|
parser.Version == ServerConfig.EventParserVersion ||
|
|
|
|
|
parser.Name == ServerConfig.RConParserVersion);
|
2019-02-01 20:49:25 -05:00
|
|
|
|
|
2021-06-11 12:52:30 -04:00
|
|
|
|
RconParser ??= Manager.AdditionalRConParsers[0];
|
|
|
|
|
EventParser ??= Manager.AdditionalEventParsers[0];
|
2019-02-02 19:54:30 -05:00
|
|
|
|
|
2021-07-11 18:26:30 -04:00
|
|
|
|
RemoteConnection = RConConnectionFactory.CreateConnection(ResolvedIpEndPoint, Password, RconParser.RConEngine);
|
2020-11-11 19:53:23 -05:00
|
|
|
|
RemoteConnection.SetConfiguration(RconParser);
|
2018-06-05 17:31:36 -04:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
var version = await this.GetMappedDvarValueOrDefaultAsync<string>("version", token: Manager.CancellationToken);
|
2018-12-16 22:16:56 -05:00
|
|
|
|
Version = version.Value;
|
2021-04-16 14:35:51 -04:00
|
|
|
|
GameName = Utilities.GetGame(version.Value ?? RconParser.Version);
|
2019-02-05 19:02:45 -05:00
|
|
|
|
|
|
|
|
|
if (GameName == Game.UKN)
|
|
|
|
|
{
|
|
|
|
|
GameName = RconParser.GameName;
|
|
|
|
|
}
|
2017-08-08 22:44:52 -04:00
|
|
|
|
|
2022-02-01 19:27:03 -05:00
|
|
|
|
if (version.Value?.Length != 0)
|
2019-02-02 21:19:24 -05:00
|
|
|
|
{
|
2019-06-24 12:01:34 -04:00
|
|
|
|
var matchedRconParser = Manager.AdditionalRConParsers.FirstOrDefault(_parser => _parser.Version == version.Value);
|
|
|
|
|
RconParser.Configuration = matchedRconParser != null ? matchedRconParser.Configuration : RconParser.Configuration;
|
2019-02-03 21:47:05 -05:00
|
|
|
|
EventParser = Manager.AdditionalEventParsers.FirstOrDefault(_parser => _parser.Version == version.Value) ?? EventParser;
|
2019-02-12 21:34:29 -05:00
|
|
|
|
Version = RconParser.Version;
|
2019-02-02 21:19:24 -05:00
|
|
|
|
}
|
2018-04-26 20:19:42 -04:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
var svRunning = await this.GetMappedDvarValueOrDefaultAsync<string>("sv_running", token: Manager.CancellationToken);
|
2020-01-13 17:51:16 -05:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
if (!string.IsNullOrEmpty(svRunning.Value) && svRunning.Value != "1")
|
2020-01-13 17:51:16 -05:00
|
|
|
|
{
|
2020-11-07 11:40:58 -05:00
|
|
|
|
throw new ServerException(loc["SERVER_ERROR_NOT_RUNNING"].FormatExt(this.ToString()));
|
2020-01-13 17:51:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 20:49:25 -05:00
|
|
|
|
var infoResponse = RconParser.Configuration.CommandPrefixes.RConGetInfo != null ? await this.GetInfoAsync() : null;
|
2020-06-16 18:16:12 -04:00
|
|
|
|
|
2022-02-28 21:44:30 -05:00
|
|
|
|
var hostname = (await this.GetMappedDvarValueOrDefaultAsync<string>("sv_hostname", "hostname", infoResponse, token: Manager.CancellationToken)).Value;
|
|
|
|
|
var mapname = (await this.GetMappedDvarValueOrDefaultAsync<string>("mapname", infoResponse: infoResponse, token: Manager.CancellationToken)).Value;
|
|
|
|
|
var maxplayers = (await this.GetMappedDvarValueOrDefaultAsync<int>("sv_maxclients", infoResponse: infoResponse, token: Manager.CancellationToken)).Value;
|
|
|
|
|
var gametype = (await this.GetMappedDvarValueOrDefaultAsync<string>("g_gametype", "gametype", infoResponse, token: Manager.CancellationToken)).Value;
|
|
|
|
|
var basepath = await this.GetMappedDvarValueOrDefaultAsync<string>("fs_basepath", token: Manager.CancellationToken);
|
|
|
|
|
var basegame = await this.GetMappedDvarValueOrDefaultAsync<string>("fs_basegame", token: Manager.CancellationToken);
|
|
|
|
|
var homepath = await this.GetMappedDvarValueOrDefaultAsync<string>("fs_homepath", token: Manager.CancellationToken);
|
|
|
|
|
var game = await this.GetMappedDvarValueOrDefaultAsync<string>("fs_game", infoResponse: infoResponse, token: Manager.CancellationToken);
|
|
|
|
|
var logfile = await this.GetMappedDvarValueOrDefaultAsync<string>("g_log", token: Manager.CancellationToken);
|
|
|
|
|
var logsync = await this.GetMappedDvarValueOrDefaultAsync<int>("g_logsync", token: Manager.CancellationToken);
|
|
|
|
|
var ip = await this.GetMappedDvarValueOrDefaultAsync<string>("net_ip", token: Manager.CancellationToken);
|
|
|
|
|
var gamePassword = await this.GetMappedDvarValueOrDefaultAsync("g_password", overrideDefault: "", token: Manager.CancellationToken);
|
2017-08-08 22:44:52 -04:00
|
|
|
|
|
2020-04-20 11:45:58 -04:00
|
|
|
|
if (Manager.GetApplicationSettings().Configuration().EnableCustomSayName)
|
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
await this.SetDvarAsync("sv_sayname", Manager.GetApplicationSettings().Configuration().CustomSayName,
|
|
|
|
|
Manager.CancellationToken);
|
2020-04-20 11:45:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-22 02:04:30 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
var website = await this.GetMappedDvarValueOrDefaultAsync<string>("_website", token: Manager.CancellationToken);
|
2020-04-13 19:15:46 -04:00
|
|
|
|
|
2020-04-01 15:11:56 -04:00
|
|
|
|
// this occurs for games that don't give us anything back when
|
|
|
|
|
// the dvar is not set
|
|
|
|
|
if (string.IsNullOrWhiteSpace(website.Value))
|
|
|
|
|
{
|
|
|
|
|
throw new DvarException("value is empty");
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
Website = website.Value;
|
|
|
|
|
}
|
2015-10-10 22:32:12 -04:00
|
|
|
|
|
2018-04-15 00:26:27 -04:00
|
|
|
|
catch (DvarException)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2018-04-22 16:04:18 -04:00
|
|
|
|
Website = loc["SERVER_WEBSITE_GENERIC"];
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2020-11-17 19:24:54 -05:00
|
|
|
|
|
|
|
|
|
// todo: remove this once _website is weaned off
|
|
|
|
|
if (string.IsNullOrEmpty(Manager.GetApplicationSettings().Configuration().ContactUri))
|
|
|
|
|
{
|
|
|
|
|
Manager.GetApplicationSettings().Configuration().ContactUri = Website;
|
|
|
|
|
}
|
2021-07-09 17:50:33 -04:00
|
|
|
|
|
|
|
|
|
var defaultConfig = _serviceProvider.GetRequiredService<DefaultSettings>();
|
|
|
|
|
var gameMaps = defaultConfig?.Maps?.FirstOrDefault(map => map.Game == GameName);
|
2015-08-23 17:58:48 -04:00
|
|
|
|
|
2021-07-09 17:50:33 -04:00
|
|
|
|
if (gameMaps != null)
|
|
|
|
|
{
|
|
|
|
|
Maps.AddRange(gameMaps.Maps);
|
|
|
|
|
}
|
2018-03-14 14:22:04 -04:00
|
|
|
|
|
2020-04-13 17:16:31 -04:00
|
|
|
|
WorkingDirectory = basepath.Value;
|
2019-08-01 10:37:33 -04:00
|
|
|
|
this.Hostname = hostname;
|
2018-04-26 20:19:42 -04:00
|
|
|
|
this.MaxClients = maxplayers;
|
2020-06-16 18:16:12 -04:00
|
|
|
|
this.FSGame = game.Value;
|
2018-04-26 20:19:42 -04:00
|
|
|
|
this.Gametype = gametype;
|
2019-02-02 19:54:30 -05:00
|
|
|
|
this.IP = ip.Value == "localhost" ? ServerConfig.IPAddress : ip.Value ?? ServerConfig.IPAddress;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
this.GamePassword = gamePassword.Value;
|
2019-11-18 15:02:35 -05:00
|
|
|
|
UpdateMap(mapname);
|
2018-11-25 21:00:36 -05:00
|
|
|
|
|
2019-06-27 21:06:30 -04:00
|
|
|
|
if (RconParser.CanGenerateLogPath)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2019-06-27 21:06:30 -04:00
|
|
|
|
bool needsRestart = false;
|
|
|
|
|
|
|
|
|
|
if (logsync.Value == 0)
|
|
|
|
|
{
|
2022-02-28 21:44:30 -05:00
|
|
|
|
await this.SetDvarAsync("g_logsync", 2, Manager.CancellationToken); // set to 2 for continous in other games, clamps to 1 for IW4
|
2019-06-27 21:06:30 -04:00
|
|
|
|
needsRestart = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(logfile.Value))
|
|
|
|
|
{
|
|
|
|
|
logfile.Value = "games_mp.log";
|
2022-02-28 21:44:30 -05:00
|
|
|
|
await this.SetDvarAsync("g_log", logfile.Value, Manager.CancellationToken);
|
2019-06-27 21:06:30 -04:00
|
|
|
|
needsRestart = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (needsRestart)
|
2020-11-07 11:40:58 -05:00
|
|
|
|
{
|
|
|
|
|
// disabling this for the time being
|
|
|
|
|
/*Logger.WriteWarning("Game log file not properly initialized, restarting map...");
|
|
|
|
|
await this.ExecuteCommandAsync("map_restart");*/
|
2019-06-27 21:06:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
// this DVAR isn't set until the a map is loaded
|
2022-02-28 21:44:30 -05:00
|
|
|
|
await this.SetDvarAsync("logfile", 2, Manager.CancellationToken);
|
2018-09-29 15:52:22 -04:00
|
|
|
|
}
|
2017-11-02 12:49:45 -04:00
|
|
|
|
|
2018-09-08 18:29:30 -04:00
|
|
|
|
CustomCallback = await ScriptLoaded();
|
2019-02-17 19:48:40 -05:00
|
|
|
|
|
2019-02-09 16:35:13 -05:00
|
|
|
|
// they've manually specified the log path
|
2020-04-17 16:05:16 -04:00
|
|
|
|
if (!string.IsNullOrEmpty(ServerConfig.ManualLogPath) || !RconParser.CanGenerateLogPath)
|
2018-04-23 01:43:48 -04:00
|
|
|
|
{
|
2019-02-09 16:35:13 -05:00
|
|
|
|
LogPath = ServerConfig.ManualLogPath;
|
2020-04-17 16:05:16 -04:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(LogPath) && !RconParser.CanGenerateLogPath)
|
|
|
|
|
{
|
|
|
|
|
throw new ServerException(loc["SERVER_ERROR_REQUIRES_PATH"].FormatExt(GameName.ToString()));
|
|
|
|
|
}
|
2018-04-23 01:43:48 -04:00
|
|
|
|
}
|
2019-02-09 16:35:13 -05:00
|
|
|
|
|
2018-04-23 01:43:48 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-14 16:46:14 -04:00
|
|
|
|
var logInfo = new LogPathGeneratorInfo()
|
|
|
|
|
{
|
|
|
|
|
BaseGameDirectory = basegame.Value,
|
|
|
|
|
BasePathDirectory = basepath.Value,
|
2021-08-27 21:47:06 -04:00
|
|
|
|
HomePathDirectory = homepath.Value,
|
2020-04-14 16:46:14 -04:00
|
|
|
|
GameDirectory = EventParser.Configuration.GameDirectory ?? "",
|
2020-06-16 18:16:12 -04:00
|
|
|
|
ModDirectory = game.Value ?? "",
|
2020-04-14 16:46:14 -04:00
|
|
|
|
LogFile = logfile.Value,
|
2021-06-07 17:58:36 -04:00
|
|
|
|
IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows),
|
|
|
|
|
IsOneLog = RconParser.IsOneLog
|
2020-04-14 16:46:14 -04:00
|
|
|
|
};
|
|
|
|
|
LogPath = GenerateLogPath(logInfo);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogInformation("Game log information {@logInfo}", logInfo);
|
|
|
|
|
|
2019-02-09 22:24:54 -05:00
|
|
|
|
if (!File.Exists(LogPath) && ServerConfig.GameLogServerUrl == null)
|
2018-12-16 22:16:56 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
Console.WriteLine(loc["SERVER_ERROR_DNE"].FormatExt(LogPath));
|
|
|
|
|
ServerLogger.LogCritical("Game log path does not exist {logPath}", LogPath);
|
2019-04-09 16:02:49 -04:00
|
|
|
|
throw new ServerException(loc["SERVER_ERROR_DNE"].FormatExt(LogPath));
|
2018-12-16 22:16:56 -05:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
2018-04-28 21:11:13 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
ServerLogger.LogInformation("Generated game log path is {logPath}", LogPath);
|
|
|
|
|
LogEvent = new GameLogEventDetection( _serviceProvider.GetRequiredService<ILogger<GameLogEventDetection>>(),
|
|
|
|
|
this,
|
|
|
|
|
GenerateUriForLog(LogPath, ServerConfig.GameLogServerUrl?.AbsoluteUri), gameLogReaderFactory);
|
2018-08-30 21:53:00 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
await _serverCache.InitializeAsync();
|
2018-09-04 13:40:29 -04:00
|
|
|
|
_ = Task.Run(() => LogEvent.PollForChanges());
|
2020-08-17 22:21:11 -04:00
|
|
|
|
|
|
|
|
|
if (!Utilities.IsDevelopment)
|
|
|
|
|
{
|
|
|
|
|
Broadcast(loc["BROADCAST_ONLINE"]);
|
|
|
|
|
}
|
2015-03-08 17:20:10 -04:00
|
|
|
|
}
|
2015-07-06 13:13:42 -04:00
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
public Uri[] GenerateUriForLog(string logPath, string gameLogServerUrl)
|
|
|
|
|
{
|
2021-06-03 11:51:03 -04:00
|
|
|
|
var logUri = new Uri(logPath, UriKind.Absolute);
|
2020-05-04 17:50:02 -04:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(gameLogServerUrl))
|
|
|
|
|
{
|
|
|
|
|
return new[] { logUri };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new[] { new Uri(gameLogServerUrl), logUri };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-14 16:46:14 -04:00
|
|
|
|
public static string GenerateLogPath(LogPathGeneratorInfo logInfo)
|
2020-04-13 17:16:31 -04:00
|
|
|
|
{
|
|
|
|
|
string logPath;
|
2021-06-07 17:58:36 -04:00
|
|
|
|
var workingDirectory = logInfo.BasePathDirectory;
|
2021-08-27 21:47:06 -04:00
|
|
|
|
|
|
|
|
|
bool IsValidGamePath (string path)
|
|
|
|
|
{
|
|
|
|
|
var baseGameIsDirectory = !string.IsNullOrWhiteSpace(path) &&
|
|
|
|
|
path.IndexOfAny(Utilities.DirectorySeparatorChars) != -1;
|
2020-04-13 17:16:31 -04:00
|
|
|
|
|
2021-08-27 21:47:06 -04:00
|
|
|
|
var baseGameIsRelative = path.FixDirectoryCharacters()
|
|
|
|
|
.Equals(logInfo.GameDirectory.FixDirectoryCharacters(), StringComparison.InvariantCultureIgnoreCase);
|
2020-04-13 19:15:46 -04:00
|
|
|
|
|
2021-08-27 21:47:06 -04:00
|
|
|
|
return baseGameIsDirectory && !baseGameIsRelative;
|
|
|
|
|
}
|
2020-04-13 19:15:46 -04:00
|
|
|
|
|
2020-04-13 17:16:31 -04:00
|
|
|
|
// we want to see if base game is provided and it 'looks' like a directory
|
2021-08-27 21:47:06 -04:00
|
|
|
|
if (IsValidGamePath(logInfo.HomePathDirectory))
|
|
|
|
|
{
|
|
|
|
|
workingDirectory = logInfo.HomePathDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (IsValidGamePath(logInfo.BaseGameDirectory))
|
2020-04-13 17:16:31 -04:00
|
|
|
|
{
|
2020-04-14 16:46:14 -04:00
|
|
|
|
workingDirectory = logInfo.BaseGameDirectory;
|
2020-04-13 17:16:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 17:58:36 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(logInfo.ModDirectory) || logInfo.IsOneLog)
|
2020-04-13 17:16:31 -04:00
|
|
|
|
{
|
2020-04-14 16:46:14 -04:00
|
|
|
|
logPath = Path.Combine(workingDirectory, logInfo.GameDirectory, logInfo.LogFile);
|
2020-04-13 17:16:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-14 16:46:14 -04:00
|
|
|
|
logPath = Path.Combine(workingDirectory, logInfo.ModDirectory, logInfo.LogFile);
|
2020-04-13 17:16:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fix wine drive name mangling
|
2020-04-14 16:46:14 -04:00
|
|
|
|
if (!logInfo.IsWindows)
|
2020-04-13 17:16:31 -04:00
|
|
|
|
{
|
2020-04-14 16:46:14 -04:00
|
|
|
|
logPath = $"{Path.DirectorySeparatorChar}{Regex.Replace(logPath, @"[A-Z]:(\/|\\)", "")}";
|
2020-04-13 17:16:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return logPath.FixDirectoryCharacters();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 18:34:00 -04:00
|
|
|
|
public override async Task Warn(string reason, EFClient targetClient, EFClient targetOrigin)
|
2016-01-16 17:58:24 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
// ensure player gets warned if command not performed on them in game
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var activeClient = Manager.FindActiveClient(targetClient);
|
2017-11-29 19:35:50 -05:00
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var newPenalty = new EFPenalty
|
2017-11-29 19:35:50 -05:00
|
|
|
|
{
|
2019-05-29 17:55:35 -04:00
|
|
|
|
Type = EFPenalty.PenaltyType.Warning,
|
2017-11-29 19:35:50 -05:00
|
|
|
|
Expires = DateTime.UtcNow,
|
2020-04-21 18:34:00 -04:00
|
|
|
|
Offender = targetClient,
|
|
|
|
|
Punisher = targetOrigin,
|
|
|
|
|
Offense = reason,
|
|
|
|
|
Link = targetClient.AliasLink
|
2017-11-29 19:35:50 -05:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
ServerLogger.LogDebug("Creating warn penalty for {TargetClient}", targetClient.ToString());
|
2020-11-11 18:31:26 -05:00
|
|
|
|
await newPenalty.TryCreatePenalty(Manager.GetPenaltyService(), ServerLogger);
|
2016-01-16 17:58:24 -05:00
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
if (activeClient.IsIngame)
|
2016-01-15 17:15:39 -05:00
|
|
|
|
{
|
2021-10-31 12:57:32 -04:00
|
|
|
|
if (activeClient.Warnings >= 4)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2021-10-31 12:57:32 -04:00
|
|
|
|
activeClient.Kick(loc["SERVER_WARNLIMT_REACHED"], Utilities.IW4MAdminClient(this));
|
2017-11-29 19:35:50 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 18:26:33 -05:00
|
|
|
|
var message = loc["COMMANDS_WARNING_FORMAT_V2"]
|
2021-10-31 12:57:32 -04:00
|
|
|
|
.FormatExt(activeClient.Warnings, activeClient.Name, reason);
|
|
|
|
|
activeClient.CurrentServer.Broadcast(message);
|
2020-04-21 18:34:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-06 12:04:36 -05:00
|
|
|
|
|
2020-11-17 19:24:54 -05:00
|
|
|
|
public override async Task Kick(string reason, EFClient targetClient, EFClient originClient, EFPenalty previousPenalty)
|
2020-04-21 18:34:00 -04:00
|
|
|
|
{
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var activeClient = Manager.FindActiveClient(targetClient);
|
2017-11-29 19:35:50 -05:00
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var newPenalty = new EFPenalty
|
2017-11-29 19:35:50 -05:00
|
|
|
|
{
|
2019-05-29 17:55:35 -04:00
|
|
|
|
Type = EFPenalty.PenaltyType.Kick,
|
2017-11-29 19:35:50 -05:00
|
|
|
|
Expires = DateTime.UtcNow,
|
2020-04-21 18:34:00 -04:00
|
|
|
|
Offender = targetClient,
|
2020-11-17 19:24:54 -05:00
|
|
|
|
Offense = reason,
|
2020-04-21 18:34:00 -04:00
|
|
|
|
Punisher = originClient,
|
|
|
|
|
Link = targetClient.AliasLink
|
2017-11-29 19:35:50 -05:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
ServerLogger.LogDebug("Creating kick penalty for {TargetClient}", targetClient.ToString());
|
2020-11-11 18:31:26 -05:00
|
|
|
|
await newPenalty.TryCreatePenalty(Manager.GetPenaltyService(), ServerLogger);
|
2016-01-15 17:15:39 -05:00
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
if (activeClient.IsIngame)
|
2017-11-29 19:35:50 -05:00
|
|
|
|
{
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var gameEvent = new GameEvent
|
2017-11-29 19:35:50 -05:00
|
|
|
|
{
|
2020-04-21 18:34:00 -04:00
|
|
|
|
Type = GameEvent.EventType.PreDisconnect,
|
2021-10-31 12:57:32 -04:00
|
|
|
|
Origin = activeClient,
|
2020-04-21 18:34:00 -04:00
|
|
|
|
Owner = this
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
Manager.AddEvent(gameEvent);
|
2021-06-03 11:51:03 -04:00
|
|
|
|
|
2020-11-17 19:24:54 -05:00
|
|
|
|
var formattedKick = string.Format(RconParser.Configuration.CommandPrefixes.Kick,
|
2021-10-31 12:57:32 -04:00
|
|
|
|
activeClient.TemporalClientNumber,
|
2020-11-17 19:24:54 -05:00
|
|
|
|
_messageFormatter.BuildFormattedMessage(RconParser.Configuration,
|
|
|
|
|
newPenalty,
|
|
|
|
|
previousPenalty));
|
2021-10-31 12:57:32 -04:00
|
|
|
|
ServerLogger.LogDebug("Executing tempban kick command for {ActiveClient}", activeClient.ToString());
|
|
|
|
|
await activeClient.CurrentServer.ExecuteCommandAsync(formattedKick);
|
2018-04-13 02:32:30 -04:00
|
|
|
|
}
|
2020-04-21 18:34:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
public override async Task TempBan(string reason, TimeSpan length, EFClient targetClient, EFClient originClient)
|
2020-04-21 18:34:00 -04:00
|
|
|
|
{
|
|
|
|
|
// ensure player gets kicked if command not performed on them in the same server
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var activeClient = Manager.FindActiveClient(targetClient);
|
2017-11-29 19:35:50 -05:00
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var newPenalty = new EFPenalty
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2019-05-29 17:55:35 -04:00
|
|
|
|
Type = EFPenalty.PenaltyType.TempBan,
|
2017-11-25 20:29:58 -05:00
|
|
|
|
Expires = DateTime.UtcNow + length,
|
2020-04-21 18:34:00 -04:00
|
|
|
|
Offender = targetClient,
|
2021-10-31 12:57:32 -04:00
|
|
|
|
Offense = reason,
|
2020-04-21 18:34:00 -04:00
|
|
|
|
Punisher = originClient,
|
|
|
|
|
Link = targetClient.AliasLink
|
2017-11-25 20:29:58 -05:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
ServerLogger.LogDebug("Creating tempban penalty for {TargetClient}", targetClient.ToString());
|
2020-11-11 18:31:26 -05:00
|
|
|
|
await newPenalty.TryCreatePenalty(Manager.GetPenaltyService(), ServerLogger);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
if (activeClient.IsIngame)
|
2017-11-29 19:35:50 -05:00
|
|
|
|
{
|
2020-11-17 19:24:54 -05:00
|
|
|
|
var formattedKick = string.Format(RconParser.Configuration.CommandPrefixes.Kick,
|
2021-10-31 12:57:32 -04:00
|
|
|
|
activeClient.TemporalClientNumber,
|
2020-11-17 19:24:54 -05:00
|
|
|
|
_messageFormatter.BuildFormattedMessage(RconParser.Configuration, newPenalty));
|
2021-10-31 12:57:32 -04:00
|
|
|
|
ServerLogger.LogDebug("Executing tempban kick command for {ActiveClient}", activeClient.ToString());
|
|
|
|
|
await activeClient.CurrentServer.ExecuteCommandAsync(formattedKick);
|
2017-11-29 19:35:50 -05:00
|
|
|
|
}
|
2020-04-21 18:34:00 -04:00
|
|
|
|
}
|
2017-11-29 19:35:50 -05:00
|
|
|
|
|
2020-11-17 19:24:54 -05:00
|
|
|
|
public override async Task Ban(string reason, EFClient targetClient, EFClient originClient, bool isEvade = false)
|
2020-04-21 18:34:00 -04:00
|
|
|
|
{
|
|
|
|
|
// ensure player gets kicked if command not performed on them in the same server
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var activeClient = Manager.FindActiveClient(targetClient);
|
2017-11-29 19:35:50 -05:00
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var newPenalty = new EFPenalty
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2019-05-29 17:55:35 -04:00
|
|
|
|
Type = EFPenalty.PenaltyType.Ban,
|
2018-10-15 20:51:04 -04:00
|
|
|
|
Expires = null,
|
2018-12-16 22:16:56 -05:00
|
|
|
|
Offender = targetClient,
|
|
|
|
|
Offense = reason,
|
|
|
|
|
Punisher = originClient,
|
|
|
|
|
IsEvadedOffense = isEvade
|
2017-11-25 20:29:58 -05:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
ServerLogger.LogDebug("Creating ban penalty for {TargetClient}", targetClient.ToString());
|
|
|
|
|
activeClient.SetLevel(Permission.Banned, originClient);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
await newPenalty.TryCreatePenalty(Manager.GetPenaltyService(), ServerLogger);
|
2020-04-21 18:34:00 -04:00
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
if (activeClient.IsIngame)
|
2020-04-21 18:34:00 -04:00
|
|
|
|
{
|
2021-10-31 12:57:32 -04:00
|
|
|
|
ServerLogger.LogDebug("Attempting to kicking newly banned client {ActiveClient}", activeClient.ToString());
|
2021-06-03 11:51:03 -04:00
|
|
|
|
|
2020-11-17 19:24:54 -05:00
|
|
|
|
var formattedString = string.Format(RconParser.Configuration.CommandPrefixes.Kick,
|
2021-10-31 12:57:32 -04:00
|
|
|
|
activeClient.TemporalClientNumber,
|
2020-11-17 19:24:54 -05:00
|
|
|
|
_messageFormatter.BuildFormattedMessage(RconParser.Configuration, newPenalty));
|
2021-10-31 12:57:32 -04:00
|
|
|
|
await activeClient.CurrentServer.ExecuteCommandAsync(formattedString);
|
2020-04-21 18:34:00 -04:00
|
|
|
|
}
|
2015-03-08 17:20:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
public override async Task Unban(string reason, EFClient targetClient, EFClient originClient)
|
2015-03-08 17:20:10 -04:00
|
|
|
|
{
|
2021-10-31 12:57:32 -04:00
|
|
|
|
var unbanPenalty = new EFPenalty
|
2018-02-17 01:13:38 -05:00
|
|
|
|
{
|
2019-05-29 17:55:35 -04:00
|
|
|
|
Type = EFPenalty.PenaltyType.Unban,
|
2019-06-27 21:06:30 -04:00
|
|
|
|
Expires = DateTime.Now,
|
2021-10-31 12:57:32 -04:00
|
|
|
|
Offender = targetClient,
|
2018-02-17 01:13:38 -05:00
|
|
|
|
Offense = reason,
|
2021-10-31 12:57:32 -04:00
|
|
|
|
Punisher = originClient,
|
2018-02-17 01:13:38 -05:00
|
|
|
|
When = DateTime.UtcNow,
|
|
|
|
|
Active = true,
|
2021-10-31 12:57:32 -04:00
|
|
|
|
Link = targetClient.AliasLink
|
2018-02-17 01:13:38 -05:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
ServerLogger.LogDebug("Creating unban penalty for {targetClient}", targetClient.ToString());
|
|
|
|
|
targetClient.SetLevel(Permission.User, originClient);
|
2022-02-22 18:09:50 -05:00
|
|
|
|
await Manager.GetPenaltyService().RemoveActivePenalties(targetClient.AliasLink.AliasLinkId,
|
|
|
|
|
targetClient.NetworkId, targetClient.CurrentAlias?.IPAddress);
|
2019-06-27 21:06:30 -04:00
|
|
|
|
await Manager.GetPenaltyService().Create(unbanPenalty);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 12:57:32 -04:00
|
|
|
|
public override void InitializeTokens()
|
2015-03-13 19:40:16 -04:00
|
|
|
|
{
|
2019-06-11 09:00:14 -04:00
|
|
|
|
Manager.GetMessageTokens().Add(new MessageToken("TOTALPLAYERS", (Server s) => Task.Run(async () => (await Manager.GetClientService().GetTotalClientsAsync()).ToString())));
|
|
|
|
|
Manager.GetMessageTokens().Add(new MessageToken("VERSION", (Server s) => Task.FromResult(Application.Program.Version.ToString())));
|
2020-01-26 19:06:50 -05:00
|
|
|
|
Manager.GetMessageTokens().Add(new MessageToken("NEXTMAP", (Server s) => SharedLibraryCore.Commands.NextMapCommand.GetNextMap(s, _translationLookup)));
|
2021-11-23 18:26:33 -05:00
|
|
|
|
Manager.GetMessageTokens().Add(new MessageToken("ADMINS", (Server s) => Task.FromResult(ListAdminsCommand.OnlineAdmins(s, _translationLookup))));
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2015-03-08 17:20:10 -04:00
|
|
|
|
}
|
|
|
|
|
}
|