MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/chartapi.imenso.co/node_modules/internal-ip/index.js
'use strict';
const defaultGateway = require('default-gateway');
const {networkInterfaces} = require('os');
const {parse, parseCIDR} = require('ipaddr.js');

function findIp(gateway) {
	const gatewayIp = parse(gateway);

	// Look for the matching interface in all local interfaces.
	for (const addresses of Object.values(networkInterfaces())) {
		for (const {cidr} of addresses) {
			const net = parseCIDR(cidr);

			if (net[0] && net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) {
				return net[0].toString();
			}
		}
	}
}

async function promise(family) {
	try {
		const {gateway} = await defaultGateway[family]();
		return findIp(gateway);
	} catch {}
}

function sync(family) {
	try {
		const {gateway} = defaultGateway[family].sync();
		return findIp(gateway);
	} catch {}
}

const internalIp = {};
internalIp.v6 = () => promise('v6');
internalIp.v4 = () => promise('v4');
internalIp.v6.sync = () => sync('v6');
internalIp.v4.sync = () => sync('v4');

module.exports = internalIp;