Add blacklist env

This commit is contained in:
Wildan M 2022-06-16 23:11:01 +07:00
commit fad216c968
9 changed files with 313 additions and 208 deletions

View file

@ -1,10 +1,10 @@
const record_prefix = 'forward-domain=';
const path = require('path');
const {
client
} = require('./sni');
const {
findTxtRecord
findTxtRecord,
isHostBlacklisted
} = require('./util');
const combineURLs = require('axios/lib/helpers/combineURLs');
@ -26,13 +26,14 @@ async function buildCache(host) {
return {
url,
expand,
blacklisted: isHostBlacklisted(host),
expire: Date.now() + 86400 * 1000,
};
}
const acme_prefix = '/.well-known/acme-challenge/';
const listener = async function (/** @type {import('http').IncomingMessage} */ req, /** @type {import('http').ServerResponse} */ res) {
const listener = async function ( /** @type {import('http').IncomingMessage} */ req, /** @type {import('http').ServerResponse} */ res) {
try {
if (req.url.startsWith(acme_prefix)) {
if (client.challengeCallbacks) {
@ -52,6 +53,12 @@ const listener = async function (/** @type {import('http').IncomingMessage} */ r
cache = await buildCache(req.headers.host);
resolveCache[req.headers.host] = cache;
}
if (cache.blacklisted) {
res.writeHead(301, {
'Location': process.env.BLACKLIST_REDIRECT || 'https://forwarddomain.net/blacklisted',
});
return;
}
res.writeHead(301, {
'Location': cache.expand ? combineURLs(cache.url, req.url) : cache.url,
});

View file

@ -5,7 +5,6 @@ const path = require('path');
const {
md5,
ensureDir,
findTxtRecord
} = require('./util');
const {
default: AwaitLock

View file

@ -1,11 +1,26 @@
const { default: axios } = require('axios');
const {
default: axios
} = require('axios');
var crypto = require('crypto');
const fs = require('fs');
const blacklistURL = (process.env.BLACKLIST_HOSTS || "").split(',').reduce((acc, host) => {
acc[host] = true;
return acc;
}, {});
function md5(str) {
return crypto.createHash('md5').update(str).digest('hex');
}
function isHostBlacklisted(domain = '') {
if (domain.length > 6) {
let p = domain.lastIndexOf('.', domain.length - 6);
if (p > 0) {
domain = domain.substring(p + 1);
}
}
return blacklistURL[domain];
}
async function ensureDir(dir) {
try {
@ -40,4 +55,5 @@ module.exports = {
md5,
ensureDir,
findTxtRecord,
isHostBlacklisted,
}