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
|
|
|
|
|
|
|
|
|
public Task OnEventAsync(GameEvent E, Server S)
|
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
if (E.IsRemote || _configHandler.Configuration().RequirePrivilegedClientLogin == false)
|
2018-04-14 00:51:38 -04:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
if (E.Type == GameEvent.EventType.Connect)
|
|
|
|
|
{
|
|
|
|
|
AuthorizedClients.TryAdd(E.Origin.ClientId, false);
|
2018-10-13 19:49:08 -04:00
|
|
|
|
E.Origin.SetAdditionalProperty("IsLoggedIn", false);
|
2018-04-14 00:51:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (E.Type == GameEvent.EventType.Disconnect)
|
|
|
|
|
{
|
|
|
|
|
AuthorizedClients.TryRemove(E.Origin.ClientId, out bool value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (E.Type == GameEvent.EventType.Command)
|
|
|
|
|
{
|
2018-11-05 22:01:29 -05:00
|
|
|
|
if (E.Origin.Level < EFClient.Permission.Moderator ||
|
|
|
|
|
E.Origin.Level == EFClient.Permission.Console)
|
2018-04-15 21:27:43 -04:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
2020-01-31 21:15:07 -05:00
|
|
|
|
if (E.Extra.GetType() == typeof(SetPasswordCommand) &&
|
2019-05-03 21:13:51 -04:00
|
|
|
|
E.Origin?.Password == null)
|
2018-04-14 00:51:38 -04:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
2020-01-31 21:15:07 -05:00
|
|
|
|
if (E.Extra.GetType() == typeof(LoginCommand))
|
2018-04-14 00:51:38 -04:00
|
|
|
|
return Task.CompletedTask;
|
2020-01-14 19:56:23 -05:00
|
|
|
|
|
|
|
|
|
if (E.Extra.GetType() == typeof(RequestTokenCommand))
|
|
|
|
|
return Task.CompletedTask;
|
2018-04-14 00:51:38 -04:00
|
|
|
|
|
|
|
|
|
if (!AuthorizedClients[E.Origin.ClientId])
|
2018-10-13 19:49:08 -04:00
|
|
|
|
{
|
2018-05-08 00:58:46 -04:00
|
|
|
|
throw new AuthorizationException(Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_LOGIN_AUTH"]);
|
2018-10-13 19:49:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
E.Origin.SetAdditionalProperty("IsLoggedIn", true);
|
|
|
|
|
}
|
2018-04-14 00:51:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task OnLoadAsync(IManager manager)
|
|
|
|
|
{
|
|
|
|
|
AuthorizedClients = new ConcurrentDictionary<int, bool>();
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|