Socket
Socket
Sign inDemoInstall

default-gateway

Package Overview
Dependencies
17
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.0 to 2.0.1

49

darwin.js
"use strict";
const net = require("net");
const exec = require("child_process").exec;
const execa = require("execa");
const dests = ["default", "0.0.0.0", "0.0.0.0/0", "::", "::/0"];
const get = (cmd, family) => {
return new Promise(function(resolve, reject) {
exec(cmd, function(err, stdout) {
if (err) return reject(err);
(stdout || "").trim().split("\n").some(line => {
let target, gateway, _flags, _ref, _use, iface;
if (family === "v4") {
[target, gateway, _flags, _ref, _use, iface] = line.split(/ +/) || [];
} else {
[target, gateway, _flags, iface] = line.split(/ +/) || [];
}
if (dests.includes(target) && gateway && net.isIP(gateway)) {
resolve({gateway: gateway, interface: (iface ? iface : null)});
return true;
}
});
reject(new Error("Unable to determine default gateway"));
const args = {
v4: ["-rn", "-f", "inet"],
v6: ["-rn", "-f", "inet6"],
};
const get = family => {
return execa.stdout("netstat", args[family]).then(stdout => {
let result;
(stdout || "").trim().split("\n").some(line => {
let target, gateway, _flags, _ref, _use, iface;
if (family === "v4") {
[target, gateway, _flags, _ref, _use, iface] = line.split(/ +/) || [];
} else {
[target, gateway, _flags, iface] = line.split(/ +/) || [];
}
if (dests.includes(target) && gateway && net.isIP(gateway)) {
result = {gateway: gateway, interface: (iface ? iface : null)};
return true;
}
});
if (!result) {
throw new Error("Unable to determine default gateway");
}
return result;
});
};
module.exports.v4 = () => get("netstat -rn -f inet", "v4");
module.exports.v6 = () => get("netstat -rn -f inet6", "v6");
module.exports.v4 = () => get("v4");
module.exports.v6 = () => get("v6");

@@ -5,8 +5,8 @@ "use strict";

if (/^linux|darwin|win32/.test(platform)) {
module.exports.v4 = () => require("./" + platform).v4();
module.exports.v6 = () => require("./" + platform).v6();
if (["linux", "darwin", "win32"].includes(platform)) {
module.exports.v4 = () => require(`./${platform}`).v4();
module.exports.v6 = () => require(`./${platform}`).v6();
} else {
module.exports.v4 = () => { throw new Error("Unsupported Platform: " + platform); };
module.exports.v6 = () => { throw new Error("Unsupported Platform: " + platform); };
module.exports.v4 = () => { throw new Error(`Unsupported Platform: ${platform}`); };
module.exports.v6 = () => { throw new Error(`Unsupported Platform: ${platform}`); };
}
"use strict";
const net = require("net");
const exec = require("child_process").exec;
const execa = require("execa");
const get = cmd => {
return new Promise(function(resolve, reject) {
exec(cmd, function(err, stdout) {
if (err) return reject(err);
(stdout || "").trim().split("\n").some(line => {
const [_, gateway, iface] = /default via (.+?) dev (.+?)( |$)/.exec(line) || [];
if (gateway && net.isIP(gateway)) {
resolve({gateway: gateway, interface: (iface ? iface : null)});
return true;
}
});
reject(new Error("Unable to determine default gateway"));
const args = {
v4: ["-4", "r"],
v6: ["-6", "r"],
};
const get = family => {
return execa.stdout("ip", args[family]).then(stdout => {
let result;
(stdout || "").trim().split("\n").some(line => {
const [_, gateway, iface] = /default via (.+?) dev (.+?)( |$)/.exec(line) || [];
if (gateway && net.isIP(gateway)) {
result = {gateway: gateway, interface: (iface ? iface : null)};
return true;
}
});
if (!result) {
throw new Error("Unable to determine default gateway");
}
return result;
});
};
module.exports.v4 = () => get("ip -4 r");
module.exports.v6 = () => get("ip -6 r");
module.exports.v4 = () => get("v4");
module.exports.v6 = () => get("v6");
{
"name": "default-gateway",
"version": "2.0.0",
"version": "2.0.1",
"description": "Get the default network gateway, cross-platform.",

@@ -15,2 +15,3 @@ "author": "silverwind <me@silverwind.io>",

"dependencies": {
"execa": "^0.7.0",
"ip-regex": "^2.1.0"

@@ -17,0 +18,0 @@ },

# default-gateway
[![](https://img.shields.io/npm/v/default-gateway.svg?style=flat)](https://www.npmjs.org/package/default-gateway) [![](https://img.shields.io/npm/dm/default-gateway.svg)](https://www.npmjs.org/package/default-gateway) [![](https://api.travis-ci.org/silverwind/default-gateway.svg?style=flat)](https://travis-ci.org/silverwind/default-gateway)
> Get the default network gateway, cross-platform.

@@ -8,6 +9,9 @@

## Installation
```
$ npm install --save default-gateway
$ npm install default-gateway
```
## Example
```js

@@ -26,10 +30,14 @@ const defaultGateway = require('default-gateway');

## API
### default-gateway.v4()
### defaultGateway.v4()
Returns a promise that resolves to a object containing the IPv4 `gateway` and `interface`. If it succeeds, `gateway` will always be defined, while `interface` can be `null` if it cannot be determined. Rejects when the gateway cannot be determined.
### default-gateway.v6()
### defaultGateway.v6()
Returns a promise that resolves to a object containing the IPv6 `gateway` and `interface`. If it succeeds, `gateway` will always be defined, while `interface` can be `null` if it cannot be determined. Rejects when the gateway cannot be determined.
## License
© [silverwind](https://github.com/silverwind), distributed under BSD licence
"use strict";
const exec = require("child_process").exec;
const execa = require("execa");
const ipRegex = require("ip-regex");
const gwCmd = "wmic path Win32_NetworkAdapterConfiguration where IPEnabled=true get DefaultIPGateway,Index /format:table";
const ifCmd = "wmic path Win32_NetworkAdapter get Index,NetConnectionID /format:table";
const gwArgs = "path Win32_NetworkAdapterConfiguration where IPEnabled=true get DefaultIPGateway,Index /format:table".split(" ");
const ifArgs = "path Win32_NetworkAdapter get Index,NetConnectionID /format:table".split(" ");
function wmic(proto) {
return new Promise(function(resolve, reject) {
let gateway, gwid;
exec(gwCmd, function(err, gwTable) {
if (err) return reject(err);
exec(ifCmd, function(err, ifTable) {
if (err) return reject(err);
(gwTable || "").trim().split("\n").splice(1).some(function(line) {
const [gw, id] = line.trim().split(/} +/);
gateway = (ipRegex[proto]().exec((gw || "").trim()) || [])[0];
if (gateway) {
gwid = id;
return true;
}
});
(ifTable || "").trim().split("\n").splice(1).some(function(line) {
const i = line.indexOf(" ");
const id = line.substr(0, i).trim();
const name = line.substr(i + 1).trim();
if (id === gwid) {
resolve({gateway: gateway, interface: name ? name : null});
return true;
}
});
reject(new Error("Unable to determine default gateway"));
});
function wmic(family) {
let gateway, gwid, result;
return Promise.all([
execa.stdout("wmic", gwArgs),
execa.stdout("wmic", ifArgs),
]).then(results => {
const [gwTable, ifTable] = results;
(gwTable || "").trim().split("\n").splice(1).some(line => {
const [gw, id] = line.trim().split(/} +/);
gateway = (ipRegex[family]().exec((gw || "").trim()) || [])[0];
if (gateway) {
gwid = id;
return true;
}
});
(ifTable || "").trim().split("\n").splice(1).some(line => {
const i = line.indexOf(" ");
const id = line.substr(0, i).trim();
const name = line.substr(i + 1).trim();
if (id === gwid) {
result = {gateway: gateway, interface: name ? name : null};
return true;
}
});
if (!result) {
throw new Error("Unable to determine default gateway");
}
return result;
});

@@ -37,0 +43,0 @@ }

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