systeminformation 5.27.15 → 5.27.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +16 -16
  2. package/lib/cpu.js +51 -47
  3. package/lib/network.js +361 -219
  4. package/package.json +1 -1
package/lib/network.js CHANGED
@@ -21,13 +21,13 @@ const util = require('./util');
21
21
 
22
22
  let _platform = process.platform;
23
23
 
24
- const _linux = (_platform === 'linux' || _platform === 'android');
25
- const _darwin = (_platform === 'darwin');
26
- const _windows = (_platform === 'win32');
27
- const _freebsd = (_platform === 'freebsd');
28
- const _openbsd = (_platform === 'openbsd');
29
- const _netbsd = (_platform === 'netbsd');
30
- const _sunos = (_platform === 'sunos');
24
+ const _linux = _platform === 'linux' || _platform === 'android';
25
+ const _darwin = _platform === 'darwin';
26
+ const _windows = _platform === 'win32';
27
+ const _freebsd = _platform === 'freebsd';
28
+ const _openbsd = _platform === 'openbsd';
29
+ const _netbsd = _platform === 'netbsd';
30
+ const _sunos = _platform === 'sunos';
31
31
 
32
32
  let _network = {};
33
33
  let _default_iface = '';
@@ -38,7 +38,6 @@ let _mac = {};
38
38
  let pathToIp;
39
39
 
40
40
  function getDefaultNetworkInterface() {
41
-
42
41
  let ifacename = '';
43
42
  let ifacenameFirst = '';
44
43
  try {
@@ -68,9 +67,9 @@ function getDefaultNetworkInterface() {
68
67
  const cmd = 'netstat -r';
69
68
  const result = execSync(cmd, util.execOptsWin);
70
69
  const lines = result.toString().split(os.EOL);
71
- lines.forEach(line => {
70
+ lines.forEach((line) => {
72
71
  line = line.replace(/\s+/g, ' ').trim();
73
- if (line.indexOf('0.0.0.0 0.0.0.0') > -1 && !(/[a-zA-Z]/.test(line))) {
72
+ if (line.indexOf('0.0.0.0 0.0.0.0') > -1 && !/[a-zA-Z]/.test(line)) {
74
73
  const parts = line.split(' ');
75
74
  if (parts.length >= 5) {
76
75
  defaultIp = parts[parts.length - 2];
@@ -105,9 +104,15 @@ function getDefaultNetworkInterface() {
105
104
  }
106
105
  if (_darwin || _freebsd || _openbsd || _netbsd || _sunos) {
107
106
  let cmd = '';
108
- if (_linux) { cmd = 'ip route 2> /dev/null | grep default | awk \'{print $5}\''; }
109
- if (_darwin) { cmd = 'route -n get default 2>/dev/null | grep interface: | awk \'{print $2}\''; }
110
- if (_freebsd || _openbsd || _netbsd || _sunos) { cmd = 'route get 0.0.0.0 | grep interface:'; }
107
+ if (_linux) {
108
+ cmd = "ip route 2> /dev/null | grep default | awk '{print $5}'";
109
+ }
110
+ if (_darwin) {
111
+ cmd = "route -n get default 2>/dev/null | grep interface: | awk '{print $2}'";
112
+ }
113
+ if (_freebsd || _openbsd || _netbsd || _sunos) {
114
+ cmd = 'route get 0.0.0.0 | grep interface:';
115
+ }
111
116
  let result = execSync(cmd);
112
117
  ifacename = result.toString().split('\n')[0];
113
118
  if (ifacename.indexOf(':') > -1) {
@@ -117,7 +122,9 @@ function getDefaultNetworkInterface() {
117
122
  } catch (e) {
118
123
  util.noop();
119
124
  }
120
- if (ifacename) { _default_iface = ifacename; }
125
+ if (ifacename) {
126
+ _default_iface = ifacename;
127
+ }
121
128
  return _default_iface;
122
129
  }
123
130
 
@@ -141,7 +148,7 @@ function getMacAddresses() {
141
148
  }
142
149
  }
143
150
  try {
144
- const cmd = 'export LC_ALL=C; ' + ((pathToIp) ? pathToIp + ' link show up' : '/sbin/ifconfig') + '; unset LC_ALL';
151
+ const cmd = 'export LC_ALL=C; ' + (pathToIp ? pathToIp + ' link show up' : '/sbin/ifconfig') + '; unset LC_ALL';
145
152
  let res = execSync(cmd, util.execOptsLinux);
146
153
  const lines = res.toString().split('\n');
147
154
  for (let i = 0; i < lines.length; i++) {
@@ -194,11 +201,12 @@ function getMacAddresses() {
194
201
  }
195
202
 
196
203
  function networkInterfaceDefault(callback) {
197
-
198
204
  return new Promise((resolve) => {
199
205
  process.nextTick(() => {
200
206
  let result = getDefaultNetworkInterface();
201
- if (callback) { callback(result); }
207
+ if (callback) {
208
+ callback(result);
209
+ }
202
210
  resolve(result);
203
211
  });
204
212
  });
@@ -214,9 +222,7 @@ function parseLinesWindowsNics(sections, nconfigsections) {
214
222
  for (let i in sections) {
215
223
  try {
216
224
  if ({}.hasOwnProperty.call(sections, i)) {
217
-
218
225
  if (sections[i].trim() !== '') {
219
-
220
226
  let lines = sections[i].trim().split('\r\n');
221
227
  let linesNicConfig = null;
222
228
  try {
@@ -256,7 +262,7 @@ function parseLinesWindowsNics(sections, nconfigsections) {
256
262
  function getWindowsNics() {
257
263
  return new Promise((resolve) => {
258
264
  process.nextTick(() => {
259
- let cmd = 'Get-CimInstance Win32_NetworkAdapter | fl *' + '; echo \'#-#-#-#\';';
265
+ let cmd = 'Get-CimInstance Win32_NetworkAdapter | fl *' + "; echo '#-#-#-#';";
260
266
  cmd += 'Get-CimInstance Win32_NetworkAdapterConfiguration | fl DHCPEnabled' + '';
261
267
  try {
262
268
  util.powerShell(cmd).then((data) => {
@@ -273,13 +279,12 @@ function getWindowsNics() {
273
279
  }
274
280
 
275
281
  function getWindowsDNSsuffixes() {
276
-
277
282
  let iface = {};
278
283
 
279
284
  let dnsSuffixes = {
280
285
  primaryDNS: '',
281
286
  exitCode: 0,
282
- ifaces: [],
287
+ ifaces: []
283
288
  };
284
289
 
285
290
  try {
@@ -287,17 +292,18 @@ function getWindowsDNSsuffixes() {
287
292
  const ipconfigArray = ipconfig.split('\r\n\r\n');
288
293
 
289
294
  ipconfigArray.forEach((element, index) => {
290
-
291
- if (index == 1) {
295
+ if (index === 1) {
292
296
  const longPrimaryDNS = element.split('\r\n').filter((element) => {
293
297
  return element.toUpperCase().includes('DNS');
294
298
  });
295
299
  const primaryDNS = longPrimaryDNS[0].substring(longPrimaryDNS[0].lastIndexOf(':') + 1);
296
300
  dnsSuffixes.primaryDNS = primaryDNS.trim();
297
- if (!dnsSuffixes.primaryDNS) { dnsSuffixes.primaryDNS = 'Not defined'; }
301
+ if (!dnsSuffixes.primaryDNS) {
302
+ dnsSuffixes.primaryDNS = 'Not defined';
303
+ }
298
304
  }
299
305
  if (index > 1) {
300
- if (index % 2 == 0) {
306
+ if (index % 2 === 0) {
301
307
  const name = element.substring(element.lastIndexOf(' ') + 1).replace(':', '');
302
308
  iface.name = name;
303
309
  } else {
@@ -317,7 +323,7 @@ function getWindowsDNSsuffixes() {
317
323
  return {
318
324
  primaryDNS: '',
319
325
  exitCode: 0,
320
- ifaces: [],
326
+ ifaces: []
321
327
  };
322
328
  }
323
329
  }
@@ -327,13 +333,17 @@ function getWindowsIfaceDNSsuffix(ifaces, ifacename) {
327
333
  // Adding (.) to ensure ifacename compatibility when duplicated iface-names
328
334
  const interfaceName = ifacename + '.';
329
335
  try {
330
- const connectionDnsSuffix = ifaces.filter((iface) => {
331
- return interfaceName.includes(iface.name + '.');
332
- }).map((iface) => iface.dnsSuffix);
336
+ const connectionDnsSuffix = ifaces
337
+ .filter((iface) => {
338
+ return interfaceName.includes(iface.name + '.');
339
+ })
340
+ .map((iface) => iface.dnsSuffix);
333
341
  if (connectionDnsSuffix[0]) {
334
342
  dnsSuffix = connectionDnsSuffix[0];
335
343
  }
336
- if (!dnsSuffix) { dnsSuffix = ''; }
344
+ if (!dnsSuffix) {
345
+ dnsSuffix = '';
346
+ }
337
347
  return dnsSuffix;
338
348
  } catch (error) {
339
349
  return 'Unknown';
@@ -366,7 +376,7 @@ function getWindowsWirelessIfaceSSID(interfaceName) {
366
376
  function getWindowsIEEE8021x(connectionType, iface, ifaces) {
367
377
  let i8021x = {
368
378
  state: 'Unknown',
369
- protocol: 'Unknown',
379
+ protocol: 'Unknown'
370
380
  };
371
381
 
372
382
  if (ifaces === 'Disabled') {
@@ -375,7 +385,7 @@ function getWindowsIEEE8021x(connectionType, iface, ifaces) {
375
385
  return i8021x;
376
386
  }
377
387
 
378
- if (connectionType == 'wired' && ifaces.length > 0) {
388
+ if (connectionType === 'wired' && ifaces.length > 0) {
379
389
  try {
380
390
  // Get 802.1x information by interface name
381
391
  const iface8021xInfo = ifaces.find((element) => {
@@ -399,17 +409,13 @@ function getWindowsIEEE8021x(connectionType, iface, ifaces) {
399
409
  } catch (error) {
400
410
  return i8021x;
401
411
  }
402
- } else if (connectionType == 'wireless') {
403
-
412
+ } else if (connectionType === 'wireless') {
404
413
  let i8021xState = '';
405
414
  let i8021xProtocol = '';
406
415
 
407
-
408
-
409
416
  try {
410
417
  const SSID = getWindowsWirelessIfaceSSID(iface);
411
418
  if (SSID !== 'Unknown') {
412
-
413
419
  let ifaceSanitized = '';
414
420
  const s = util.isPrototypePolluted() ? '---' : util.sanitizeShellString(SSID);
415
421
  const l = util.mathMin(s.length, 32);
@@ -459,7 +465,7 @@ function splitSectionsNics(lines) {
459
465
 
460
466
  function parseLinesDarwinNics(sections) {
461
467
  let nics = [];
462
- sections.forEach(section => {
468
+ sections.forEach((section) => {
463
469
  let nic = {
464
470
  iface: '',
465
471
  mtu: null,
@@ -480,7 +486,7 @@ function parseLinesDarwinNics(sections) {
480
486
  nic.mtu = null;
481
487
  }
482
488
  nic.internal = parts[0].toLowerCase().indexOf('loopback') > -1;
483
- section.forEach(line => {
489
+ section.forEach((line) => {
484
490
  if (line.trim().startsWith('ether ')) {
485
491
  nic.mac = line.split('ether ')[1].toLowerCase().trim();
486
492
  }
@@ -506,7 +512,7 @@ function parseLinesDarwinNics(sections) {
506
512
  }
507
513
  nic.type = util.getValue(section, 'type').toLowerCase().indexOf('wi-fi') > -1 ? 'wireless' : 'wired';
508
514
  const operstate = util.getValue(section, 'status').toLowerCase();
509
- nic.operstate = (operstate === 'active' ? 'up' : (operstate === 'inactive' ? 'down' : 'unknown'));
515
+ nic.operstate = operstate === 'active' ? 'up' : operstate === 'inactive' ? 'down' : 'unknown';
510
516
  nic.duplex = util.getValue(section, 'media').toLowerCase().indexOf('half-duplex') > -1 ? 'half' : 'full';
511
517
  if (nic.ip6 || nic.ip4 || nic.mac) {
512
518
  nics.push(nic);
@@ -518,9 +524,11 @@ function parseLinesDarwinNics(sections) {
518
524
  function getDarwinNics() {
519
525
  const cmd = '/sbin/ifconfig -v';
520
526
  try {
521
- const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n');
527
+ const lines = execSync(cmd, { maxBuffer: 1024 * 20000 })
528
+ .toString()
529
+ .split('\n');
522
530
  const nsections = splitSectionsNics(lines);
523
- return (parseLinesDarwinNics(nsections));
531
+ return parseLinesDarwinNics(nsections);
524
532
  } catch (e) {
525
533
  return [];
526
534
  }
@@ -534,7 +542,7 @@ function getLinuxIfaceConnectionName(interfaceName) {
534
542
  const resultFormat = result.replace(/\s+/g, ' ').trim();
535
543
  const connectionNameLines = resultFormat.split(' ').slice(3);
536
544
  const connectionName = connectionNameLines.join(' ');
537
- return connectionName != '--' ? connectionName : '';
545
+ return connectionName !== '--' ? connectionName : '';
538
546
  } catch (e) {
539
547
  return '';
540
548
  }
@@ -543,10 +551,10 @@ function getLinuxIfaceConnectionName(interfaceName) {
543
551
  function checkLinuxDCHPInterfaces(file) {
544
552
  let result = [];
545
553
  try {
546
- let cmd = `cat ${file} 2> /dev/null | grep 'iface\\|source'`;
554
+ const cmd = `cat ${file} 2> /dev/null | grep 'iface\\|source'`;
547
555
  const lines = execSync(cmd, util.execOptsLinux).toString().split('\n');
548
556
 
549
- lines.forEach(line => {
557
+ lines.forEach((line) => {
550
558
  const parts = line.replace(/\s+/g, ' ').trim().split(' ');
551
559
  if (parts.length >= 4) {
552
560
  if (line.toLowerCase().indexOf(' inet ') >= 0 && line.toLowerCase().indexOf('dhcp') >= 0) {
@@ -571,7 +579,7 @@ function getLinuxDHCPNics() {
571
579
  try {
572
580
  const lines = execSync(cmd, util.execOptsLinux).toString().split('\n');
573
581
  const nsections = splitSectionsNics(lines);
574
- result = (parseLinuxDHCPNics(nsections));
582
+ result = parseLinuxDHCPNics(nsections);
575
583
  } catch (e) {
576
584
  util.noop();
577
585
  }
@@ -586,7 +594,7 @@ function getLinuxDHCPNics() {
586
594
  function parseLinuxDHCPNics(sections) {
587
595
  const result = [];
588
596
  if (sections && sections.length) {
589
- sections.forEach(lines => {
597
+ sections.forEach((lines) => {
590
598
  if (lines && lines.length) {
591
599
  const parts = lines[0].split(':');
592
600
  if (parts.length > 2) {
@@ -625,10 +633,10 @@ function getLinuxIfaceDHCPstatus(iface, connectionName, DHCPNics) {
625
633
  }
626
634
  return result;
627
635
  } catch (e) {
628
- return (DHCPNics.indexOf(iface) >= 0);
636
+ return DHCPNics.indexOf(iface) >= 0;
629
637
  }
630
638
  } else {
631
- return (DHCPNics.indexOf(iface) >= 0);
639
+ return DHCPNics.indexOf(iface) >= 0;
632
640
  }
633
641
  }
634
642
 
@@ -653,7 +661,7 @@ function getLinuxIfaceDNSsuffix(connectionName) {
653
661
  const result = execSync(cmd, util.execOptsLinux).toString();
654
662
  const resultFormat = result.replace(/\s+/g, ' ').trim();
655
663
  const dnsSuffix = resultFormat.split(' ').slice(1).toString();
656
- return dnsSuffix == '--' ? 'Not defined' : dnsSuffix;
664
+ return dnsSuffix === '--' ? 'Not defined' : dnsSuffix;
657
665
  } catch (e) {
658
666
  return 'Unknown';
659
667
  }
@@ -670,8 +678,7 @@ function getLinuxIfaceIEEE8021xAuth(connectionName) {
670
678
  const resultFormat = result.replace(/\s+/g, ' ').trim();
671
679
  const authenticationProtocol = resultFormat.split(' ').slice(1).toString();
672
680
 
673
-
674
- return authenticationProtocol == '--' ? '' : authenticationProtocol;
681
+ return authenticationProtocol === '--' ? '' : authenticationProtocol;
675
682
  } catch (e) {
676
683
  return 'Not defined';
677
684
  }
@@ -682,7 +689,7 @@ function getLinuxIfaceIEEE8021xAuth(connectionName) {
682
689
 
683
690
  function getLinuxIfaceIEEE8021xState(authenticationProtocol) {
684
691
  if (authenticationProtocol) {
685
- if (authenticationProtocol == 'Not defined') {
692
+ if (authenticationProtocol === 'Not defined') {
686
693
  return 'Disabled';
687
694
  }
688
695
  return 'Enabled';
@@ -692,9 +699,35 @@ function getLinuxIfaceIEEE8021xState(authenticationProtocol) {
692
699
  }
693
700
 
694
701
  function testVirtualNic(iface, ifaceName, mac) {
695
- const virtualMacs = ['00:00:00:00:00:00', '00:03:FF', '00:05:69', '00:0C:29', '00:0F:4B', '00:13:07', '00:13:BE', '00:15:5d', '00:16:3E', '00:1C:42', '00:21:F6', '00:24:0B', '00:50:56', '00:A0:B1', '00:E0:C8', '08:00:27', '0A:00:27', '18:92:2C', '16:DF:49', '3C:F3:92', '54:52:00', 'FC:15:97'];
702
+ const virtualMacs = [
703
+ '00:00:00:00:00:00',
704
+ '00:03:FF',
705
+ '00:05:69',
706
+ '00:0C:29',
707
+ '00:0F:4B',
708
+ '00:13:07',
709
+ '00:13:BE',
710
+ '00:15:5d',
711
+ '00:16:3E',
712
+ '00:1C:42',
713
+ '00:21:F6',
714
+ '00:24:0B',
715
+ '00:50:56',
716
+ '00:A0:B1',
717
+ '00:E0:C8',
718
+ '08:00:27',
719
+ '0A:00:27',
720
+ '18:92:2C',
721
+ '16:DF:49',
722
+ '3C:F3:92',
723
+ '54:52:00',
724
+ 'FC:15:97'
725
+ ];
696
726
  if (mac) {
697
- return virtualMacs.filter(item => { return mac.toUpperCase().toUpperCase().startsWith(item.substring(0, mac.length)); }).length > 0 ||
727
+ return (
728
+ virtualMacs.filter((item) => {
729
+ return mac.toUpperCase().toUpperCase().startsWith(item.substring(0, mac.length));
730
+ }).length > 0 ||
698
731
  iface.toLowerCase().indexOf(' virtual ') > -1 ||
699
732
  ifaceName.toLowerCase().indexOf(' virtual ') > -1 ||
700
733
  iface.toLowerCase().indexOf('vethernet ') > -1 ||
@@ -702,12 +735,14 @@ function testVirtualNic(iface, ifaceName, mac) {
702
735
  iface.toLowerCase().startsWith('veth') ||
703
736
  ifaceName.toLowerCase().startsWith('veth') ||
704
737
  iface.toLowerCase().startsWith('vboxnet') ||
705
- ifaceName.toLowerCase().startsWith('vboxnet');
706
- } else { return false; }
738
+ ifaceName.toLowerCase().startsWith('vboxnet')
739
+ );
740
+ } else {
741
+ return false;
742
+ }
707
743
  }
708
744
 
709
745
  function networkInterfaces(callback, rescan, defaultString) {
710
-
711
746
  if (typeof callback === 'string') {
712
747
  defaultString = callback;
713
748
  rescan = true;
@@ -727,7 +762,6 @@ function networkInterfaces(callback, rescan, defaultString) {
727
762
 
728
763
  return new Promise((resolve) => {
729
764
  process.nextTick(() => {
730
-
731
765
  let ifaces = os.networkInterfaces();
732
766
 
733
767
  let result = [];
@@ -736,11 +770,13 @@ function networkInterfaces(callback, rescan, defaultString) {
736
770
  let nics8021xInfo = [];
737
771
  // seperate handling in OSX
738
772
  if (_darwin || _freebsd || _openbsd || _netbsd) {
739
- if ((JSON.stringify(ifaces) === JSON.stringify(_ifaces)) && !rescan) {
773
+ if (JSON.stringify(ifaces) === JSON.stringify(_ifaces) && !rescan) {
740
774
  // no changes - just return object
741
775
  result = _networkInterfaces;
742
776
 
743
- if (callback) { callback(result); }
777
+ if (callback) {
778
+ callback(result);
779
+ }
744
780
  resolve(result);
745
781
  } else {
746
782
  const defaultInterface = getDefaultNetworkInterface();
@@ -748,9 +784,7 @@ function networkInterfaces(callback, rescan, defaultString) {
748
784
 
749
785
  nics = getDarwinNics();
750
786
 
751
-
752
- nics.forEach(nic => {
753
-
787
+ nics.forEach((nic) => {
754
788
  if ({}.hasOwnProperty.call(ifaces, nic.iface)) {
755
789
  ifaces[nic.iface].forEach(function (details) {
756
790
  if (details.family === 'IPv4' || details.family === 4) {
@@ -796,23 +830,27 @@ function networkInterfaces(callback, rescan, defaultString) {
796
830
  });
797
831
  _networkInterfaces = result;
798
832
  if (defaultString.toLowerCase().indexOf('default') >= 0) {
799
- result = result.filter(item => item.default);
833
+ result = result.filter((item) => item.default);
800
834
  if (result.length > 0) {
801
835
  result = result[0];
802
836
  } else {
803
837
  result = [];
804
838
  }
805
839
  }
806
- if (callback) { callback(result); }
840
+ if (callback) {
841
+ callback(result);
842
+ }
807
843
  resolve(result);
808
844
  }
809
845
  }
810
846
  if (_linux) {
811
- if ((JSON.stringify(ifaces) === JSON.stringify(_ifaces)) && !rescan) {
847
+ if (JSON.stringify(ifaces) === JSON.stringify(_ifaces) && !rescan) {
812
848
  // no changes - just return object
813
849
  result = _networkInterfaces;
814
850
 
815
- if (callback) { callback(result); }
851
+ if (callback) {
852
+ callback(result);
853
+ }
816
854
  resolve(result);
817
855
  } else {
818
856
  _ifaces = JSON.parse(JSON.stringify(ifaces));
@@ -850,7 +888,7 @@ function networkInterfaces(callback, rescan, defaultString) {
850
888
  mac = details.mac;
851
889
  // fallback due to https://github.com/nodejs/node/issues/13581 (node 8.1 - node 8.2)
852
890
  const nodeMainVersion = parseInt(process.versions.node.split('.'), 10);
853
- if (mac.indexOf('00:00:0') > -1 && (_linux || _darwin) && (!details.internal) && nodeMainVersion >= 8 && nodeMainVersion <= 11) {
891
+ if (mac.indexOf('00:00:0') > -1 && (_linux || _darwin) && !details.internal && nodeMainVersion >= 8 && nodeMainVersion <= 11) {
854
892
  if (Object.keys(_mac).length === 0) {
855
893
  _mac = getMacAddresses();
856
894
  }
@@ -900,7 +938,7 @@ function networkInterfaces(callback, rescan, defaultString) {
900
938
  dnsSuffix = getLinuxIfaceDNSsuffix(connectionName);
901
939
  ieee8021xAuth = getLinuxIfaceIEEE8021xAuth(connectionName);
902
940
  ieee8021xState = getLinuxIfaceIEEE8021xState(ieee8021xAuth);
903
- } catch (e) {
941
+ } catch {
904
942
  util.noop();
905
943
  }
906
944
  duplex = util.getValue(lines, 'duplex');
@@ -916,9 +954,11 @@ function networkInterfaces(callback, rescan, defaultString) {
916
954
  carrierChanges = parseInt(util.getValue(lines, 'carrier_changes'), 10);
917
955
  const operstate = util.getValue(lines, 'operstate');
918
956
  type = operstate === 'up' ? (util.getValue(lines, 'wireless').trim() ? 'wireless' : 'wired') : 'unknown';
919
- if (ifaceSanitized === 'lo' || ifaceSanitized.startsWith('bond')) { type = 'virtual'; }
957
+ if (ifaceSanitized === 'lo' || ifaceSanitized.startsWith('bond')) {
958
+ type = 'virtual';
959
+ }
920
960
 
921
- let internal = (ifaces[dev] && ifaces[dev][0]) ? ifaces[dev][0].internal : false;
961
+ let internal = ifaces[dev] && ifaces[dev][0] ? ifaces[dev][0].internal : false;
922
962
  if (dev.toLowerCase().indexOf('loopback') > -1 || ifaceName.toLowerCase().indexOf('loopback') > -1) {
923
963
  internal = true;
924
964
  }
@@ -943,40 +983,44 @@ function networkInterfaces(callback, rescan, defaultString) {
943
983
  dnsSuffix,
944
984
  ieee8021xAuth,
945
985
  ieee8021xState,
946
- carrierChanges,
986
+ carrierChanges
947
987
  });
948
988
  }
949
989
  }
950
990
  _networkInterfaces = result;
951
991
  if (defaultString.toLowerCase().indexOf('default') >= 0) {
952
- result = result.filter(item => item.default);
992
+ result = result.filter((item) => item.default);
953
993
  if (result.length > 0) {
954
994
  result = result[0];
955
995
  } else {
956
996
  result = [];
957
997
  }
958
998
  }
959
- if (callback) { callback(result); }
999
+ if (callback) {
1000
+ callback(result);
1001
+ }
960
1002
  resolve(result);
961
1003
  }
962
1004
  }
963
1005
  if (_windows) {
964
- if ((JSON.stringify(ifaces) === JSON.stringify(_ifaces)) && !rescan) {
1006
+ if (JSON.stringify(ifaces) === JSON.stringify(_ifaces) && !rescan) {
965
1007
  // no changes - just return object
966
1008
  result = _networkInterfaces;
967
1009
 
968
- if (callback) { callback(result); }
1010
+ if (callback) {
1011
+ callback(result);
1012
+ }
969
1013
  resolve(result);
970
1014
  } else {
971
1015
  _ifaces = JSON.parse(JSON.stringify(ifaces));
972
1016
  const defaultInterface = getDefaultNetworkInterface();
973
1017
 
974
1018
  getWindowsNics().then(function (nics) {
975
- nics.forEach(nic => {
1019
+ nics.forEach((nic) => {
976
1020
  let found = false;
977
- Object.keys(ifaces).forEach(key => {
1021
+ Object.keys(ifaces).forEach((key) => {
978
1022
  if (!found) {
979
- ifaces[key].forEach(value => {
1023
+ ifaces[key].forEach((value) => {
980
1024
  if (Object.keys(value).indexOf('mac') >= 0) {
981
1025
  found = value['mac'] === nic.mac;
982
1026
  }
@@ -991,7 +1035,6 @@ function networkInterfaces(callback, rescan, defaultString) {
991
1035
  nics8021xInfo = getWindowsWiredProfilesInformation();
992
1036
  dnsSuffixes = getWindowsDNSsuffixes();
993
1037
  for (let dev in ifaces) {
994
-
995
1038
  let ifaceSanitized = '';
996
1039
  const s = util.isPrototypePolluted() ? '---' : util.sanitizeShellString(dev);
997
1040
  const l = util.mathMin(s.length, 2000);
@@ -1034,7 +1077,7 @@ function networkInterfaces(callback, rescan, defaultString) {
1034
1077
  mac = details.mac;
1035
1078
  // fallback due to https://github.com/nodejs/node/issues/13581 (node 8.1 - node 8.2)
1036
1079
  const nodeMainVersion = parseInt(process.versions.node.split('.'), 10);
1037
- if (mac.indexOf('00:00:0') > -1 && (_linux || _darwin) && (!details.internal) && nodeMainVersion >= 8 && nodeMainVersion <= 11) {
1080
+ if (mac.indexOf('00:00:0') > -1 && (_linux || _darwin) && !details.internal && nodeMainVersion >= 8 && nodeMainVersion <= 11) {
1038
1081
  if (Object.keys(_mac).length === 0) {
1039
1082
  _mac = getMacAddresses();
1040
1083
  }
@@ -1042,11 +1085,9 @@ function networkInterfaces(callback, rescan, defaultString) {
1042
1085
  }
1043
1086
  });
1044
1087
 
1045
-
1046
-
1047
1088
  dnsSuffix = getWindowsIfaceDNSsuffix(dnsSuffixes.ifaces, ifaceSanitized);
1048
1089
  let foundFirst = false;
1049
- nics.forEach(detail => {
1090
+ nics.forEach((detail) => {
1050
1091
  if (detail.mac === mac && !foundFirst) {
1051
1092
  iface = detail.iface || iface;
1052
1093
  ifaceName = detail.name;
@@ -1058,14 +1099,21 @@ function networkInterfaces(callback, rescan, defaultString) {
1058
1099
  }
1059
1100
  });
1060
1101
 
1061
- if (dev.toLowerCase().indexOf('wlan') >= 0 || ifaceName.toLowerCase().indexOf('wlan') >= 0 || ifaceName.toLowerCase().indexOf('802.11n') >= 0 || ifaceName.toLowerCase().indexOf('wireless') >= 0 || ifaceName.toLowerCase().indexOf('wi-fi') >= 0 || ifaceName.toLowerCase().indexOf('wifi') >= 0) {
1102
+ if (
1103
+ dev.toLowerCase().indexOf('wlan') >= 0 ||
1104
+ ifaceName.toLowerCase().indexOf('wlan') >= 0 ||
1105
+ ifaceName.toLowerCase().indexOf('802.11n') >= 0 ||
1106
+ ifaceName.toLowerCase().indexOf('wireless') >= 0 ||
1107
+ ifaceName.toLowerCase().indexOf('wi-fi') >= 0 ||
1108
+ ifaceName.toLowerCase().indexOf('wifi') >= 0
1109
+ ) {
1062
1110
  type = 'wireless';
1063
1111
  }
1064
1112
 
1065
1113
  const IEEE8021x = getWindowsIEEE8021x(type, ifaceSanitized, nics8021xInfo);
1066
1114
  ieee8021xAuth = IEEE8021x.protocol;
1067
1115
  ieee8021xState = IEEE8021x.state;
1068
- let internal = (ifaces[dev] && ifaces[dev][0]) ? ifaces[dev][0].internal : false;
1116
+ let internal = ifaces[dev] && ifaces[dev][0] ? ifaces[dev][0].internal : false;
1069
1117
  if (dev.toLowerCase().indexOf('loopback') > -1 || ifaceName.toLowerCase().indexOf('loopback') > -1) {
1070
1118
  internal = true;
1071
1119
  }
@@ -1090,20 +1138,22 @@ function networkInterfaces(callback, rescan, defaultString) {
1090
1138
  dnsSuffix,
1091
1139
  ieee8021xAuth,
1092
1140
  ieee8021xState,
1093
- carrierChanges,
1141
+ carrierChanges
1094
1142
  });
1095
1143
  }
1096
1144
  }
1097
1145
  _networkInterfaces = result;
1098
1146
  if (defaultString.toLowerCase().indexOf('default') >= 0) {
1099
- result = result.filter(item => item.default);
1147
+ result = result.filter((item) => item.default);
1100
1148
  if (result.length > 0) {
1101
1149
  result = result[0];
1102
1150
  } else {
1103
1151
  result = [];
1104
1152
  }
1105
1153
  }
1106
- if (callback) { callback(result); }
1154
+ if (callback) {
1155
+ callback(result);
1156
+ }
1107
1157
  resolve(result);
1108
1158
  });
1109
1159
  }
@@ -1134,8 +1184,8 @@ function calcNetworkSpeed(iface, rx_bytes, tx_bytes, operstate, rx_dropped, rx_e
1134
1184
 
1135
1185
  if (_network[iface] && _network[iface].ms) {
1136
1186
  result.ms = Date.now() - _network[iface].ms;
1137
- result.rx_sec = (rx_bytes - _network[iface].rx_bytes) >= 0 ? (rx_bytes - _network[iface].rx_bytes) / (result.ms / 1000) : 0;
1138
- result.tx_sec = (tx_bytes - _network[iface].tx_bytes) >= 0 ? (tx_bytes - _network[iface].tx_bytes) / (result.ms / 1000) : 0;
1187
+ result.rx_sec = rx_bytes - _network[iface].rx_bytes >= 0 ? (rx_bytes - _network[iface].rx_bytes) / (result.ms / 1000) : 0;
1188
+ result.tx_sec = tx_bytes - _network[iface].tx_bytes >= 0 ? (tx_bytes - _network[iface].tx_bytes) / (result.ms / 1000) : 0;
1139
1189
  _network[iface].rx_bytes = rx_bytes;
1140
1190
  _network[iface].tx_bytes = tx_bytes;
1141
1191
  _network[iface].rx_sec = result.rx_sec;
@@ -1144,7 +1194,9 @@ function calcNetworkSpeed(iface, rx_bytes, tx_bytes, operstate, rx_dropped, rx_e
1144
1194
  _network[iface].last_ms = result.ms;
1145
1195
  _network[iface].operstate = operstate;
1146
1196
  } else {
1147
- if (!_network[iface]) { _network[iface] = {}; }
1197
+ if (!_network[iface]) {
1198
+ _network[iface] = {};
1199
+ }
1148
1200
  _network[iface].rx_bytes = rx_bytes;
1149
1201
  _network[iface].tx_bytes = tx_bytes;
1150
1202
  _network[iface].rx_sec = null;
@@ -1157,19 +1209,19 @@ function calcNetworkSpeed(iface, rx_bytes, tx_bytes, operstate, rx_dropped, rx_e
1157
1209
  }
1158
1210
 
1159
1211
  function networkStats(ifaces, callback) {
1160
-
1161
1212
  let ifacesArray = [];
1162
1213
 
1163
1214
  return new Promise((resolve) => {
1164
1215
  process.nextTick(() => {
1165
-
1166
1216
  // fallback - if only callback is given
1167
1217
  if (util.isFunction(ifaces) && !callback) {
1168
1218
  callback = ifaces;
1169
1219
  ifacesArray = [getDefaultNetworkInterface()];
1170
1220
  } else {
1171
1221
  if (typeof ifaces !== 'string' && ifaces !== undefined) {
1172
- if (callback) { callback([]); }
1222
+ if (callback) {
1223
+ callback([]);
1224
+ }
1173
1225
  return resolve([]);
1174
1226
  }
1175
1227
  ifaces = ifaces || getDefaultNetworkInterface();
@@ -1195,12 +1247,14 @@ function networkStats(ifaces, callback) {
1195
1247
  const workload = [];
1196
1248
  if (ifacesArray.length && ifacesArray[0].trim() === '*') {
1197
1249
  ifacesArray = [];
1198
- networkInterfaces(false).then(allIFaces => {
1250
+ networkInterfaces(false).then((allIFaces) => {
1199
1251
  for (let iface of allIFaces) {
1200
1252
  ifacesArray.push(iface.iface);
1201
1253
  }
1202
- networkStats(ifacesArray.join(',')).then(result => {
1203
- if (callback) { callback(result); }
1254
+ networkStats(ifacesArray.join(',')).then((result) => {
1255
+ if (callback) {
1256
+ callback(result);
1257
+ }
1204
1258
  resolve(result);
1205
1259
  });
1206
1260
  });
@@ -1209,14 +1263,16 @@ function networkStats(ifaces, callback) {
1209
1263
  workload.push(networkStatsSingle(iface.trim()));
1210
1264
  }
1211
1265
  if (workload.length) {
1212
- Promise.all(
1213
- workload
1214
- ).then((data) => {
1215
- if (callback) { callback(data); }
1266
+ Promise.all(workload).then((data) => {
1267
+ if (callback) {
1268
+ callback(data);
1269
+ }
1216
1270
  resolve(data);
1217
1271
  });
1218
1272
  } else {
1219
- if (callback) { callback(result); }
1273
+ if (callback) {
1274
+ callback(result);
1275
+ }
1220
1276
  resolve(result);
1221
1277
  }
1222
1278
  }
@@ -1225,7 +1281,6 @@ function networkStats(ifaces, callback) {
1225
1281
  }
1226
1282
 
1227
1283
  function networkStatsSingle(iface) {
1228
-
1229
1284
  function parseLinesWindowsPerfData(sections) {
1230
1285
  let perfData = [];
1231
1286
  for (let i in sections) {
@@ -1233,7 +1288,11 @@ function networkStatsSingle(iface) {
1233
1288
  if (sections[i].trim() !== '') {
1234
1289
  let lines = sections[i].trim().split('\r\n');
1235
1290
  perfData.push({
1236
- name: util.getValue(lines, 'Name', ':').replace(/[()[\] ]+/g, '').replace(/#|\//g, '_').toLowerCase(),
1291
+ name: util
1292
+ .getValue(lines, 'Name', ':')
1293
+ .replace(/[()[\] ]+/g, '')
1294
+ .replace(/#|\//g, '_')
1295
+ .toLowerCase(),
1237
1296
  rx_bytes: parseInt(util.getValue(lines, 'BytesReceivedPersec', ':'), 10),
1238
1297
  rx_errors: parseInt(util.getValue(lines, 'PacketsReceivedErrors', ':'), 10),
1239
1298
  rx_dropped: parseInt(util.getValue(lines, 'PacketsReceivedDiscarded', ':'), 10),
@@ -1281,17 +1340,35 @@ function networkStatsSingle(iface) {
1281
1340
  let tx_errors = 0;
1282
1341
 
1283
1342
  let cmd, lines, stats;
1284
- if (!_network[ifaceSanitized] || (_network[ifaceSanitized] && !_network[ifaceSanitized].ms) || (_network[ifaceSanitized] && _network[ifaceSanitized].ms && Date.now() - _network[ifaceSanitized].ms >= 500)) {
1343
+ if (
1344
+ !_network[ifaceSanitized] ||
1345
+ (_network[ifaceSanitized] && !_network[ifaceSanitized].ms) ||
1346
+ (_network[ifaceSanitized] && _network[ifaceSanitized].ms && Date.now() - _network[ifaceSanitized].ms >= 500)
1347
+ ) {
1285
1348
  if (_linux) {
1286
1349
  if (fs.existsSync('/sys/class/net/' + ifaceSanitized)) {
1287
1350
  cmd =
1288
- 'cat /sys/class/net/' + ifaceSanitized + '/operstate; ' +
1289
- 'cat /sys/class/net/' + ifaceSanitized + '/statistics/rx_bytes; ' +
1290
- 'cat /sys/class/net/' + ifaceSanitized + '/statistics/tx_bytes; ' +
1291
- 'cat /sys/class/net/' + ifaceSanitized + '/statistics/rx_dropped; ' +
1292
- 'cat /sys/class/net/' + ifaceSanitized + '/statistics/rx_errors; ' +
1293
- 'cat /sys/class/net/' + ifaceSanitized + '/statistics/tx_dropped; ' +
1294
- 'cat /sys/class/net/' + ifaceSanitized + '/statistics/tx_errors; ';
1351
+ 'cat /sys/class/net/' +
1352
+ ifaceSanitized +
1353
+ '/operstate; ' +
1354
+ 'cat /sys/class/net/' +
1355
+ ifaceSanitized +
1356
+ '/statistics/rx_bytes; ' +
1357
+ 'cat /sys/class/net/' +
1358
+ ifaceSanitized +
1359
+ '/statistics/tx_bytes; ' +
1360
+ 'cat /sys/class/net/' +
1361
+ ifaceSanitized +
1362
+ '/statistics/rx_dropped; ' +
1363
+ 'cat /sys/class/net/' +
1364
+ ifaceSanitized +
1365
+ '/statistics/rx_errors; ' +
1366
+ 'cat /sys/class/net/' +
1367
+ ifaceSanitized +
1368
+ '/statistics/tx_dropped; ' +
1369
+ 'cat /sys/class/net/' +
1370
+ ifaceSanitized +
1371
+ '/statistics/tx_errors; ';
1295
1372
  exec(cmd, function (error, stdout) {
1296
1373
  if (!error) {
1297
1374
  lines = stdout.toString().split('\n');
@@ -1304,7 +1381,6 @@ function networkStatsSingle(iface) {
1304
1381
  tx_errors = parseInt(lines[6], 10);
1305
1382
 
1306
1383
  result = calcNetworkSpeed(ifaceSanitized, rx_bytes, tx_bytes, operstate, rx_dropped, rx_errors, tx_dropped, tx_errors);
1307
-
1308
1384
  }
1309
1385
  resolve(result);
1310
1386
  });
@@ -1313,7 +1389,7 @@ function networkStatsSingle(iface) {
1313
1389
  }
1314
1390
  }
1315
1391
  if (_freebsd || _openbsd || _netbsd) {
1316
- cmd = 'netstat -ibndI ' + ifaceSanitized; // lgtm [js/shell-command-constructed-from-input]
1392
+ cmd = 'netstat -ibndI ' + ifaceSanitized; // lgtm [js/shell-command-constructed-from-input]
1317
1393
  exec(cmd, function (error, stdout) {
1318
1394
  if (!error) {
1319
1395
  lines = stdout.toString().split('\n');
@@ -1321,11 +1397,19 @@ function networkStatsSingle(iface) {
1321
1397
  const line = lines[i].replace(/ +/g, ' ').split(' ');
1322
1398
  if (line && line[0] && line[7] && line[10]) {
1323
1399
  rx_bytes = rx_bytes + parseInt(line[7]);
1324
- if (line[6].trim() !== '-') { rx_dropped = rx_dropped + parseInt(line[6]); }
1325
- if (line[5].trim() !== '-') { rx_errors = rx_errors + parseInt(line[5]); }
1400
+ if (line[6].trim() !== '-') {
1401
+ rx_dropped = rx_dropped + parseInt(line[6]);
1402
+ }
1403
+ if (line[5].trim() !== '-') {
1404
+ rx_errors = rx_errors + parseInt(line[5]);
1405
+ }
1326
1406
  tx_bytes = tx_bytes + parseInt(line[10]);
1327
- if (line[12].trim() !== '-') { tx_dropped = tx_dropped + parseInt(line[12]); }
1328
- if (line[9].trim() !== '-') { tx_errors = tx_errors + parseInt(line[9]); }
1407
+ if (line[12].trim() !== '-') {
1408
+ tx_dropped = tx_dropped + parseInt(line[12]);
1409
+ }
1410
+ if (line[9].trim() !== '-') {
1411
+ tx_errors = tx_errors + parseInt(line[9]);
1412
+ }
1329
1413
  operstate = 'up';
1330
1414
  }
1331
1415
  }
@@ -1335,12 +1419,12 @@ function networkStatsSingle(iface) {
1335
1419
  });
1336
1420
  }
1337
1421
  if (_darwin) {
1338
- cmd = 'ifconfig ' + ifaceSanitized + ' | grep "status"'; // lgtm [js/shell-command-constructed-from-input]
1422
+ cmd = 'ifconfig ' + ifaceSanitized + ' | grep "status"'; // lgtm [js/shell-command-constructed-from-input]
1339
1423
  exec(cmd, function (error, stdout) {
1340
1424
  result.operstate = (stdout.toString().split(':')[1] || '').trim();
1341
1425
  result.operstate = (result.operstate || '').toLowerCase();
1342
- result.operstate = (result.operstate === 'active' ? 'up' : (result.operstate === 'inactive' ? 'down' : 'unknown'));
1343
- cmd = 'netstat -bdI ' + ifaceSanitized; // lgtm [js/shell-command-constructed-from-input]
1426
+ result.operstate = result.operstate === 'active' ? 'up' : result.operstate === 'inactive' ? 'down' : 'unknown';
1427
+ cmd = 'netstat -bdI ' + ifaceSanitized; // lgtm [js/shell-command-constructed-from-input]
1344
1428
  exec(cmd, function (error, stdout) {
1345
1429
  if (!error) {
1346
1430
  lines = stdout.toString().split('\n');
@@ -1368,42 +1452,58 @@ function networkStatsSingle(iface) {
1368
1452
  let ifaceName = ifaceSanitized;
1369
1453
 
1370
1454
  // Performance Data
1371
- util.powerShell('Get-CimInstance Win32_PerfRawData_Tcpip_NetworkInterface | select Name,BytesReceivedPersec,PacketsReceivedErrors,PacketsReceivedDiscarded,BytesSentPersec,PacketsOutboundErrors,PacketsOutboundDiscarded | fl').then((stdout, error) => {
1372
- if (!error) {
1373
- const psections = stdout.toString().split(/\n\s*\n/);
1374
- perfData = parseLinesWindowsPerfData(psections);
1375
- }
1455
+ util
1456
+ .powerShell(
1457
+ 'Get-CimInstance Win32_PerfRawData_Tcpip_NetworkInterface | select Name,BytesReceivedPersec,PacketsReceivedErrors,PacketsReceivedDiscarded,BytesSentPersec,PacketsOutboundErrors,PacketsOutboundDiscarded | fl'
1458
+ )
1459
+ .then((stdout, error) => {
1460
+ if (!error) {
1461
+ const psections = stdout.toString().split(/\n\s*\n/);
1462
+ perfData = parseLinesWindowsPerfData(psections);
1463
+ }
1376
1464
 
1377
- // Network Interfaces
1378
- networkInterfaces(false).then(interfaces => {
1379
- // get bytes sent, received from perfData by name
1380
- rx_bytes = 0;
1381
- tx_bytes = 0;
1382
- perfData.forEach(detail => {
1383
- interfaces.forEach(det => {
1384
- if ((det.iface.toLowerCase() === ifaceSanitized.toLowerCase() ||
1385
- det.mac.toLowerCase() === ifaceSanitized.toLowerCase() ||
1386
- det.ip4.toLowerCase() === ifaceSanitized.toLowerCase() ||
1387
- det.ip6.toLowerCase() === ifaceSanitized.toLowerCase() ||
1388
- det.ifaceName.replace(/[()[\] ]+/g, '').replace(/#|\//g, '_').toLowerCase() === ifaceSanitized.replace(/[()[\] ]+/g, '').replace('#', '_').toLowerCase()) &&
1389
- (det.ifaceName.replace(/[()[\] ]+/g, '').replace(/#|\//g, '_').toLowerCase() === detail.name)) {
1390
- ifaceName = det.iface;
1391
- rx_bytes = detail.rx_bytes;
1392
- rx_dropped = detail.rx_dropped;
1393
- rx_errors = detail.rx_errors;
1394
- tx_bytes = detail.tx_bytes;
1395
- tx_dropped = detail.tx_dropped;
1396
- tx_errors = detail.tx_errors;
1397
- operstate = det.operstate;
1398
- }
1465
+ // Network Interfaces
1466
+ networkInterfaces(false).then((interfaces) => {
1467
+ // get bytes sent, received from perfData by name
1468
+ rx_bytes = 0;
1469
+ tx_bytes = 0;
1470
+ perfData.forEach((detail) => {
1471
+ interfaces.forEach((det) => {
1472
+ if (
1473
+ (det.iface.toLowerCase() === ifaceSanitized.toLowerCase() ||
1474
+ det.mac.toLowerCase() === ifaceSanitized.toLowerCase() ||
1475
+ det.ip4.toLowerCase() === ifaceSanitized.toLowerCase() ||
1476
+ det.ip6.toLowerCase() === ifaceSanitized.toLowerCase() ||
1477
+ det.ifaceName
1478
+ .replace(/[()[\] ]+/g, '')
1479
+ .replace(/#|\//g, '_')
1480
+ .toLowerCase() ===
1481
+ ifaceSanitized
1482
+ .replace(/[()[\] ]+/g, '')
1483
+ .replace('#', '_')
1484
+ .toLowerCase()) &&
1485
+ det.ifaceName
1486
+ .replace(/[()[\] ]+/g, '')
1487
+ .replace(/#|\//g, '_')
1488
+ .toLowerCase() === detail.name
1489
+ ) {
1490
+ ifaceName = det.iface;
1491
+ rx_bytes = detail.rx_bytes;
1492
+ rx_dropped = detail.rx_dropped;
1493
+ rx_errors = detail.rx_errors;
1494
+ tx_bytes = detail.tx_bytes;
1495
+ tx_dropped = detail.tx_dropped;
1496
+ tx_errors = detail.tx_errors;
1497
+ operstate = det.operstate;
1498
+ }
1499
+ });
1399
1500
  });
1501
+ if (rx_bytes && tx_bytes) {
1502
+ result = calcNetworkSpeed(ifaceName, parseInt(rx_bytes), parseInt(tx_bytes), operstate, rx_dropped, rx_errors, tx_dropped, tx_errors);
1503
+ }
1504
+ resolve(result);
1400
1505
  });
1401
- if (rx_bytes && tx_bytes) {
1402
- result = calcNetworkSpeed(ifaceName, parseInt(rx_bytes), parseInt(tx_bytes), operstate, rx_dropped, rx_errors, tx_dropped, tx_errors);
1403
- }
1404
- resolve(result);
1405
1506
  });
1406
- });
1407
1507
  }
1408
1508
  } else {
1409
1509
  result.rx_bytes = _network[ifaceSanitized].rx_bytes;
@@ -1425,7 +1525,7 @@ exports.networkStats = networkStats;
1425
1525
 
1426
1526
  function getProcessName(processes, pid) {
1427
1527
  let cmd = '';
1428
- processes.forEach(line => {
1528
+ processes.forEach((line) => {
1429
1529
  const parts = line.split(' ');
1430
1530
  const id = parseInt(parts[0], 10) || -1;
1431
1531
  if (id === pid) {
@@ -1441,16 +1541,19 @@ function getProcessName(processes, pid) {
1441
1541
  }
1442
1542
 
1443
1543
  function networkConnections(callback) {
1444
-
1445
1544
  return new Promise((resolve) => {
1446
1545
  process.nextTick(() => {
1447
1546
  let result = [];
1448
1547
  if (_linux || _freebsd || _openbsd || _netbsd) {
1449
- let cmd = 'export LC_ALL=C; netstat -tunap | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"; unset LC_ALL';
1450
- if (_freebsd || _openbsd || _netbsd) { cmd = 'export LC_ALL=C; netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"; unset LC_ALL'; }
1548
+ let cmd =
1549
+ 'export LC_ALL=C; netstat -tunap | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"; unset LC_ALL';
1550
+ if (_freebsd || _openbsd || _netbsd) {
1551
+ cmd =
1552
+ 'export LC_ALL=C; netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"; unset LC_ALL';
1553
+ }
1451
1554
  exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
1452
1555
  let lines = stdout.toString().split('\n');
1453
- if (!error && (lines.length > 1 || lines[0] != '')) {
1556
+ if (!error && (lines.length > 1 || lines[0] !== '')) {
1454
1557
  lines.forEach(function (line) {
1455
1558
  line = line.replace(/ +/g, ' ').split(' ');
1456
1559
  if (line.length >= 7) {
@@ -1494,7 +1597,6 @@ function networkConnections(callback) {
1494
1597
  } else {
1495
1598
  cmd = 'ss -tunap | grep "ESTAB\\|SYN-SENT\\|SYN-RECV\\|FIN-WAIT1\\|FIN-WAIT2\\|TIME-WAIT\\|CLOSE\\|CLOSE-WAIT\\|LAST-ACK\\|LISTEN\\|CLOSING"';
1496
1599
  exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
1497
-
1498
1600
  if (!error) {
1499
1601
  let lines = stdout.toString().split('\n');
1500
1602
  lines.forEach(function (line) {
@@ -1517,8 +1619,12 @@ function networkConnections(callback) {
1517
1619
  peerip = peeraddress.join(':');
1518
1620
  }
1519
1621
  let connstate = line[1];
1520
- if (connstate === 'ESTAB') { connstate = 'ESTABLISHED'; }
1521
- if (connstate === 'TIME-WAIT') { connstate = 'TIME_WAIT'; }
1622
+ if (connstate === 'ESTAB') {
1623
+ connstate = 'ESTABLISHED';
1624
+ }
1625
+ if (connstate === 'TIME-WAIT') {
1626
+ connstate = 'TIME_WAIT';
1627
+ }
1522
1628
  let pid = null;
1523
1629
  let process = '';
1524
1630
  if (line.length >= 7 && line[6].indexOf('users:') > -1) {
@@ -1558,12 +1664,18 @@ function networkConnections(callback) {
1558
1664
  if (!error) {
1559
1665
  exec('ps -axo pid,command', { maxBuffer: 1024 * 20000 }, function (err2, stdout2) {
1560
1666
  let processes = stdout2.toString().split('\n');
1561
- processes = processes.map((line => { return line.trim().replace(/ +/g, ' '); }));
1667
+ processes = processes.map((line) => {
1668
+ return line.trim().replace(/ +/g, ' ');
1669
+ });
1562
1670
  let lines = stdout.toString().split('\n');
1563
1671
  lines.shift();
1564
1672
  let pidPos = 8;
1565
1673
  if (lines.length > 1 && lines[0].indexOf('pid') > 0) {
1566
- const header = (lines.shift() || '').replace(/ Address/g, '_Address').replace(/process:/g, "").replace(/ +/g, ' ').split(' ');
1674
+ const header = (lines.shift() || '')
1675
+ .replace(/ Address/g, '_Address')
1676
+ .replace(/process:/g, '')
1677
+ .replace(/ +/g, ' ')
1678
+ .split(' ');
1567
1679
  pidPos = header.indexOf('pid');
1568
1680
  }
1569
1681
  lines.forEach(function (line) {
@@ -1587,14 +1699,14 @@ function networkConnections(callback) {
1587
1699
  }
1588
1700
  const hasState = states.indexOf(line[5]) >= 0;
1589
1701
  let connstate = hasState ? line[5] : 'UNKNOWN';
1590
- let pidField = "";
1591
- if (line[line.length - 9].indexOf(":") >= 0) {
1592
- pidField = line[line.length - 9].split(":")[1];
1702
+ let pidField = '';
1703
+ if (line[line.length - 9].indexOf(':') >= 0) {
1704
+ pidField = line[line.length - 9].split(':')[1];
1593
1705
  } else {
1594
1706
  pidField = line[pidPos + (hasState ? 0 : -1)];
1595
1707
 
1596
- if (pidField.indexOf(":") >= 0) {
1597
- pidField = pidField.split(":")[1];
1708
+ if (pidField.indexOf(':') >= 0) {
1709
+ pidField = pidField.split(':')[1];
1598
1710
  }
1599
1711
  }
1600
1712
  let pid = parseInt(pidField, 10);
@@ -1617,7 +1729,6 @@ function networkConnections(callback) {
1617
1729
  }
1618
1730
  resolve(result);
1619
1731
  });
1620
-
1621
1732
  }
1622
1733
  });
1623
1734
  }
@@ -1626,7 +1737,6 @@ function networkConnections(callback) {
1626
1737
  try {
1627
1738
  exec(cmd, util.execOptsWin, function (error, stdout) {
1628
1739
  if (!error) {
1629
-
1630
1740
  let lines = stdout.toString().split('\r\n');
1631
1741
 
1632
1742
  lines.forEach(function (line) {
@@ -1652,16 +1762,34 @@ function networkConnections(callback) {
1652
1762
  peerip = peerip.replace(/\[/g, '').replace(/\]/g, '');
1653
1763
  let pid = util.toInt(line[4]);
1654
1764
  let connstate = line[3];
1655
- if (connstate === 'HERGESTELLT') { connstate = 'ESTABLISHED'; }
1656
- if (connstate.startsWith('ABH')) { connstate = 'LISTEN'; }
1657
- if (connstate === 'SCHLIESSEN_WARTEN') { connstate = 'CLOSE_WAIT'; }
1658
- if (connstate === 'WARTEND') { connstate = 'TIME_WAIT'; }
1659
- if (connstate === 'SYN_GESENDET') { connstate = 'SYN_SENT'; }
1660
-
1661
- if (connstate === 'LISTENING') { connstate = 'LISTEN'; }
1662
- if (connstate === 'SYN_RECEIVED') { connstate = 'SYN_RECV'; }
1663
- if (connstate === 'FIN_WAIT_1') { connstate = 'FIN_WAIT1'; }
1664
- if (connstate === 'FIN_WAIT_2') { connstate = 'FIN_WAIT2'; }
1765
+ if (connstate === 'HERGESTELLT') {
1766
+ connstate = 'ESTABLISHED';
1767
+ }
1768
+ if (connstate.startsWith('ABH')) {
1769
+ connstate = 'LISTEN';
1770
+ }
1771
+ if (connstate === 'SCHLIESSEN_WARTEN') {
1772
+ connstate = 'CLOSE_WAIT';
1773
+ }
1774
+ if (connstate === 'WARTEND') {
1775
+ connstate = 'TIME_WAIT';
1776
+ }
1777
+ if (connstate === 'SYN_GESENDET') {
1778
+ connstate = 'SYN_SENT';
1779
+ }
1780
+
1781
+ if (connstate === 'LISTENING') {
1782
+ connstate = 'LISTEN';
1783
+ }
1784
+ if (connstate === 'SYN_RECEIVED') {
1785
+ connstate = 'SYN_RECV';
1786
+ }
1787
+ if (connstate === 'FIN_WAIT_1') {
1788
+ connstate = 'FIN_WAIT1';
1789
+ }
1790
+ if (connstate === 'FIN_WAIT_2') {
1791
+ connstate = 'FIN_WAIT2';
1792
+ }
1665
1793
  if (line[0].toLowerCase() !== 'udp' && connstate) {
1666
1794
  result.push({
1667
1795
  protocol: line[0].toLowerCase(),
@@ -1694,7 +1822,9 @@ function networkConnections(callback) {
1694
1822
  }
1695
1823
  });
1696
1824
  } catch (e) {
1697
- if (callback) { callback(result); }
1825
+ if (callback) {
1826
+ callback(result);
1827
+ }
1698
1828
  resolve(result);
1699
1829
  }
1700
1830
  }
@@ -1705,7 +1835,6 @@ function networkConnections(callback) {
1705
1835
  exports.networkConnections = networkConnections;
1706
1836
 
1707
1837
  function networkGatewayDefault(callback) {
1708
-
1709
1838
  return new Promise((resolve) => {
1710
1839
  process.nextTick(() => {
1711
1840
  let result = '';
@@ -1733,7 +1862,9 @@ function networkGatewayDefault(callback) {
1733
1862
  }
1734
1863
  });
1735
1864
  } catch (e) {
1736
- if (callback) { callback(result); }
1865
+ if (callback) {
1866
+ callback(result);
1867
+ }
1737
1868
  resolve(result);
1738
1869
  }
1739
1870
  }
@@ -1742,14 +1873,22 @@ function networkGatewayDefault(callback) {
1742
1873
  try {
1743
1874
  exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
1744
1875
  if (!error) {
1745
- const lines = stdout.toString().split('\n').map(line => line.trim());
1876
+ const lines = stdout
1877
+ .toString()
1878
+ .split('\n')
1879
+ .map((line) => line.trim());
1746
1880
  result = util.getValue(lines, 'gateway');
1747
1881
  }
1748
1882
  if (!result) {
1749
- cmd = 'netstat -rn | awk \'/default/ {print $2}\'';
1883
+ cmd = "netstat -rn | awk '/default/ {print $2}'";
1750
1884
  exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
1751
- const lines = stdout.toString().split('\n').map(line => line.trim());
1752
- result = lines.find(line => (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(line)));
1885
+ const lines = stdout
1886
+ .toString()
1887
+ .split('\n')
1888
+ .map((line) => line.trim());
1889
+ result = lines.find((line) =>
1890
+ /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(line)
1891
+ );
1753
1892
  if (callback) {
1754
1893
  callback(result);
1755
1894
  }
@@ -1763,7 +1902,9 @@ function networkGatewayDefault(callback) {
1763
1902
  }
1764
1903
  });
1765
1904
  } catch (e) {
1766
- if (callback) { callback(result); }
1905
+ if (callback) {
1906
+ callback(result);
1907
+ }
1767
1908
  resolve(result);
1768
1909
  }
1769
1910
  }
@@ -1771,41 +1912,40 @@ function networkGatewayDefault(callback) {
1771
1912
  try {
1772
1913
  exec('netstat -r', util.execOptsWin, function (error, stdout) {
1773
1914
  const lines = stdout.toString().split(os.EOL);
1774
- lines.forEach(line => {
1915
+ lines.forEach((line) => {
1775
1916
  line = line.replace(/\s+/g, ' ').trim();
1776
- if (line.indexOf('0.0.0.0 0.0.0.0') > -1 && !(/[a-zA-Z]/.test(line))) {
1917
+ if (line.indexOf('0.0.0.0 0.0.0.0') > -1 && !/[a-zA-Z]/.test(line)) {
1777
1918
  const parts = line.split(' ');
1778
- if (parts.length >= 5 && (parts[parts.length - 3]).indexOf('.') > -1) {
1919
+ if (parts.length >= 5 && parts[parts.length - 3].indexOf('.') > -1) {
1779
1920
  result = parts[parts.length - 3];
1780
1921
  }
1781
1922
  }
1782
1923
  });
1783
1924
  if (!result) {
1784
- util.powerShell('Get-CimInstance -ClassName Win32_IP4RouteTable | Where-Object { $_.Destination -eq \'0.0.0.0\' -and $_.Mask -eq \'0.0.0.0\' }')
1785
- .then((data) => {
1786
- let lines = data.toString().split('\r\n');
1787
- if (lines.length > 1 && !result) {
1788
- result = util.getValue(lines, 'NextHop');
1789
- if (callback) {
1790
- callback(result);
1791
- }
1792
- resolve(result);
1793
- // } else {
1794
- // exec('ipconfig', util.execOptsWin, function (error, stdout) {
1795
- // let lines = stdout.toString().split('\r\n');
1796
- // lines.forEach(function (line) {
1797
- // line = line.trim().replace(/\. /g, '');
1798
- // line = line.trim().replace(/ +/g, '');
1799
- // const parts = line.split(':');
1800
- // if ((parts[0].toLowerCase().startsWith('standardgate') || parts[0].toLowerCase().indexOf('gateway') > -1 || parts[0].toLowerCase().indexOf('enlace') > -1) && parts[1]) {
1801
- // result = parts[1];
1802
- // }
1803
- // });
1804
- // if (callback) { callback(result); }
1805
- // resolve(result);
1806
- // });
1925
+ util.powerShell("Get-CimInstance -ClassName Win32_IP4RouteTable | Where-Object { $_.Destination -eq '0.0.0.0' -and $_.Mask -eq '0.0.0.0' }").then((data) => {
1926
+ let lines = data.toString().split('\r\n');
1927
+ if (lines.length > 1 && !result) {
1928
+ result = util.getValue(lines, 'NextHop');
1929
+ if (callback) {
1930
+ callback(result);
1807
1931
  }
1808
- });
1932
+ resolve(result);
1933
+ // } else {
1934
+ // exec('ipconfig', util.execOptsWin, function (error, stdout) {
1935
+ // let lines = stdout.toString().split('\r\n');
1936
+ // lines.forEach(function (line) {
1937
+ // line = line.trim().replace(/\. /g, '');
1938
+ // line = line.trim().replace(/ +/g, '');
1939
+ // const parts = line.split(':');
1940
+ // if ((parts[0].toLowerCase().startsWith('standardgate') || parts[0].toLowerCase().indexOf('gateway') > -1 || parts[0].toLowerCase().indexOf('enlace') > -1) && parts[1]) {
1941
+ // result = parts[1];
1942
+ // }
1943
+ // });
1944
+ // if (callback) { callback(result); }
1945
+ // resolve(result);
1946
+ // });
1947
+ }
1948
+ });
1809
1949
  } else {
1810
1950
  if (callback) {
1811
1951
  callback(result);
@@ -1814,7 +1954,9 @@ function networkGatewayDefault(callback) {
1814
1954
  }
1815
1955
  });
1816
1956
  } catch (e) {
1817
- if (callback) { callback(result); }
1957
+ if (callback) {
1958
+ callback(result);
1959
+ }
1818
1960
  resolve(result);
1819
1961
  }
1820
1962
  }