sysinfo: add interface to get IPv6 address

This commit is contained in:
Girish Ramakrishnan
2022-01-05 18:07:36 -08:00
parent 235d18cbb1
commit bbf1a5af3d
13 changed files with 100 additions and 32 deletions

View File

@@ -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');
}