pax_global_header00006660000000000000000000000064133135503760014520gustar00rootroot0000000000000052 comment=3379146455636d8e86ccb20fbff2912032d74e66 node-macaddress-0.2.9/000077500000000000000000000000001331355037600145615ustar00rootroot00000000000000node-macaddress-0.2.9/.gitignore000066400000000000000000000000661331355037600165530ustar00rootroot00000000000000.DS_Store /node_modules .*.swp /.npmcache /dist *.log node-macaddress-0.2.9/.travis.yml000066400000000000000000000002431331355037600166710ustar00rootroot00000000000000language: node_js os: - linux - osx node_js: - stable - "0.12" - "0.11" - "0.10" - "0.9" - "0.8" - iojs - iojs-v1.0.4 install: - npm install node-macaddress-0.2.9/LICENSE000066400000000000000000000020611331355037600155650ustar00rootroot00000000000000MIT License Copyright (c) 2018 Julian Fleischer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-macaddress-0.2.9/README.md000066400000000000000000000057111331355037600160440ustar00rootroot00000000000000node-macaddress =============== [![Build Status](https://travis-ci.org/scravy/node-macaddress.svg?branch=master)](https://travis-ci.org/scravy/node-macaddress) **!! IMPORANT !!** *Please upgrade to the latest release 0.2.9 – versions prior to this one are vulnerable to an arbitrary code execution attack which was fixed in https://github.com/scravy/node-macaddress/pull/16/files* Retrieve MAC addresses in Linux, OS X, and Windows. A common misconception about MAC addresses is that every *host* had *one* MAC address, while a host may have *multiple* MAC addresses – since *every network interface* may have its own MAC address. This library allows to discover the MAC address per network interface and chooses an appropriate interface if all you're interested in is *one* MAC address identifying the host system (see `API + Examples` below). **Features:** + works on `Linux`, `Mac OS X`, `Windows`, and on most `UNIX` systems. + `node ≥ 0.12` and `io.js` report MAC addresses in `os.networkInterfaces()` this library utilizes this information when available. + also features a sane replacement for `os.networkInterfaces()` (see `API + Examples` below). + works with stoneage node versions ≥ v0.8 (...) Usage ----- ``` npm install --save macaddress ``` ```JavaScript var macaddress = require('macaddress'); ``` API + Examples -------------- (async) .one(iface, callback) → string (async) .one(callback) → string (async) .all(callback) → { iface: { type: address } } (sync) .networkInterfaces() → { iface: { type: address } } --- ### `.one([iface], callback)` Retrieves the MAC address of the given `iface`. If `iface` is omitted, this function automatically chooses an appropriate device (e.g. `eth0` in Linux, `en0` in OS X, etc.). **Without `iface` parameter:** ```JavaScript macaddress.one(function (err, mac) { console.log("Mac address for this host: %s", mac); }); ``` ``` → Mac address for this host: ab:42:de:13:ef:37 ``` **With `iface` parameter:** ```JavaScript macaddress.one('awdl0', function (err, mac) { console.log("Mac address for awdl0: %s", mac); }); ``` ``` → Mac address for awdl0: ab:cd:ef:34:12:56 ``` --- ### `.all(callback)` Retrieves the MAC addresses for all non-internal interfaces. ```JavaScript macaddress.all(function (err, all) { console.log(JSON.stringify(all, null, 2)); }); ``` ```JavaScript { "en0": { "ipv6": "fe80::cae0:ebff:fe14:1da9", "ipv4": "192.168.178.20", "mac": "ab:42:de:13:ef:37" }, "awdl0": { "ipv6": "fe80::58b9:daff:fea9:23a9", "mac": "ab:cd:ef:34:12:56" } } ``` --- ### `.networkInterfaces()` A useful replacement of `os.networkInterfaces()`. Reports only non-internal interfaces. ```JavaScript console.log(JSON.stringify(macaddress.networkInterfaces(), null, 2)); ``` ```JavaScript { "en0": { "ipv6": "fe80::cae0:ebff:fe14:1dab", "ipv4": "192.168.178.22" }, "awdl0": { "ipv6": "fe80::58b9:daff:fea9:23a9" } } ``` node-macaddress-0.2.9/index.js000066400000000000000000000101041331355037600162220ustar00rootroot00000000000000/* 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 allAddresses = {}; try { var ifaces = os.networkInterfaces(); } catch (e) { // At October 2016 WSL does not support os.networkInterfaces() and throws // Return empty object as if no interfaces were found // https://github.com/Microsoft/BashOnWindows/issues/468 if (e.syscall === 'uv_interface_addresses') { return allAddresses; } else { throw e; }; }; 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': case 'freebsd': _getMacAddress = require('./lib/unix.js'); break; default: console.warn("node-macaddress: Unknown 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; node-macaddress-0.2.9/lib/000077500000000000000000000000001331355037600153275ustar00rootroot00000000000000node-macaddress-0.2.9/lib/linux.js000066400000000000000000000005031331355037600170220ustar00rootroot00000000000000var execFile = require('child_process').execFile; module.exports = function (iface, callback) { execFile("cat", ["/sys/class/net/" + iface + "/address"], function (err, out) { if (err) { callback(err, null); return; } callback(null, out.trim().toLowerCase()); }); };node-macaddress-0.2.9/lib/unix.js000066400000000000000000000007401331355037600166510ustar00rootroot00000000000000var execFile = require('child_process').execFile; module.exports = function (iface, callback) { execFile("ifconfig", [iface], function (err, out) { if (err) { callback(err, null); return; } var match = /[a-f0-9]{2}(:[a-f0-9]{2}){5}/.exec(out.toLowerCase()); if (!match) { callback("did not find a mac address", null); return; } callback(null, match[0].toLowerCase()); }); }; node-macaddress-0.2.9/lib/windows.js000066400000000000000000000015211331355037600173560ustar00rootroot00000000000000var execFile = require('child_process').execFile; var regexRegex = /[-\/\\^$*+?.()|[\]{}]/g; function escape(string) { return string.replace(regexRegex, '\\$&'); } module.exports = function (iface, callback) { execFile("ipconfig", ["/all"], function (err, out) { if (err) { callback(err, null); return; } var match = new RegExp(escape(iface)).exec(out); if (!match) { callback("did not find interface in `ipconfig /all`", null); return; } out = out.substring(match.index + iface.length); match = /[A-Fa-f0-9]{2}(\-[A-Fa-f0-9]{2}){5}/.exec(out); if (!match) { callback("did not find a mac address", null); return; } callback(null, match[0].toLowerCase().replace(/\-/g, ':')); }); }; node-macaddress-0.2.9/package.json000066400000000000000000000011401331355037600170430ustar00rootroot00000000000000{ "name": "macaddress", "version": "0.2.9", "description": "Get the MAC addresses (hardware addresses) of the hosts network interfaces.", "main": "index.js", "scripts": { "test": "node test.js" }, "repository": { "type": "git", "url": "https://github.com/scravy/node-macaddress.git" }, "keywords": [ "mac", "mac-address", "hardware-address", "network", "system" ], "author": "Julian Fleischer", "license": "MIT", "bugs": { "url": "https://github.com/scravy/node-macaddress/issues" }, "homepage": "https://github.com/scravy/node-macaddress" } node-macaddress-0.2.9/test.js000066400000000000000000000010071331355037600160740ustar00rootroot00000000000000/* jshint node: true */ 'use strict'; var macaddress = require('./index'); var sync = macaddress.one(function (err, mac) { if (err || !/[a-f0-9]{2}(:[a-f0-9]{2}){5}/.test(mac)) { throw err || mac; } console.log("Mac address for this host: %s", mac); }); console.log("Mac address obtained synchronously: %s", sync); macaddress.all(function (err, all) { if (err) { throw err; } console.log(JSON.stringify(all, null, 2)); }); console.log(JSON.stringify(macaddress.networkInterfaces(), null, 2));