Last change
on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
997 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | const net = require("net");
|
---|
| 4 | const execa = require("execa");
|
---|
| 5 |
|
---|
| 6 | const args = {
|
---|
| 7 | v4: ["-4", "r"],
|
---|
| 8 | v6: ["-6", "r"],
|
---|
| 9 | };
|
---|
| 10 |
|
---|
| 11 | const parse = stdout => {
|
---|
| 12 | let result;
|
---|
| 13 |
|
---|
| 14 | (stdout || "").trim().split("\n").some(line => {
|
---|
| 15 | const results = /default via (.+?) dev (.+?)( |$)/.exec(line) || [];
|
---|
| 16 | const gateway = results[1];
|
---|
| 17 | const iface = results[2];
|
---|
| 18 | if (gateway && net.isIP(gateway)) {
|
---|
| 19 | result = {gateway, interface: (iface ? iface : null)};
|
---|
| 20 | return true;
|
---|
| 21 | }
|
---|
| 22 | });
|
---|
| 23 |
|
---|
| 24 | if (!result) {
|
---|
| 25 | throw new Error("Unable to determine default gateway");
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | return result;
|
---|
| 29 | };
|
---|
| 30 |
|
---|
| 31 | const promise = family => {
|
---|
| 32 | return execa.stdout("ip", args[family]).then(stdout => {
|
---|
| 33 | return parse(stdout);
|
---|
| 34 | });
|
---|
| 35 | };
|
---|
| 36 |
|
---|
| 37 | const sync = family => {
|
---|
| 38 | const result = execa.sync("ip", args[family]);
|
---|
| 39 | return parse(result.stdout);
|
---|
| 40 | };
|
---|
| 41 |
|
---|
| 42 | module.exports.v4 = () => promise("v4");
|
---|
| 43 | module.exports.v6 = () => promise("v6");
|
---|
| 44 |
|
---|
| 45 | module.exports.v4.sync = () => sync("v4");
|
---|
| 46 | module.exports.v6.sync = () => sync("v6");
|
---|
Note:
See
TracBrowser
for help on using the repository browser.