forked from scravy/node-macaddress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
145 lines (127 loc) · 3.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* jshint node: true */
'use strict';
var os = require('os');
var lib = {};
function parallel(tasks, done) {
var results = [];
var errs = [];
var length = 0;
var doneLength = 0;
function doneIt(ix, err, result) {
if (err) {
errs[ix] = err;
} else {
results[ix] = result;
}
doneLength += 1;
if (doneLength >= length) {
done(errs.length > 0 ? errs : errs, results);
}
}
Object.keys(tasks).forEach(function (key) {
length += 1;
var task = tasks[key];
(process.nextTick || global.setImmediate || global.setTimeout)(function () {
task(doneIt.bind(null, key), 1);
});
});
}
lib.networkInterfaces = function () {
var ifaces = os.networkInterfaces();
var allAddresses = {};
Object.keys(ifaces).forEach(function (iface) {
var addresses = {};
var hasAddresses = false;
ifaces[iface].forEach(function (address) {
if (!address.internal) {
addresses[(address.family || "").toLowerCase()] = address.address;
hasAddresses = true;
if (address.mac) {
addresses.mac = address.mac;
}
}
});
if (hasAddresses) {
allAddresses[iface] = addresses;
}
});
return allAddresses;
};
var _getMacAddress;
switch (os.platform()) {
case 'win32':
_getMacAddress = require('./lib/windows.js');
break;
case 'linux':
_getMacAddress = require('./lib/linux.js');
break;
case 'darwin':
case 'sunos':
_getMacAddress = require('./lib/unix.js');
break;
default:
console.warn("node-macaddress: Unkown os.platform(), defaulting to `unix'.");
_getMacAddress = require('./lib/unix.js');
break;
}
lib.one = function (iface, callback) {
if (typeof iface === 'function') {
callback = iface;
var ifaces = lib.networkInterfaces();
var alleged = [ 'eth0', 'eth1', 'en0', 'en1' ];
iface = Object.keys(ifaces)[0];
for (var i = 0; i < alleged.length; i++) {
if (ifaces[alleged[i]]) {
iface = alleged[i];
break;
}
}
if (!ifaces[iface]) {
if (typeof callback === 'function') {
process.nextTick(function() {
callback(new Error("no interfaces found"), null);
});
}
return null;
}
if (ifaces[iface].mac) {
if (typeof callback === 'function') {
process.nextTick(function() {
callback(null, ifaces[iface].mac);
});
}
return ifaces[iface].mac;
}
}
if (typeof callback === 'function') {
_getMacAddress(iface, callback);
}
return null;
};
lib.all = function (callback) {
var ifaces = lib.networkInterfaces();
var resolve = {};
Object.keys(ifaces).forEach(function (iface) {
if (!ifaces[iface].mac) {
resolve[iface] = _getMacAddress.bind(null, iface);
}
});
if (Object.keys(resolve).length === 0) {
if (typeof callback === 'function') {
process.nextTick(function(){
callback(null, ifaces);
});
}
return ifaces;
}
parallel(resolve, function (err, result) {
Object.keys(result).forEach(function (iface) {
ifaces[iface].mac = result[iface];
});
if (typeof callback === 'function') {
callback(null, ifaces);
}
});
return null;
};
module.exports = lib;