2019-10-29 16:12:58 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2022-01-05 18:07:36 -08:00
|
|
|
getServerIPv4,
|
|
|
|
|
getServerIPv6,
|
2019-10-29 20:08:45 -07:00
|
|
|
testConfig
|
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
|
|
|
|
2022-01-05 18:07:36 -08:00
|
|
|
async function getServerIPv4(config) {
|
2019-10-29 16:12:58 -07:00
|
|
|
assert.strictEqual(typeof config, 'object');
|
|
|
|
|
|
2021-08-27 09:52:24 -07:00
|
|
|
return config.ip;
|
2019-10-29 16:12:58 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-05 18:07:36 -08:00
|
|
|
async function getServerIPv6(config) {
|
|
|
|
|
assert.strictEqual(typeof config, 'object');
|
|
|
|
|
|
|
|
|
|
return config.ipv6;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 09:52:24 -07:00
|
|
|
async function testConfig(config) {
|
2019-10-29 20:08:45 -07:00
|
|
|
assert.strictEqual(typeof config, 'object');
|
|
|
|
|
|
2022-01-05 18:07:36 -08:00
|
|
|
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, 'ip is not a valid ipv4');
|
|
|
|
|
|
|
|
|
|
if ('ipv6' in config) {
|
|
|
|
|
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, 'ipv6 is not a valid 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
|
|
|
}
|