2018-04-14 00:51:38 -04:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
using IW4MAdmin.Plugins.Login.Commands;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
using SharedLibraryCore;
|
2020-01-14 19:56:23 -05:00
|
|
|
|
using SharedLibraryCore.Commands;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
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; }
|
2020-02-11 17:44:06 -05:00
|
|
|
|
private readonly IConfigurationHandler<Configuration> _configHandler;
|
|
|
|
|
|
|
|
|
|
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory)
|
|
|
|
|
{
|
|
|
|
|
_configHandler = configurationHandlerFactory.GetConfigurationHandler<Configuration>("LoginPluginSettings");
|
|
|
|
|
}
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2022-10-12 11:32:45 -04:00
|
|
|
|
public Task OnEventAsync(GameEvent gameEvent, Server server)
|
2018-04-14 00:51:38 -04:00
|
|
|
|
{
|
2022-10-12 11:32:45 -04:00
|
|
|
|
if (gameEvent.IsRemote || _configHandler.Configuration().RequirePrivilegedClientLogin == false)
|
2018-04-14 00:51:38 -04:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
2022-10-12 11:32:45 -04:00
|
|
|
|
if (gameEvent.Type == GameEvent.EventType.Connect)
|
2018-04-14 00:51:38 -04:00
|
|
|
|
{
|
2022-10-12 11:32:45 -04:00
|
|
|
|
AuthorizedClients.TryAdd(gameEvent.Origin.ClientId, false);
|
|
|
|
|
gameEvent.Origin.SetAdditionalProperty("IsLoggedIn", false);
|
2018-04-14 00:51:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 11:32:45 -04:00
|
|
|
|
if (gameEvent.Type == GameEvent.EventType.Disconnect)
|
2018-04-14 00:51:38 -04:00
|
|
|
|
{
|
2022-10-12 11:32:45 -04:00
|
|
|
|
AuthorizedClients.TryRemove(gameEvent.Origin.ClientId, out _);
|
2018-04-14 00:51:38 -04:00
|
|
|
|
}
|
2022-10-12 11:32:45 -04:00
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2022-10-12 11:32:45 -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
|
|
|
|
{
|
2022-10-25 14:22:33 -04:00
|
|
|
|
if (gameEvent.Type != GameEvent.EventType.Command || gameEvent.Extra is null || gameEvent.IsRemote)
|
2022-10-12 11:32:45 -04:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-04-15 21:27:43 -04:00
|
|
|
|
|
2022-10-12 11:32:45 -04:00
|
|
|
|
if (gameEvent.Origin.Level < EFClient.Permission.Moderator ||
|
|
|
|
|
gameEvent.Origin.Level == EFClient.Permission.Console)
|
|
|
|
|
return true;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2022-10-12 11:32:45 -04:00
|
|
|
|
if (gameEvent.Extra.GetType() == typeof(SetPasswordCommand) &&
|
|
|
|
|
gameEvent.Origin?.Password == null)
|
|
|
|
|
return true;
|
2020-01-14 19:56:23 -05:00
|
|
|
|
|
2022-10-12 11:32:45 -04:00
|
|
|
|
if (gameEvent.Extra.GetType() == typeof(LoginCommand))
|
|
|
|
|
return true;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2022-10-12 11:32:45 -04:00
|
|
|
|
if (gameEvent.Extra.GetType() == typeof(RequestTokenCommand))
|
|
|
|
|
return true;
|
2018-10-13 19:49:08 -04:00
|
|
|
|
|
2022-10-12 11:32:45 -04:00
|
|
|
|
if (!AuthorizedClients[gameEvent.Origin.ClientId])
|
2018-10-13 19:49:08 -04:00
|
|
|
|
{
|
2022-10-12 11:32:45 -04:00
|
|
|
|
return false;
|
2018-10-13 19:49:08 -04:00
|
|
|
|
}
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2022-10-12 11:32:45 -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();
|
2020-02-11 17:44:06 -05:00
|
|
|
|
if (_configHandler.Configuration() == null)
|
2018-04-14 00:51:38 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
_configHandler.Set((Configuration)new Configuration().Generate());
|
|
|
|
|
await _configHandler.Save();
|
2018-04-14 00:51:38 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public Task OnTickAsync(Server S) => Task.CompletedTask;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public Task OnUnloadAsync() => Task.CompletedTask;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
}
|
|
|
|
|
}
|