2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2022-01-05 18:07:36 -08:00
|
|
|
getServerIPv4,
|
|
|
|
|
getServerIPv6,
|
2021-05-01 11:21:09 -07:00
|
|
|
testConfig,
|
2019-07-25 11:26:53 -07:00
|
|
|
|
2021-05-01 11:21:09 -07:00
|
|
|
hasIPv6
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
2021-08-19 13:24:38 -07:00
|
|
|
const assert = require('assert'),
|
2019-07-25 11:26:53 -07:00
|
|
|
fs = require('fs'),
|
2021-08-27 09:52:24 -07:00
|
|
|
settings = require('./settings.js');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-10-29 16:12:58 -07:00
|
|
|
function api(provider) {
|
|
|
|
|
assert.strictEqual(typeof provider, 'string');
|
2019-10-29 15:46:33 -07:00
|
|
|
|
|
|
|
|
switch (provider) {
|
2019-10-29 16:12:58 -07:00
|
|
|
case 'fixed': return require('./sysinfo/fixed.js');
|
|
|
|
|
case 'network-interface': return require('./sysinfo/network-interface.js');
|
|
|
|
|
default: return require('./sysinfo/generic.js');
|
2016-01-06 14:34:23 +01:00
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-05 18:07:36 -08:00
|
|
|
async function getServerIPv4() {
|
2021-08-27 09:52:24 -07:00
|
|
|
const config = await settings.getSysinfoConfig();
|
2016-01-05 12:10:25 +01:00
|
|
|
|
2022-01-05 18:07:36 -08:00
|
|
|
return await api(config.provider).getServerIPv4(config);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 12:22:16 -08:00
|
|
|
// returns RFC 5952 formatted address (https://datatracker.ietf.org/doc/html/rfc5952)
|
2022-01-05 18:07:36 -08:00
|
|
|
async function getServerIPv6() {
|
|
|
|
|
const config = await settings.getSysinfoConfig();
|
|
|
|
|
|
2022-01-06 12:22:16 -08:00
|
|
|
return await api(config.provider).getServerIPv6(config);
|
2016-01-05 12:10:25 +01:00
|
|
|
}
|
2019-07-25 11:26:53 -07:00
|
|
|
|
|
|
|
|
function hasIPv6() {
|
|
|
|
|
const IPV6_PROC_FILE = '/proc/net/if_inet6';
|
|
|
|
|
// on contabo, /proc/net/if_inet6 is an empty file. so just exists is not enough
|
|
|
|
|
return fs.existsSync(IPV6_PROC_FILE) && fs.readFileSync(IPV6_PROC_FILE, 'utf8').trim().length !== 0;
|
|
|
|
|
}
|
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 await api(config.provider).testConfig(config);
|
2019-10-29 20:08:45 -07:00
|
|
|
}
|