default-gateway
Advanced tools
Comparing version 5.0.0 to 5.0.1
{ | ||
"name": "default-gateway", | ||
"version": "5.0.0", | ||
"version": "5.0.1", | ||
"description": "Get the default network gateway, cross-platform.", | ||
@@ -5,0 +5,0 @@ "author": "silverwind <me@silverwind.io>", |
87
win32.js
@@ -5,56 +5,69 @@ "use strict"; | ||
const ipRegex = require("ip-regex"); | ||
const os = require("os"); | ||
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(" "); | ||
const ifArgs = index => `path Win32_NetworkAdapter where Index=${index} get NetConnectionID,MACAddress /format:table`.split(" "); | ||
const parse = (gwTable, ifTable, family) => { | ||
let gateway, gwid, result; | ||
const spawnOpts = { | ||
windowsHide: true, | ||
}; | ||
(gwTable || "").trim().split("\n").splice(1).some(line => { | ||
function parseGwTable(gwTable, family) { | ||
for (const line of (gwTable || "").trim().split("\n").splice(1)) { | ||
const [gw, id] = line.trim().split(/} +/) || []; | ||
gateway = (ipRegex[family]().exec((gw || "").trim()) || [])[0]; | ||
if (gateway) { | ||
gwid = id; | ||
return true; | ||
} | ||
}); | ||
const gateway = (ipRegex[family]().exec((gw || "").trim()) || [])[0]; | ||
if (gateway) return [gateway, id]; | ||
} | ||
} | ||
(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, interface: name ? name : null}; | ||
return true; | ||
function parseIfTable(ifTable) { | ||
const line = (ifTable || "").trim().split("\n")[1]; | ||
let [mac, name] = line.trim().split(/\s+/); | ||
mac = mac.toLowerCase(); | ||
// try to get the interface name by matching the mac to os.networkInterfaces to avoid wmic's encoding issues | ||
// https://github.com/silverwind/default-gateway/issues/14 | ||
for (const [osname, addrs] of Object.entries(os.networkInterfaces())) { | ||
for (const addr of addrs) { | ||
if (addr && addr.mac === mac) { | ||
return osname; | ||
} | ||
} | ||
}); | ||
} | ||
return name; | ||
} | ||
if (!result) { | ||
const promise = async family => { | ||
const gwTable = await execa.stdout("wmic", gwArgs, spawnOpts); | ||
const [gateway, id] = parseGwTable(gwTable, family) || []; | ||
if (!gateway) { | ||
throw new Error("Unable to determine default gateway"); | ||
} | ||
return result; | ||
}; | ||
let name; | ||
if (id) { | ||
const ifTable = execa.sync("wmic", ifArgs(id), spawnOpts).stdout; | ||
name = parseIfTable(ifTable); | ||
} | ||
const spawnOpts = { | ||
windowsHide: true, | ||
return {gateway, interface: name ? name : null}; | ||
}; | ||
const promise = family => { | ||
return Promise.all([ | ||
execa.stdout("wmic", gwArgs, spawnOpts), | ||
execa.stdout("wmic", ifArgs, spawnOpts), | ||
]).then(results => { | ||
const gwTable = results[0]; | ||
const ifTable = results[1]; | ||
return parse(gwTable, ifTable, family); | ||
}); | ||
}; | ||
const sync = family => { | ||
const gwTable = execa.sync("wmic", gwArgs, spawnOpts).stdout; | ||
const ifTable = execa.sync("wmic", ifArgs, spawnOpts).stdout; | ||
const [gateway, id] = parseGwTable(gwTable, family) || []; | ||
return parse(gwTable, ifTable, family); | ||
if (!gateway) { | ||
throw new Error("Unable to determine default gateway"); | ||
} | ||
let name; | ||
if (id) { | ||
const ifTable = execa.sync("wmic", ifArgs(id), spawnOpts).stdout; | ||
name = parseIfTable(ifTable); | ||
} | ||
return {gateway, interface: name ? name : null}; | ||
}; | ||
@@ -61,0 +74,0 @@ |
15166
342