mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
882 B
30 lines
882 B
5 years ago
|
'use strict';
|
||
|
|
||
|
const cache = require(__dirname+'/../redis.js')
|
||
|
, dynamicResponse = require(__dirname+'/dynamic.js')
|
||
|
, { dnsbl } = require(__dirname+'/../configs/main.js')
|
||
|
, { batch } = require('dnsbl');
|
||
|
|
||
|
module.exports = async (req, res, next) => {
|
||
|
|
||
|
if (dnsbl.enabled) {
|
||
|
const ip = req.headers['x-real-ip'] || req.connection.remoteAddress;
|
||
|
let isBlacklisted = await cache.get(`blacklisted:${ip}`);
|
||
|
if (isBlacklisted === null) { //not cached
|
||
|
const dnsblResp = await batch(ip, dnsbl.blacklists);
|
||
|
isBlacklisted = dnsblResp.some(r => r.listed === true);
|
||
|
await cache.set(`blacklisted:${ip}`, isBlacklisted, dnsbl.cacheTime);
|
||
|
}
|
||
|
if (isBlacklisted) {
|
||
|
return dynamicResponse(req, res, 403, 'message', {
|
||
|
'title': 'Forbidden',
|
||
|
'message': 'Your IP address is listed on a blacklist',
|
||
|
'redirect': req.headers.referer || '/'
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
return next();
|
||
|
|
||
|
}
|
||
|
|