Sha256: 06675849c52983c09fe331096f8b3e070c1a4b4e99000e7628dd6240d28918f3

Contents?: true

Size: 997 Bytes

Versions: 26

Compression:

Stored size: 997 Bytes

Contents

"use strict";

const net = require("net");
const execa = require("execa");

const args = {
  v4: ["-4", "r"],
  v6: ["-6", "r"],
};

const parse = stdout => {
  let result;

  (stdout || "").trim().split("\n").some(line => {
    const results = /default via (.+?) dev (.+?)( |$)/.exec(line) || [];
    const gateway = results[1];
    const iface = results[2];
    if (gateway && net.isIP(gateway)) {
      result = {gateway, interface: (iface ? iface : null)};
      return true;
    }
  });

  if (!result) {
    throw new Error("Unable to determine default gateway");
  }

  return result;
};

const promise = family => {
  return execa.stdout("ip", args[family]).then(stdout => {
    return parse(stdout);
  });
};

const sync = family => {
  const result = execa.sync("ip", args[family]);
  return parse(result.stdout);
};

module.exports.v4 = () => promise("v4");
module.exports.v6 = () => promise("v6");

module.exports.v4.sync = () => sync("v4");
module.exports.v6.sync = () => sync("v6");

Version data entries

26 entries across 25 versions & 8 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/default-gateway/android.js
disco_app-0.18.0 test/dummy/node_modules/default-gateway/android.js
disco_app-0.18.2 test/dummy/node_modules/default-gateway/android.js
disco_app-0.16.1 test/dummy/node_modules/default-gateway/android.js
disco_app-0.15.2 test/dummy/node_modules/default-gateway/android.js
disco_app-0.18.4 test/dummy/node_modules/default-gateway/android.js
disco_app-0.18.1 test/dummy/node_modules/default-gateway/android.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/default-gateway/android.js
disco_app-0.14.0 test/dummy/node_modules/default-gateway/android.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/default-gateway/android.js
tang-0.2.1 spec/tang_app/node_modules/default-gateway/android.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/default-gateway/android.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/default-gateway/android.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/default-gateway/android.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/default-gateway/android.js
tang-0.2.0 spec/tang_app/node_modules/default-gateway/android.js
tang-0.1.0 spec/tang_app/node_modules/default-gateway/android.js
tang-0.0.9 spec/tang_app/node_modules/default-gateway/android.js
enju_library-0.3.8 spec/dummy/node_modules/default-gateway/android.js
jester-data-8.0.0 node_modules/default-gateway/android.js