PUT /files/util/ifconfig.js HTTP/1.1
Host: 127.0.0.1
Accept-Encoding: identity
Content-Length: 1299
Content-MD5: fSTWsZFTTtDV7JWh02jRoA==
Expect: 100-continue
Date: Thu, 08 Dec 2016 13:02:37 GMT
User-Agent: aws-cli/1.11.26 Python/2.7.10 Darwin/16.1.0 botocore/1.4.83
Content-Type: application/javascript
Authorization: AWS 123:5x9q5xPgF57li64vss6UCay2dLU=

'use strict';

var _ = require('lodash');
var os = require('os');

module.exports = {
    get_main_external_ipv4: get_main_external_ipv4,
    get_external_ipv4: get_external_ipv4,
};


/**
 *
 * get_main_external_ipv4
 *
 * return main external IP of this OS.
 *
 * @return single string
 *
 */
function get_main_external_ipv4() {
    var ips = get_external_ipv4();
    if (!_.isArray(ips)) {
        return ips;
    }
    // if multiple external IPs we use an incredible algorithm
    // to find the main external ip, and it is ... to pick the first.
    // this is how AI is done in real life, bitches.
    return ips[0];
}


/**
 *
 * get_external_ipv4
 *
 * return external IPs of this OS.
 *
 * @return if only one external IP then return single string, otherwise array of strings.
 *
 */
function get_external_ipv4() {
    var ips;
    _.each(os.networkInterfaces(), function(ifcs, name) {
        _.each(ifcs, function(ifc) {
            if (ifc.internal || !ifc.address || ifc.family !== 'IPv4') {
                return;
            }
            if (!ips) {
                ips = ifc.address;
            } else if (!_.isArray(ips)) {
                ips = [ips, ifc.address];
            } else {
                ips.push(ifc.address);
            }
        });
    });
    return ips;
}
