Skip to content

Commit 9f9fbaa

Browse files
committed
fixed interface name wrong on liunx
1 parent 7bb6300 commit 9f9fbaa

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

lib/utility.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,14 @@ exports.datestruct = function (now) {
259259

260260
function _getIP(family, interfaceName) {
261261
var interfaces = os.networkInterfaces();
262-
interfaceName = interfaceName || 'en';
262+
if (!interfaceName) {
263+
if (os.platform() === 'linux') {
264+
interfaceName = 'eth';
265+
} else {
266+
interfaceName = 'en';
267+
}
268+
}
269+
263270
for (var i = 0; i < 8; i++) {
264271
var items = interfaces[interfaceName + i];
265272
var found = false;
@@ -277,7 +284,7 @@ function _getIP(family, interfaceName) {
277284
/**
278285
* Get current machine IPv4
279286
*
280-
* @param {String} [interfaceName] interface name, default is 'en'
287+
* @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
281288
* @return {String} IP address
282289
*/
283290
exports.getIP = exports.getIPv4 = function (interfaceName) {
@@ -287,7 +294,7 @@ exports.getIP = exports.getIPv4 = function (interfaceName) {
287294
/**
288295
* Get current machine IPv6
289296
*
290-
* @param {String} [interfaceName] interface name, default is 'en'
297+
* @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
291298
* @return {String} IP address
292299
*/
293300
exports.getIPv6 = function (interfaceName) {

test/utility.test.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,63 @@ Encode string s using a URL-safe alphabet, which substitutes - instead of + and
174174
});
175175

176176
describe('getIP(), getIPv6(), getIPv4()', function () {
177+
afterEach(mm.restore);
178+
177179
it('should return ip version 4 address', function () {
180+
mm(os, 'networkInterfaces', function () {
181+
return { lo0:
182+
[ { address: '::1', family: 'IPv6', internal: true },
183+
{ address: 'fe80::1', family: 'IPv6', internal: true },
184+
{ address: '127.0.0.1', family: 'IPv4', internal: true } ],
185+
en0:
186+
[ { address: 'fe81::cabc:c8ff:feef:f996', family: 'IPv6',
187+
internal: true },
188+
{ address: '10.0.1.123', family: 'IPv4', internal: false } ],
189+
en7:
190+
[ { address: 'fe80::cabc:c8ff:feef:f996', family: 'IPv6',
191+
internal: false },
192+
{ address: '10.0.1.123', family: 'IPv4', internal: false } ],
193+
vmnet1: [ { address: '10.99.99.254', family: 'IPv4', internal: false } ],
194+
vmnet8: [ { address: '10.88.88.1', family: 'IPv4', internal: false } ],
195+
ppp0: [ { address: '10.2.0.231', family: 'IPv4', internal: false } ]
196+
};
197+
});
198+
mm(os, 'platform', function () {
199+
return 'darwin';
200+
});
178201
var address = utils.getIP();
202+
should.exists(address);
203+
address.should.equal(utils.getIPv4());
204+
address.should.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/);
205+
address.should.equal('10.0.1.123');
206+
mm.restore();
207+
208+
mm(os, 'networkInterfaces', function () {
209+
return { lo0:
210+
[ { address: '::1', family: 'IPv6', internal: true },
211+
{ address: 'fe80::1', family: 'IPv6', internal: true },
212+
{ address: '127.0.0.1', family: 'IPv4', internal: true } ],
213+
eth0:
214+
[ { address: 'fe81::cabc:c8ff:feef:f996', family: 'IPv6',
215+
internal: true },
216+
{ address: '10.0.1.124', family: 'IPv4', internal: false } ],
217+
eth2:
218+
[ { address: 'fe80::cabc:c8ff:feef:f996', family: 'IPv6',
219+
internal: false },
220+
{ address: '10.0.1.123', family: 'IPv4', internal: false } ],
221+
vmnet1: [ { address: '10.99.99.254', family: 'IPv4', internal: false } ],
222+
vmnet8: [ { address: '10.88.88.1', family: 'IPv4', internal: false } ],
223+
ppp0: [ { address: '10.2.0.231', family: 'IPv4', internal: false } ]
224+
};
225+
});
226+
mm(os, 'platform', function () {
227+
return 'linux';
228+
});
229+
address = utils.getIP();
230+
should.exists(address);
179231
address.should.equal(utils.getIPv4());
180232
address.should.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/);
233+
address.should.equal('10.0.1.124');
181234
});
182235

183236
it('should return ip version 6 address', function () {
@@ -186,11 +239,11 @@ Encode string s using a URL-safe alphabet, which substitutes - instead of + and
186239
[ { address: '::1', family: 'IPv6', internal: true },
187240
{ address: 'fe80::1', family: 'IPv6', internal: true },
188241
{ address: '127.0.0.1', family: 'IPv4', internal: true } ],
189-
en0:
242+
eth0:
190243
[ { address: 'fe81::cabc:c8ff:feef:f996', family: 'IPv6',
191244
internal: true },
192245
{ address: '10.0.1.123', family: 'IPv4', internal: false } ],
193-
en7:
246+
eth7:
194247
[ { address: 'fe80::cabc:c8ff:feef:f996', family: 'IPv6',
195248
internal: false },
196249
{ address: '10.0.1.123', family: 'IPv4', internal: false } ],
@@ -199,6 +252,10 @@ Encode string s using a URL-safe alphabet, which substitutes - instead of + and
199252
ppp0: [ { address: '10.2.0.231', family: 'IPv4', internal: false } ]
200253
};
201254
});
255+
mm(os, 'platform', function () {
256+
return 'linux';
257+
});
258+
202259
var address = utils.getIPv6();
203260
should.exists(address);
204261
address.should.equal('fe80::cabc:c8ff:feef:f996');

0 commit comments

Comments
 (0)