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

45 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
2023-08-03 13:38:42 +05:30
getIPv4,
getIPv6,
2022-02-15 12:31:55 -08:00
testIPv4Config,
testIPv6Config
};
2021-08-27 09:52:24 -07:00
const assert = require('assert'),
2019-10-29 20:08:45 -07:00
BoxError = require('../boxerror.js'),
2022-01-05 18:07:36 -08:00
net = require('net');
2023-08-03 13:38:42 +05:30
async function getIPv4(config) {
assert.strictEqual(typeof config, 'object');
return config.ipv4;
}
2023-08-03 13:38:42 +05:30
async function getIPv6(config) {
2022-01-05 18:07:36 -08:00
assert.strictEqual(typeof config, 'object');
2022-01-06 12:22:16 -08:00
if ('ipv6' in config) return config.ipv6;
throw new BoxError(BoxError.NETWORK_ERROR, 'No IPv6 configured');
2022-01-05 18:07:36 -08:00
}
2022-02-15 12:31:55 -08:00
async function testIPv4Config(config) {
2019-10-29 20:08:45 -07:00
assert.strictEqual(typeof config, 'object');
if (typeof config.ipv4 !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ipv4 must be a string');
if (!net.isIPv4(config.ipv4)) return new BoxError(BoxError.BAD_FIELD, 'invalid IPv4');
2022-01-05 18:07:36 -08:00
2022-02-15 12:31:55 -08:00
return null;
}
async function testIPv6Config(config) {
assert.strictEqual(typeof config, 'object');
if (typeof config.ipv6 !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ipv6 must be a string');
if (!net.isIPv6(config.ipv6)) return new BoxError(BoxError.BAD_FIELD, 'invalid IPv6');
2019-10-29 20:08:45 -07:00
2021-08-27 09:52:24 -07:00
return null;
2019-10-29 20:08:45 -07:00
}