Files
cloudron-box/src/routes/network.js
T

78 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-08-31 18:22:33 -07:00
'use strict';
exports = module.exports = {
getBlocklist,
2023-05-13 14:59:57 +02:00
setBlocklist,
getTrustedIps,
2023-08-02 22:53:29 +05:30
setTrustedIps,
getDynamicDns,
setDynamicDns
2020-08-31 18:22:33 -07:00
};
const assert = require('assert'),
2021-09-30 09:50:30 -07:00
AuditSource = require('../auditsource.js'),
2020-08-31 18:22:33 -07:00
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
network = require('../network.js'),
2023-05-13 14:59:57 +02:00
reverseProxy = require('../reverseproxy.js'),
safe = require('safetydance');
2020-08-31 18:22:33 -07:00
async function getBlocklist(req, res, next) {
const [error, blocklist] = await safe(network.getBlocklist());
if (error) return next(BoxError.toHttpError(error));
2020-08-31 18:22:33 -07:00
next(new HttpSuccess(200, { blocklist }));
2020-08-31 18:22:33 -07:00
}
async function setBlocklist(req, res, next) {
2020-08-31 18:22:33 -07:00
assert.strictEqual(typeof req.body, 'object');
2020-09-14 10:29:48 -07:00
if (typeof req.body.blocklist !== 'string') return next(new HttpError(400, 'blocklist must be a string'));
2020-08-31 18:22:33 -07:00
req.clearTimeout(); // can take a while if there is a lot of network ranges
2021-09-30 09:50:30 -07:00
const [error] = await safe(network.setBlocklist(req.body.blocklist, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
2020-08-31 18:22:33 -07:00
next(new HttpSuccess(200, {}));
2020-08-31 18:22:33 -07:00
}
2023-05-13 14:59:57 +02:00
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, {}));
}
2023-08-02 22:53:29 +05:30
async function getDynamicDns(req, res, next) {
const [error, enabled] = await safe(network.getDynamicDns());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { enabled }));
}
async function setDynamicDns(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled boolean is required'));
const [error] = await safe(network.setDynamicDns(req.body.enabled));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}