fold sysinfo into network
the backends are network backends
This commit is contained in:
96
src/network/generic.js
Normal file
96
src/network/generic.js
Normal file
@@ -0,0 +1,96 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getIPv4,
|
||||
getIPv6,
|
||||
testIPv4Config,
|
||||
testIPv6Config
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
debug = require('debug')('box:network/generic'),
|
||||
safe = require('safetydance'),
|
||||
superagent = require('superagent');
|
||||
|
||||
const gCache = { ipv4: {}, ipv6: {} }; // each has { timestamp, value, request }
|
||||
|
||||
async function getIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (process.env.BOX_ENV === 'test') return '127.0.0.1';
|
||||
|
||||
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('getIPv4: 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;
|
||||
|
||||
const [networkError, response] = await safe(request);
|
||||
gCache.ipv4.request = null;
|
||||
|
||||
if (networkError || response.status !== 200) {
|
||||
debug('getIPv4: Error getting IP. %o', networkError);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IPv4. API server (ipv4.api.cloudron.io) unreachable');
|
||||
}
|
||||
|
||||
if (!response.body && !response.body.ip) {
|
||||
debug('getIPv4: 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');
|
||||
}
|
||||
|
||||
gCache.ipv4.value = response.body.ip;
|
||||
gCache.ipv4.timestamp = Date.now();
|
||||
return response.body.ip;
|
||||
}
|
||||
|
||||
async function getIPv6(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (process.env.BOX_ENV === 'test') return '::1';
|
||||
|
||||
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('getIPv6: 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;
|
||||
|
||||
const [networkError, response] = await safe(request);
|
||||
gCache.ipv6.request = null;
|
||||
|
||||
if (networkError || response.status !== 200) {
|
||||
debug('getIPv6: Error getting IP. %o', networkError);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IPv6. API server (ipv6.api.cloudron.io) unreachable');
|
||||
}
|
||||
|
||||
if (!response.body && !response.body.ip) {
|
||||
debug('getIPv6: 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');
|
||||
}
|
||||
|
||||
gCache.ipv6.value = response.body.ip;
|
||||
gCache.ipv6.timestamp = Date.now();
|
||||
return response.body.ip;
|
||||
}
|
||||
|
||||
async function testIPv4Config(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function testIPv6Config(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user