2016-10-08 16:40:58 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2019-10-29 20:08:45 -07:00
|
|
|
getServerIp,
|
|
|
|
|
testConfig
|
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
|
|
|
promiseRetry = require('../promise-retry.js'),
|
|
|
|
|
safe = require('safetydance'),
|
2019-10-22 14:09:44 -07:00
|
|
|
superagent = require('superagent');
|
2016-10-08 16:40:58 -07:00
|
|
|
|
2021-08-27 09:52:24 -07:00
|
|
|
async function getServerIp(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';
|
|
|
|
|
|
|
|
|
|
let attempt = 0;
|
|
|
|
|
|
|
|
|
|
await promiseRetry({ times: 10, interval: 5000 }, async () => {
|
|
|
|
|
if (attempt) debug(`getServerIp: getting server IP. attempt ${attempt}`);
|
|
|
|
|
++attempt;
|
|
|
|
|
|
|
|
|
|
const [networkError, response] = await safe(superagent.get('https://api.cloudron.io/api/v1/helper/public_ip')
|
|
|
|
|
.timeout(30 * 1000)
|
|
|
|
|
.ok(() => true));
|
|
|
|
|
|
|
|
|
|
if (networkError || response.status !== 200) {
|
|
|
|
|
debug('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);
|
|
|
|
|
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IP. No IP found in response');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.body.ip;
|
2016-11-29 15:26:25 +01:00
|
|
|
});
|
|
|
|
|
}
|
2019-10-29 20:08:45 -07:00
|
|
|
|
2021-08-27 09:52:24 -07:00
|
|
|
async function testConfig(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
|
|
|
}
|