Files
RSSHub/lib/middleware/access-control.js
2019-09-18 17:45:21 +08:00

40 lines
1.1 KiB
JavaScript

const art = require('art-template');
const path = require('path');
const config = require('@/config').value;
const reject = (ctx) => {
ctx.response.status = 403;
ctx.body = art(path.resolve(__dirname, '../views/rss.art'), {
lastBuildDate: new Date().toUTCString(),
updated: new Date().toISOString(),
ttl: 24 * 60 * 60,
title: '没有访问权限. Access denied.',
link: 'https://docs.rsshub.app/install/#%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6',
});
};
module.exports = async (ctx, next) => {
const ip = ctx.ips[0] || ctx.ip;
const requestPath = ctx.request.path;
if (requestPath === '/') {
await next();
} else {
if (config.whitelist) {
if (!(config.whitelist.includes(ip) || config.whitelist.includes(requestPath))) {
reject(ctx);
}
} else {
if (config.blacklist) {
if (config.blacklist.includes(ip) || config.blacklist.includes(requestPath)) {
reject(ctx);
}
}
}
if (ctx.response.status !== 403) {
await next();
}
}
};