Skip to content

Commit 8c730a2

Browse files
committed
switch to execa - fixes #2
1 parent 11284a4 commit 8c730a2

File tree

5 files changed

+87
-68
lines changed

5 files changed

+87
-68
lines changed

darwin.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
"use strict";
22

33
const net = require("net");
4-
const exec = require("child_process").exec;
4+
const execa = require("execa");
55
const dests = ["default", "0.0.0.0", "0.0.0.0/0", "::", "::/0"];
66

7-
const get = (cmd, family) => {
8-
return new Promise((resolve, reject) => {
9-
exec(cmd, (err, stdout) => {
10-
if (err) return reject(err);
11-
(stdout || "").trim().split("\n").some(line => {
12-
let target, gateway, _flags, _ref, _use, iface;
13-
if (family === "v4") {
14-
[target, gateway, _flags, _ref, _use, iface] = line.split(/ +/) || [];
15-
} else {
16-
[target, gateway, _flags, iface] = line.split(/ +/) || [];
17-
}
18-
if (dests.includes(target) && gateway && net.isIP(gateway)) {
19-
resolve({gateway: gateway, interface: (iface ? iface : null)});
20-
return true;
21-
}
22-
});
23-
reject(new Error("Unable to determine default gateway"));
7+
const args = {
8+
v4: ["-rn", "-f", "inet"],
9+
v6: ["-rn", "-f", "inet6"],
10+
};
11+
12+
const get = family => {
13+
return execa.stdout("netstat", args[family]).then(stdout => {
14+
let result;
15+
16+
(stdout || "").trim().split("\n").some(line => {
17+
let target, gateway, _flags, _ref, _use, iface;
18+
if (family === "v4") {
19+
[target, gateway, _flags, _ref, _use, iface] = line.split(/ +/) || [];
20+
} else {
21+
[target, gateway, _flags, iface] = line.split(/ +/) || [];
22+
}
23+
if (dests.includes(target) && gateway && net.isIP(gateway)) {
24+
result = {gateway: gateway, interface: (iface ? iface : null)};
25+
return true;
26+
}
2427
});
28+
29+
if (!result) {
30+
throw new Error("Unable to determine default gateway");
31+
}
32+
33+
return result;
2534
});
2635
};
2736

28-
module.exports.v4 = () => get("netstat -rn -f inet", "v4");
29-
module.exports.v6 = () => get("netstat -rn -f inet6", "v6");
37+
module.exports.v4 = () => get("v4");
38+
module.exports.v6 = () => get("v6");

linux.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
"use strict";
22

33
const net = require("net");
4-
const exec = require("child_process").exec;
4+
const execa = require("execa");
55

6-
const get = cmd => {
7-
return new Promise((resolve, reject) => {
8-
exec(cmd, (err, stdout) => {
9-
if (err) return reject(err);
10-
(stdout || "").trim().split("\n").some(line => {
11-
const [_, gateway, iface] = /default via (.+?) dev (.+?)( |$)/.exec(line) || [];
12-
if (gateway && net.isIP(gateway)) {
13-
resolve({gateway: gateway, interface: (iface ? iface : null)});
14-
return true;
15-
}
16-
});
17-
reject(new Error("Unable to determine default gateway"));
6+
const args = {
7+
v4: ["-4", "-r"],
8+
v6: ["-6", "-r"],
9+
};
10+
11+
const get = family => {
12+
return execa.stdout("ip", args[family]).then(stdout => {
13+
let result;
14+
15+
(stdout || "").trim().split("\n").some(line => {
16+
const [_, gateway, iface] = /default via (.+?) dev (.+?)( |$)/.exec(line) || [];
17+
if (gateway && net.isIP(gateway)) {
18+
result = {gateway: gateway, interface: (iface ? iface : null)};
19+
return true;
20+
}
1821
});
22+
23+
if (!result) {
24+
throw new Error("Unable to determine default gateway");
25+
}
26+
27+
return result;
1928
});
2029
};
2130

22-
module.exports.v4 = () => get("ip -4 r");
23-
module.exports.v6 = () => get("ip -6 r");
31+
module.exports.v4 = () => get("v4");
32+
module.exports.v6 = () => get("v6");

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"node": ">=6"
1313
},
1414
"dependencies": {
15+
"execa": "^0.7.0",
1516
"ip-regex": "^2.1.0"
1617
},
1718
"os": [

test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"use strict";
22

3-
// travis VMs don't have IPs on their interfaces
4-
// https://docs.travis-ci.com/user/ci-environment/#Networking
5-
if (process.env.CI) return;
6-
73
const assert = require("assert");
84
const net = require("net");
95
const defaultGateway = require(".");
106

11-
defaultGateway.v4().then(result => {
12-
assert(net.isIPv4(result.gateway));
7+
Promise.all([
8+
defaultGateway.v4(),
9+
defaultGateway.v6(),
10+
]).then(results => {
11+
assert(net.isIPv4(results[0].gateway));
12+
assert(net.isIPv6(results[1].gateway));
1313
}).catch(err => {
1414
console.error(err.stack);
1515
process.exit(1);

win32.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
"use strict";
22

3-
const exec = require("child_process").exec;
3+
const execa = require("execa");
44
const ipRegex = require("ip-regex");
55

66
const gwCmd = "wmic path Win32_NetworkAdapterConfiguration where IPEnabled=true get DefaultIPGateway,Index /format:table";
77
const ifCmd = "wmic path Win32_NetworkAdapter get Index,NetConnectionID /format:table";
88

9-
function wmic(proto) {
10-
return new Promise((resolve, reject) => {
11-
let gateway, gwid;
12-
exec(gwCmd, (err, gwTable) => {
13-
if (err) return reject(err);
14-
exec(ifCmd, (err, ifTable) => {
15-
if (err) return reject(err);
16-
(gwTable || "").trim().split("\n").splice(1).some(line => {
17-
const [gw, id] = line.trim().split(/} +/);
18-
gateway = (ipRegex[proto]().exec((gw || "").trim()) || [])[0];
19-
if (gateway) {
20-
gwid = id;
21-
return true;
22-
}
23-
});
24-
(ifTable || "").trim().split("\n").splice(1).some(line => {
25-
const i = line.indexOf(" ");
26-
const id = line.substr(0, i).trim();
27-
const name = line.substr(i + 1).trim();
28-
if (id === gwid) {
29-
resolve({gateway: gateway, interface: name ? name : null});
30-
return true;
31-
}
32-
});
33-
reject(new Error("Unable to determine default gateway"));
34-
});
9+
function wmic(family) {
10+
let gateway, gwid, result;
11+
return execa.stdout(gwCmd, gwTable => {
12+
(gwTable || "").trim().split("\n").splice(1).some(line => {
13+
const [gw, id] = line.trim().split(/} +/);
14+
gateway = (ipRegex[family]().exec((gw || "").trim()) || [])[0];
15+
if (gateway) {
16+
gwid = id;
17+
return true;
18+
}
3519
});
20+
}).then(execa.stdout(ifCmd, ifTable => {
21+
(ifTable || "").trim().split("\n").splice(1).some(line => {
22+
const i = line.indexOf(" ");
23+
const id = line.substr(0, i).trim();
24+
const name = line.substr(i + 1).trim();
25+
if (id === gwid) {
26+
result = {gateway: gateway, interface: name ? name : null};
27+
return true;
28+
}
29+
});
30+
})).then(() => {
31+
if (!result) {
32+
throw new Error("Unable to determine default gateway");
33+
}
34+
35+
return result;
3636
});
3737
}
3838

0 commit comments

Comments
 (0)