Socket
Socket
Sign inDemoInstall

default-gateway

Package Overview
Dependencies
16
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 7.1.0 to 7.2.0

130

index.js

@@ -10,7 +10,2 @@ import {isIP} from "node:net";

if (plat === "linux") {
const args = {
v4: ["-4", "r"],
v6: ["-6", "r"],
};
const parse = (stdout, family) => {

@@ -27,4 +22,4 @@ for (const line of (stdout || "").trim().split("\n")) {

for (const addr of addresses || []) {
if (addr.family.substring(2) === family && isIP(addr.address)) {
return {gateway: addr.address, int: (iface ?? null)};
if (Number(family.substring(3)) === family && isIP(addr.address)) {
return {gateway: addr.address, version: family, int: (iface ?? null)};
}

@@ -38,3 +33,3 @@ }

promise = async family => {
const {stdout} = await execa("ip", args[family]);
const {stdout} = await execa("ip", [`-${family}`, "r"]);
return parse(stdout, family);

@@ -44,11 +39,6 @@ };

sync = family => {
const {stdout} = execaSync("ip", args[family]);
const {stdout} = execaSync("ip", [`-${family}`, "r"]);
return parse(stdout, family);
};
} else if (plat === "darwin") {
const args = {
v4: ["-rn", "-f", "inet"],
v6: ["-rn", "-f", "inet6"],
};
// The IPv4 gateway is in column 3 in Darwin 19 (macOS 10.15 Catalina) and higher,

@@ -63,5 +53,5 @@ // previously it was in column 5

const gateway = results[1];
const iface = results[family === "v4" ? v4IfaceColumn : 3];
const iface = results[family === 4 ? v4IfaceColumn : 3];
if (dests.has(target) && gateway && isIP(gateway)) {
return {gateway, int: (iface ?? null)};
return {gateway, version: family, int: (iface ?? null)};
}

@@ -73,3 +63,3 @@ }

promise = async family => {
const {stdout} = await execa("netstat", args[family]);
const {stdout} = await execa("netstat", ["-rn", "-f", family === 4 ? "inet" : "inet6"]);
return parse(stdout, family);

@@ -79,3 +69,3 @@ };

sync = family => {
const {stdout} = execaSync("netstat", args[family]);
const {stdout} = execaSync("netstat", ["-rn", "-f", family === 4 ? "inet" : "inet6"]);
return parse(stdout, family);

@@ -108,3 +98,3 @@ };

for (const [index, gateway] of Object.entries(gateways)) {
if (!gateway || `v${isIP(gateway)}` !== family) continue;
if (!gateway || isIP(gateway) !== family) continue;

@@ -131,3 +121,3 @@ const metric = parseInt(gatewayCosts[index]) + parseInt(ipMetric);

for (const addr of addrs) {
if (addr && addr.mac && addr.mac.toLowerCase() === mac) {
if (addr?.mac?.toLowerCase() === mac) {
return osname;

@@ -151,3 +141,3 @@ }

return {gateway, int: name ?? null};
return {gateway, version: family, int: name ?? null};
};

@@ -166,15 +156,10 @@

return {gateway, int: name ?? null};
return {gateway, version: family, int: name ?? null};
};
} else if (plat === "android") {
const args = {
v4: ["-4", "r"],
v6: ["-6", "r"],
};
const parse = stdout => {
const parse = (stdout, family) => {
for (const line of (stdout || "").trim().split("\n")) {
const [_, gateway, iface] = /default via (.+?) dev (.+?)( |$)/.exec(line) || [];
if (gateway && isIP(gateway)) {
return {gateway, int: (iface ?? null)};
return {gateway, version: family, int: (iface ?? null)};
}

@@ -186,21 +171,16 @@ }

promise = async family => {
const {stdout} = await execa("ip", args[family]);
return parse(stdout);
const {stdout} = await execa("ip", [`-${family}`, "r"]);
return parse(stdout, family);
};
sync = family => {
const {stdout} = execaSync("ip", args[family]);
return parse(stdout);
const {stdout} = execaSync("ip", [`-${family}`, "r"]);
return parse(stdout, family);
};
} else if (plat === "freebsd") {
const args = {
v4: ["-rn", "-f", "inet"],
v6: ["-rn", "-f", "inet6"],
};
const parse = stdout => {
const parse = (stdout, family) => {
for (const line of (stdout || "").trim().split("\n")) {
const [target, gateway, _, iface] = line.split(/ +/) || [];
if (dests.has(target) && gateway && isIP(gateway)) {
return {gateway, int: (iface ?? null)};
return {gateway, version: family, int: (iface ?? null)};
}

@@ -212,20 +192,15 @@ }

promise = async family => {
const {stdout} = await execa("netstat", args[family]);
return parse(stdout);
const {stdout} = await execa("netstat", ["-rn", "-f", family === 4 ? "inet" : "inet6"]);
return parse(stdout, family);
};
sync = family => {
const {stdout} = execaSync("netstat", args[family]);
return parse(stdout);
const {stdout} = execaSync("netstat", ["-rn", "-f", family === 4 ? "inet" : "inet6"]);
return parse(stdout, family);
};
} else if (plat === "aix" && type() === "OS400") {
const args = {
v4: "IPV4",
v6: "IPV6",
};
const db2util = "/QOpenSys/pkgs/bin/db2util";
const sql = "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT' and CONNECTION_TYPE=?";
const parse = stdout => {
const parse = (stdout, family) => {
try {

@@ -235,3 +210,3 @@ const resultObj = JSON.parse(stdout);

const iface = resultObj.records[0].LOCAL_BINDING_INTERFACE;
return {gateway, iface};
return {gateway, version: family, iface};
} catch {}

@@ -242,17 +217,12 @@ throw new Error("Unable to determine default gateway");

promise = async family => {
const {stdout} = await execa(db2util, [sql, "-p", args[family], "-o", "json"]);
return parse(stdout);
const {stdout} = await execa(db2util, [sql, "-p", `IPV${family}`, "-o", "json"]);
return parse(stdout, family);
};
sync = family => {
const {stdout} = execaSync(db2util, [sql, "-p", args[family], "-o", "json"]);
return parse(stdout);
const {stdout} = execaSync(db2util, [sql, "-p", `IPV${family}`, "-o", "json"]);
return parse(stdout, family);
};
} else if (plat === "openbsd") {
const args = {
v4: ["-rn", "-f", "inet"],
v6: ["-rn", "-f", "inet6"],
};
const parse = stdout => {
const parse = (stdout, family) => {
for (const line of (stdout || "").trim().split("\n")) {

@@ -264,3 +234,3 @@ const results = line.split(/ +/) || [];

if (dests.has(target) && gateway && isIP(gateway)) {
return {gateway, int: (iface ?? null)};
return {gateway, version: family, int: (iface ?? null)};
}

@@ -272,18 +242,12 @@ }

promise = async family => {
const {stdout} = await execa("netstat", args[family]);
return parse(stdout);
const {stdout} = await execa("netstat", ["-rn", "-f", family === 4 ? "inet" : "inet6"]);
return parse(stdout, family);
};
sync = family => {
const {stdout} = execaSync("netstat", args[family]);
return parse(stdout);
const {stdout} = execaSync("netstat", ["-rn", "-f", family === 4 ? "inet" : "inet6"]);
return parse(stdout, family);
};
} else if (plat === "sunos" || (plat === "aix" && type() !== "OS400")) {
// AIX `netstat` output is compatible with Solaris
const args = {
v4: ["-rn", "-f", "inet"],
v6: ["-rn", "-f", "inet6"],
};
const parse = stdout => {
} else if (plat === "sunos" || (plat === "aix" && type() !== "OS400")) { // AIX `netstat` output is compatible with Solaris
const parse = (stdout, family) => {
for (const line of (stdout || "").trim().split("\n")) {

@@ -295,3 +259,3 @@ const results = line.split(/ +/) || [];

if (dests.has(target) && gateway && isIP(gateway)) {
return {gateway, int: (iface ?? null)};
return {gateway, version: family, int: (iface ?? null)};
}

@@ -303,9 +267,9 @@ }

promise = async family => {
const {stdout} = await execa("netstat", args[family]);
return parse(stdout);
const {stdout} = await execa("netstat", ["-rn", "-f", family === 4 ? "inet" : "inet6"]);
return parse(stdout, family);
};
sync = family => {
const {stdout} = execaSync("netstat", args[family]);
return parse(stdout);
const {stdout} = execaSync("netstat", ["-rn", "-f", family === 4 ? "inet" : "inet6"]);
return parse(stdout, family);
};

@@ -317,6 +281,6 @@ } else {

export const gateway4async = () => promise("v4");
export const gateway6async = () => promise("v6");
export const gateway4sync = () => sync("v4");
export const gateway6sync = () => sync("v6");
export const gateway4async = () => promise(4);
export const gateway6async = () => promise(6);
export const gateway4sync = () => sync(4);
export const gateway6sync = () => sync(6);

@@ -323,0 +287,0 @@ export default {

{
"name": "default-gateway",
"version": "7.1.0",
"version": "7.2.0",
"description": "Get the default network gateway, cross-platform.",

@@ -5,0 +5,0 @@ "author": "silverwind",

@@ -7,5 +7,5 @@ # default-gateway

- On Linux and Android, the `ip` command must be available (usually provided by the `iproute2` package).
- On Unix (and macOS), the `netstat` command must be available.
- On Windows, `wmic` must be available.
- On IBM i, the `db2util` command must be available (provided by the `db2util` package).
- On Unix (and macOS), the `netstat` command must be available.

@@ -17,13 +17,13 @@ ## Usage

const {gateway, int} = await gateway4async();
// gateway = '1.2.3.4', int = 'en1'
const {gateway, version, int} = await gateway4async();
// gateway = '1.2.3.4', version = 4, int = 'en1'
const {gateway, int} = await gateway6async();
// gateway = '2001:db8::1', int = 'en2'
const {gateway, version, int} = await gateway6async();
// gateway = '2001:db8::1', version = 6,int = 'en2'
const {gateway, int} = gateway4sync();
// gateway = '1.2.3.4', int = 'en1'
const {gateway, version, int} = gateway4sync();
// gateway = '1.2.3.4', version = 4, int = 'en1'
const {gateway, int} = gateway6sync();
// gateway = '2001:db8::1', int = 'en2'
const {gateway, version, int} = gateway6sync();
// gateway = '2001:db8::1', version = 6, int = 'en2'
```

@@ -38,4 +38,5 @@

Returns: `result` *Object*
- `gateway`: The IP address of the default gateway.
- `int`: The name of the interface. On Windows, this is the network adapter name.
- `gateway` *String*: The IP address of the default gateway.
- `version` *Number*: The IP address version of `gateway`.
- `int` *String*: The name of the interface. On Windows, this is the network adapter name.

@@ -42,0 +43,0 @@ The `gateway` property will always be defined on success, while `int` can be `null` if it cannot be determined. All methods reject/throw on unexpected conditions.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc