fold sysinfo into network

the backends are network backends
This commit is contained in:
Girish Ramakrishnan
2023-08-03 13:38:42 +05:30
parent a4d57e7b08
commit 47d57a3971
23 changed files with 143 additions and 161 deletions

View File

@@ -1,6 +1,9 @@
'use strict';
exports = module.exports = {
testIPv4Config,
testIPv6Config,
getBlocklist,
setBlocklist,
@@ -12,22 +15,55 @@ exports = module.exports = {
getIPv6Config,
setIPv6Config,
getIPv4,
hasIPv6,
getIPv6
};
const assert = require('assert'),
BoxError = require('./boxerror.js'),
cron = require('./cron.js'),
fs = require('fs'),
ipaddr = require('ipaddr.js'),
path = require('path'),
paths = require('./paths.js'),
safe = require('safetydance'),
settings = require('./settings.js'),
shell = require('./shell.js'),
sysinfo = require('./sysinfo.js'),
validator = require('validator');
const SET_BLOCKLIST_CMD = path.join(__dirname, 'scripts/setblocklist.sh');
function api(provider) {
assert.strictEqual(typeof provider, 'string');
switch (provider) {
case 'noop': return require('./network/noop.js');
case 'fixed': return require('./network/fixed.js');
case 'network-interface': return require('./network/network-interface.js');
default: return require('./network/generic.js');
}
}
function hasIPv6() {
const IPV6_PROC_FILE = '/proc/net/if_inet6';
// on contabo, /proc/net/if_inet6 is an empty file. so just exists is not enough
return fs.existsSync(IPV6_PROC_FILE) && fs.readFileSync(IPV6_PROC_FILE, 'utf8').trim().length !== 0;
}
async function testIPv4Config(config) {
assert.strictEqual(typeof config, 'object');
return await api(config.provider).testIPv4Config(config);
}
async function testIPv6Config(config) {
assert.strictEqual(typeof config, 'object');
return await api(config.provider).testIPv6Config(config);
}
async function getBlocklist() {
const value = await settings.getBlob(settings.FIREWALL_BLOCKLIST_KEY);
return value ? value.toString('utf8') : '';
@@ -87,7 +123,7 @@ async function setIPv4Config(ipv4Config) {
if (settings.isDemo()) throw new BoxError(BoxError.BAD_FIELD, 'Not allowed in demo mode');
const error = await sysinfo.testIPv4Config(ipv4Config);
const error = await testIPv4Config(ipv4Config);
if (error) throw error;
await settings.setJson(settings.IPV4_CONFIG_KEY, ipv4Config);
@@ -103,8 +139,22 @@ async function setIPv6Config(ipv6Config) {
if (settings.isDemo()) throw new BoxError(BoxError.BAD_FIELD, 'Not allowed in demo mode');
const error = await sysinfo.testIPv6Config(ipv6Config);
const error = await testIPv6Config(ipv6Config);
if (error) throw error;
await settings.setJson(settings.IPV6_CONFIG_KEY, ipv6Config);
}
async function getIPv4() {
const config = await getIPv4Config();
return await api(config.provider).getIPv4(config);
}
// returns RFC 5952 formatted address (https://datatracker.ietf.org/doc/html/rfc5952)
async function getIPv6() {
const config = await getIPv6Config();
const result = await api(config.provider).getIPv6(config);
if (!result) return null;
return ipaddr.parse(result).toRFC5952String();
}