"use strict";
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2020 The Board of Trustees of the Leland Stanford Junior University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getServerAddresses = exports.getServerName = void 0;
const os_1 = __importDefault(require("os"));
const dns_1 = __importDefault(require("dns"));
const ip_1 = __importDefault(require("ip"));
const util_1 = __importDefault(require("util"));
function getPublicIP() {
    // try the ipv4 address first, then the ipv6, then
    // fallback on ipv4 if both are private
    let ipv4 = ip_1.default.address('public', 'ipv4');
    if (!ip_1.default.isPrivate(ipv4))
        return ipv4;
    let ipv6 = ip_1.default.address('public', 'ipv6');
    if (!ip_1.default.isPrivate(ipv6))
        return ipv6;
    return ip_1.default.address('private', 'ipv4');
}
function getServerName() {
    // first check if the hostname resolves to a public routable IP
    // if so, we return that because it's likely to be human readable
    let hostname = os_1.default.hostname();
    return Promise.all([util_1.default.promisify(dns_1.default.resolve4)(hostname), util_1.default.promisify(dns_1.default.resolve6)(hostname)]).then((resolved4, resolved6) => {
        // in a well configured system, the hostname *always* resolves to something
        // but that something might be 127.0.0.1 or another private IP
        let public4 = resolved4.filter((a) => !ip_1.default.isPrivate(a));
        let public6 = resolved6.filter((a) => !ip_1.default.isPrivate(a));
        if (public4.length > 0 || public6.length > 0)
            return hostname;
        return getPublicIP();
    }).catch((e) => {
        // ignore dns resolution errors
        return getPublicIP();
    });
}
exports.getServerName = getServerName;
function getServerAddresses(host) {
    let ifaces = os_1.default.networkInterfaces();
    let addresses = [];
    for (let iface in ifaces) {
        ifaces[iface].forEach((_if) => {
            addresses.push(_if.address);
        });
    }
    return addresses.filter((a) => !ip_1.default.isLoopback(a) && a !== host);
}
exports.getServerAddresses = getServerAddresses;
//# sourceMappingURL=ip_address.js.map