2019-10-29 16:12:58 -07:00
|
|
|
'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
|
2019-10-29 16:12:58 -07:00
|
|
|
};
|
|
|
|
|
|
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');
|
2019-10-29 16:12:58 -07:00
|
|
|
|
2023-08-03 13:38:42 +05:30
|
|
|
async function getIPv4(config) {
|
2019-10-29 16:12:58 -07:00
|
|
|
assert.strictEqual(typeof config, 'object');
|
|
|
|
|
|
2024-06-10 19:18:08 +02:00
|
|
|
if ('ip' in config) return config.ip;
|
|
|
|
|
|
|
|
|
|
throw new BoxError(BoxError.NETWORK_ERROR, 'No IPv4 configured');
|
2019-10-29 16:12:58 -07:00
|
|
|
}
|
|
|
|
|
|
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');
|
|
|
|
|
|
2023-09-12 20:13:27 +05:30
|
|
|
if ('ip' in config) return config.ip;
|
2022-01-06 12:22:16 -08:00
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
2023-09-12 20:13:27 +05:30
|
|
|
if (typeof config.ip !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ipv4 must be a string');
|
|
|
|
|
if (!net.isIPv4(config.ip)) 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');
|
|
|
|
|
|
2023-09-12 20:13:27 +05:30
|
|
|
if (typeof config.ip !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ipv6 must be a string');
|
|
|
|
|
if (!net.isIPv6(config.ip)) 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
|
|
|
}
|