summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Powell <shellscape@users.noreply.github.com>2017-09-30 03:00:23 -0400
committersilverwind <me@silverwind.io>2017-09-30 09:00:23 +0200
commit009f2606dae85182ac84d12b11b3da2cea26d15d (patch)
tree128c4904c2f6ebaf6f5374d3160ce67b1637e596
parent5d250d2c052322267040eabebdbd0c8c6580270d (diff)
downloadinternal-ip-009f2606dae85182ac84d12b11b3da2cea26d15d.tar
internal-ip-009f2606dae85182ac84d12b11b3da2cea26d15d.tar.gz
Reimplement a synchronous method - Fixes #13
* reimplement a synchronous method * match -> findIp, catching error in sync
-rw-r--r--index.js58
-rw-r--r--package.json2
-rw-r--r--test.js8
3 files changed, 46 insertions, 22 deletions
diff --git a/index.js b/index.js
index 97f3131..2e7b805 100644
--- a/index.js
+++ b/index.js
@@ -8,29 +8,45 @@ const defaults = {
v4: '127.0.0.1'
};
-function internalIp(family) {
- return defaultGateway[family]().then(result => {
- const interfaces = os.networkInterfaces();
- const gatewayIp = ipaddr.parse(result.gateway);
- let ret;
-
- // Look for the matching interface in all local interfaces
- Object.keys(interfaces).some(name => {
- return interfaces[name].some(addr => {
- const prefix = ipaddr.parse(addr.netmask).prefixLengthFromSubnetMask();
- const net = ipaddr.parseCIDR(`${addr.address}/${prefix}`);
-
- if (net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) {
- ret = net[0].toString();
- }
-
- return Boolean(ret);
- });
+function findIp(gateway, family) {
+ const interfaces = os.networkInterfaces();
+ const gatewayIp = ipaddr.parse(gateway);
+ let ret;
+
+ // Look for the matching interface in all local interfaces
+ Object.keys(interfaces).some(name => {
+ return interfaces[name].some(addr => {
+ const prefix = ipaddr.parse(addr.netmask).prefixLengthFromSubnetMask();
+ const net = ipaddr.parseCIDR(`${addr.address}/${prefix}`);
+
+ if (net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) {
+ ret = net[0].toString();
+ }
+
+ return Boolean(ret);
});
+ });
+
+ return ret ? ret : defaults[family];
+}
- return ret ? ret : defaults[family];
+function promise(family) {
+ return defaultGateway[family]().then(result => {
+ return findIp(result.gateway, family);
}).catch(() => defaults[family]);
}
-module.exports.v6 = () => internalIp('v6');
-module.exports.v4 = () => internalIp('v4');
+function sync(family) {
+ try {
+ const result = defaultGateway[family].sync();
+ return findIp(result.gateway, family);
+ } catch (err) {
+ return defaults[family];
+ }
+}
+
+module.exports.v6 = () => promise('v6');
+module.exports.v4 = () => promise('v4');
+
+module.exports.v6.sync = () => sync('v6');
+module.exports.v4.sync = () => sync('v4');
diff --git a/package.json b/package.json
index 9fe0bb5..090f133 100644
--- a/package.json
+++ b/package.json
@@ -31,7 +31,7 @@
"gateway"
],
"dependencies": {
- "default-gateway": "^2.2.2",
+ "default-gateway": "^2.6.0",
"ipaddr.js": "^1.5.2"
},
"devDependencies": {
diff --git a/test.js b/test.js
index c8e70ce..a1d7ff6 100644
--- a/test.js
+++ b/test.js
@@ -9,3 +9,11 @@ test('IPv6', async t => {
test('IPv4', async t => {
t.true(isIPv4(await m.v4()));
});
+
+test('synchronous IPv6', t => {
+ t.true(isIPv6(m.v6.sync()));
+});
+
+test('synchronous IPv4', t => {
+ t.true(isIPv4(m.v4.sync()));
+});