2020-08-31 18:22:33 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
getBlocklist,
|
2023-05-13 14:59:57 +02:00
|
|
|
setBlocklist,
|
|
|
|
|
|
2023-08-02 22:53:29 +05:30
|
|
|
getDynamicDns,
|
2023-08-03 06:05:29 +05:30
|
|
|
setDynamicDns,
|
|
|
|
|
|
|
|
|
|
getIPv4Config,
|
|
|
|
|
setIPv4Config,
|
|
|
|
|
|
|
|
|
|
getIPv6Config,
|
2023-08-03 13:38:42 +05:30
|
|
|
setIPv6Config,
|
|
|
|
|
|
|
|
|
|
getIPv4,
|
|
|
|
|
getIPv6,
|
2020-08-31 18:22:33 -07:00
|
|
|
};
|
|
|
|
|
|
2021-08-18 15:31:07 -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,
|
2021-08-18 15:31:07 -07:00
|
|
|
network = require('../network.js'),
|
|
|
|
|
safe = require('safetydance');
|
2020-08-31 18:22:33 -07:00
|
|
|
|
2021-08-18 15:31:07 -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
|
|
|
|
2021-08-18 15:31:07 -07:00
|
|
|
next(new HttpSuccess(200, { blocklist }));
|
2020-08-31 18:22:33 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-18 15:31:07 -07:00
|
|
|
async function setBlocklist(req, res, next) {
|
2020-08-31 18:22:33 -07:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
2023-09-09 07:57:53 +05:30
|
|
|
// this is a string to allow comments
|
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
|
|
|
|
2020-09-14 09:56:35 -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)));
|
2021-08-18 15:31:07 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2020-08-31 18:22:33 -07:00
|
|
|
|
2021-08-18 15:31:07 -07:00
|
|
|
next(new HttpSuccess(200, {}));
|
2020-08-31 18:22:33 -07:00
|
|
|
}
|
2023-05-13 14:59:57 +02:00
|
|
|
|
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, {}));
|
|
|
|
|
}
|
2023-08-03 06:05:29 +05:30
|
|
|
|
|
|
|
|
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, {}));
|
|
|
|
|
}
|
2023-08-03 13:38:42 +05:30
|
|
|
|
|
|
|
|
async function getIPv4(req, res, next) {
|
2023-09-12 20:13:27 +05:30
|
|
|
const [error, ip] = await safe(network.getIPv4());
|
2023-08-03 13:38:42 +05:30
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
2023-09-12 20:13:27 +05:30
|
|
|
next(new HttpSuccess(200, { ip }));
|
2023-08-03 13:38:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getIPv6(req, res, next) {
|
2023-09-12 20:13:27 +05:30
|
|
|
const [error, ip] = await safe(network.getIPv6()); // ignore any error
|
2023-08-03 13:38:42 +05:30
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
2023-09-12 20:13:27 +05:30
|
|
|
next(new HttpSuccess(200, { ip }));
|
2023-08-03 13:38:42 +05:30
|
|
|
}
|