-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
132 lines (125 loc) · 3.85 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
/* jshint node: true */
"use strict";
var util = require("./lib/util.js");
var lib = {};
lib.getMacAddress = require("./lib/getmacaddress.js");
lib.getAllInterfaces = require("./lib/getallinterfaces.js");
lib.networkInterfaces = require("./lib/networkinterfaces.js");
// devices like en0 (mac), eth3 (linux), Ethernet (windows), etc. are preferred
var goodIfaces = new RegExp("^((en|eth)[0-9]+|ethernet)$", "i");
// https://github.com/scravy/node-macaddress/issues/32
var badIfaces = new RegExp("^(vboxnet[0-9]+)$", "i");
lib.one = function () {
// one() can be invoked in several ways:
// one() -> Promise<string>
// one(iface: string) -> Promise<string>
// one(iface: string, callback) -> async, yields a string
// one(callback) -> async, yields a string
var iface = null;
var callback = null;
if (arguments.length >= 1) {
if (typeof arguments[0] === "function") {
callback = arguments[0];
} else if (typeof arguments[0] === "string") {
iface = arguments[0];
}
if (arguments.length >= 2) {
if (typeof arguments[1] === "function") {
callback = arguments[1];
}
}
}
if (!callback) {
return util.promisify(function (callback) {
lib.one(iface, callback);
});
}
if (iface) {
lib.getMacAddress(iface, callback);
return;
}
var ifaces = lib.networkInterfaces();
var addresses = {};
var best = [];
var args = [];
Object.keys(ifaces).forEach(function (name) {
args.push(name);
var score = 0;
var iface = ifaces[name];
if (typeof iface.mac === "string" && iface.mac !== "00:00:00:00:00:00") {
addresses[name] = iface.mac;
if (iface.ipv4) {
score += 1;
}
if (iface.ipv6) {
score += 1;
}
if (goodIfaces.test(name)) {
score += 2;
}
if (badIfaces.test(name)) {
score -= 3;
}
best.push({
name: name,
score: score,
mac: iface.mac
});
}
});
if (best.length > 0) {
best.sort(function (left, right) {
// the following will sort items with a higher score to the beginning
var comparison = right.score - left.score;
if (comparison !== 0) {
return comparison;
}
if (left.name < right.name) {
return -1;
}
if (left.name > right.name) {
return 1;
}
return 0;
});
util.nextTick(callback.bind(null, null, best[0].mac));
return;
}
args.push(lib.getAllInterfaces);
var getMacAddress = function (d, cb) {
if (addresses[d]) {
cb(null, addresses[d]);
return;
}
lib.getMacAddress(d, cb);
};
util.iterate(args, getMacAddress, callback);
};
lib.all = function (callback) {
if (typeof callback !== "function") {
return util.promisify(lib.all);
}
var ifaces = lib.networkInterfaces();
var resolve = {};
Object.keys(ifaces).forEach(function (iface) {
if (!ifaces[iface].mac) {
resolve[iface] = lib.getMacAddress.bind(null, iface);
}
});
if (Object.keys(resolve).length === 0) {
if (typeof callback === "function") {
util.nextTick(callback.bind(null, null, ifaces));
}
return ifaces;
}
util.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;