win32.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. const execa = require("execa");
  3. const os = require("os");
  4. const net = require("net");
  5. const gwArgs = "path Win32_NetworkAdapterConfiguration where IPEnabled=true get DefaultIPGateway,GatewayCostMetric,IPConnectionMetric,Index /format:table".split(" ");
  6. const ifArgs = index => `path Win32_NetworkAdapter where Index=${index} get NetConnectionID,MACAddress /format:table`.split(" ");
  7. const spawnOpts = {
  8. windowsHide: true,
  9. };
  10. // Parsing tables like this. The final metric is GatewayCostMetric + IPConnectionMetric
  11. //
  12. // DefaultIPGateway GatewayCostMetric Index IPConnectionMetric
  13. // {"1.2.3.4", "2001:db8::1"} {0, 256} 12 25
  14. // {"2.3.4.5"} {25} 12 55
  15. function parseGwTable(gwTable, family) {
  16. let [bestGw, bestMetric, bestId] = [null, null, null];
  17. for (let line of (gwTable || "").trim().split(/\r?\n/).splice(1)) {
  18. line = line.trim();
  19. const [_, gwArr, gwCostsArr, id, ipMetric] = /({.+?}) +?({.+?}) +?([0-9]+) +?([0-9]+)/g.exec(line) || [];
  20. if (!gwArr) continue;
  21. const gateways = (gwArr.match(/"(.+?)"/g) || []).map(match => match.substring(1, match.length - 1));
  22. const gatewayCosts = (gwCostsArr.match(/[0-9]+/g) || []);
  23. for (const [index, gateway] of Object.entries(gateways)) {
  24. if (!gateway || `v${net.isIP(gateway)}` !== family) continue;
  25. const metric = parseInt(gatewayCosts[index]) + parseInt(ipMetric);
  26. if (!bestGw || metric < bestMetric) {
  27. [bestGw, bestMetric, bestId] = [gateway, metric, id];
  28. }
  29. }
  30. }
  31. if (bestGw) return [bestGw, bestId];
  32. }
  33. function parseIfTable(ifTable) {
  34. const line = (ifTable || "").trim().split("\n")[1];
  35. let [mac, name] = line.trim().split(/\s+/);
  36. mac = mac.toLowerCase();
  37. // try to get the interface name by matching the mac to os.networkInterfaces to avoid wmic's encoding issues
  38. // https://github.com/silverwind/default-gateway/issues/14
  39. for (const [osname, addrs] of Object.entries(os.networkInterfaces())) {
  40. for (const addr of addrs) {
  41. if (addr && addr.mac && addr.mac.toLowerCase() === mac) {
  42. return osname;
  43. }
  44. }
  45. }
  46. return name;
  47. }
  48. const promise = async family => {
  49. const {stdout} = await execa("wmic", gwArgs, spawnOpts);
  50. const [gateway, id] = parseGwTable(stdout, family) || [];
  51. if (!gateway) {
  52. throw new Error("Unable to determine default gateway");
  53. }
  54. let name;
  55. if (id) {
  56. const {stdout} = await execa("wmic", ifArgs(id), spawnOpts);
  57. name = parseIfTable(stdout);
  58. }
  59. return {gateway, interface: name ? name : null};
  60. };
  61. const sync = family => {
  62. const {stdout} = execa.sync("wmic", gwArgs, spawnOpts);
  63. const [gateway, id] = parseGwTable(stdout, family) || [];
  64. if (!gateway) {
  65. throw new Error("Unable to determine default gateway");
  66. }
  67. let name;
  68. if (id) {
  69. const {stdout} = execa.sync("wmic", ifArgs(id), spawnOpts);
  70. name = parseIfTable(stdout);
  71. }
  72. return {gateway, interface: name ? name : null};
  73. };
  74. module.exports.v4 = () => promise("v4");
  75. module.exports.v6 = () => promise("v6");
  76. module.exports.v4.sync = () => sync("v4");
  77. module.exports.v6.sync = () => sync("v6");