default-gateway
Advanced tools
Comparing version 5.0.2 to 5.0.3
@@ -29,11 +29,10 @@ "use strict"; | ||
const promise = family => { | ||
return execa.stdout("ip", args[family]).then(stdout => { | ||
return parse(stdout); | ||
}); | ||
const promise = async family => { | ||
const {stdout} = await execa("ip", args[family]); | ||
return parse(stdout, family); | ||
}; | ||
const sync = family => { | ||
const result = execa.sync("ip", args[family]); | ||
return parse(result.stdout); | ||
const {stdout} = execa.sync("ip", args[family]); | ||
return parse(stdout); | ||
}; | ||
@@ -40,0 +39,0 @@ |
@@ -33,11 +33,10 @@ "use strict"; | ||
const promise = family => { | ||
return execa.stdout("netstat", args[family]).then(stdout => { | ||
return parse(stdout, family); | ||
}); | ||
const promise = async family => { | ||
const {stdout} = await execa("netstat", args[family]); | ||
return parse(stdout, family); | ||
}; | ||
const sync = family => { | ||
const result = execa.sync("netstat", args[family]); | ||
return parse(result.stdout, family); | ||
const {stdout} = execa.sync("netstat", args[family]); | ||
return parse(stdout, family); | ||
}; | ||
@@ -44,0 +43,0 @@ |
@@ -30,11 +30,10 @@ "use strict"; | ||
const promise = family => { | ||
return execa.stdout("netstat", args[family]).then(stdout => { | ||
return parse(stdout); | ||
}); | ||
const promise = async family => { | ||
const {stdout} = await execa("netstat", args[family]); | ||
return parse(stdout); | ||
}; | ||
const sync = family => { | ||
const result = execa.sync("netstat", args[family]); | ||
return parse(result.stdout); | ||
const {stdout} = execa.sync("netstat", args[family]); | ||
return parse(stdout); | ||
}; | ||
@@ -41,0 +40,0 @@ |
@@ -22,4 +22,5 @@ "use strict"; | ||
const promise = family => { | ||
return execa.stdout(db2util, [sql, "-p", family, "-o", "json"]).then(stdout => parse(stdout)); | ||
const promise = async family => { | ||
const {stdout} = await execa(db2util, [sql, "-p", family, "-o", "json"]); | ||
return parse(stdout); | ||
}; | ||
@@ -26,0 +27,0 @@ |
11
linux.js
@@ -43,11 +43,10 @@ "use strict"; | ||
const promise = family => { | ||
return execa.stdout("ip", args[family]).then(stdout => { | ||
return parse(stdout, family); | ||
}); | ||
const promise = async family => { | ||
const {stdout} = await execa("ip", args[family]); | ||
return parse(stdout, family); | ||
}; | ||
const sync = family => { | ||
const result = execa.sync("ip", args[family]); | ||
return parse(result.stdout, family); | ||
const {stdout} = execa.sync("ip", args[family]); | ||
return parse(stdout, family); | ||
}; | ||
@@ -54,0 +53,0 @@ |
@@ -33,11 +33,10 @@ "use strict"; | ||
const promise = family => { | ||
return execa.stdout("netstat", args[family]).then(stdout => { | ||
return parse(stdout); | ||
}); | ||
const promise = async family => { | ||
const {stdout} = await execa("netstat", args[family]); | ||
return parse(stdout); | ||
}; | ||
const sync = family => { | ||
const result = execa.sync("netstat", args[family]); | ||
return parse(result.stdout); | ||
const {stdout} = execa.sync("netstat", args[family]); | ||
return parse(stdout); | ||
}; | ||
@@ -44,0 +43,0 @@ |
{ | ||
"name": "default-gateway", | ||
"version": "5.0.2", | ||
"version": "5.0.3", | ||
"description": "Get the default network gateway, cross-platform.", | ||
@@ -15,10 +15,9 @@ "author": "silverwind <me@silverwind.io>", | ||
"dependencies": { | ||
"execa": "^1.0.0", | ||
"ip-regex": "^2.1.0" | ||
"execa": "^2.0.3" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^5.16.0", | ||
"eslint-config-silverwind": "^3.0.3", | ||
"updates": "^8.1.0", | ||
"ver": "5.0.1" | ||
"eslint": "^6.1.0", | ||
"eslint-config-silverwind": "^4.0.3", | ||
"updates": "^8.5.1", | ||
"ver": "5.1.2" | ||
}, | ||
@@ -25,0 +24,0 @@ "files": [ |
11
sunos.js
@@ -33,11 +33,10 @@ "use strict"; | ||
const promise = family => { | ||
return execa.stdout("netstat", args[family]).then(stdout => { | ||
return parse(stdout); | ||
}); | ||
const promise = async family => { | ||
const {stdout} = await execa("netstat", args[family]); | ||
return parse(stdout); | ||
}; | ||
const sync = family => { | ||
const result = execa.sync("netstat", args[family]); | ||
return parse(result.stdout); | ||
const {stdout} = execa.sync("netstat", args[family]); | ||
return parse(stdout); | ||
}; | ||
@@ -44,0 +43,0 @@ |
51
win32.js
"use strict"; | ||
const execa = require("execa"); | ||
const ipRegex = require("ip-regex"); | ||
const os = require("os"); | ||
const net = require("net"); | ||
const gwArgs = "path Win32_NetworkAdapterConfiguration where IPEnabled=true get DefaultIPGateway,Index /format:table".split(" "); | ||
const gwArgs = "path Win32_NetworkAdapterConfiguration where IPEnabled=true get DefaultIPGateway,GatewayCostMetric,IPConnectionMetric,Index /format:table".split(" "); | ||
const ifArgs = index => `path Win32_NetworkAdapter where Index=${index} get NetConnectionID,MACAddress /format:table`.split(" "); | ||
@@ -14,8 +14,29 @@ | ||
// Parsing tables like this. The final metric is GatewayCostMetric + IPConnectionMetric | ||
// | ||
// DefaultIPGateway GatewayCostMetric Index IPConnectionMetric | ||
// {"1.2.3.4", "2001:db8::1"} {0, 256} 12 25 | ||
// {"2.3.4.5"} {25} 12 55 | ||
function parseGwTable(gwTable, family) { | ||
for (const line of (gwTable || "").trim().split("\n").splice(1)) { | ||
const [gw, id] = line.trim().split(/} +/) || []; | ||
const gateway = (ipRegex[family]().exec((gw || "").trim()) || [])[0]; | ||
if (gateway) return [gateway, id]; | ||
let [bestGw, bestMetric, bestId] = [null, null, null]; | ||
for (let line of (gwTable || "").trim().split(/\r?\n/).splice(1)) { | ||
line = line.trim(); | ||
const [_, gwArr, gwCostsArr, id, ipMetric] = /({.+?}) +?({.+?}) +?([0-9]+) +?([0-9]+)/g.exec(line) || []; | ||
if (!gwArr) continue; | ||
const gateways = (gwArr.match(/"(.+?)"/g) || []).map(match => match.substring(1, match.length - 1)); | ||
const gatewayCosts = (gwCostsArr.match(/[0-9]+/g) || []); | ||
for (const [index, gateway] of Object.entries(gateways)) { | ||
if (!gateway || `v${net.isIP(gateway)}` !== family) continue; | ||
const metric = parseInt(gatewayCosts[index]) + parseInt(ipMetric); | ||
if (!bestGw || metric < bestMetric) { | ||
[bestGw, bestMetric, bestId] = [gateway, metric, id]; | ||
} | ||
} | ||
} | ||
if (bestGw) return [bestGw, bestId]; | ||
} | ||
@@ -33,3 +54,3 @@ | ||
for (const addr of addrs) { | ||
if (addr && addr.mac === mac) { | ||
if (addr && addr.mac && addr.mac.toLowerCase() === mac) { | ||
return osname; | ||
@@ -43,4 +64,4 @@ } | ||
const promise = async family => { | ||
const gwTable = await execa.stdout("wmic", gwArgs, spawnOpts); | ||
const [gateway, id] = parseGwTable(gwTable, family) || []; | ||
const {stdout} = await execa("wmic", gwArgs, spawnOpts); | ||
const [gateway, id] = parseGwTable(stdout, family) || []; | ||
@@ -53,4 +74,4 @@ if (!gateway) { | ||
if (id) { | ||
const ifTable = await execa.stdout("wmic", ifArgs(id), spawnOpts); | ||
name = parseIfTable(ifTable); | ||
const {stdout} = await execa("wmic", ifArgs(id), spawnOpts); | ||
name = parseIfTable(stdout); | ||
} | ||
@@ -62,4 +83,4 @@ | ||
const sync = family => { | ||
const gwTable = execa.sync("wmic", gwArgs, spawnOpts).stdout; | ||
const [gateway, id] = parseGwTable(gwTable, family) || []; | ||
const {stdout} = execa.sync("wmic", gwArgs, spawnOpts); | ||
const [gateway, id] = parseGwTable(stdout, family) || []; | ||
@@ -72,4 +93,4 @@ if (!gateway) { | ||
if (id) { | ||
const ifTable = execa.sync("wmic", ifArgs(id), spawnOpts).stdout; | ||
name = parseIfTable(ifTable); | ||
const {stdout} = execa.sync("wmic", ifArgs(id), spawnOpts); | ||
name = parseIfTable(stdout); | ||
} | ||
@@ -76,0 +97,0 @@ |
15976
1
353
7
+ Addedcross-spawn@7.0.3(transitive)
+ Addedexeca@2.1.0(transitive)
+ Addedget-stream@5.2.0(transitive)
+ Addedis-stream@2.0.1(transitive)
+ Addedmerge-stream@2.0.0(transitive)
+ Addedmimic-fn@2.1.0(transitive)
+ Addednpm-run-path@3.1.0(transitive)
+ Addedonetime@5.1.2(transitive)
+ Addedp-finally@2.0.1(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedstrip-final-newline@2.0.0(transitive)
+ Addedwhich@2.0.2(transitive)
- Removedip-regex@^2.1.0
- Removedcross-spawn@6.0.5(transitive)
- Removedexeca@1.0.0(transitive)
- Removedget-stream@4.1.0(transitive)
- Removedip-regex@2.1.0(transitive)
- Removedis-stream@1.1.0(transitive)
- Removednice-try@1.0.5(transitive)
- Removednpm-run-path@2.0.2(transitive)
- Removedp-finally@1.0.0(transitive)
- Removedpath-key@2.0.1(transitive)
- Removedsemver@5.7.2(transitive)
- Removedshebang-command@1.2.0(transitive)
- Removedshebang-regex@1.0.0(transitive)
- Removedstrip-eof@1.0.0(transitive)
- Removedwhich@1.3.1(transitive)
Updatedexeca@^2.0.3