linkfilter

This commit is contained in:
Erik 2022-06-28 17:51:03 +03:00
parent 79b941f1b8
commit 288d660fd4
Signed by untrusted user: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
2 changed files with 34 additions and 11 deletions

View File

@ -7,6 +7,7 @@ const Component = require('../interfaces/Component');
const { Constants: { InfractionResolves } } = require('../../constants'); const { Constants: { InfractionResolves } } = require('../../constants');
const { GuildWrapper } = require('./wrappers'); const { GuildWrapper } = require('./wrappers');
const { Resolver: DNSResolver } = require('dns').promises;
class Resolver { class Resolver {
/** /**
@ -20,6 +21,8 @@ class Resolver {
* @type {DiscordClient} * @type {DiscordClient}
*/ */
this.client = client; this.client = client;
this.dnsresolver = new DNSResolver();
} }
@ -397,6 +400,16 @@ class Resolver {
return infraction; return infraction;
} }
async validateDomain(domain) {
try {
await this.dnsresolver.resolveAny(domain);
return true;
} catch (err) {
this.client.error(err);
return false;
}
}
} }
module.exports = Resolver; module.exports = Resolver;

View File

@ -29,6 +29,9 @@ const CONSTANTS = {
} }
}; };
const linkRegG = /(https?:\/\/(www\.)?)?(?<domain>([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\.)?)?(?<domain>([a-z0-9-]{1,63}\.)?([a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})?)(\/\S*)?/iu;
// TODO: // TODO:
// Clean up commented out code once testing of new code is done // Clean up commented out code once testing of new code is done
// Implement missing automod features -- done // Implement missing automod features -- done
@ -498,7 +501,7 @@ module.exports = class AutoModeration extends Observer {
const { resolver } = this.client; const { resolver } = this.client;
const settings = await wrapper.settings(); const settings = await wrapper.settings();
const { linkfilter: setting } = 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; if (!enabled) return;
const roles = member?.roles.cache.map((r) => r.id) || []; const roles = member?.roles.cache.map((r) => r.id) || [];
@ -507,29 +510,36 @@ module.exports = class AutoModeration extends Observer {
const msg = edited || message; const msg = edited || message;
if (!msg.content) return; if (!msg.content) return;
const content = msg.content.split('').join(''); //Copy the string... const content = msg.content.split('').join(''); //Copy the string...
const linkRegG = /(https?:\/\/(www\.)?)?(?<domain>([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\.)?)?(?<domain>([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); let matches = content.match(linkRegG);
if (!matches) matches = content.replace(/\s/u, '').match(linkRegG); if (!matches) matches = content.replace(/\s/u, '').match(linkRegG);
if (!matches) return; if (!matches) return;
let remove = false; let remove = false;
const filterResult = {}; const filterResult = {};
const _whitelist = mode === 'whitelist';
for (const match of matches) { for (const match of matches) {
const { domain } = match.match(linkReg).groups; const { domain } = match.match(linkReg).groups;
// eslint-disable-next-line brace-style const predicate = (dom) => {
if (!_whitelist && blacklist.some((dom) => { return dom.includes(domain) || domain.includes(dom); })) { return dom.includes(domain) || domain.includes(dom);
};
if (blacklist.some(predicate)) {
filterResult.match = domain; filterResult.match = domain;
filterResult.matcher = 'link blacklist'; filterResult.matcher = 'link blacklist';
remove = true; remove = true;
break; break;
} else if (_whitelist) { } else if (greylist.some(predicate)) {
// eslint-disable-next-line brace-style filterResult.match = domain;
if (whitelist.some((dom) => { return dom.includes(domain) || domain.includes(dom); })) continue; filterResult.matcher = 'link greylist';
remove = true;
break;
} else if (whitelistMode) {
if (whitelist.some(predicate)) continue;
const valid = await resolver.validateDomain(domain); 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.match = domain;
filterResult.matcher = 'link whitelist'; filterResult.matcher = 'link whitelist';
@ -558,7 +568,7 @@ module.exports = class AutoModeration extends Observer {
return act.trigger.includes(filterResult.match); return act.trigger.includes(filterResult.match);
}); });
if (!action) action = actions.find((act) => { if (!action) action = actions.find((act) => {
return act.trigger === mode; return act.trigger === filterResult.matcher.split(' ')[1];
}); });
if (!action) action = actions.find((act) => { if (!action) action = actions.find((act) => {
return act.trigger === 'generic'; return act.trigger === 'generic';