2023-04-08 10:43:33 -04:00
|
|
|
|
using System.Threading;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
using IW4MAdmin.Plugins.Login.Commands;
|
2023-02-11 21:46:57 -05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
using SharedLibraryCore;
|
2020-01-14 19:56:23 -05:00
|
|
|
|
using SharedLibraryCore.Commands;
|
2023-02-11 21:46:57 -05:00
|
|
|
|
using SharedLibraryCore.Events.Management;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2023-02-11 21:46:57 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces.Events;
|
|
|
|
|
using EFClient = Data.Models.Client.EFClient;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2023-02-11 21:46:57 -05:00
|
|
|
|
namespace IW4MAdmin.Plugins.Login;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2023-02-11 21:46:57 -05:00
|
|
|
|
public class Plugin : IPluginV2
|
|
|
|
|
{
|
|
|
|
|
public string Name => "Login";
|
|
|
|
|
public string Version => Utilities.GetVersionAsString();
|
|
|
|
|
public string Author => "RaidMax";
|
2023-04-08 10:43:33 -04:00
|
|
|
|
|
|
|
|
|
private readonly LoginStates _loginStates;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2023-04-08 10:43:33 -04:00
|
|
|
|
public Plugin(LoginConfiguration configuration, LoginStates loginStates)
|
2023-02-11 21:46:57 -05:00
|
|
|
|
{
|
2023-04-08 10:43:33 -04:00
|
|
|
|
_loginStates = loginStates;
|
|
|
|
|
if (!(configuration?.RequirePrivilegedClientLogin ?? false))
|
2020-02-11 17:44:06 -05:00
|
|
|
|
{
|
2023-02-11 21:46:57 -05:00
|
|
|
|
return;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
}
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2023-02-11 21:46:57 -05:00
|
|
|
|
IManagementEventSubscriptions.Load += OnLoad;
|
|
|
|
|
IManagementEventSubscriptions.ClientStateInitialized += OnClientStateInitialized;
|
|
|
|
|
IManagementEventSubscriptions.ClientStateDisposed += (clientEvent, token) =>
|
2018-04-14 00:51:38 -04:00
|
|
|
|
{
|
2023-04-08 10:43:33 -04:00
|
|
|
|
_loginStates.AuthorizedClients.TryRemove(clientEvent.Client.ClientId, out _);
|
2023-02-11 21:46:57 -05:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RegisterDependencies(IServiceCollection serviceCollection)
|
|
|
|
|
{
|
2023-04-08 10:43:33 -04:00
|
|
|
|
serviceCollection.AddConfiguration<LoginConfiguration>("LoginPluginSettings");
|
|
|
|
|
serviceCollection.AddSingleton(new LoginStates());
|
2023-02-11 21:46:57 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 10:43:33 -04:00
|
|
|
|
private Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token)
|
2023-02-11 21:46:57 -05:00
|
|
|
|
{
|
2023-04-08 10:43:33 -04:00
|
|
|
|
_loginStates.AuthorizedClients.TryAdd(clientEvent.Client.ClientId, false);
|
|
|
|
|
clientEvent.Client.SetAdditionalProperty(LoginStates.LoginKey, false);
|
2023-02-11 21:46:57 -05:00
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2023-04-08 10:43:33 -04:00
|
|
|
|
private Task OnLoad(IManager manager, CancellationToken token)
|
2023-02-11 21:46:57 -05:00
|
|
|
|
{
|
|
|
|
|
manager.CommandInterceptors.Add(gameEvent =>
|
|
|
|
|
{
|
|
|
|
|
if (gameEvent.Type != GameEvent.EventType.Command || gameEvent.Extra is null || gameEvent.IsRemote)
|
2018-04-14 00:51:38 -04:00
|
|
|
|
{
|
2023-02-11 21:46:57 -05:00
|
|
|
|
return true;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 21:46:57 -05:00
|
|
|
|
if (gameEvent.Origin.Level is < EFClient.Permission.Moderator or EFClient.Permission.Console)
|
2018-04-14 00:51:38 -04:00
|
|
|
|
{
|
2023-02-11 21:46:57 -05:00
|
|
|
|
return true;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
}
|
2022-10-12 11:32:45 -04:00
|
|
|
|
|
2023-02-11 21:46:57 -05:00
|
|
|
|
if (gameEvent.Extra.GetType() == typeof(SetPasswordCommand) &&
|
|
|
|
|
gameEvent.Origin?.Password == null)
|
2018-04-14 00:51:38 -04:00
|
|
|
|
{
|
2023-02-11 21:46:57 -05:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-13 19:49:08 -04:00
|
|
|
|
|
2023-02-11 21:46:57 -05:00
|
|
|
|
if (gameEvent.Extra.GetType() == typeof(LoginCommand))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2023-02-11 21:46:57 -05:00
|
|
|
|
if (gameEvent.Extra.GetType() == typeof(RequestTokenCommand))
|
|
|
|
|
{
|
2022-10-12 11:32:45 -04:00
|
|
|
|
return true;
|
2023-02-11 21:46:57 -05:00
|
|
|
|
}
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2023-04-08 10:43:33 -04:00
|
|
|
|
if (!_loginStates.AuthorizedClients[gameEvent.Origin.ClientId])
|
2018-04-14 00:51:38 -04:00
|
|
|
|
{
|
2023-02-11 21:46:57 -05:00
|
|
|
|
return false;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 10:43:33 -04:00
|
|
|
|
gameEvent.Origin.SetAdditionalProperty(LoginStates.LoginKey, true);
|
2023-02-11 21:46:57 -05:00
|
|
|
|
return true;
|
|
|
|
|
});
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2023-02-11 21:46:57 -05:00
|
|
|
|
return Task.CompletedTask;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
}
|
|
|
|
|
}
|