Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b50e281
commit bb1c153
Showing
5 changed files
with
45 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,38 @@ | ||
import {networkInterfaces} from 'node:os'; | ||
import defaultGateway from 'default-gateway'; | ||
import ip from 'ipaddr.js'; | ||
import {gateway4async, gateway4sync, gateway6async, gateway6sync} from 'default-gateway'; | ||
import {contains, normalize} from 'cidr-tools'; | ||
|
||
function findIp(gateway) { | ||
const gatewayIp = ip.parse(gateway); | ||
|
||
// Look for the matching interface in all local interfaces. | ||
function findIp({gateway}) { | ||
// Look for the matching interface in all local interfaces | ||
for (const addresses of Object.values(networkInterfaces())) { | ||
for (const {cidr} of addresses) { | ||
const net = ip.parseCIDR(cidr); | ||
|
||
// eslint-disable-next-line unicorn/prefer-regexp-test | ||
if (net[0] && net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) { | ||
return net[0].toString(); | ||
if (contains(cidr, gateway)) { | ||
return normalize(cidr).split('/')[0]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
async function async(family) { | ||
export async function internalIpV6() { | ||
try { | ||
const {gateway} = await defaultGateway[family](); | ||
return findIp(gateway); | ||
return findIp((await gateway6async())); | ||
} catch {} | ||
} | ||
|
||
function sync(family) { | ||
export async function internalIpV4() { | ||
try { | ||
const {gateway} = defaultGateway[family].sync(); | ||
return findIp(gateway); | ||
return findIp((await gateway4async())); | ||
} catch {} | ||
} | ||
|
||
export async function internalIpV6() { | ||
return async('v6'); | ||
} | ||
|
||
export async function internalIpV4() { | ||
return async('v4'); | ||
} | ||
|
||
export function internalIpV6Sync() { | ||
return sync('v6'); | ||
try { | ||
return findIp(gateway6sync()); | ||
} catch {} | ||
} | ||
|
||
export function internalIpV4Sync() { | ||
return sync('v4'); | ||
try { | ||
return findIp(gateway4sync()); | ||
} catch {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
import {isIPv4, isIPv6} from 'node:net'; | ||
import process from 'node:process'; | ||
import {env} from 'node:process'; | ||
import {platform} from 'node:os'; | ||
import test from 'ava'; | ||
import {internalIpV6, internalIpV4, internalIpV6Sync, internalIpV4Sync} from './index.js'; | ||
|
||
const isCI = Boolean(process.env.CI); | ||
// Only Darwin has IPv6 on GitHub Actions | ||
const canTestV6 = env.CI ? platform() === 'darwin' : true; | ||
|
||
test('IPv6 - async', async t => { | ||
const ip = await internalIpV6(); | ||
if (isCI) { | ||
t.is(ip, undefined); | ||
if (canTestV6) { | ||
t.true(isIPv6(await internalIpV6())); | ||
} else { | ||
t.true(isIPv6(ip)); | ||
t.is(await internalIpV6(), undefined); | ||
} | ||
}); | ||
|
||
test('IPv4 - async', async t => { | ||
const ip = await internalIpV4(); | ||
t.true(isIPv4(ip)); | ||
t.true(isIPv4(await internalIpV4())); | ||
}); | ||
|
||
test('IPv6 - sync', t => { | ||
const ip = internalIpV6Sync(); | ||
if (isCI) { | ||
t.is(ip, undefined); | ||
if (canTestV6) { | ||
t.true(isIPv6(internalIpV6Sync())); | ||
} else { | ||
t.true(isIPv6(ip)); | ||
t.is(internalIpV6Sync(), undefined); | ||
} | ||
}); | ||
|
||
test('IPv4 - sync', t => { | ||
const ip = internalIpV4Sync(); | ||
t.true(isIPv4(ip)); | ||
t.true(isIPv4(internalIpV4Sync())); | ||
}); |