source: trip-planner-front/node_modules/default-gateway/win32.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.7 KB
Line 
1"use strict";
2
3const execa = require("execa");
4const ipRegex = require("ip-regex");
5
6const gwArgs = "path Win32_NetworkAdapterConfiguration where IPEnabled=true get DefaultIPGateway,Index /format:table".split(" ");
7const ifArgs = "path Win32_NetworkAdapter get Index,NetConnectionID /format:table".split(" ");
8
9const parse = (gwTable, ifTable, family) => {
10 let gateway, gwid, result;
11
12 (gwTable || "").trim().split("\n").splice(1).some(line => {
13 const results = line.trim().split(/} +/) || [];
14 const gw = results[0];
15 const id = results[1];
16 gateway = (ipRegex[family]().exec((gw || "").trim()) || [])[0];
17 if (gateway) {
18 gwid = id;
19 return true;
20 }
21 });
22
23 (ifTable || "").trim().split("\n").splice(1).some(line => {
24 const i = line.indexOf(" ");
25 const id = line.substr(0, i).trim();
26 const name = line.substr(i + 1).trim();
27 if (id === gwid) {
28 result = {gateway, interface: name ? name : null};
29 return true;
30 }
31 });
32
33 if (!result) {
34 throw new Error("Unable to determine default gateway");
35 }
36
37 return result;
38};
39
40const spawnOpts = {
41 windowsHide: true,
42};
43
44const promise = family => {
45 return Promise.all([
46 execa.stdout("wmic", gwArgs, spawnOpts),
47 execa.stdout("wmic", ifArgs, spawnOpts),
48 ]).then(results => {
49 const gwTable = results[0];
50 const ifTable = results[1];
51
52 return parse(gwTable, ifTable, family);
53 });
54};
55
56const sync = family => {
57 const gwTable = execa.sync("wmic", gwArgs, spawnOpts).stdout;
58 const ifTable = execa.sync("wmic", ifArgs, spawnOpts).stdout;
59
60 return parse(gwTable, ifTable, family);
61};
62
63module.exports.v4 = () => promise("v4");
64module.exports.v6 = () => promise("v6");
65
66module.exports.v4.sync = () => sync("v4");
67module.exports.v6.sync = () => sync("v6");
Note: See TracBrowser for help on using the repository browser.