Import Upstream version 1.0.3

parents
coverage
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
pids
logs
results
node_modules
npm-debug.log
.DS_Store
.idea/
\ No newline at end of file
sudo: false
language: node_js
node_js:
- '8'
- '6'
- '4'
script: 'npm run test-travis'
after_script: 'npm i codecov && codecov'
# Ordered by date of first contribution.
# Auto-generated by 'contributors' on Wed, 05 Aug 2015 23:46:41 GMT.
# https://github.com/xingrz/node-contributors
fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
alsotang <alsotang@gmail.com> (https://github.com/alsotang)
1.0.3 / 2017-08-24
==================
**fixes**
* [[`ed491c5`](http://github.com/node-modules/address/commit/ed491c5bd353118e4e4d384f47f13c3e1cfeb80e)] - fix: ignore wrong mac address on node 8.x (#10) (fengmk2 <<fengmk2@gmail.com>>)
1.0.2 / 2017-05-26
==================
* fix: win32 get mac failed (#9)
1.0.1 / 2016-09-30
==================
* test: remove 0.12
* fix: search interface before family match
* add contributors
1.0.0 / 2015-08-06
==================
* chore: use npm scripts instead of Makefile
* add benchmark
0.0.3 / 2013-11-04
==================
* get the first not local ip when interface not exists
0.0.2 / 2013-08-08
==================
* use networkInterface() to get mac fix #3
0.0.1 / 2013-07-31
==================
* ip(), ipv6(), mac(), dns() work on osx and linux now.
* first commit
This software is licensed under the MIT License.
Copyright (C) 2013 - 2014 fengmk2 <fengmk2@gmail.com>
Copyright (C) 2015 - 2016 node-modules
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
address
=======
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Gittip][gittip-image]][gittip-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]
[npm-image]: https://img.shields.io/npm/v/address.svg?style=flat-square
[npm-url]: https://npmjs.org/package/address
[travis-image]: https://img.shields.io/travis/node-modules/address.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/address
[coveralls-image]: https://img.shields.io/coveralls/node-modules/address.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/node-modules/address?branch=master
[gittip-image]: https://img.shields.io/gittip/fengmk2.svg?style=flat-square
[gittip-url]: https://www.gittip.com/fengmk2/
[david-image]: https://img.shields.io/david/node-modules/address.svg?style=flat-square
[david-url]: https://david-dm.org/node-modules/address
[download-image]: https://img.shields.io/npm/dm/address.svg?style=flat-square
[download-url]: https://npmjs.org/package/address
Get current machine IP, MAC and DNS servers.
DNS servers receive from `/etc/resolv.conf`.
## Install
```bash
$ npm install address
```
## Usage
Get IP is sync and get MAC is async for now.
```js
var address = require('address');
// default interface 'eth' on linux, 'en' on osx.
address.ip(); // '192.168.0.2'
address.ipv6(); // 'fe80::7aca:39ff:feb0:e67d'
address.mac(function (err, addr) {
console.log(addr); // '78:ca:39:b0:e6:7d'
});
// local loopback
address.ip('lo'); // '127.0.0.1'
// vboxnet MAC
address.mac('vboxnet', function (err, addr) {
console.log(addr); // '0a:00:27:00:00:00'
});
```
### Get all addresses: IPv4, IPv6 and MAC
```js
address(function (err, addrs) {
console.log(addrs.ip, addrs.ipv6, addrs.mac);
// '192.168.0.2', 'fe80::7aca:39ff:feb0:e67d', '78:ca:39:b0:e6:7d'
});
address('vboxnet', function (err, addrs) {
console.log(addrs.ip, addrs.ipv6, addrs.mac);
// '192.168.56.1', null, '0a:00:27:00:00:00'
});
```
### Get an interface info with family
```js
address.interface('IPv4', 'eth1');
// { address: '192.168.1.1', family: 'IPv4', mac: '78:ca:39:b0:e6:7d' }
```
### Get DNS servers
```js
address.dns(function (err, addrs) {
console.log(addrs);
// ['10.13.2.1', '10.13.2.6']
});
```
## benchmark
run `$ npm run benchmark`
```
18,929 op/s » #ip
17,622 op/s » #ipv6
16,347 op/s » #mac
11,906 op/s » #dns
```
## License
[MIT](LICENSE.txt)
var Benchmark = require('benchmark');
var benchmarks = require('beautify-benchmark');
var suite = new Benchmark.Suite();
var address = require('..');
var os = require('os');
suite.add('os.platform()', function() {
os.platform();
});
suite.on('cycle', function(event) {
benchmarks.add(event.target);
});
suite.on('start', function(event) {
console.log('\n Starting...',
process.version, Date());
});
suite.on('complete', function done() {
benchmarks.log();
});
suite.run({ 'async': false });
// os.platform() x 67,436,816 ops/sec ±1.56% (84 runs sampled)
\ No newline at end of file
'use strict';
var os = require('os');
var fs = require('fs');
var child = require('child_process');
var DEFAULT_RESOLV_FILE = '/etc/resolv.conf';
function getInterfaceName() {
var val = 'eth';
var platform = os.platform();
if (platform === 'darwin') {
val = 'en';
} else if (platform === 'win32') {
val = null;
}
return val;
}
function getIfconfigCMD() {
if (os.platform() === 'win32') {
return 'ipconfig/all';
}
return '/sbin/ifconfig';
}
/**
* Get all addresses.
*
* @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
* @param {Function(err, addr)} callback
* - {Object} addr {
* - {String} ip
* - {String} ipv6
* - {String} mac
* }
*/
function address(interfaceName, callback) {
if (typeof interfaceName === 'function') {
callback = interfaceName;
interfaceName = null;
}
var addr = {
ip: address.ip(interfaceName),
ipv6: address.ipv6(interfaceName),
mac: null
};
address.mac(interfaceName, function (err, mac) {
if (mac) {
addr.mac = mac;
}
callback(err, addr);
});
}
address.interface = function (family, name) {
var interfaces = os.networkInterfaces();
var noName = !name;
name = name || getInterfaceName();
family = family || 'IPv4';
for (var i = -1; i < 8; i++) {
var interfaceName = name + (i >= 0 ? i : ''); // support 'lo' and 'lo0'
var items = interfaces[interfaceName];
if (items) {
for (var j = 0; j < items.length; j++) {
var item = items[j];
if (item.family === family) {
return item;
}
}
}
}
if (noName) {
// filter 127.0.0.1, get the first ip
for (var k in interfaces) {
var items = interfaces[k];
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (item.family === family && item.address !== '127.0.0.1') {
return item;
}
}
}
}
return;
};
/**
* Get current machine IPv4
*
* @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
* @return {String} IP address
*/
address.ip = function (interfaceName) {
var item = address.interface('IPv4', interfaceName);
return item && item.address;
};
/**
* Get current machine IPv6
*
* @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
* @return {String} IP address
*/
address.ipv6 = function (interfaceName) {
var item = address.interface('IPv6', interfaceName);
return item && item.address;
};
// osx start line 'en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500'
// linux start line 'eth0 Link encap:Ethernet HWaddr 00:16:3E:00:0A:29 '
var MAC_OSX_START_LINE = /^(\w+)\:\s+flags=/;
var MAC_LINUX_START_LINE = /^(\w+)\s{2,}link encap:\w+/i;
// ether 78:ca:39:b0:e6:7d
// HWaddr 00:16:3E:00:0A:29
var MAC_RE = address.MAC_RE = /(?:ether|HWaddr)\s+((?:[a-z0-9]{2}\:){5}[a-z0-9]{2})/i;
// osx: inet 192.168.2.104 netmask 0xffffff00 broadcast 192.168.2.255
// linux: inet addr:10.125.5.202 Bcast:10.125.15.255 Mask:255.255.240.0
var MAC_IP_RE = address.MAC_IP_RE = /inet\s(?:addr\:)?(\d+\.\d+\.\d+\.\d+)/;
function getMAC(content, interfaceName, matchIP) {
var lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trimRight();
var m = MAC_OSX_START_LINE.exec(line) || MAC_LINUX_START_LINE.exec(line);
if (!m) {
continue;
}
// check interface name
var name = m[1];
if (name.indexOf(interfaceName) !== 0) {
continue;
}
var ip = null;
var mac = null;
var match = MAC_RE.exec(line);
if (match) {
mac = match[1];
}
i++;
while (true) {
line = lines[i];
if (!line || MAC_OSX_START_LINE.exec(line) || MAC_LINUX_START_LINE.exec(line)) {
i--;
break; // hit next interface, handle next interface
}
if (!mac) {
match = MAC_RE.exec(line);
if (match) {
mac = match[1];
}
}
if (!ip) {
match = MAC_IP_RE.exec(line);
if (match) {
ip = match[1];
}
}
i++;
}
if (ip === matchIP) {
return mac;
}
}
}
/**
* Get current machine MAC address
*
* @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
* @param {Function(err, address)} callback
*/
address.mac = function (interfaceName, callback) {
if (typeof interfaceName === 'function') {
callback = interfaceName;
interfaceName = null;
}
interfaceName = interfaceName || getInterfaceName();
var item = address.interface('IPv4', interfaceName);
if (!item) {
return callback();
}
// https://github.com/nodejs/node/issues/13581
// bug in node 7.x and <= 8.4.0
if (!process.env.CI && (item.mac === 'ff:00:00:00:00:00' || item.mac === '00:00:00:00:00:00')) {
// wrong address, ignore it
item.mac = '';
}
if (item.mac) {
return callback(null, item.mac);
}
child.exec(getIfconfigCMD(), {timeout: 5000}, function (err, stdout, stderr) {
if (err || !stdout) {
return callback(err);
}
var mac = getMAC(stdout || '', interfaceName, item.address);
callback(null, mac);
});
};
// nameserver 172.24.102.254
var DNS_SERVER_RE = /^nameserver\s+(\d+\.\d+\.\d+\.\d+)$/i;
/**
* Get DNS servers.
*
* @param {String} [filepath] resolv config file path. default is '/etc/resolv.conf'.
* @param {Function(err, servers)} callback
*/
address.dns = function (filepath, callback) {
if (typeof filepath === 'function') {
callback = filepath;
filepath = null;
}
filepath = filepath || DEFAULT_RESOLV_FILE;
fs.readFile(filepath, 'utf8', function (err, content) {
if (err) {
return callback(err);
}
var servers = [];
content = content || '';
var lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trim();
var m = DNS_SERVER_RE.exec(line);
if (m) {
servers.push(m[1]);
}
}
callback(null, servers);
});
};
module.exports = address;
{
"name": "address",
"version": "1.0.3",
"description": "Get current machine IP, MAC and DNS servers.",
"main": "lib/address.js",
"files": [
"lib"
],
"scripts": {
"test": "mocha --check-leaks -R spec -t 5000 test/*.test.js",
"test-cov": "istanbul cover node_modules/.bin/_mocha -- --check-leaks -t 5000 test/*.test.js",
"test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- --check-leaks -t 5000 test/*.test.js",
"benchmark": "matcha",
"autod": "autod -w --prefix '^'",
"cnpm": "npm install --registry=https://registry.npm.taobao.org",
"contributors": "contributors -f plain -o AUTHORS"
},
"dependencies": {},
"devDependencies": {
"contributors": "*",
"istanbul": "*",
"matcha": "*",
"mm": "*",
"mocha": "*",
"pedding": "*",
"should": "*",
"benchmark": "*",
"beautify-benchmark": "*",
"webstorm-disable-index": "1"
},
"repository": {
"type": "git",
"url": "git://github.com/node-modules/address.git"
},
"keywords": [
"address",
"ip",
"ipv4",
"mac"
],
"engines": {
"node": ">= 0.12.0"
},
"author": "fengmk2 <fengmk2@gmail.com>",
"license": "MIT"
}
\ No newline at end of file
'use strict';
var os = require('os');
var child = require('child_process');
var path = require('path');
var should = require('should');
var mm = require('mm');
var fs = require('fs');
var pedding = require('pedding');
var address = require('../');
var fixtures = path.join(__dirname, 'fixtures');
describe('address.test.js', function () {
beforeEach(mm.restore);
describe('regex check', function () {
it('should MAC_IP_RE pass', function () {
should.ok(address.MAC_IP_RE.test(' inet 10.7.84.211 netmask 0xfffffc00 broadcast 10.7.87.255'));
should.ok(address.MAC_IP_RE.test(' inet addr:10.125.5.202 Bcast:10.125.15.255 Mask:255.255.240.0'));
});
it('should MAC_RE pass', function () {
should.ok(address.MAC_RE.test(' ether c4:2c:03:32:d5:3d '));
should.ok(address.MAC_RE.test('eth0 Link encap:Ethernet HWaddr 00:16:3E:00:0A:29 '));
});
});
describe('address()', function () {
it('should return first ethernet addrs', function (done) {
address(function (err, addr) {
should.not.exists(err);
addr.should.have.keys('ip', 'ipv6', 'mac');
addr.mac && addr.mac.should.match(/^(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}$/i);
addr.ip && addr.ip.should.match(/^\d+\.\d+\.\d+\.\d+$/);
done();
});
});
it('should return first ethernet addrs from osx', function (done) {
mm(address, 'interface', function () {
return {address: '192.168.2.104'};
});
mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'darwin.txt'), 'utf8'));
address('en', function (err, addr) {
should.not.exists(err);
addr.should.have.keys('ip', 'ipv6', 'mac');
addr.ip.should.equal('192.168.2.104');
// addr.ipv6.should.match(/^[a-z0-9]{4}\:\:[a-z0-9]{4}\:[a-z0-9]{4}\:[a-z0-9]{4}\:[a-z0-9]{4}$/);
addr.mac.should.equal('78:ca:39:b0:e6:7d');
done();
});
});
it('should return first ethernet addrs from linux', function (done) {
mm(address, 'interface', function () {
return {address: '10.125.5.202'};
});
mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'linux.txt'), 'utf8'));
address('eth', function (err, addr) {
should.not.exists(err);
addr.should.have.keys('ip', 'ipv6', 'mac');
addr.ip.should.equal('10.125.5.202');
// addr.ipv6.should.match(/^[a-z0-9]{4}\:\:[a-z0-9]{4}\:[a-z0-9]{4}\:[a-z0-9]{4}\:[a-z0-9]{4}$/);
addr.mac.should.equal('00:16:3E:00:0A:29');
done();
});
});
it('should return first vnic interface addrs from osx', function (done) {
mm(address, 'ip', function () {
return '10.211.55.2';
});
mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'darwin.txt'), 'utf8'));
address('vnic', function (err, addr) {
should.not.exists(err);
addr.ip.should.equal('10.211.55.2')
// console.log(addr)
// addr.mac.should.equal('00:1c:42:00:00:08');
should.not.exists(addr.ipv6);
done();
});
});
it('should return first local loopback addrs', function (done) {
address('lo', function (err, addr) {
should.not.exists(err);
addr.should.have.keys('ip', 'ipv6', 'mac');
addr.should.property('ip').with.equal('127.0.0.1');
done();
});
});
it('should return first local loopback addrs from linux', function (done) {
mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'linux.txt'), 'utf8'));
address('lo', function (err, addr) {
should.not.exists(err);
addr.should.have.keys('ip', 'ipv6', 'mac');
addr.should.property('ip').with.equal('127.0.0.1');
done();
});
});
});
describe('interface()', function () {
it('should return interface with family', function () {
var item = address.interface();
should.exists(item);
item.should.have.property('address');
item.should.have.property('family');
});
});
describe('address.mac()', function () {
it.skip('should return mac', function (done) {
address.mac(function (err, mac) {
should.not.exists(err);
should.exists(mac);
mac.should.match(/(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}/i);
done();
});
});
it('should return mock mac address', function (done) {
mm(address, 'interface', function () {
return {address: os.platform() === 'linux' ? '10.125.5.202' : '192.168.2.104'};
});
mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, os.platform() + '.txt'), 'utf8'));
address.mac(os.platform() === 'linux' ? 'eth' : 'en', function (err, mac) {
should.not.exists(err);
should.exists(mac);
mac.should.match(/(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}/i);
done();
});
});
it('should return null when ip not exists', function (done) {
mm(address, 'interface', function () {
return null;
});
address.mac(function (err, mac) {
should.not.exists(err);
should.not.exists(mac);
done();
});
});
it('should return err when ifconfig cmd exec error', function (done) {
mm(address, 'interface', function () {
return null;
});
mm.error(child, 'exec');
address.mac(function (err, mac) {
// should.exists(err);
should.not.exists(mac);
done();
});
});
it('should return mac mock win32', function (done) {
mm(os, 'platform', function () {
return 'win32';
});
mm(os, 'networkInterfaces', function () {
return require(path.join(__dirname, './fixtures/win32_interfaces.json'));
});
address.mac(function (err, mac) {
should.not.exists(err);
should.exists(mac);
mac.should.equal('e8:2a:ea:8b:c2:20');
done();
});
});
});
describe('address.ip()', function () {
it('should return 127.0.0.1', function () {
address.ip('lo').should.equal('127.0.0.1');
});
it('should return the first not 127.0.0.1 interface', function () {
mm(os, 'networkInterfaces', function () {
return {
lo:
[ { address: '127.0.0.1',
family: 'IPv4',
internal: true } ],
bond0:
[ { address: '10.206.52.79',
family: 'IPv4',
internal: false } ] };
});
address.ip().should.equal('10.206.52.79');
});
it('should return utun1', function () {
mm(os, 'networkInterfaces', function () {
return {
lo:
[ { address: '127.0.0.1',
family: 'IPv4',