From dbca3675bab9d656fb7eb893d59598f69e6d3dad Mon Sep 17 00:00:00 2001 From: RaidMax Date: Mon, 24 Oct 2022 18:54:48 -0500 Subject: [PATCH] add unban subnet command and subnet list interaction --- Plugins/ScriptPlugins/SubnetBan.js | 86 ++++++++++++++++++++++++++++-- WebfrontCore/wwwroot/js/action.js | 3 +- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/Plugins/ScriptPlugins/SubnetBan.js b/Plugins/ScriptPlugins/SubnetBan.js index 84bbce88e..00f98eaa6 100644 --- a/Plugins/ScriptPlugins/SubnetBan.js +++ b/Plugins/ScriptPlugins/SubnetBan.js @@ -1,5 +1,6 @@ const cidrRegex = /^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$/; const validCIDR = input => cidrRegex.test(input); +const subnetBanlistKey = 'Webfront::Nav::Admin::SubnetBanlist'; let subnetList = []; const commands = [{ @@ -26,7 +27,36 @@ const commands = [{ gameEvent.Origin.Tell(`Added ${input} to subnet banlist`); } -}]; +}, + { + name: 'unbansubnet', + description: 'unbans an IPv4 subnet', + alias: 'ubs', + permission: 'SeniorAdmin', + targetRequired: false, + arguments: [{ + name: 'subnet in IPv4 CIDR notation', + required: true + }], + execute: (gameEvent) => { + const input = String(gameEvent.Data).trim(); + + if (!validCIDR(input)) { + gameEvent.Origin.Tell('Invalid CIDR input'); + return; + } + + if (!subnetList.includes(input)) { + gameEvent.Origin.Tell('Subnet is not banned'); + return; + } + + subnetList = subnetList.filter(item => item !== input); + _configHandler.SetValue('SubnetBanList', subnetList); + + gameEvent.Origin.Tell(`Removed ${input} from subnet banlist`); + } + }]; convertIPtoLong = ip => { let components = String(ip).match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/); @@ -66,7 +96,7 @@ isSubnetBanned = (ip, list) => { const plugin = { author: 'RaidMax', - version: 1.0, + version: 1.1, name: 'Subnet Banlist Plugin', manager: null, logger: null, @@ -86,7 +116,8 @@ const plugin = { this.manager = manager; this.logger = manager.GetLogger(0); this.configHandler = _configHandler; - this.subnetList = []; + subnetList = []; + this.interactionRegistration = _serviceResolver.ResolveService('IInteractionRegistration'); const list = this.configHandler.GetValue('SubnetBanList'); if (list !== undefined) { @@ -105,9 +136,58 @@ const plugin = { this.banMessage = 'You are not allowed to join this server.'; this.configHandler.SetValue('BanMessage', this.banMessage); } + + this.interactionRegistration.RegisterScriptInteraction(subnetBanlistKey, plugin.name, (targetId, game, token) => { + const helpers = importNamespace('SharedLibraryCore.Helpers'); + const interactionData = new helpers.InteractionData(); + + interactionData.Name = 'Subnet Banlist'; // navigation link name + interactionData.Description = `List of banned subnets (${subnetList.length} Total)`; // alt and title + interactionData.DisplayMeta = 'oi-circle-x'; // nav icon + interactionData.InteractionId = subnetBanlistKey; + interactionData.MinimumPermission = 3; // moderator + interactionData.InteractionType = 2; // 1 is RawContent for apis etc..., 2 is + interactionData.Source = plugin.name; + + interactionData.ScriptAction = (sourceId, targetId, game, meta, token) => { + let table = ''; + + const unbanSubnetInteraction = { + InteractionId: 'command', + Data: 'unbansubnet', + ActionButtonLabel: 'Unban', + Name: 'Unban Subnet' + }; + + subnetList.forEach(subnet => { + unbanSubnetInteraction.Data += ' ' + subnet + table += ` + + + `; + }); + + table += '
+

${subnet}

+
+ +
+ + Unban Subnet +
+
+
'; + + return table; + } + + return interactionData; + }); }, onUnloadAsync: () => { + this.interactionRegistration.UnregisterInteraction(subnetBanlistKey); }, onTickAsync: server => { diff --git a/WebfrontCore/wwwroot/js/action.js b/WebfrontCore/wwwroot/js/action.js index af7f914f4..82d50fee0 100644 --- a/WebfrontCore/wwwroot/js/action.js +++ b/WebfrontCore/wwwroot/js/action.js @@ -98,7 +98,8 @@ $(document).ready(function () { let actionKeys = actionId === undefined ? '' : `?id=${actionId}`; if (actionMeta !== undefined) { - actionKeys = actionKeys + '&meta=' + JSON.stringify(actionMeta); + const and = actionKeys === '' ? '?' : '&'; + actionKeys = actionKeys + and + 'meta=' + JSON.stringify(actionMeta); } showLoader();