Skip to content

Commit

Permalink
fix #22, fix #27
Browse files Browse the repository at this point in the history
  • Loading branch information
scravy committed May 4, 2020
1 parent 27054c5 commit ca9e24d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
69 changes: 43 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function parallel(tasks, done) {
});
}

// Retrieves all interfaces that do feature some non-internal address.
// This function does NOT employ caching as to reflect the current state
// of the machine accurately.
lib.networkInterfaces = function () {
var allAddresses = {};

Expand All @@ -53,7 +56,7 @@ lib.networkInterfaces = function () {
if (!address.internal) {
addresses[(address.family || "").toLowerCase()] = address.address;
hasAddresses = true;
if (address.mac) {
if (address.mac && address.mac !== '00:00:00:00:00:00') {
addresses.mac = address.mac;
}
}
Expand Down Expand Up @@ -89,17 +92,42 @@ switch (os.platform()) {

}

lib.one = function (iface, callback) {
if (!callback && typeof iface !== 'function') {
return new Promise(function (resolve, reject) {
lib.one(iface, function (err, mac) {
if (err) {
reject(new Error(err));
return;
var validIfaceRegExp = '^[a-z0-9]+$';
var validIfaceRegExpObj = new RegExp(validIfaceRegExp, 'i');

function getMacAddress(iface, callback) {

if (!validIfaceRegExpObj.test(iface)) {
callback(new Error([
'invalid iface: \'', iface,
'\' (must conform to reg exp /',
validIfaceRegExp, '/)'
].join('')), null);
return;
}

_getMacAddress(iface, callback);
}

function promisify(func) {
return new Promise(function (resolve, reject) {
func(function (err, data) {
if (err) {
if (!err instanceof Error) {
err = new Error(err);
}
reject(err);
return;
}
resolve(data);
});
});
}

resolve(mac);
});
lib.one = function (iface, callback) {
if (!callback && typeof iface !== 'function') {
return promisify(function (callback) {
lib.one(iface, callback);
});
}

Expand Down Expand Up @@ -133,39 +161,28 @@ lib.one = function (iface, callback) {
}
}
if (typeof callback === 'function') {
_getMacAddress(iface, callback);
getMacAddress(iface, callback);
}
return null;
};

lib.all = function (callback) {
if (!callback) {
return new Promise(function (resolve, reject) {
lib.all(function (err, all) {
if (err) {
reject(new Error(err));
return;
}

resolve(all);
});
});
if (typeof callback !== 'function') {
return promisify(lib.all);
}

var ifaces = lib.networkInterfaces();
var resolve = {};

Object.keys(ifaces).forEach(function (iface) {
if (!ifaces[iface].mac) {
resolve[iface] = _getMacAddress.bind(null, iface);
resolve[iface] = getMacAddress.bind(null, iface);
}
});

if (Object.keys(resolve).length === 0) {
if (typeof callback === 'function') {
process.nextTick(function(){
callback(null, ifaces);
});
process.nextTick(callback.bind(null, ifaces));
}
return ifaces;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/linux.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* jshint node: true */
var execFile = require('child_process').execFile;

module.exports = function (iface, callback) {
Expand All @@ -8,4 +9,4 @@ module.exports = function (iface, callback) {
}
callback(null, out.trim().toLowerCase());
});
};
};
1 change: 1 addition & 0 deletions lib/unix.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* jshint node: true */
var execFile = require('child_process').execFile;

module.exports = function (iface, callback) {
Expand Down
1 change: 1 addition & 0 deletions lib/windows.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* jshint node: true */
var execFile = require('child_process').execFile;

var regexRegex = /[-\/\\^$*+?.()|[\]{}]/g;
Expand Down

0 comments on commit ca9e24d

Please sign in to comment.