sysinfo: add interface to get IPv6 address
This commit is contained in:
+17
-5
@@ -1,25 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getServerIp,
|
||||
getServerIPv4,
|
||||
getServerIPv6,
|
||||
testConfig
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
validator = require('validator');
|
||||
net = require('net');
|
||||
|
||||
async function getServerIp(config) {
|
||||
async function getServerIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
return config.ip;
|
||||
}
|
||||
|
||||
async function getServerIPv6(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
return config.ipv6;
|
||||
}
|
||||
|
||||
async function testConfig(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (typeof config.ip !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ip must be a string');
|
||||
if (!validator.isIP(config.ip, 4)) return new BoxError(BoxError.BAD_FIELD, 'ip is not a valid ipv4');
|
||||
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');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user