summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2017-09-30 09:16:12 +0200
committersilverwind <me@silverwind.io>2017-09-30 09:16:15 +0200
commit17a54afa1c962151e0f0cc39f16aabbd7566395a (patch)
tree7cc184730b861206d48417e961364d85be04b1ee
parent8a3e5afdd26d0b8ad6ba94101c71e6f27d9800d7 (diff)
downloadinternal-ip-17a54afa1c962151e0f0cc39f16aabbd7566395a.tar
internal-ip-17a54afa1c962151e0f0cc39f16aabbd7566395a.tar.gz
BREAKING: return null in place of loopback addresses, update docs
Fixes: https://github.com/sindresorhus/internal-ip/issues/15
-rw-r--r--index.js19
-rw-r--r--readme.md19
-rw-r--r--test.js28
3 files changed, 36 insertions, 30 deletions
diff --git a/index.js b/index.js
index fe94208..e320a10 100644
--- a/index.js
+++ b/index.js
@@ -3,12 +3,7 @@ const os = require('os');
const defaultGateway = require('default-gateway');
const ipaddr = require('ipaddr.js');
-const defaults = {
- v6: '::1',
- v4: '127.0.0.1'
-};
-
-function findIp(gateway, family) {
+function findIp(gateway) {
const interfaces = os.networkInterfaces();
const gatewayIp = ipaddr.parse(gateway);
let ip;
@@ -23,25 +18,25 @@ function findIp(gateway, family) {
ip = net[0].toString();
}
- return Boolean(ret);
+ return Boolean(ip);
});
});
- return ip || defaults[family];
+ return ip;
}
function promise(family) {
return defaultGateway[family]().then(result => {
- return findIp(result.gateway, family);
- }).catch(() => defaults[family]);
+ return findIp(result.gateway);
+ }).catch(() => null);
}
function sync(family) {
try {
const result = defaultGateway[family].sync();
- return findIp(result.gateway, family);
+ return findIp(result.gateway);
} catch (err) {
- return defaults[family];
+ return null;
}
}
diff --git a/readme.md b/readme.md
index 9a53a24..bb5d900 100644
--- a/readme.md
+++ b/readme.md
@@ -15,26 +15,33 @@ $ npm install internal-ip
```js
const internalIp = require('internal-ip');
-internalIp.v6().then(ip => {
+internalIp.v4().then(ip => {
console.log(ip);
- //=> 'fe80::1'
+ //=> '10.0.0.79'
});
-internalIp.v4().then(ip => {
+internalIp.v6().then(ip => {
console.log(ip);
- //=> '10.0.0.79'
+ //=> 'fe80::1'
});
+
+console.log(internalIp.v4().sync())
+//=> '10.0.0.79'
+
+console.log(internalIp.v6().sync())
+//=> 'fe80::1'
```
-The module relies on tools provided by most operating systems. One notable exception may be the `ip` command which is used on Linux. If it's missing, it can usually be installed with the `iproute2` package in your package manager.
+The module returns the address of the internet-facing interface, as determined from the default gateway. When the adress cannot be determined for any reason, `null` will be returned.
-In the case no address can be determined, `::1` or `127.0.0.1` will be returned as a fallback. If you think this is incorrect, please open an [issue](https://github.com/sindresorhus/internal-ip/issues/new).
+The module relies on operating systems tools. On Linux and Android, the `ip` command must be available, which depending on distribution might not be installed by default. It is usually provided by the `iproute2` package.
## Related
- [internal-ip-cli](https://github.com/sindresorhus/internal-ip-cli) - CLI for this module
- [public-ip](https://github.com/sindresorhus/public-ip) - Get your public IP address
+- [default-gateway](https://github.com/silverwind/default-gateway) - Get your default gateway address
## License
diff --git a/test.js b/test.js
index a1d7ff6..c762d76 100644
--- a/test.js
+++ b/test.js
@@ -2,18 +2,22 @@ import {isIPv4, isIPv6} from 'net';
import test from 'ava';
import m from '.';
-test('IPv6', async t => {
- t.true(isIPv6(await m.v6()));
-});
+// Skip tests on Travis as their VMs don't have IPs on their interfaces
+// https://docs.travis-ci.com/user/ci-environment/#Networking
+if (!process.env.CI) {
+ test('IPv6', async t => {
+ t.true(isIPv6(await m.v6()));
+ });
-test('IPv4', async t => {
- t.true(isIPv4(await m.v4()));
-});
+ test('IPv4', async t => {
+ t.true(isIPv4(await m.v4()));
+ });
-test('synchronous IPv6', t => {
- t.true(isIPv6(m.v6.sync()));
-});
+ test('synchronous IPv6', t => {
+ t.true(isIPv6(m.v6.sync()));
+ });
-test('synchronous IPv4', t => {
- t.true(isIPv4(m.v4.sync()));
-});
+ test('synchronous IPv4', t => {
+ t.true(isIPv4(m.v4.sync()));
+ });
+}