Skip to content

Commit 8c7e613

Browse files
committed
add getIP()
1 parent 4594467 commit 8c7e613

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ utils.datestruct(); // { YYYYMMDD: 20130416, H: 8 }
5252

5353
// Safe encodeURIComponent and decodeURIComponent
5454
utils.decodeURIComponent(utils.encodeURIComponent('你好, nodejs')).should.equal('你好, nodejs');
55+
56+
// get first ip
57+
utils.getIP(); // "10.7.68.72"
58+
utils.getIPv4(); // "10.7.68.72"
59+
utils.getIP('ppp'); // "10.2.0.231"
60+
61+
utils.getIPv6(); // "fe80::cabc:c8ff:feef:f996"
5562
```
5663

5764
## benchmark

lib/utility.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Module dependencies.
1111
*/
1212

13+
var os = require('os');
1314
var crypto = require('crypto');
1415

1516
/**
@@ -255,3 +256,40 @@ exports.datestruct = function (now) {
255256
H: now.getHours()
256257
};
257258
};
259+
260+
function _getIP(family, interfaceName) {
261+
var interfaces = os.networkInterfaces();
262+
interfaceName = interfaceName || 'en';
263+
for (var i = 0; i < 8; i++) {
264+
var items = interfaces[interfaceName + i];
265+
var found = false;
266+
if (items && items.length > 0) {
267+
for (var j = 0; j < items.length; j++) {
268+
var item = items[j];
269+
if (item.family === family && !item.internal) {
270+
return item.address;
271+
}
272+
}
273+
}
274+
}
275+
}
276+
277+
/**
278+
* Get current machine IPv4
279+
*
280+
* @param {String} [interfaceName] interface name, default is 'en'
281+
* @return {String} IP address
282+
*/
283+
exports.getIP = exports.getIPv4 = function (interfaceName) {
284+
return _getIP('IPv4', interfaceName);
285+
};
286+
287+
/**
288+
* Get current machine IPv6
289+
*
290+
* @param {String} [interfaceName] interface name, default is 'en'
291+
* @return {String} IP address
292+
*/
293+
exports.getIPv6 = function (interfaceName) {
294+
return _getIP('IPv6', interfaceName);
295+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"devDependencies": {
1515
"should": "*",
16+
"mm": "*",
1617
"moment": "*",
1718
"blanket": "*",
1819
"travis-cov": "*",

test/utility.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
* Module dependencies.
1111
*/
1212

13+
var os = require('os');
1314
var utils = require('../');
15+
var mm = require('mm');
1416
var should = require('should');
1517
var moment = require('moment');
1618

@@ -170,4 +172,36 @@ Encode string s using a URL-safe alphabet, which substitutes - instead of + and
170172
}
171173
});
172174
});
175+
176+
describe('getIP(), getIPv6(), getIPv4()', function () {
177+
it('should return ip version 4 address', function () {
178+
var address = utils.getIP();
179+
address.should.equal(utils.getIPv4());
180+
address.should.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/);
181+
});
182+
183+
it('should return ip version 6 address', function () {
184+
mm(os, 'networkInterfaces', function () {
185+
return { lo0:
186+
[ { address: '::1', family: 'IPv6', internal: true },
187+
{ address: 'fe80::1', family: 'IPv6', internal: true },
188+
{ address: '127.0.0.1', family: 'IPv4', internal: true } ],
189+
en0:
190+
[ { address: 'fe81::cabc:c8ff:feef:f996', family: 'IPv6',
191+
internal: true },
192+
{ address: '10.0.1.123', family: 'IPv4', internal: false } ],
193+
en7:
194+
[ { address: 'fe80::cabc:c8ff:feef:f996', family: 'IPv6',
195+
internal: false },
196+
{ address: '10.0.1.123', family: 'IPv4', internal: false } ],
197+
vmnet1: [ { address: '10.99.99.254', family: 'IPv4', internal: false } ],
198+
vmnet8: [ { address: '10.88.88.1', family: 'IPv4', internal: false } ],
199+
ppp0: [ { address: '10.2.0.231', family: 'IPv4', internal: false } ]
200+
};
201+
});
202+
var address = utils.getIPv6();
203+
should.exists(address);
204+
address.should.equal('fe80::cabc:c8ff:feef:f996');
205+
});
206+
});
173207
});

0 commit comments

Comments
 (0)