2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2016-01-05 12:10:25 +01:00
|
|
|
SysInfoError: SysInfoError,
|
|
|
|
|
|
2019-07-25 11:26:53 -07:00
|
|
|
getPublicIp: getPublicIp,
|
|
|
|
|
|
2019-07-26 07:25:40 -07:00
|
|
|
hasIPv6: hasIPv6,
|
|
|
|
|
provider: provider
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
2016-04-19 08:23:54 -07:00
|
|
|
var assert = require('assert'),
|
|
|
|
|
ec2 = require('./sysinfo/ec2.js'),
|
2019-07-25 11:26:53 -07:00
|
|
|
fs = require('fs'),
|
2016-10-08 16:40:58 -07:00
|
|
|
generic = require('./sysinfo/generic.js'),
|
2019-07-26 07:25:40 -07:00
|
|
|
paths = require('./paths.js'),
|
2016-10-11 19:41:57 -07:00
|
|
|
scaleway = require('./sysinfo/scaleway.js'),
|
2019-07-26 07:25:40 -07:00
|
|
|
safe = require('safetydance'),
|
2016-04-19 08:23:54 -07:00
|
|
|
util = require('util');
|
|
|
|
|
|
2016-01-05 12:10:25 +01:00
|
|
|
function SysInfoError(reason, errorOrMessage) {
|
|
|
|
|
assert.strictEqual(typeof reason, 'string');
|
|
|
|
|
assert(errorOrMessage instanceof Error || typeof errorOrMessage === 'string' || typeof errorOrMessage === 'undefined');
|
|
|
|
|
|
|
|
|
|
Error.call(this);
|
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
|
|
|
|
|
|
this.name = this.constructor.name;
|
|
|
|
|
this.reason = reason;
|
|
|
|
|
if (typeof errorOrMessage === 'undefined') {
|
|
|
|
|
this.message = reason;
|
|
|
|
|
} else if (typeof errorOrMessage === 'string') {
|
|
|
|
|
this.message = errorOrMessage;
|
|
|
|
|
} else {
|
|
|
|
|
this.message = 'Internal error';
|
|
|
|
|
this.nestedError = errorOrMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
util.inherits(SysInfoError, Error);
|
|
|
|
|
SysInfoError.INTERNAL_ERROR = 'Internal Error';
|
2019-01-24 14:58:04 -08:00
|
|
|
SysInfoError.EXTERNAL_ERROR = 'External Error';
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-07-26 07:25:40 -07:00
|
|
|
let gProvider = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function provider() {
|
|
|
|
|
if (gProvider) return gProvider;
|
|
|
|
|
|
|
|
|
|
gProvider = safe.fs.readFileSync(paths.PROVIDER_FILE, 'utf8');
|
|
|
|
|
if (!gProvider) return gProvider = 'generic';
|
|
|
|
|
|
|
|
|
|
return gProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-05 12:10:25 +01:00
|
|
|
function getApi(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-07-26 07:25:40 -07:00
|
|
|
switch (provider()) {
|
2019-01-24 14:58:04 -08:00
|
|
|
case 'ec2': return callback(null, ec2);
|
|
|
|
|
case 'lightsail': return callback(null, ec2);
|
|
|
|
|
case 'ami': return callback(null, ec2);
|
|
|
|
|
case 'scaleway': return callback(null, scaleway);
|
|
|
|
|
default: return callback(null, generic);
|
2016-01-06 14:34:23 +01:00
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2017-02-23 22:03:44 -08:00
|
|
|
function getPublicIp(callback) {
|
2016-01-05 12:10:25 +01:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-01-05 13:18:56 +01:00
|
|
|
getApi(function (error, api) {
|
2016-01-05 12:10:25 +01:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2017-02-23 22:03:44 -08:00
|
|
|
api.getPublicIp(callback);
|
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;
|
|
|
|
|
}
|