Files
cloudron-box/src/routes/network.js
T
Girish Ramakrishnan 92a103d635 settings: move ipv6/ipv4 config into network
this also rename sysinfo_config to ipv4_config
2023-08-03 06:40:04 +05:30

120 lines
3.7 KiB
JavaScript

'use strict';
exports = module.exports = {
getBlocklist,
setBlocklist,
getTrustedIps,
setTrustedIps,
getDynamicDns,
setDynamicDns,
getIPv4Config,
setIPv4Config,
getIPv6Config,
setIPv6Config
};
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'),
reverseProxy = require('../reverseproxy.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, {}));
}
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, {}));
}
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, {}));
}
async function getIPv4Config(req, res, next) {
const [error, ipv4Config] = await safe(network.getIPv4Config());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, ipv4Config));
}
async function setIPv4Config(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
const [error] = await safe(network.setIPv4Config(req.body));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}
async function getIPv6Config(req, res, next) {
const [error, ipv6Config] = await safe(network.getIPv6Config());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, ipv6Config));
}
async function setIPv6Config(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
const [error] = await safe(network.setIPv6Config(req.body));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}