IW4M-Admin/Plugins/Login/Plugin.cs

95 lines
3.3 KiB
C#
Raw Normal View History

2018-04-14 00:51:38 -04:00
using System.Collections.Concurrent;
using System.Reflection;
using System.Threading.Tasks;
using IW4MAdmin.Plugins.Login.Commands;
2018-04-14 00:51:38 -04:00
using SharedLibraryCore;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Database.Models;
2018-04-14 00:51:38 -04:00
using SharedLibraryCore.Exceptions;
using SharedLibraryCore.Interfaces;
namespace IW4MAdmin.Plugins.Login
{
public class Plugin : IPlugin
{
public string Name => "Login";
public float Version => Assembly.GetExecutingAssembly().GetName().Version.Major + Assembly.GetExecutingAssembly().GetName().Version.Minor / 10.0f;
public string Author => "RaidMax";
public static ConcurrentDictionary<int, bool> AuthorizedClients { get; private set; }
private readonly IConfigurationHandler<Configuration> _configHandler;
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory)
{
_configHandler = configurationHandlerFactory.GetConfigurationHandler<Configuration>("LoginPluginSettings");
}
2018-04-14 00:51:38 -04:00
public Task OnEventAsync(GameEvent gameEvent, Server server)
2018-04-14 00:51:38 -04:00
{
if (gameEvent.IsRemote || _configHandler.Configuration().RequirePrivilegedClientLogin == false)
2018-04-14 00:51:38 -04:00
return Task.CompletedTask;
if (gameEvent.Type == GameEvent.EventType.Connect)
2018-04-14 00:51:38 -04:00
{
AuthorizedClients.TryAdd(gameEvent.Origin.ClientId, false);
gameEvent.Origin.SetAdditionalProperty("IsLoggedIn", false);
2018-04-14 00:51:38 -04:00
}
if (gameEvent.Type == GameEvent.EventType.Disconnect)
2018-04-14 00:51:38 -04:00
{
AuthorizedClients.TryRemove(gameEvent.Origin.ClientId, out _);
2018-04-14 00:51:38 -04:00
}
return Task.CompletedTask;
}
2018-04-14 00:51:38 -04:00
public async Task OnLoadAsync(IManager manager)
{
AuthorizedClients = new ConcurrentDictionary<int, bool>();
manager.CommandInterceptors.Add(gameEvent =>
2018-04-14 00:51:38 -04:00
{
if (gameEvent.Type != GameEvent.EventType.Command || gameEvent.Extra is null)
{
return true;
}
if (gameEvent.Origin.Level < EFClient.Permission.Moderator ||
gameEvent.Origin.Level == EFClient.Permission.Console)
return true;
2018-04-14 00:51:38 -04:00
if (gameEvent.Extra.GetType() == typeof(SetPasswordCommand) &&
gameEvent.Origin?.Password == null)
return true;
if (gameEvent.Extra.GetType() == typeof(LoginCommand))
return true;
2018-04-14 00:51:38 -04:00
if (gameEvent.Extra.GetType() == typeof(RequestTokenCommand))
return true;
if (!AuthorizedClients[gameEvent.Origin.ClientId])
{
return false;
}
2018-04-14 00:51:38 -04:00
gameEvent.Origin.SetAdditionalProperty("IsLoggedIn", true);
return true;
});
2018-04-14 00:51:38 -04:00
2022-01-28 10:35:01 -05:00
await _configHandler.BuildAsync();
if (_configHandler.Configuration() == null)
2018-04-14 00:51:38 -04:00
{
_configHandler.Set((Configuration)new Configuration().Generate());
await _configHandler.Save();
2018-04-14 00:51:38 -04:00
}
}
public Task OnTickAsync(Server S) => Task.CompletedTask;
2018-04-14 00:51:38 -04:00
public Task OnUnloadAsync() => Task.CompletedTask;
2018-04-14 00:51:38 -04:00
}
}