internal-ip
Advanced tools
Comparing version 1.2.0 to 2.0.0
63
index.js
'use strict'; | ||
var os = require('os'); | ||
const os = require('os'); | ||
const defaultGateway = require('default-gateway'); | ||
// TODO: Remove dependency on ip module | ||
// https://github.com/whitequark/ipaddr.js/issues/59 | ||
const ip = require('ip'); | ||
const ipaddr = require('ipaddr.js'); | ||
var type = { | ||
v4: { | ||
def: '127.0.0.1', | ||
family: 'IPv4' | ||
}, | ||
v6: { | ||
def: '::1', | ||
family: 'IPv6' | ||
} | ||
const defaults = { | ||
v6: '::1', | ||
v4: '127.0.0.1' | ||
}; | ||
function internalIp(version) { | ||
var options = type[version]; | ||
var ret = options.def; | ||
var interfaces = os.networkInterfaces(); | ||
function internalIp(family) { | ||
return defaultGateway[family]().then(result => { | ||
const interfaces = os.networkInterfaces(); | ||
let ret; | ||
Object.keys(interfaces).forEach(function (el) { | ||
interfaces[el].forEach(function (el2) { | ||
if (!el2.internal && el2.family === options.family) { | ||
ret = el2.address; | ||
} | ||
}); | ||
}); | ||
// Remove IPv6 zone index and parse gateway address as a ipaddr.js object | ||
// https://github.com/whitequark/ipaddr.js/issues/60 | ||
const gatewayIp = ipaddr.parse(result.gateway.replace(/%.+/, '')); | ||
return ret; | ||
} | ||
// Look for the matching interface in all local interfaces | ||
Object.keys(interfaces).some(name => { | ||
return interfaces[name].some(addr => { | ||
const subnet = ip.subnet(addr.address, addr.netmask); | ||
const net = ipaddr.parseCIDR(`${addr.address}/${subnet.subnetMaskLength}`); | ||
function v4() { | ||
return internalIp('v4'); | ||
} | ||
if (net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) { | ||
ret = net[0].toString(); | ||
} | ||
function v6() { | ||
return internalIp('v6'); | ||
return Boolean(ret); | ||
}); | ||
}); | ||
return ret ? ret : defaults[family]; | ||
}).catch(() => defaults[family]); | ||
} | ||
module.exports = v4; | ||
module.exports.v4 = v4; | ||
module.exports.v6 = v6; | ||
module.exports.v6 = () => internalIp('v6'); | ||
module.exports.v4 = () => internalIp('v4'); |
{ | ||
"name": "internal-ip", | ||
"version": "1.2.0", | ||
"description": "Get your internal IPv4 or IPv6 address", | ||
"license": "MIT", | ||
"repository": "sindresorhus/internal-ip", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"bin": "cli.js", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"cli.js" | ||
], | ||
"keywords": [ | ||
"cli-app", | ||
"cli", | ||
"bin", | ||
"ip", | ||
"ipv4", | ||
"ipv6", | ||
"address", | ||
"internal", | ||
"local", | ||
"machine" | ||
], | ||
"dependencies": { | ||
"meow": "^3.3.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"is-ip": "^1.0.0", | ||
"xo": "*" | ||
} | ||
"name": "internal-ip", | ||
"version": "2.0.0", | ||
"description": "Get your internal IP address", | ||
"license": "MIT", | ||
"repository": "sindresorhus/internal-ip", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"ip", | ||
"ipv6", | ||
"ipv4", | ||
"address", | ||
"internal", | ||
"local", | ||
"machine", | ||
"system", | ||
"net", | ||
"gateway" | ||
], | ||
"dependencies": { | ||
"default-gateway": "^2.0.0", | ||
"ip": "^1.1.5", | ||
"ipaddr.js": "^1.4.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
} |
# internal-ip [![Build Status](https://travis-ci.org/sindresorhus/internal-ip.svg?branch=master)](https://travis-ci.org/sindresorhus/internal-ip) | ||
> Get your internal IPv4 or IPv6 address | ||
> Get your internal IP address | ||
## CLI | ||
## Install | ||
``` | ||
$ npm install --global internal-ip | ||
$ npm install internal-ip | ||
``` | ||
``` | ||
$ internal-ip --help | ||
Usage | ||
$ internal-ip | ||
## Usage | ||
Options | ||
-4, --ipv4 Return the IPv4 address (default) | ||
-6, --ipv6 Return the IPv6 address | ||
```js | ||
const internalIp = require('internal-ip'); | ||
Example | ||
$ internal-ip | ||
192.168.0.123 | ||
$ internal-ip -6 | ||
fe80::200:f8ff:fe21:67cf | ||
``` | ||
internalIp.v6().then(ip => { | ||
console.log(ip); | ||
//=> 'fe80::1' | ||
}); | ||
## API | ||
internalIp.v4().then(ip => { | ||
console.log(ip); | ||
//=> '10.0.0.79' | ||
}); | ||
``` | ||
$ npm install --save internal-ip | ||
``` | ||
```js | ||
var internalIp = require('internal-ip'); | ||
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). | ||
internalIp.v4(); | ||
//=> '192.168.0.123' | ||
internalIp.v6(); | ||
//=> 'fe80::200:f8ff:fe21:67cf' | ||
``` | ||
## Related | ||
See [public-ip](https://github.com/sindresorhus/public-ip) or [ipify](https://github.com/sindresorhus/ipify) to get your external IP address. | ||
- [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 | ||
@@ -54,2 +40,2 @@ | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
3881
3
4
34
41
+ Addeddefault-gateway@^2.0.0
+ Addedip@^1.1.5
+ Addedipaddr.js@^1.4.0
+ Addedcross-spawn@6.0.5(transitive)
+ Addeddefault-gateway@2.7.2(transitive)
+ Addedexeca@0.10.0(transitive)
+ Addedget-stream@3.0.0(transitive)
+ Addedip@1.1.9(transitive)
+ Addedip-regex@2.1.0(transitive)
+ Addedipaddr.js@1.9.1(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addednice-try@1.0.5(transitive)
+ Addednpm-run-path@2.0.2(transitive)
+ Addedp-finally@1.0.0(transitive)
+ Addedpath-key@2.0.1(transitive)
+ Addedshebang-command@1.2.0(transitive)
+ Addedshebang-regex@1.0.0(transitive)
+ Addedstrip-eof@1.0.0(transitive)
+ Addedwhich@1.3.1(transitive)
- Removedmeow@^3.3.0
- Removedarray-find-index@1.0.2(transitive)
- Removedcamelcase@2.1.1(transitive)
- Removedcamelcase-keys@2.1.0(transitive)
- Removedcurrently-unhandled@0.4.1(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removederror-ex@1.3.2(transitive)
- Removedfind-up@1.1.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-stdin@4.0.1(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhosted-git-info@2.8.9(transitive)
- Removedindent-string@2.1.0(transitive)
- Removedis-arrayish@0.2.1(transitive)
- Removedis-core-module@2.15.1(transitive)
- Removedis-finite@1.1.0(transitive)
- Removedis-utf8@0.2.1(transitive)
- Removedload-json-file@1.1.0(transitive)
- Removedloud-rejection@1.6.0(transitive)
- Removedmap-obj@1.0.1(transitive)
- Removedmeow@3.7.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removednormalize-package-data@2.5.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedparse-json@2.2.0(transitive)
- Removedpath-exists@2.1.0(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedpath-type@1.1.0(transitive)
- Removedpify@2.3.0(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedread-pkg@1.1.0(transitive)
- Removedread-pkg-up@1.0.1(transitive)
- Removedredent@1.0.0(transitive)
- Removedrepeating@2.0.1(transitive)
- Removedresolve@1.22.8(transitive)
- Removedspdx-correct@3.2.0(transitive)
- Removedspdx-exceptions@2.5.0(transitive)
- Removedspdx-expression-parse@3.0.1(transitive)
- Removedspdx-license-ids@3.0.20(transitive)
- Removedstrip-bom@2.0.0(transitive)
- Removedstrip-indent@1.0.1(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedtrim-newlines@1.0.0(transitive)
- Removedvalidate-npm-package-license@3.0.4(transitive)