IW4M-Admin/Plugins/Login/Plugin.cs

97 lines
3.0 KiB
C#
Raw Normal View History

2018-04-14 00:51:38 -04:00
using System.Collections.Concurrent;
2023-02-11 21:46:57 -05:00
using System.Threading;
2018-04-14 00:51:38 -04:00
using System.Threading.Tasks;
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;
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";
2018-04-14 00:51:38 -04:00
2023-02-11 21:46:57 -05:00
public static ConcurrentDictionary<int, bool> AuthorizedClients { get; private set; }
private const string LoginKey = "IsLoggedIn";
2023-02-11 21:46:57 -05:00
public Plugin(LoginConfiguration configuration)
{
if (!configuration?.RequirePrivilegedClientLogin ?? false)
{
2023-02-11 21:46:57 -05:00
return;
}
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-02-11 21:46:57 -05:00
AuthorizedClients.TryRemove(clientEvent.Client.ClientId, out _);
return Task.CompletedTask;
};
}
public static void RegisterDependencies(IServiceCollection serviceCollection)
{
serviceCollection.AddConfiguration<LoginConfiguration>("LoginConfiguration");
}
private static Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token)
{
AuthorizedClients.TryAdd(clientEvent.Client.ClientId, false);
clientEvent.Client.SetAdditionalProperty(LoginKey, false);
return Task.CompletedTask;
}
2018-04-14 00:51:38 -04:00
2023-02-11 21:46:57 -05:00
private static Task OnLoad(IManager manager, CancellationToken token)
{
AuthorizedClients = new ConcurrentDictionary<int, bool>();
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
}
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;
}
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))
{
return true;
2023-02-11 21:46:57 -05:00
}
2018-04-14 00:51:38 -04:00
2023-02-11 21:46:57 -05:00
if (!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-02-11 21:46:57 -05:00
gameEvent.Origin.SetAdditionalProperty(LoginKey, true);
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
}
}