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 { 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;

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:
// 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\.)?)?(?<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);
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';