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
|
|
|
|
2023-03-26 18:53:10 +02:00
|
|
|
const gCache = { ipv4: {}, ipv6: {} }; // each has { timestamp, value, request }
|
|
|
|
|
|
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';
|
|
|
|
|
|
2023-03-26 18:53:10 +02:00
|
|
|
if (gCache.ipv4.value && (Date.now() - gCache.ipv4.timestamp <= 5 * 60 * 1000)) return gCache.ipv4.value;
|
|
|
|
|
|
|
|
|
|
let request = gCache.ipv4.request; // allow reuse for parallel requests
|
|
|
|
|
if (!request) {
|
|
|
|
|
debug('getServerIPv4: querying ipv4.api.cloudron.io to get server IPv4');
|
|
|
|
|
request = superagent.get('https://ipv4.api.cloudron.io/api/v1/helper/public_ip').timeout(30 * 1000).retry(2).ok(() => true);
|
|
|
|
|
gCache.ipv4.request = request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gCache.ipv4.value = null;
|
2021-08-27 09:52:24 -07:00
|
|
|
|
2023-03-26 18:53:10 +02:00
|
|
|
const [networkError, response] = await safe(request);
|
|
|
|
|
gCache.ipv4.request = null;
|
2021-08-27 09:52:24 -07:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
if (networkError || response.status !== 200) {
|
2023-04-16 10:49:59 +02:00
|
|
|
debug('getServerIPv4: Error getting IP. %o', 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
|
|
|
|
2023-03-26 18:53:10 +02:00
|
|
|
gCache.ipv4.value = response.body.ip;
|
|
|
|
|
gCache.ipv4.timestamp = Date.now();
|
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
|
|
|
|
2023-03-26 18:53:10 +02:00
|
|
|
if (gCache.ipv6.value && (Date.now() - gCache.ipv6.timestamp <= 5 * 60 * 1000)) return gCache.ipv6.value;
|
|
|
|
|
|
|
|
|
|
let request = gCache.ipv6.request; // allow reuse for parallel requests
|
|
|
|
|
if (!request) {
|
|
|
|
|
debug('getServerIPv6: querying ipv6.api.cloudron.io to get server IPv6');
|
|
|
|
|
request = superagent.get('https://ipv6.api.cloudron.io/api/v1/helper/public_ip').timeout(30 * 1000).retry(2).ok(() => true);
|
|
|
|
|
gCache.ipv6.request = request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gCache.ipv6.value = null;
|
2022-01-05 18:07:36 -08:00
|
|
|
|
2023-03-26 18:53:10 +02:00
|
|
|
const [networkError, response] = await safe(request);
|
|
|
|
|
gCache.ipv6.request = null;
|
2022-01-05 18:07:36 -08:00
|
|
|
|
2022-01-06 12:49:56 -08:00
|
|
|
if (networkError || response.status !== 200) {
|
2023-04-16 10:49:59 +02:00
|
|
|
debug('getServerIPv6: Error getting IP. %o', 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
|
|
|
|
2023-03-26 18:53:10 +02:00
|
|
|
gCache.ipv6.value = response.body.ip;
|
|
|
|
|
gCache.ipv6.timestamp = Date.now();
|
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
|
|
|
}
|