Projekt

Obecné

Profil

Stáhnout (1.74 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
"use strict";
2
3
const execa = require("execa");
4
const ipRegex = require("ip-regex");
5
6
const gwArgs = "path Win32_NetworkAdapterConfiguration where IPEnabled=true get DefaultIPGateway,Index /format:table".split(" ");
7
const ifArgs = "path Win32_NetworkAdapter get Index,NetConnectionID /format:table".split(" ");
8
9
const 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
40
const spawnOpts = {
41
  windowsHide: true,
42
};
43
44
const 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
56
const 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
63
module.exports.v4 = () => promise("v4");
64
module.exports.v6 = () => promise("v6");
65
66
module.exports.v4.sync = () => sync("v4");
67
module.exports.v6.sync = () => sync("v6");