network: add trusted ips

This allows the user to set trusted ips to Cloudflare or some other CDN
and have the logs have the correct IPs.

fixes #801
This commit is contained in:
Girish Ramakrishnan
2023-05-13 14:59:57 +02:00
parent 951ed4bf33
commit b26c8d20cd
13 changed files with 228 additions and 54 deletions
+23 -1
View File
@@ -2,7 +2,10 @@
exports = module.exports = {
getBlocklist,
setBlocklist
setBlocklist,
getTrustedIps,
setTrustedIps
};
const assert = require('assert'),
@@ -11,6 +14,7 @@ const assert = require('assert'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
network = require('../network.js'),
reverseProxy = require('../reverseproxy.js'),
safe = require('safetydance');
async function getBlocklist(req, res, next) {
@@ -32,3 +36,21 @@ async function setBlocklist(req, res, next) {
next(new HttpSuccess(200, {}));
}
async function getTrustedIps(req, res, next) {
const [error, trustedIps] = await safe(reverseProxy.getTrustedIps());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { trustedIps }));
}
async function setTrustedIps(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.trustedIps !== 'string') return next(new HttpError(400, 'trustedIps must be a string'));
const [error] = await safe(reverseProxy.setTrustedIps(req.body.trustedIps, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}