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;
|
||||
}
|
||||
|
||||
+32
-5
@@ -1,7 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getServerIp,
|
||||
getServerIPv4,
|
||||
getServerIPv6,
|
||||
testConfig
|
||||
};
|
||||
|
||||
@@ -12,25 +13,51 @@ const assert = require('assert'),
|
||||
safe = require('safetydance'),
|
||||
superagent = require('superagent');
|
||||
|
||||
async function getServerIp(config) {
|
||||
async function getServerIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (process.env.BOX_ENV === 'test') return '127.0.0.1';
|
||||
|
||||
return await promiseRetry({ times: 10, interval: 5000, debug }, async () => {
|
||||
debug('getServerIp: getting server IP');
|
||||
debug('getServerIPv4: getting server IP');
|
||||
|
||||
const [networkError, response] = await safe(superagent.get('https://ipv4.api.cloudron.io/api/v1/helper/public_ip')
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
|
||||
if (networkError || response.status !== 200) {
|
||||
debug('Error getting IP', networkError);
|
||||
debug('getServerIPv4: Error getting IP', networkError);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IP. API server unreachable');
|
||||
}
|
||||
|
||||
if (!response.body && !response.body.ip) {
|
||||
debug('Unexpected answer. No "ip" found in response body.', response.body);
|
||||
debug('getServerIPv4: Unexpected answer. No "ip" found in response body.', response.body);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IP. No IP found in response');
|
||||
}
|
||||
|
||||
return response.body.ip;
|
||||
});
|
||||
}
|
||||
|
||||
async function getServerIPv6(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (process.env.BOX_ENV === 'test') return '127.0.0.1';
|
||||
|
||||
return await promiseRetry({ times: 10, interval: 5000, debug }, async () => {
|
||||
debug('getServerIPv6: getting server IP');
|
||||
|
||||
const [networkError, response] = await safe(superagent.get('https://ipv6.api.cloudron.io/api/v1/helper/public_ip')
|
||||
.timeout(30 * 1000)
|
||||
.ok(() => true));
|
||||
|
||||
if (networkError || response.status !== 200) {
|
||||
debug('getServerIPv6: Error getting IP', networkError);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IP. API server unreachable');
|
||||
}
|
||||
|
||||
if (!response.body && !response.body.ip) {
|
||||
debug('getServerIPv6: Unexpected answer. No "ip" found in response body.', response.body);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IP. No IP found in response');
|
||||
}
|
||||
|
||||
|
||||
@@ -7,17 +7,24 @@
|
||||
// -------------------------------------------
|
||||
|
||||
exports = module.exports = {
|
||||
getServerIp,
|
||||
getServerIPv4,
|
||||
getServerIPv6,
|
||||
testConfig
|
||||
};
|
||||
|
||||
var assert = require('assert'),
|
||||
const assert = require('assert'),
|
||||
BoxError = require('../boxerror.js');
|
||||
|
||||
async function getServerIp(config) {
|
||||
async function getServerIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
throw new BoxError(BoxError.NOT_IMPLEMENTED, 'testConfig is not implemented');
|
||||
throw new BoxError(BoxError.NOT_IMPLEMENTED, 'getServerIPv4 is not implemented');
|
||||
}
|
||||
|
||||
async function getServerIPv6(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
throw new BoxError(BoxError.NOT_IMPLEMENTED, 'getServerIPv6 is not implemented');
|
||||
}
|
||||
|
||||
async function testConfig(config) {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getServerIp,
|
||||
getServerIPv4,
|
||||
getServerIPv6,
|
||||
testConfig
|
||||
};
|
||||
|
||||
@@ -11,7 +12,7 @@ const assert = require('assert'),
|
||||
os = require('os'),
|
||||
safe = require('safetydance');
|
||||
|
||||
async function getServerIp(config) {
|
||||
async function getServerIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
const ifaces = os.networkInterfaces();
|
||||
@@ -25,12 +26,26 @@ async function getServerIp(config) {
|
||||
return addresses[0];
|
||||
}
|
||||
|
||||
async function getServerIPv6(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
const ifaces = os.networkInterfaces();
|
||||
const iface = ifaces[config.ifname]; // array of addresses
|
||||
if (!iface) throw new BoxError(BoxError.NETWORK_ERROR, `No interface named ${config.ifname}`);
|
||||
|
||||
const addresses = iface.filter(i => i.family === 'IPv6').map(i => i.address);
|
||||
if (addresses.length === 0) throw new BoxError(BoxError.NETWORK_ERROR, `${config.ifname} does not have any IPv4 address`);
|
||||
if (addresses.length > 1) debug(`${config.ifname} has multiple ipv6 - ${JSON.stringify(addresses)}. choosing the first one.`);
|
||||
|
||||
return addresses[0];
|
||||
}
|
||||
|
||||
async function testConfig(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
if (typeof config.ifname !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ifname is not a string');
|
||||
|
||||
const [error] = await safe(getServerIp(config));
|
||||
const [error] = await safe(getServerIPv4(config));
|
||||
return error || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user