fold sysinfo into network
the backends are network backends
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getIPv4,
|
||||
getIPv6,
|
||||
testIPv4Config,
|
||||
testIPv6Config
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
net = require('net');
|
||||
|
||||
async function getIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
return config.ipv4;
|
||||
}
|
||||
|
||||
async function getIPv6(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if ('ipv6' in config) return config.ipv6;
|
||||
|
||||
throw new BoxError(BoxError.NETWORK_ERROR, 'No IPv6 configured');
|
||||
}
|
||||
|
||||
async function testIPv4Config(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (typeof config.ipv4 !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ipv4 must be a string');
|
||||
if (!net.isIPv4(config.ipv4)) return new BoxError(BoxError.BAD_FIELD, 'invalid IPv4');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function testIPv6Config(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (typeof config.ipv6 !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ipv6 must be a string');
|
||||
if (!net.isIPv6(config.ipv6)) return new BoxError(BoxError.BAD_FIELD, 'invalid IPv6');
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
// -------------------------------------------
|
||||
// This file just describes the interface
|
||||
//
|
||||
// New backends can start from here
|
||||
// -------------------------------------------
|
||||
|
||||
exports = module.exports = {
|
||||
getIPv4,
|
||||
getIPv6,
|
||||
testIPv4Config,
|
||||
testIPv6Config
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
BoxError = require('../boxerror.js');
|
||||
|
||||
async function getIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
throw new BoxError(BoxError.NOT_IMPLEMENTED, 'getIPv4 is not implemented');
|
||||
}
|
||||
|
||||
async function getIPv6(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
throw new BoxError(BoxError.NOT_IMPLEMENTED, 'getIPv6 is not implemented');
|
||||
}
|
||||
|
||||
async function testIPv4Config(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function testIPv6Config(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getIPv4,
|
||||
getIPv6,
|
||||
testIPv4Config,
|
||||
testIPv6Config
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
debug = require('debug')('box:network/network-interface'),
|
||||
os = require('os'),
|
||||
safe = require('safetydance');
|
||||
|
||||
async function getIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
const ifaces = os.networkInterfaces();
|
||||
const iface = ifaces[config.ifname]; // array of addresses
|
||||
if (!iface) throw new BoxError(BoxError.NETWORK_ERROR, `No interface named ${config.ifname}`);
|
||||
|
||||
const addresses = iface.filter(i => i.family === 'IPv4').map(i => i.address);
|
||||
if (addresses.length === 0) throw new BoxError(BoxError.NETWORK_ERROR, `${config.ifname} does not have any IPv4 address`);
|
||||
if (addresses.length > 1) debug(`${config.ifname} has multiple ipv4 - ${JSON.stringify(addresses)}. choosing the first one.`);
|
||||
|
||||
return addresses[0];
|
||||
}
|
||||
|
||||
async function getIPv6(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
const ifaces = os.networkInterfaces();
|
||||
const iface = ifaces[config.ifname]; // array of addresses
|
||||
if (!iface) throw new BoxError(BoxError.NETWORK_ERROR, `No interface named ${config.ifname}`);
|
||||
|
||||
const addresses = iface.filter(i => i.family === 'IPv6').map(i => i.address);
|
||||
if (addresses.length === 0) throw new BoxError(BoxError.NETWORK_ERROR, `${config.ifname} does not have any IPv6 address`);
|
||||
if (addresses.length > 1) debug(`${config.ifname} has multiple ipv6 - ${JSON.stringify(addresses)}. choosing the first one.`);
|
||||
|
||||
return addresses[0];
|
||||
}
|
||||
|
||||
async function testIPv4Config(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (typeof config.ifname !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ifname is not a string');
|
||||
|
||||
const [error] = await safe(getIPv4(config));
|
||||
return error || null;
|
||||
}
|
||||
|
||||
async function testIPv6Config(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
if (typeof config.ifname !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ifname is not a string');
|
||||
|
||||
const [error] = await safe(getIPv6(config));
|
||||
return error || null;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getIPv4,
|
||||
getIPv6,
|
||||
testIPv4Config,
|
||||
testIPv6Config
|
||||
};
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
async function getIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function getIPv6(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
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