From 7be096e0b6645e55ed64c27bea733f2b4793c670 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Wed, 23 Mar 2022 13:34:04 -0500 Subject: [PATCH] add vpn whitelist command --- .../Misc/ScriptPluginConfigurationWrapper.cs | 5 +- Plugins/ScriptPlugins/VPNDetection.js | 58 +++++++++++++------ 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Application/Misc/ScriptPluginConfigurationWrapper.cs b/Application/Misc/ScriptPluginConfigurationWrapper.cs index b511a3057..50aa4124e 100644 --- a/Application/Misc/ScriptPluginConfigurationWrapper.cs +++ b/Application/Misc/ScriptPluginConfigurationWrapper.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Text.Json; using System.Threading.Tasks; using IW4MAdmin.Application.Configuration; using Jint; @@ -84,9 +85,9 @@ namespace IW4MAdmin.Application.Misc var item = _config[_pluginName][key]; - if (item is JArray array) + if (item is JsonElement { ValueKind: JsonValueKind.Array } jElem) { - item = array.ToObject>(); + item = jElem.Deserialize>(); } return JsValue.FromObject(_scriptEngine, item); diff --git a/Plugins/ScriptPlugins/VPNDetection.js b/Plugins/ScriptPlugins/VPNDetection.js index d014d9c11..f76b93af1 100644 --- a/Plugins/ScriptPlugins/VPNDetection.js +++ b/Plugins/ScriptPlugins/VPNDetection.js @@ -1,14 +1,31 @@ -var plugin = { - author: 'RaidMax', - version: 1.2, - name: 'VPN Detection Plugin', +const commands = [{ + name: "whitelistvpn", + description: "whitelists a player's client id from VPN detection", + alias: "wv", + permission: "SeniorAdmin", + targetRequired: true, + arguments: [{ + name: "players", + required: true + }], + execute: (gameEvent) => { + plugin.vpnExceptionIds.push(gameEvent.Target.ClientId); + plugin.configHandler.SetValue('vpnExceptionIds', plugin.vpnExceptionIds); + gameEvent.Origin.Tell(`Successfully whitelisted ${gameEvent.Target.Name}`); + } +}]; + +const plugin = { + author: 'RaidMax', + version: 1.3, + name: 'VPN Detection Plugin', manager: null, logger: null, vpnExceptionIds: [], checkForVpn: function (origin) { - var exempt = false; + let exempt = false; // prevent players that are exempt from being kicked this.vpnExceptionIds.forEach(function (id) { if (id === origin.ClientId) { @@ -18,40 +35,41 @@ var plugin = { }); if (exempt) { + this.logger.WriteInfo(`${origin} is whitelisted, so we are not checking VPN status`); return; } - var usingVPN = false; + let usingVPN = false; try { - var cl = new System.Net.Http.HttpClient(); - var re = cl.GetAsync('https://api.xdefcon.com/proxy/check/?ip=' + origin.IPAddressString).Result; - var userAgent = 'IW4MAdmin-' + this.manager.GetApplicationSettings().Configuration().Id; + const cl = new System.Net.Http.HttpClient(); + const re = cl.GetAsync(`https://api.xdefcon.com/proxy/check/?ip=${origin.IPAddressString}`).Result; + const userAgent = `IW4MAdmin-${this.manager.GetApplicationSettings().Configuration().Id}`; cl.DefaultRequestHeaders.Add('User-Agent', userAgent); - var co = re.Content; - var parsedJSON = JSON.parse(co.ReadAsStringAsync().Result); + const co = re.Content; + const parsedJSON = JSON.parse(co.ReadAsStringAsync().Result); co.Dispose(); re.Dispose(); cl.Dispose(); usingVPN = parsedJSON.success && parsedJSON.proxy; } catch (e) { - this.logger.WriteWarning('There was a problem checking client IP for VPN ' + e.message); + this.logger.WriteWarning(`There was a problem checking client IP for VPN ${e.message}`); } if (usingVPN) { this.logger.WriteInfo(origin + ' is using a VPN (' + origin.IPAddressString + ')'); - var contactUrl = this.manager.GetApplicationSettings().Configuration().ContactUri; - var additionalInfo = ''; + const contactUrl = this.manager.GetApplicationSettings().Configuration().ContactUri; + let additionalInfo = ''; if (contactUrl) { - additionalInfo = _localization.LocalizationIndex["SERVER_KICK_VPNS_NOTALLOWED_INFO"] + ' ' + contactUrl; + additionalInfo = _localization.LocalizationIndex['SERVER_KICK_VPNS_NOTALLOWED_INFO'] + ' ' + contactUrl; } - origin.Kick(_localization.LocalizationIndex["SERVER_KICK_VPNS_NOTALLOWED"] + ' ' + additionalInfo, _IW4MAdminClient); + origin.Kick(_localization.LocalizationIndex['SERVER_KICK_VPNS_NOTALLOWED'] + ' ' + additionalInfo, _IW4MAdminClient); } }, onEventAsync: function (gameEvent, server) { // join event - if (gameEvent.Type === 4) { + if (gameEvent.TypeName === 'Join') { this.checkForVpn(gameEvent.Origin); } }, @@ -59,6 +77,10 @@ var plugin = { onLoadAsync: function (manager) { this.manager = manager; this.logger = manager.GetLogger(0); + + this.configHandler = _configHandler; + this.configHandler.GetValue('vpnExceptionIds').forEach(element => this.vpnExceptionIds.push(element)); + this.logger.WriteInfo(`Loaded ${this.vpnExceptionIds.length} ids into whitelist`); }, onUnloadAsync: function () { @@ -66,4 +88,4 @@ var plugin = { onTickAsync: function (server) { } -}; \ No newline at end of file +};