2016-01-05 12:10:25 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2017-02-23 22:03:44 -08:00
|
|
|
getPublicIp: getPublicIp
|
2016-01-05 12:10:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2016-09-02 15:53:51 +02:00
|
|
|
superagent = require('superagent'),
|
2016-09-05 18:00:43 +02:00
|
|
|
safe = require('safetydance'),
|
2016-01-05 12:10:25 +01:00
|
|
|
SysInfoError = require('../sysinfo.js').SysInfoError;
|
|
|
|
|
|
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-09-06 13:33:12 -07:00
|
|
|
if (process.env.BOX_ENV === 'test') return callback(null, '127.0.0.1');
|
|
|
|
|
|
2016-09-12 12:53:51 -07:00
|
|
|
superagent.get('http://169.254.169.254/metadata/v1.json').timeout(30 * 1000).end(function (error, result) {
|
2016-09-02 15:53:51 +02:00
|
|
|
if (error || result.statusCode !== 200) {
|
|
|
|
|
console.error('Error getting metadata', error);
|
|
|
|
|
return callback(new SysInfoError(SysInfoError.INTERNAL_ERROR, 'No IP found'));
|
2016-01-05 12:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
2016-09-15 22:14:21 -07:00
|
|
|
// Note that we do not use a floating IP for 3 reasons:
|
|
|
|
|
// The PTR record is not set to floating IP, the outbound interface is not changeable to floating IP
|
|
|
|
|
// and there are reports that port 25 on floating IP is blocked.
|
|
|
|
|
var ip = safe.query(result.body, 'interfaces.public[0].ipv4.ip_address');
|
2016-09-05 18:00:43 +02:00
|
|
|
if (!ip) return callback(new SysInfoError(SysInfoError.INTERNAL_ERROR, 'No IP found'));
|
2016-09-02 15:53:51 +02:00
|
|
|
|
2016-09-05 18:00:43 +02:00
|
|
|
callback(null, ip);
|
2016-09-02 15:53:51 +02:00
|
|
|
});
|
2016-01-05 12:10:25 +01:00
|
|
|
}
|