Files
cloudron-box/src/routes/network.js
Girish Ramakrishnan 445c83c8b9 make auditsource a class
this allows us to use AuditSource for the class and auditSource for
the instances!
2021-09-30 10:13:36 -07:00

35 lines
1.1 KiB
JavaScript

'use strict';
exports = module.exports = {
getBlocklist,
setBlocklist
};
const assert = require('assert'),
AuditSource = require('../auditsource.js'),
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
network = require('../network.js'),
safe = require('safetydance');
async function getBlocklist(req, res, next) {
const [error, blocklist] = await safe(network.getBlocklist());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { blocklist }));
}
async function setBlocklist(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.blocklist !== 'string') return next(new HttpError(400, 'blocklist must be a string'));
req.clearTimeout(); // can take a while if there is a lot of network ranges
const [error] = await safe(network.setBlocklist(req.body.blocklist, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}