2016-10-08 16:40:58 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2022-01-05 18:07:36 -08:00
|
|
|
getServerIPv4,
|
|
|
|
|
getServerIPv6,
|
2022-02-15 12:31:55 -08:00
|
|
|
testIPv4Config,
|
|
|
|
|
testIPv6Config
|
2016-10-08 16:40:58 -07:00
|
|
|
};
|
|
|
|
|
|
2021-08-27 09:52:24 -07:00
|
|
|
const assert = require('assert'),
|
2019-10-22 14:09:44 -07:00
|
|
|
BoxError = require('../boxerror.js'),
|
2020-08-02 11:43:18 -07:00
|
|
|
debug = require('debug')('box:sysinfo/generic'),
|
2021-08-27 09:52:24 -07:00
|
|
|
safe = require('safetydance'),
|
2019-10-22 14:09:44 -07:00
|
|
|
superagent = require('superagent');
|
2016-10-08 16:40:58 -07:00
|
|
|
|
2022-01-05 18:07:36 -08:00
|
|
|
async function getServerIPv4(config) {
|
2019-10-29 15:46:33 -07:00
|
|
|
assert.strictEqual(typeof config, 'object');
|
2021-08-27 09:52:24 -07:00
|
|
|
|
|
|
|
|
if (process.env.BOX_ENV === 'test') return '127.0.0.1';
|
|
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
debug('getServerIPv4: getting server IP');
|
2021-08-27 09:52:24 -07:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
const [networkError, response] = await safe(superagent.get('https://ipv4.api.cloudron.io/api/v1/helper/public_ip')
|
|
|
|
|
.timeout(30 * 1000)
|
|
|
|
|
.retry(2)
|
|
|
|
|
.ok(() => true));
|
2021-08-27 09:52:24 -07:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
if (networkError || response.status !== 200) {
|
|
|
|
|
debug('getServerIPv4: Error getting IP', networkError);
|
2022-02-04 11:15:53 -08:00
|
|
|
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IPv4. API server (ipv4.api.cloudron.io) unreachable');
|
2022-01-06 12:49:56 -08:00
|
|
|
}
|
2021-08-27 09:52:24 -07:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
if (!response.body && !response.body.ip) {
|
|
|
|
|
debug('getServerIPv4: Unexpected answer. No "ip" found in response body.', response.body);
|
|
|
|
|
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IPv4. No IP found in response');
|
|
|
|
|
}
|
2022-01-05 18:07:36 -08:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
return response.body.ip;
|
2022-01-05 18:07:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getServerIPv6(config) {
|
|
|
|
|
assert.strictEqual(typeof config, 'object');
|
|
|
|
|
|
2022-01-06 12:22:16 -08:00
|
|
|
if (process.env.BOX_ENV === 'test') return '::1';
|
2022-01-05 18:07:36 -08:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
debug('getServerIPv6: getting server IP');
|
2022-01-05 18:07:36 -08:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
const [networkError, response] = await safe(superagent.get('https://ipv6.api.cloudron.io/api/v1/helper/public_ip')
|
|
|
|
|
.timeout(30 * 1000)
|
|
|
|
|
.retry(2)
|
|
|
|
|
.ok(() => true));
|
2022-01-05 18:07:36 -08:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
if (networkError || response.status !== 200) {
|
|
|
|
|
debug('getServerIPv6: Error getting IP', networkError);
|
2022-02-04 11:15:53 -08:00
|
|
|
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IPv6. API server (ipv6.api.cloudron.io) unreachable');
|
2022-01-06 12:49:56 -08:00
|
|
|
}
|
2022-01-05 18:07:36 -08:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
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 IPv6. No IP found in response');
|
|
|
|
|
}
|
2021-08-27 09:52:24 -07:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
return response.body.ip;
|
2016-11-29 15:26:25 +01:00
|
|
|
}
|
2019-10-29 20:08:45 -07:00
|
|
|
|
2022-02-15 12:31:55 -08:00
|
|
|
async function testIPv4Config(config) {
|
|
|
|
|
assert.strictEqual(typeof config, 'object');
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function testIPv6Config(config) {
|
2019-10-29 20:08:45 -07:00
|
|
|
assert.strictEqual(typeof config, 'object');
|
|
|
|
|
|
2021-08-27 09:52:24 -07:00
|
|
|
return null;
|
2019-10-29 20:08:45 -07:00
|
|
|
}
|