From 288d660fd42680f05845a0be358491bf727730d2 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Tue, 28 Jun 2022 17:51:03 +0300 Subject: [PATCH] linkfilter --- src/structure/client/Resolver.js | 13 ++++++++ .../components/observers/Automoderation.js | 32 ++++++++++++------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/structure/client/Resolver.js b/src/structure/client/Resolver.js index 758550e..817843a 100644 --- a/src/structure/client/Resolver.js +++ b/src/structure/client/Resolver.js @@ -7,6 +7,7 @@ const Component = require('../interfaces/Component'); const { Constants: { InfractionResolves } } = require('../../constants'); const { GuildWrapper } = require('./wrappers'); +const { Resolver: DNSResolver } = require('dns').promises; class Resolver { /** @@ -20,6 +21,8 @@ class Resolver { * @type {DiscordClient} */ this.client = client; + + this.dnsresolver = new DNSResolver(); } @@ -397,6 +400,16 @@ class Resolver { return infraction; } + async validateDomain(domain) { + try { + await this.dnsresolver.resolveAny(domain); + return true; + } catch (err) { + this.client.error(err); + return false; + } + } + } module.exports = Resolver; diff --git a/src/structure/components/observers/Automoderation.js b/src/structure/components/observers/Automoderation.js index d3b8b05..4069536 100644 --- a/src/structure/components/observers/Automoderation.js +++ b/src/structure/components/observers/Automoderation.js @@ -29,6 +29,9 @@ const CONSTANTS = { } }; +const linkRegG = /(https?:\/\/(www\.)?)?(?([a-z0-9-]{1,63}\.)?([a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})?)(\/\S*)?/iug; +const linkReg = /(https?:\/\/(www\.)?)?(?([a-z0-9-]{1,63}\.)?([a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})?)(\/\S*)?/iu; + // TODO: // Clean up commented out code once testing of new code is done // Implement missing automod features -- done @@ -498,7 +501,7 @@ module.exports = class AutoModeration extends Observer { const { resolver } = this.client; const settings = await wrapper.settings(); const { linkfilter: setting } = settings; - const { bypass, ignore, actions, silent, enabled, blacklist, whitelist, mode } = setting; + const { bypass, ignore, actions, silent, enabled, blacklist, whitelist, whitelistMode, greylist } = setting; if (!enabled) return; const roles = member?.roles.cache.map((r) => r.id) || []; @@ -507,29 +510,36 @@ module.exports = class AutoModeration extends Observer { const msg = edited || message; if (!msg.content) return; const content = msg.content.split('').join(''); //Copy the string... - const linkRegG = /(https?:\/\/(www\.)?)?(?([a-z0-9-]{1,63}\.)?([a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})?)(\/\S*)?/iug; - const linkReg = /(https?:\/\/(www\.)?)?(?([a-z0-9-]{1,63}\.)?([a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})?)(\/\S*)?/iu; let matches = content.match(linkRegG); if (!matches) matches = content.replace(/\s/u, '').match(linkRegG); if (!matches) return; let remove = false; const filterResult = {}; - const _whitelist = mode === 'whitelist'; for (const match of matches) { const { domain } = match.match(linkReg).groups; - // eslint-disable-next-line brace-style - if (!_whitelist && blacklist.some((dom) => { return dom.includes(domain) || domain.includes(dom); })) { + const predicate = (dom) => { + return dom.includes(domain) || domain.includes(dom); + }; + + if (blacklist.some(predicate)) { filterResult.match = domain; filterResult.matcher = 'link blacklist'; remove = true; break; - } else if (_whitelist) { - // eslint-disable-next-line brace-style - if (whitelist.some((dom) => { return dom.includes(domain) || domain.includes(dom); })) continue; + } else if (greylist.some(predicate)) { + filterResult.match = domain; + filterResult.matcher = 'link greylist'; + remove = true; + break; + } else if (whitelistMode) { + if (whitelist.some(predicate)) continue; const valid = await resolver.validateDomain(domain); - if (!valid) continue; + if (!valid) { + this.client.emit('linkFilterWarn', { message: guild.format('LINKFILTER_WARN', { domain }) }); + continue; + } filterResult.match = domain; filterResult.matcher = 'link whitelist'; @@ -558,7 +568,7 @@ module.exports = class AutoModeration extends Observer { return act.trigger.includes(filterResult.match); }); if (!action) action = actions.find((act) => { - return act.trigger === mode; + return act.trigger === filterResult.matcher.split(' ')[1]; }); if (!action) action = actions.find((act) => { return act.trigger === 'generic';