default-gateway
Advanced tools
Comparing version 2.0.0 to 2.0.1
"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"); |
10
index.js
@@ -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}`); }; | ||
} |
39
linux.js
"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 |
64
win32.js
"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 @@ } |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
102
42
2
6938
2
+ Addedexeca@^0.7.0
+ Addedcross-spawn@5.1.0(transitive)
+ Addedexeca@0.7.0(transitive)
+ Addedget-stream@3.0.0(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedlru-cache@4.1.5(transitive)
+ Addednpm-run-path@2.0.2(transitive)
+ Addedp-finally@1.0.0(transitive)
+ Addedpath-key@2.0.1(transitive)
+ Addedpseudomap@1.0.2(transitive)
+ Addedshebang-command@1.2.0(transitive)
+ Addedshebang-regex@1.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedstrip-eof@1.0.0(transitive)
+ Addedwhich@1.3.1(transitive)
+ Addedyallist@2.1.2(transitive)