1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _os = require('os');
|
8 |
|
9 | var _child_process = require('child_process');
|
10 |
|
11 | const MAC_REGEXP = /(?:ether|HWaddr)\s+((?:[a-z0-9]{2}:){5}[a-z0-9]{2})/i;
|
12 |
|
13 | exports.default = function networkInterface() {
|
14 | const interfaces = (0, _os.networkInterfaces)();
|
15 | const macAddresses = [].concat(...Object.keys(interfaces).filter(key => key !== 'lo').map(key => interfaces[key].map(item => Object.assign({ interface: key }, item)))).map(i => i.mac).filter(mac => mac && mac !== '00:00:00:00:00:00' && !mac.startsWith('00:00:00:00:')).filter((value, index, array) => array.indexOf(value) === index);
|
16 |
|
17 | if (!macAddresses.length) {
|
18 | console.warn('no mac addresses, falling back to ifconfig');
|
19 | const ifConfig = (0, _child_process.execSync)('ifconfig').toString();
|
20 | macAddresses.push(...ifConfig.split('\n').map(line => line.match(MAC_REGEXP)).filter(Boolean).map(match => match[1]).filter(mac => mac && mac !== '00:00:00:00:00:00').filter((value, index, array) => array.indexOf(value) === index));
|
21 | }
|
22 |
|
23 | for (let key of Object.keys(interfaces)) {
|
24 | if (key === 'lo') continue;
|
25 |
|
26 | let netInterface = interfaces[key];
|
27 | let filtered = netInterface.filter(item => item.family === 'IPv4');
|
28 |
|
29 | if (filtered.length !== 0) {
|
30 | netInterface = filtered;
|
31 | }
|
32 |
|
33 | for (let item of netInterface) {
|
34 | if (!item.mac || !item.address) {
|
35 | continue;
|
36 | }
|
37 |
|
38 | if (item.address === '127.0.0.1' || item.address === '::1') {
|
39 | continue;
|
40 | }
|
41 |
|
42 | return {
|
43 | macAddresses,
|
44 | mac: item.mac,
|
45 | ip: item.address
|
46 | };
|
47 | }
|
48 | }
|
49 |
|
50 | throw new Error('Could not find valid ip address');
|
51 | };
|
52 |
|
\ | No newline at end of file |