source: trip-planner-front/node_modules/default-gateway/android.js@ e29cc2e

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
Line 
1"use strict";
2
3const net = require("net");
4const execa = require("execa");
5
6const args = {
7 v4: ["-4", "r"],
8 v6: ["-6", "r"],
9};
10
11const 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
31const promise = family => {
32 return execa.stdout("ip", args[family]).then(stdout => {
33 return parse(stdout);
34 });
35};
36
37const sync = family => {
38 const result = execa.sync("ip", args[family]);
39 return parse(result.stdout);
40};
41
42module.exports.v4 = () => promise("v4");
43module.exports.v6 = () => promise("v6");
44
45module.exports.v4.sync = () => sync("v4");
46module.exports.v6.sync = () => sync("v6");
Note: See TracBrowser for help on using the repository browser.