1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-12 16:18:07 -05:00

implement PluginV2 for script plugins

This commit is contained in:
RaidMax
2023-04-04 18:24:13 -05:00
parent 7c94806ece
commit d3544b4519
33 changed files with 1659 additions and 1026 deletions

View File

@ -3,151 +3,92 @@ const validCIDR = input => cidrRegex.test(input);
const subnetBanlistKey = 'Webfront::Nav::Admin::SubnetBanlist';
let subnetList = [];
const commands = [{
name: "bansubnet",
description: "bans an IPv4 subnet",
alias: "bs",
permission: "SeniorAdmin",
targetRequired: false,
arguments: [{
name: "subnet in IPv4 CIDR notation",
required: true
}],
const init = (registerNotify, serviceResolver, config) => {
registerNotify('IManagementEventSubscriptions.ClientStateAuthorized', (authorizedEvent, _) => plugin.onClientAuthorized(authorizedEvent));
execute: (gameEvent) => {
const input = String(gameEvent.Data).trim();
plugin.onLoad(serviceResolver, config);
return plugin;
};
if (!validCIDR(input)) {
gameEvent.Origin.Tell('Invalid CIDR input');
return;
}
const plugin = {
author: 'RaidMax',
version: '2.0',
name: 'Subnet Banlist Plugin',
manager: null,
logger: null,
config: null,
serviceResolver: null,
banMessage: '',
subnetList.push(input);
_configHandler.SetValue('SubnetBanList', subnetList);
gameEvent.Origin.Tell(`Added ${input} to subnet banlist`);
}
},
{
name: 'unbansubnet',
description: 'unbans an IPv4 subnet',
alias: 'ubs',
commands: [{
name: 'bansubnet',
description: 'bans an IPv4 subnet',
alias: 'bs',
permission: 'SeniorAdmin',
targetRequired: false,
arguments: [{
name: 'subnet in IPv4 CIDR notation',
required: true
}],
execute: (gameEvent) => {
const input = String(gameEvent.Data).trim();
const input = String(gameEvent.data).trim();
if (!validCIDR(input)) {
gameEvent.Origin.Tell('Invalid CIDR input');
gameEvent.origin.tell('Invalid CIDR input');
return;
}
if (!subnetList.includes(input)) {
gameEvent.Origin.Tell('Subnet is not banned');
return;
}
subnetList.push(input);
plugin.config.setValue('SubnetBanList', subnetList);
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})$/);
if (components) {
let ipLong = 0;
let power = 1;
for (let i = 4; i >= 1; i -= 1) {
ipLong += power * parseInt(components[i]);
power *= 256;
}
return ipLong;
} else {
return -1;
}
};
isInSubnet = (ip, subnet) => {
const mask = subnet.match(/^(.*?)\/(\d{1,2})$/);
if (!mask) {
return false;
}
const baseIP = convertIPtoLong(mask[1]);
const longIP = convertIPtoLong(ip);
if (mask && baseIP >= 0) {
const freedom = Math.pow(2, 32 - parseInt(mask[2]));
return (longIP > baseIP) && (longIP < baseIP + freedom - 1);
} else return false;
};
isSubnetBanned = (ip, list) => {
const matchingSubnets = list.filter(subnet => isInSubnet(ip, subnet));
return matchingSubnets.length !== 0;
}
const plugin = {
author: 'RaidMax',
version: 1.1,
name: 'Subnet Banlist Plugin',
manager: null,
logger: null,
banMessage: '',
onEventAsync: (gameEvent, server) => {
if (gameEvent.TypeName === 'Join') {
if (!isSubnetBanned(gameEvent.Origin.IPAddressString, subnetList, this.logger)) {
return;
}
this.logger.WriteInfo(`Kicking ${gameEvent.Origin} because they are subnet banned.`);
gameEvent.Origin.Kick(this.banMessage, _IW4MAdminClient);
gameEvent.origin.tell(`Added ${input} to subnet banlist`);
}
},
onLoadAsync: manager => {
this.manager = manager;
this.logger = manager.GetLogger(0);
this.configHandler = _configHandler;
subnetList = [];
this.interactionRegistration = _serviceResolver.ResolveService('IInteractionRegistration');
{
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();
const list = this.configHandler.GetValue('SubnetBanList');
if (list !== undefined) {
list.forEach(element => {
const ban = String(element);
subnetList.push(ban)
});
this.logger.WriteInfo(`Loaded ${list.length} banned subnets`);
} else {
this.configHandler.SetValue('SubnetBanList', []);
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);
plugin.config.setValue('SubnetBanList', subnetList);
gameEvent.origin.tell(`Removed ${input} from subnet banlist`);
}
}
],
this.banMessage = this.configHandler.GetValue('BanMessage');
if (this.banMessage === undefined) {
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) => {
interactions: [{
name: subnetBanlistKey,
action: function (_, __, ___) {
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.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;
interactionData.interactionType = 2;
interactionData.source = plugin.name;
interactionData.ScriptAction = (sourceId, targetId, game, meta, token) => {
let table = '<table class="table bg-dark-dm bg-light-lm">';
@ -160,7 +101,7 @@ const plugin = {
};
subnetList.forEach(subnet => {
unbanSubnetInteraction.Data += ' ' + subnet
unbanSubnetInteraction.Data += ' ' + subnet;
table += `<tr>
<td>
<p>${subnet}</p>
@ -180,16 +121,84 @@ const plugin = {
table += '</table>';
return table;
}
};
return interactionData;
});
}
}],
onLoad: function (serviceResolver, config) {
this.serviceResolver = serviceResolver;
this.config = config;
this.logger = serviceResolver.resolveService('ILogger', ['ScriptPluginV2']);
subnetList = [];
const list = this.config.getValue('SubnetBanList');
if (list !== undefined) {
list.forEach(element => {
const ban = String(element);
subnetList.push(ban);
});
this.logger.logInformation('Loaded {Count} banned subnets', list.length);
} else {
this.config.setValue('SubnetBanList', []);
}
this.banMessage = this.config.getValue('BanMessage');
if (this.banMessage === undefined) {
this.banMessage = 'You are not allowed to join this server.';
this.config.setValue('BanMessage', this.banMessage);
}
const interactionRegistration = serviceResolver.resolveService('IInteractionRegistration');
interactionRegistration.unregisterInteraction(subnetBanlistKey);
this.logger.logInformation('Subnet Ban loaded');
},
onUnloadAsync: () => {
this.interactionRegistration.UnregisterInteraction(subnetBanlistKey);
},
onClientAuthorized: (clientEvent) => {
if (!isSubnetBanned(clientEvent.client.ipAddressString, subnetList)) {
return;
}
onTickAsync: server => {
this.logger.logInformation(`Kicking {Client} because they are subnet banned.`, clientEvent.client);
clientEvent.client.kick(this.banMessage, clientEvent.client.currentServer.asConsoleClient());
}
};
const convertIPtoLong = ip => {
let components = String(ip).match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
if (components) {
let ipLong = 0;
let power = 1;
for (let i = 4; i >= 1; i -= 1) {
ipLong += power * parseInt(components[i]);
power *= 256;
}
return ipLong;
} else {
return -1;
}
};
const isInSubnet = (ip, subnet) => {
const mask = subnet.match(/^(.*?)\/(\d{1,2})$/);
if (!mask) {
return false;
}
const baseIP = convertIPtoLong(mask[1]);
const longIP = convertIPtoLong(ip);
if (mask && baseIP >= 0) {
const freedom = Math.pow(2, 32 - parseInt(mask[2]));
return (longIP > baseIP) && (longIP < baseIP + freedom - 1);
} else return false;
};
const isSubnetBanned = (ip, list) => {
const matchingSubnets = list.filter(subnet => isInSubnet(ip, subnet));
return matchingSubnets.length !== 0;
};