Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

network.js 55KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. 'use strict';
  2. // @ts-check
  3. // ==================================================================================
  4. // network.js
  5. // ----------------------------------------------------------------------------------
  6. // Description: System Information - library
  7. // for Node.js
  8. // Copyright: (c) 2014 - 2020
  9. // Author: Sebastian Hildebrandt
  10. // ----------------------------------------------------------------------------------
  11. // License: MIT
  12. // ==================================================================================
  13. // 9. Network
  14. // ----------------------------------------------------------------------------------
  15. const os = require('os');
  16. const exec = require('child_process').exec;
  17. const execSync = require('child_process').execSync;
  18. const fs = require('fs');
  19. const util = require('./util');
  20. let _platform = process.platform;
  21. const _linux = (_platform === 'linux');
  22. const _darwin = (_platform === 'darwin');
  23. const _windows = (_platform === 'win32');
  24. const _freebsd = (_platform === 'freebsd');
  25. const _openbsd = (_platform === 'openbsd');
  26. const _netbsd = (_platform === 'netbsd');
  27. const _sunos = (_platform === 'sunos');
  28. let _network = {};
  29. let _default_iface = '';
  30. let _ifaces = {};
  31. let _dhcpNics = [];
  32. let _networkInterfaces = [];
  33. let _mac = {};
  34. let pathToIp;
  35. function getDefaultNetworkInterface() {
  36. let ifaces = os.networkInterfaces();
  37. let ifacename = '';
  38. let ifacenameFirst = '';
  39. let scopeid = 9999;
  40. // fallback - "first" external interface (sorted by scopeid)
  41. for (let dev in ifaces) {
  42. if ({}.hasOwnProperty.call(ifaces, dev)) {
  43. ifaces[dev].forEach(function (details) {
  44. if (details && details.internal === false) {
  45. ifacenameFirst = ifacenameFirst || dev; // fallback if no scopeid
  46. if (details.scopeid && details.scopeid < scopeid) {
  47. ifacename = dev;
  48. scopeid = details.scopeid;
  49. }
  50. }
  51. });
  52. }
  53. }
  54. ifacename = ifacename || ifacenameFirst || '';
  55. try {
  56. if (_windows) {
  57. // https://www.inetdaemon.com/tutorials/internet/ip/routing/default_route.shtml
  58. let defaultIp = '';
  59. const cmd = 'netstat -r';
  60. const result = execSync(cmd);
  61. const lines = result.toString().split(os.EOL);
  62. lines.forEach(line => {
  63. line = line.replace(/\s+/g, ' ').trim();
  64. if (line.indexOf('0.0.0.0 0.0.0.0') > -1 && !(/[a-zA-Z]/.test(line))) {
  65. const parts = line.split(' ');
  66. if (parts.length >= 5) {
  67. defaultIp = parts[parts.length - 2];
  68. }
  69. }
  70. });
  71. if (defaultIp) {
  72. for (let dev in ifaces) {
  73. if ({}.hasOwnProperty.call(ifaces, dev)) {
  74. ifaces[dev].forEach(function (details) {
  75. if (details && details.address && details.address === defaultIp) {
  76. ifacename = dev;
  77. }
  78. });
  79. }
  80. }
  81. }
  82. }
  83. if (_linux) {
  84. let cmd = 'ip route 2> /dev/null | grep default';
  85. let result = execSync(cmd);
  86. let parts = result.toString().split('\n')[0].split(/\s+/);
  87. if (parts[0] === 'none' && parts[5]) {
  88. ifacename = parts[5];
  89. } else if (parts[4]) {
  90. ifacename = parts[4];
  91. }
  92. if (ifacename.indexOf(':') > -1) {
  93. ifacename = ifacename.split(':')[1].trim();
  94. }
  95. }
  96. if (_darwin || _freebsd || _openbsd || _netbsd || _sunos) {
  97. let cmd = '';
  98. if (_linux) cmd = 'ip route 2> /dev/null | grep default | awk \'{print $5}\'';
  99. if (_darwin) cmd = 'route -n get default 2>/dev/null | grep interface: | awk \'{print $2}\'';
  100. if (_freebsd || _openbsd || _netbsd || _sunos) cmd = 'route get 0.0.0.0 | grep interface:';
  101. let result = execSync(cmd);
  102. ifacename = result.toString().split('\n')[0];
  103. if (ifacename.indexOf(':') > -1) {
  104. ifacename = ifacename.split(':')[1].trim();
  105. }
  106. }
  107. } catch (e) {
  108. util.noop();
  109. }
  110. if (ifacename) _default_iface = ifacename;
  111. return _default_iface;
  112. }
  113. exports.getDefaultNetworkInterface = getDefaultNetworkInterface;
  114. function getMacAddresses() {
  115. let iface = '';
  116. let mac = '';
  117. let result = {};
  118. if (_linux || _freebsd || _openbsd || _netbsd) {
  119. if (typeof pathToIp === 'undefined') {
  120. try {
  121. const lines = execSync('which ip').toString().split('\n');
  122. if (lines.length && lines[0].indexOf(':') === -1 && lines[0].indexOf('/') === 0) {
  123. pathToIp = lines[0];
  124. } else {
  125. pathToIp = '';
  126. }
  127. } catch (e) {
  128. pathToIp = '';
  129. }
  130. }
  131. try {
  132. const cmd = 'export LC_ALL=C; ' + ((pathToIp) ? pathToIp + ' link show up' : '/sbin/ifconfig') + '; unset LC_ALL';
  133. let res = execSync(cmd);
  134. const lines = res.toString().split('\n');
  135. for (let i = 0; i < lines.length; i++) {
  136. if (lines[i] && lines[i][0] !== ' ') {
  137. if (pathToIp) {
  138. let nextline = lines[i + 1].trim().split(' ');
  139. if (nextline[0] === 'link/ether') {
  140. iface = lines[i].split(' ')[1];
  141. iface = iface.slice(0, iface.length - 1);
  142. mac = nextline[1];
  143. }
  144. } else {
  145. iface = lines[i].split(' ')[0];
  146. mac = lines[i].split('HWaddr ')[1];
  147. }
  148. if (iface && mac) {
  149. result[iface] = mac.trim();
  150. iface = '';
  151. mac = '';
  152. }
  153. }
  154. }
  155. } catch (e) {
  156. util.noop();
  157. }
  158. }
  159. if (_darwin) {
  160. try {
  161. const cmd = '/sbin/ifconfig';
  162. let res = execSync(cmd);
  163. const lines = res.toString().split('\n');
  164. for (let i = 0; i < lines.length; i++) {
  165. if (lines[i] && lines[i][0] !== '\t' && lines[i].indexOf(':') > 0) {
  166. iface = lines[i].split(':')[0];
  167. } else if (lines[i].indexOf('\tether ') === 0) {
  168. mac = lines[i].split('\tether ')[1];
  169. if (iface && mac) {
  170. result[iface] = mac.trim();
  171. iface = '';
  172. mac = '';
  173. }
  174. }
  175. }
  176. } catch (e) {
  177. util.noop();
  178. }
  179. }
  180. return result;
  181. }
  182. function networkInterfaceDefault(callback) {
  183. return new Promise((resolve) => {
  184. process.nextTick(() => {
  185. let result = getDefaultNetworkInterface();
  186. if (callback) { callback(result); }
  187. resolve(result);
  188. });
  189. });
  190. }
  191. exports.networkInterfaceDefault = networkInterfaceDefault;
  192. // --------------------------
  193. // NET - interfaces
  194. function parseLinesWindowsNics(sections, nconfigsections) {
  195. let nics = [];
  196. for (let i in sections) {
  197. if ({}.hasOwnProperty.call(sections, i)) {
  198. if (sections[i].trim() !== '') {
  199. let lines = sections[i].trim().split('\r\n');
  200. let linesNicConfig = nconfigsections[i].trim().split('\r\n');
  201. let netEnabled = util.getValue(lines, 'NetEnabled', '=');
  202. if (netEnabled !== '') {
  203. const speed = parseInt(util.getValue(lines, 'speed', '=').trim(), 10) / 1000000;
  204. nics.push({
  205. mac: util.getValue(lines, 'MACAddress', '=').toLowerCase(),
  206. dhcp: util.getValue(linesNicConfig, 'dhcpEnabled', '=').toLowerCase(),
  207. name: util.getValue(lines, 'Name', '=').replace(/\]/g, ')').replace(/\[/g, '('),
  208. netEnabled: netEnabled === 'TRUE',
  209. speed: isNaN(speed) ? -1 : speed,
  210. operstate: util.getValue(lines, 'NetConnectionStatus', '=') === '2' ? 'up' : 'down',
  211. type: util.getValue(lines, 'AdapterTypeID', '=') === '9' ? 'wireless' : 'wired'
  212. });
  213. }
  214. }
  215. }
  216. }
  217. return nics;
  218. }
  219. function getWindowsNics() {
  220. const cmd = util.getWmic() + ' nic get MACAddress, name, NetEnabled, Speed, NetConnectionStatus, AdapterTypeId /value';
  221. const cmdnicconfig = util.getWmic() + ' nicconfig get dhcpEnabled /value';
  222. try {
  223. const nsections = execSync(cmd, util.execOptsWin).split(/\n\s*\n/);
  224. const nconfigsections = execSync(cmdnicconfig, util.execOptsWin).split(/\n\s*\n/);
  225. return (parseLinesWindowsNics(nsections, nconfigsections));
  226. } catch (e) {
  227. return [];
  228. }
  229. }
  230. function getWindowsDNSsuffixes() {
  231. let iface = {};
  232. let dnsSuffixes = {
  233. primaryDNS: '',
  234. exitCode: 0,
  235. ifaces: [],
  236. };
  237. try {
  238. const ipconfig = execSync('ipconfig /all', util.execOptsWin);
  239. const ipconfigArray = ipconfig.split('\r\n\r\n');
  240. ipconfigArray.forEach((element, index) => {
  241. if (index == 1) {
  242. const longPrimaryDNS = element.split('\r\n').filter((element) => {
  243. return element.toUpperCase().includes('DNS');
  244. });
  245. const primaryDNS = longPrimaryDNS[0].substring(longPrimaryDNS[0].lastIndexOf(':') + 1);
  246. dnsSuffixes.primaryDNS = primaryDNS.trim();
  247. if (!dnsSuffixes.primaryDNS) dnsSuffixes.primaryDNS = 'Not defined';
  248. }
  249. if (index > 1) {
  250. if (index % 2 == 0) {
  251. const name = element.substring(element.lastIndexOf(' ') + 1).replace(':', '');
  252. iface.name = name;
  253. } else {
  254. const connectionSpecificDNS = element.split('\r\n').filter((element) => {
  255. return element.toUpperCase().includes('DNS');
  256. });
  257. const dnsSuffix = connectionSpecificDNS[0].substring(connectionSpecificDNS[0].lastIndexOf(':') + 1);
  258. iface.dnsSuffix = dnsSuffix.trim();
  259. dnsSuffixes.ifaces.push(iface);
  260. iface = {};
  261. }
  262. }
  263. });
  264. return dnsSuffixes;
  265. } catch (error) {
  266. // console.log('An error occurred trying to bring the Connection-specific DNS suffix', error.message);
  267. return {
  268. primaryDNS: '',
  269. exitCode: 0,
  270. ifaces: [],
  271. };
  272. }
  273. }
  274. function getWindowsIfaceDNSsuffix(ifaces, ifacename) {
  275. let dnsSuffix = '';
  276. // Adding (.) to ensure ifacename compatibility when duplicated iface-names
  277. const interfaceName = ifacename + '.';
  278. try {
  279. const connectionDnsSuffix = ifaces.filter((iface) => {
  280. return interfaceName.includes(iface.name + '.');
  281. }).map((iface) => iface.dnsSuffix);
  282. if (connectionDnsSuffix[0]) {
  283. dnsSuffix = connectionDnsSuffix[0];
  284. }
  285. if (!dnsSuffix) dnsSuffix = '';
  286. return dnsSuffix;
  287. } catch (error) {
  288. // console.log('Error getting Connection-specific DNS suffix: ', error.message);
  289. return 'Unknown';
  290. }
  291. }
  292. function getWindowsWiredProfilesInformation() {
  293. try {
  294. const result = execSync('netsh lan show profiles', util.execOptsWin);
  295. const profileList = result.split('\r\nProfile on interface');
  296. return profileList;
  297. } catch (error) {
  298. if (error.status === 1 && error.stdout.includes('AutoConfig')) {
  299. return 'Disabled';
  300. }
  301. return [];
  302. }
  303. }
  304. function getWindowsWirelessIfaceSSID(interfaceName) {
  305. try {
  306. const result = execSync(`netsh wlan show interface name="${interfaceName}" | findstr "SSID"`, util.execOptsWin);
  307. const SSID = result.split('\r\n').shift();
  308. const parseSSID = SSID.split(':').pop();
  309. return parseSSID;
  310. } catch (error) {
  311. return 'Unknown';
  312. }
  313. }
  314. function getWindowsIEEE8021x(connectionType, iface, ifaces) {
  315. let i8021x = {
  316. state: 'Unknown',
  317. protocol: 'Unknown',
  318. };
  319. if (ifaces === 'Disabled') {
  320. i8021x.state = 'Disabled';
  321. i8021x.protocol = 'Not defined';
  322. return i8021x;
  323. }
  324. if (connectionType == 'wired' && ifaces.length > 0) {
  325. try {
  326. // Get 802.1x information by interface name
  327. const iface8021xInfo = ifaces.find((element) => {
  328. return element.includes(iface + '\r\n');
  329. });
  330. const arrayIface8021xInfo = iface8021xInfo.split('\r\n');
  331. const state8021x = arrayIface8021xInfo.find((element) => {
  332. return element.includes('802.1x');
  333. });
  334. if (state8021x.includes('Disabled')) {
  335. i8021x.state = 'Disabled';
  336. i8021x.protocol = 'Not defined';
  337. } else if (state8021x.includes('Enabled')) {
  338. const protocol8021x = arrayIface8021xInfo.find((element) => {
  339. return element.includes('EAP');
  340. });
  341. i8021x.protocol = protocol8021x.split(':').pop();
  342. i8021x.state = 'Enabled';
  343. }
  344. } catch (error) {
  345. // console.log('Error getting wired information:', error);
  346. return i8021x;
  347. }
  348. } else if (connectionType == 'wireless') {
  349. let i8021xState = '';
  350. let i8021xProtocol = '';
  351. try {
  352. const SSID = getWindowsWirelessIfaceSSID(iface);
  353. if (SSID !== 'Unknown') {
  354. i8021xState = execSync(`netsh wlan show profiles "${SSID}" | findstr "802.1X"`, util.execOptsWin);
  355. i8021xProtocol = execSync(`netsh wlan show profiles "${SSID}" | findstr "EAP"`, util.execOptsWin);
  356. }
  357. if (i8021xState.includes(':') && i8021xProtocol.includes(':')) {
  358. i8021x.state = i8021xState.split(':').pop();
  359. i8021x.protocol = i8021xProtocol.split(':').pop();
  360. }
  361. } catch (error) {
  362. // console.log('Error getting wireless information:', error);
  363. if (error.status === 1 && error.stdout.includes('AutoConfig')) {
  364. i8021x.state = 'Disabled';
  365. i8021x.protocol = 'Not defined';
  366. }
  367. return i8021x;
  368. }
  369. }
  370. return i8021x;
  371. }
  372. function splitSectionsNics(lines) {
  373. const result = [];
  374. let section = [];
  375. lines.forEach(function (line) {
  376. if (!line.startsWith('\t') && !line.startsWith(' ')) {
  377. if (section.length) {
  378. result.push(section);
  379. section = [];
  380. }
  381. }
  382. section.push(line);
  383. });
  384. if (section.length) {
  385. result.push(section);
  386. }
  387. return result;
  388. }
  389. function parseLinesDarwinNics(sections) {
  390. let nics = [];
  391. sections.forEach(section => {
  392. let nic = {
  393. iface: '',
  394. mtu: -1,
  395. mac: '',
  396. ip6: '',
  397. ip4: '',
  398. speed: -1,
  399. type: '',
  400. operstate: '',
  401. duplex: '',
  402. internal: false
  403. };
  404. const first = section[0];
  405. nic.iface = first.split(':')[0].trim();
  406. let parts = first.split('> mtu');
  407. nic.mtu = parts.length > 1 ? parseInt(parts[1], 10) : -1;
  408. if (isNaN(nic.mtu)) {
  409. nic.mtu = -1;
  410. }
  411. nic.internal = parts[0].indexOf('LOOPBACK') > -1;
  412. section.forEach(line => {
  413. if (line.trim().startsWith('ether ')) {
  414. nic.mac = line.split('ether ')[1].toLowerCase().trim();
  415. }
  416. if (line.trim().startsWith('inet6 ') && !nic.ip6) {
  417. nic.ip6 = line.split('inet6 ')[1].toLowerCase().split('%')[0].split(' ')[0];
  418. }
  419. if (line.trim().startsWith('inet ') && !nic.ip4) {
  420. nic.ip4 = line.split('inet ')[1].toLowerCase().split(' ')[0];
  421. }
  422. });
  423. let speed = util.getValue(section, 'link rate');
  424. nic.speed = speed ? parseFloat(speed) : -1;
  425. if (nic.speed === -1) {
  426. speed = util.getValue(section, 'uplink rate');
  427. nic.speed = speed ? parseFloat(speed) : -1;
  428. if (nic.speed > -1 && speed.toLowerCase().indexOf('gbps') >= 0) {
  429. nic.speed = nic.speed * 1000;
  430. }
  431. } else {
  432. if (speed.toLowerCase().indexOf('gbps') >= 0) {
  433. nic.speed = nic.speed * 1000;
  434. }
  435. }
  436. nic.type = util.getValue(section, 'type').toLowerCase().indexOf('wi-fi') > -1 ? 'wireless' : 'wired';
  437. nic.operstate = util.getValue(section, 'status').toLowerCase().indexOf('active') > -1 ? 'up' : 'down';
  438. nic.duplex = util.getValue(section, 'media').toLowerCase().indexOf('half-duplex') > -1 ? 'half' : 'full';
  439. if (nic.ip6 || nic.ip4 || nic.mac) {
  440. nics.push(nic);
  441. }
  442. });
  443. return nics;
  444. }
  445. function getDarwinNics() {
  446. const cmd = '/sbin/ifconfig -v';
  447. try {
  448. const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n');
  449. const nsections = splitSectionsNics(lines);
  450. return (parseLinesDarwinNics(nsections));
  451. } catch (e) {
  452. return [];
  453. }
  454. }
  455. function getLinuxIfaceConnectionName(interfaceName) {
  456. const cmd = `nmcli device status 2>/dev/null | grep ${interfaceName}`;
  457. try {
  458. const result = execSync(cmd).toString();
  459. const resultFormat = result.replace(/\s+/g, ' ').trim();
  460. const connectionNameLines = resultFormat.split(' ').slice(3);
  461. const connectionName = connectionNameLines.join(' ');
  462. return connectionName != '--' ? connectionName : '';
  463. } catch (e) {
  464. return '';
  465. }
  466. }
  467. function checkLinuxDCHPInterfaces(file) {
  468. let result = [];
  469. try {
  470. let cmd = `cat ${file} 2> /dev/null | grep 'iface\\|source'`;
  471. const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n');
  472. lines.forEach(line => {
  473. const parts = line.replace(/\s+/g, ' ').trim().split(' ');
  474. if (parts.length >= 4) {
  475. if (line.toLowerCase().indexOf(' inet ') >= 0 && line.toLowerCase().indexOf('dhcp') >= 0) {
  476. result.push(parts[1]);
  477. }
  478. }
  479. if (line.toLowerCase().includes('source')) {
  480. let file = line.split(' ')[1];
  481. result = result.concat(checkLinuxDCHPInterfaces(file));
  482. }
  483. });
  484. } catch (e) {
  485. util.noop();
  486. }
  487. return result;
  488. }
  489. function getLinuxDHCPNics() {
  490. // alternate methods getting interfaces using DHCP
  491. let cmd = 'ip a 2> /dev/null';
  492. let result = [];
  493. try {
  494. const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n');
  495. const nsections = splitSectionsNics(lines);
  496. result = (parseLinuxDHCPNics(nsections));
  497. } catch (e) {
  498. util.noop();
  499. }
  500. try {
  501. result = checkLinuxDCHPInterfaces('/etc/network/interfaces');
  502. } catch (e) {
  503. util.noop();
  504. }
  505. return result;
  506. }
  507. function parseLinuxDHCPNics(sections) {
  508. const result = [];
  509. if (sections && sections.length) {
  510. sections.forEach(lines => {
  511. if (lines && lines.length) {
  512. const parts = lines[0].split(':');
  513. if (parts.length > 2) {
  514. for (let line of lines) {
  515. if (line.indexOf(' inet ') >= 0 && line.indexOf(' dynamic ') >= 0) {
  516. const parts2 = line.split(' ');
  517. const nic = parts2[parts2.length - 1].trim();
  518. result.push(nic);
  519. break;
  520. }
  521. }
  522. }
  523. }
  524. });
  525. }
  526. return result;
  527. }
  528. function getLinuxIfaceDHCPstatus(iface, connectionName, DHCPNics) {
  529. let result = false;
  530. if (connectionName) {
  531. const cmd = `nmcli connection show "${connectionName}" 2>/dev/null \| grep ipv4.method;`;
  532. try {
  533. const lines = execSync(cmd).toString();
  534. const resultFormat = lines.replace(/\s+/g, ' ').trim();
  535. let dhcStatus = resultFormat.split(' ').slice(1).toString();
  536. switch (dhcStatus) {
  537. case 'auto':
  538. result = true;
  539. break;
  540. default:
  541. result = false;
  542. break;
  543. }
  544. return result;
  545. } catch (e) {
  546. return (DHCPNics.indexOf(iface) >= 0);
  547. }
  548. } else {
  549. return (DHCPNics.indexOf(iface) >= 0);
  550. }
  551. }
  552. function getDarwinIfaceDHCPstatus(iface) {
  553. let result = false;
  554. const cmd = `ipconfig getpacket "${iface}" 2>/dev/null \| grep lease_time;`;
  555. try {
  556. const lines = execSync(cmd).toString().split('\n');
  557. if (lines.length && lines[0].startsWith('lease_time')) {
  558. result = true;
  559. }
  560. } catch (e) {
  561. util.noop();
  562. }
  563. return result;
  564. }
  565. function getLinuxIfaceDNSsuffix(connectionName) {
  566. if (connectionName) {
  567. const cmd = `nmcli connection show "${connectionName}" 2>/dev/null \| grep ipv4.dns-search;`;
  568. try {
  569. const result = execSync(cmd).toString();
  570. const resultFormat = result.replace(/\s+/g, ' ').trim();
  571. const dnsSuffix = resultFormat.split(' ').slice(1).toString();
  572. return dnsSuffix == '--' ? 'Not defined' : dnsSuffix;
  573. } catch (e) {
  574. return 'Unknown';
  575. }
  576. } else {
  577. return 'Unknown';
  578. }
  579. }
  580. function getLinuxIfaceIEEE8021xAuth(connectionName) {
  581. if (connectionName) {
  582. const cmd = `nmcli connection show "${connectionName}" 2>/dev/null \| grep 802-1x.eap;`;
  583. try {
  584. const result = execSync(cmd).toString();
  585. const resultFormat = result.replace(/\s+/g, ' ').trim();
  586. const authenticationProtocol = resultFormat.split(' ').slice(1).toString();
  587. return authenticationProtocol == '--' ? '' : authenticationProtocol;
  588. } catch (e) {
  589. return 'Not defined';
  590. }
  591. } else {
  592. return 'Not defined';
  593. }
  594. }
  595. function getLinuxIfaceIEEE8021xState(authenticationProtocol) {
  596. if (authenticationProtocol) {
  597. if (authenticationProtocol == 'Not defined') {
  598. return 'Disabled';
  599. }
  600. return 'Enabled';
  601. } else {
  602. return 'Unknown';
  603. }
  604. }
  605. function testVirtualNic(iface, ifaceName, mac) {
  606. const virtualMacs = ['00:00:00:00:00:00', '00:03:FF', '00:05:69', '00:0C:29', '00:0F:4B', '00:0F:4B', '00:13:07', '00:13:BE', '00:15:5d', '00:16:3E', '00:1C:42', '00:21:F6', '00:21:F6', '00:24:0B', '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'];
  607. if (mac) {
  608. return virtualMacs.filter(item => { return mac.toUpperCase().toUpperCase().startsWith(item.substr(0, mac.length)); }).length > 0 ||
  609. iface.toLowerCase().indexOf(' virtual ') > -1 ||
  610. ifaceName.toLowerCase().indexOf(' virtual ') > -1 ||
  611. iface.toLowerCase().indexOf('vethernet ') > -1 ||
  612. ifaceName.toLowerCase().indexOf('vethernet ') > -1 ||
  613. iface.toLowerCase().startsWith('veth') ||
  614. ifaceName.toLowerCase().startsWith('veth') ||
  615. iface.toLowerCase().startsWith('vboxnet') ||
  616. ifaceName.toLowerCase().startsWith('vboxnet');
  617. } else return false;
  618. }
  619. function networkInterfaces(callback, rescan = true) {
  620. if (typeof callback === 'boolean') {
  621. rescan = callback;
  622. callback = null;
  623. }
  624. return new Promise((resolve) => {
  625. process.nextTick(() => {
  626. let ifaces = os.networkInterfaces();
  627. let result = [];
  628. let nics = [];
  629. let dnsSuffixes = [];
  630. let nics8021xInfo = [];
  631. // seperate handling in OSX
  632. if (_darwin || _freebsd || _openbsd || _netbsd) {
  633. nics = getDarwinNics();
  634. nics.forEach(nic => {
  635. if ({}.hasOwnProperty.call(ifaces, nic.iface)) {
  636. ifaces[nic.iface].forEach(function (details) {
  637. if (details.family === 'IPv4') {
  638. nic.ip4subnet = details.netmask;
  639. }
  640. if (details.family === 'IPv6') {
  641. nic.ip6subnet = details.netmask;
  642. }
  643. });
  644. }
  645. result.push({
  646. iface: nic.iface,
  647. ifaceName: nic.iface,
  648. ip4: nic.ip4,
  649. ip4subnet: nic.ip4subnet || '',
  650. ip6: nic.ip6,
  651. ip6subnet: nic.ip6subnet || '',
  652. mac: nic.mac,
  653. internal: nic.internal,
  654. virtual: nic.internal ? false : testVirtualNic(nic.iface, nic.iface, nic.mac),
  655. operstate: nic.operstate,
  656. type: nic.type,
  657. duplex: nic.duplex,
  658. mtu: nic.mtu,
  659. speed: nic.speed,
  660. dhcp: getDarwinIfaceDHCPstatus(nic.iface),
  661. dnsSuffix: '',
  662. ieee8021xAuth: '',
  663. ieee8021xState: '',
  664. carrierChanges: 0
  665. });
  666. });
  667. _networkInterfaces = result;
  668. if (callback) { callback(result); }
  669. resolve(result);
  670. } else {
  671. if ((JSON.stringify(ifaces) === JSON.stringify(_ifaces)) && !rescan) {
  672. // no changes - just return object
  673. result = _networkInterfaces;
  674. if (callback) { callback(result); }
  675. resolve(result);
  676. } else {
  677. _ifaces = Object.assign({}, ifaces);
  678. if (_windows) {
  679. getWindowsNics().forEach(nic => {
  680. let found = false;
  681. Object.keys(ifaces).forEach(key => {
  682. if (!found) {
  683. ifaces[key].forEach(value => {
  684. if (Object.keys(value).indexOf('mac') >= 0) {
  685. found = value['mac'] === nic.mac;
  686. }
  687. });
  688. }
  689. });
  690. if (!found) {
  691. ifaces[nic.name] = [{ mac: nic.mac }];
  692. }
  693. });
  694. nics8021xInfo = getWindowsWiredProfilesInformation();
  695. nics = getWindowsNics();
  696. dnsSuffixes = getWindowsDNSsuffixes();
  697. }
  698. if (_linux) {
  699. _dhcpNics = getLinuxDHCPNics();
  700. }
  701. for (let dev in ifaces) {
  702. let ip4 = '';
  703. let ip4subnet = '';
  704. let ip6 = '';
  705. let ip6subnet = '';
  706. let mac = '';
  707. let duplex = '';
  708. let mtu = '';
  709. let speed = -1;
  710. let carrierChanges = 0;
  711. let operstate = 'down';
  712. let dhcp = false;
  713. let dnsSuffix = '';
  714. let ieee8021xAuth = '';
  715. let ieee8021xState = '';
  716. let type = '';
  717. if ({}.hasOwnProperty.call(ifaces, dev)) {
  718. let ifaceName = dev;
  719. ifaces[dev].forEach(function (details) {
  720. if (details.family === 'IPv4') {
  721. ip4 = details.address;
  722. ip4subnet = details.netmask;
  723. }
  724. if (details.family === 'IPv6') {
  725. if (!ip6 || ip6.match(/^fe80::/i)) {
  726. ip6 = details.address;
  727. ip6subnet = details.netmask;
  728. }
  729. }
  730. mac = details.mac;
  731. // fallback due to https://github.com/nodejs/node/issues/13581 (node 8.1 - node 8.2)
  732. if (mac.indexOf('00:00:0') > -1 && (_linux || _darwin) && parseInt(process.versions.node.split('.'), 10) === 8) {
  733. if (Object.keys(_mac).length === 0) {
  734. _mac = getMacAddresses();
  735. }
  736. mac = _mac[dev] || '';
  737. }
  738. });
  739. if (_linux) {
  740. let iface = dev.split(':')[0].trim().toLowerCase();
  741. const cmd = `echo -n "addr_assign_type: "; cat /sys/class/net/${iface}/addr_assign_type 2>/dev/null; echo;
  742. echo -n "address: "; cat /sys/class/net/${iface}/address 2>/dev/null; echo;
  743. echo -n "addr_len: "; cat /sys/class/net/${iface}/addr_len 2>/dev/null; echo;
  744. echo -n "broadcast: "; cat /sys/class/net/${iface}/broadcast 2>/dev/null; echo;
  745. echo -n "carrier: "; cat /sys/class/net/${iface}/carrier 2>/dev/null; echo;
  746. echo -n "carrier_changes: "; cat /sys/class/net/${iface}/carrier_changes 2>/dev/null; echo;
  747. echo -n "dev_id: "; cat /sys/class/net/${iface}/dev_id 2>/dev/null; echo;
  748. echo -n "dev_port: "; cat /sys/class/net/${iface}/dev_port 2>/dev/null; echo;
  749. echo -n "dormant: "; cat /sys/class/net/${iface}/dormant 2>/dev/null; echo;
  750. echo -n "duplex: "; cat /sys/class/net/${iface}/duplex 2>/dev/null; echo;
  751. echo -n "flags: "; cat /sys/class/net/${iface}/flags 2>/dev/null; echo;
  752. echo -n "gro_flush_timeout: "; cat /sys/class/net/${iface}/gro_flush_timeout 2>/dev/null; echo;
  753. echo -n "ifalias: "; cat /sys/class/net/${iface}/ifalias 2>/dev/null; echo;
  754. echo -n "ifindex: "; cat /sys/class/net/${iface}/ifindex 2>/dev/null; echo;
  755. echo -n "iflink: "; cat /sys/class/net/${iface}/iflink 2>/dev/null; echo;
  756. echo -n "link_mode: "; cat /sys/class/net/${iface}/link_mode 2>/dev/null; echo;
  757. echo -n "mtu: "; cat /sys/class/net/${iface}/mtu 2>/dev/null; echo;
  758. echo -n "netdev_group: "; cat /sys/class/net/${iface}/netdev_group 2>/dev/null; echo;
  759. echo -n "operstate: "; cat /sys/class/net/${iface}/operstate 2>/dev/null; echo;
  760. echo -n "proto_down: "; cat /sys/class/net/${iface}/proto_down 2>/dev/null; echo;
  761. echo -n "speed: "; cat /sys/class/net/${iface}/speed 2>/dev/null; echo;
  762. echo -n "tx_queue_len: "; cat /sys/class/net/${iface}/tx_queue_len 2>/dev/null; echo;
  763. echo -n "type: "; cat /sys/class/net/${iface}/type 2>/dev/null; echo;
  764. echo -n "wireless: "; cat /proc/net/wireless 2>/dev/null \| grep ${iface}; echo;
  765. echo -n "wirelessspeed: "; iw dev ${iface} link 2>&1 \| grep bitrate; echo;`;
  766. let lines = [];
  767. try {
  768. lines = execSync(cmd).toString().split('\n');
  769. const connectionName = getLinuxIfaceConnectionName(iface);
  770. dhcp = getLinuxIfaceDHCPstatus(iface, connectionName, _dhcpNics);
  771. dnsSuffix = getLinuxIfaceDNSsuffix(connectionName);
  772. ieee8021xAuth = getLinuxIfaceIEEE8021xAuth(connectionName);
  773. ieee8021xState = getLinuxIfaceIEEE8021xState(ieee8021xAuth);
  774. } catch (e) {
  775. util.noop();
  776. }
  777. duplex = util.getValue(lines, 'duplex');
  778. duplex = duplex.startsWith('cat') ? '' : duplex;
  779. mtu = parseInt(util.getValue(lines, 'mtu'), 10);
  780. let myspeed = parseInt(util.getValue(lines, 'speed'), 10);
  781. speed = isNaN(myspeed) ? -1 : myspeed;
  782. let wirelessspeed = util.getValue(lines, 'wirelessspeed').split('tx bitrate: ');
  783. if (speed === -1 && wirelessspeed.length === 2) {
  784. myspeed = parseFloat(wirelessspeed[1]);
  785. speed = isNaN(myspeed) ? -1 : myspeed;
  786. }
  787. carrierChanges = parseInt(util.getValue(lines, 'carrier_changes'), 10);
  788. operstate = util.getValue(lines, 'operstate');
  789. type = operstate === 'up' ? (util.getValue(lines, 'wireless').trim() ? 'wireless' : 'wired') : 'unknown';
  790. if (iface === 'lo' || iface.startsWith('bond')) { type = 'virtual'; }
  791. }
  792. if (_windows) {
  793. dnsSuffix = getWindowsIfaceDNSsuffix(dnsSuffixes.ifaces, dev);
  794. nics.forEach(detail => {
  795. if (detail.mac === mac) {
  796. ifaceName = detail.name;
  797. dhcp = detail.dhcp;
  798. operstate = detail.operstate;
  799. speed = detail.speed;
  800. type = detail.type;
  801. }
  802. });
  803. 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) {
  804. type = 'wireless';
  805. }
  806. const IEEE8021x = getWindowsIEEE8021x(type, dev, nics8021xInfo);
  807. ieee8021xAuth = IEEE8021x.protocol;
  808. ieee8021xState = IEEE8021x.state;
  809. }
  810. let internal = (ifaces[dev] && ifaces[dev][0]) ? ifaces[dev][0].internal : null;
  811. const virtual = internal ? false : testVirtualNic(dev, ifaceName, mac);
  812. result.push({
  813. iface: dev,
  814. ifaceName,
  815. ip4,
  816. ip4subnet,
  817. ip6,
  818. ip6subnet,
  819. mac,
  820. internal,
  821. virtual,
  822. operstate,
  823. type,
  824. duplex,
  825. mtu,
  826. speed,
  827. dhcp,
  828. dnsSuffix,
  829. ieee8021xAuth,
  830. ieee8021xState,
  831. carrierChanges,
  832. });
  833. }
  834. }
  835. _networkInterfaces = result;
  836. if (callback) { callback(result); }
  837. resolve(result);
  838. }
  839. }
  840. });
  841. });
  842. }
  843. exports.networkInterfaces = networkInterfaces;
  844. // --------------------------
  845. // NET - Speed
  846. function calcNetworkSpeed(iface, rx_bytes, tx_bytes, operstate, rx_dropped, rx_errors, tx_dropped, tx_errors) {
  847. let result = {
  848. iface,
  849. operstate,
  850. rx_bytes,
  851. rx_dropped,
  852. rx_errors,
  853. tx_bytes,
  854. tx_dropped,
  855. tx_errors,
  856. rx_sec: -1,
  857. tx_sec: -1,
  858. ms: 0
  859. };
  860. if (_network[iface] && _network[iface].ms) {
  861. result.ms = Date.now() - _network[iface].ms;
  862. result.rx_sec = (rx_bytes - _network[iface].rx_bytes) >= 0 ? (rx_bytes - _network[iface].rx_bytes) / (result.ms / 1000) : 0;
  863. result.tx_sec = (tx_bytes - _network[iface].tx_bytes) >= 0 ? (tx_bytes - _network[iface].tx_bytes) / (result.ms / 1000) : 0;
  864. _network[iface].rx_bytes = rx_bytes;
  865. _network[iface].tx_bytes = tx_bytes;
  866. _network[iface].rx_sec = result.rx_sec;
  867. _network[iface].tx_sec = result.tx_sec;
  868. _network[iface].ms = Date.now();
  869. _network[iface].last_ms = result.ms;
  870. _network[iface].operstate = operstate;
  871. } else {
  872. if (!_network[iface]) _network[iface] = {};
  873. _network[iface].rx_bytes = rx_bytes;
  874. _network[iface].tx_bytes = tx_bytes;
  875. _network[iface].rx_sec = -1;
  876. _network[iface].tx_sec = -1;
  877. _network[iface].ms = Date.now();
  878. _network[iface].last_ms = 0;
  879. _network[iface].operstate = operstate;
  880. }
  881. return result;
  882. }
  883. function networkStats(ifaces, callback) {
  884. let ifacesArray = [];
  885. // fallback - if only callback is given
  886. if (util.isFunction(ifaces) && !callback) {
  887. callback = ifaces;
  888. ifacesArray = [getDefaultNetworkInterface()];
  889. } else {
  890. ifaces = ifaces || getDefaultNetworkInterface();
  891. ifaces = ifaces.trim().toLowerCase().replace(/,+/g, '|');
  892. ifacesArray = ifaces.split('|');
  893. }
  894. return new Promise((resolve) => {
  895. process.nextTick(() => {
  896. const result = [];
  897. const workload = [];
  898. if (ifacesArray.length && ifacesArray[0].trim() === '*') {
  899. ifacesArray = [];
  900. networkInterfaces(false).then(allIFaces => {
  901. for (let iface of allIFaces) {
  902. ifacesArray.push(iface.iface);
  903. }
  904. networkStats(ifacesArray.join(',')).then(result => {
  905. if (callback) { callback(result); }
  906. resolve(result);
  907. });
  908. });
  909. } else {
  910. for (let iface of ifacesArray) {
  911. workload.push(networkStatsSingle(iface.trim()));
  912. }
  913. if (workload.length) {
  914. Promise.all(
  915. workload
  916. ).then(data => {
  917. if (callback) { callback(data); }
  918. resolve(data);
  919. });
  920. } else {
  921. if (callback) { callback(result); }
  922. resolve(result);
  923. }
  924. }
  925. });
  926. });
  927. }
  928. function networkStatsSingle(iface) {
  929. function parseLinesWindowsPerfData(sections) {
  930. let perfData = [];
  931. for (let i in sections) {
  932. if ({}.hasOwnProperty.call(sections, i)) {
  933. if (sections[i].trim() !== '') {
  934. let lines = sections[i].trim().split('\r\n');
  935. perfData.push({
  936. name: util.getValue(lines, 'Name', '=').replace(/[()\[\] ]+/g, '').replace('#', '_').toLowerCase(),
  937. rx_bytes: parseInt(util.getValue(lines, 'BytesReceivedPersec', '='), 10),
  938. rx_errors: parseInt(util.getValue(lines, 'PacketsReceivedErrors', '='), 10),
  939. rx_dropped: parseInt(util.getValue(lines, 'PacketsReceivedDiscarded', '='), 10),
  940. tx_bytes: parseInt(util.getValue(lines, 'BytesSentPersec', '='), 10),
  941. tx_errors: parseInt(util.getValue(lines, 'PacketsOutboundErrors', '='), 10),
  942. tx_dropped: parseInt(util.getValue(lines, 'PacketsOutboundDiscarded', '='), 10)
  943. });
  944. }
  945. }
  946. }
  947. return perfData;
  948. }
  949. return new Promise((resolve) => {
  950. process.nextTick(() => {
  951. const ifaceSanitized = util.sanitizeShellString(iface);
  952. let result = {
  953. iface: ifaceSanitized,
  954. operstate: 'unknown',
  955. rx_bytes: 0,
  956. rx_dropped: 0,
  957. rx_errors: 0,
  958. tx_bytes: 0,
  959. tx_dropped: 0,
  960. tx_errors: 0,
  961. rx_sec: -1,
  962. tx_sec: -1,
  963. ms: 0
  964. };
  965. let operstate = 'unknown';
  966. let rx_bytes = 0;
  967. let tx_bytes = 0;
  968. let rx_dropped = 0;
  969. let rx_errors = 0;
  970. let tx_dropped = 0;
  971. let tx_errors = 0;
  972. let cmd, lines, stats;
  973. if (!_network[ifaceSanitized] || (_network[ifaceSanitized] && !_network[ifaceSanitized].ms) || (_network[ifaceSanitized] && _network[ifaceSanitized].ms && Date.now() - _network[ifaceSanitized].ms >= 500)) {
  974. if (_linux) {
  975. if (fs.existsSync('/sys/class/net/' + ifaceSanitized)) {
  976. cmd =
  977. 'cat /sys/class/net/' + ifaceSanitized + '/operstate; ' +
  978. 'cat /sys/class/net/' + ifaceSanitized + '/statistics/rx_bytes; ' +
  979. 'cat /sys/class/net/' + ifaceSanitized + '/statistics/tx_bytes; ' +
  980. 'cat /sys/class/net/' + ifaceSanitized + '/statistics/rx_dropped; ' +
  981. 'cat /sys/class/net/' + ifaceSanitized + '/statistics/rx_errors; ' +
  982. 'cat /sys/class/net/' + ifaceSanitized + '/statistics/tx_dropped; ' +
  983. 'cat /sys/class/net/' + ifaceSanitized + '/statistics/tx_errors; ';
  984. exec(cmd, function (error, stdout) {
  985. if (!error) {
  986. lines = stdout.toString().split('\n');
  987. operstate = lines[0].trim();
  988. rx_bytes = parseInt(lines[1], 10);
  989. tx_bytes = parseInt(lines[2], 10);
  990. rx_dropped = parseInt(lines[3], 10);
  991. rx_errors = parseInt(lines[4], 10);
  992. tx_dropped = parseInt(lines[5], 10);
  993. tx_errors = parseInt(lines[6], 10);
  994. result = calcNetworkSpeed(ifaceSanitized, rx_bytes, tx_bytes, operstate, rx_dropped, rx_errors, tx_dropped, tx_errors);
  995. }
  996. resolve(result);
  997. });
  998. } else {
  999. resolve(result);
  1000. }
  1001. }
  1002. if (_freebsd || _openbsd || _netbsd) {
  1003. cmd = 'netstat -ibndI ' + ifaceSanitized;
  1004. exec(cmd, function (error, stdout) {
  1005. if (!error) {
  1006. lines = stdout.toString().split('\n');
  1007. for (let i = 1; i < lines.length; i++) {
  1008. const line = lines[i].replace(/ +/g, ' ').split(' ');
  1009. if (line && line[0] && line[7] && line[10]) {
  1010. rx_bytes = rx_bytes + parseInt(line[7]);
  1011. if (line[6].trim() !== '-') { rx_dropped = rx_dropped + parseInt(line[6]); }
  1012. if (line[5].trim() !== '-') { rx_errors = rx_errors + parseInt(line[5]); }
  1013. tx_bytes = tx_bytes + parseInt(line[10]);
  1014. if (line[12].trim() !== '-') { tx_dropped = tx_dropped + parseInt(line[12]); }
  1015. if (line[9].trim() !== '-') { tx_errors = tx_errors + parseInt(line[9]); }
  1016. operstate = 'up';
  1017. }
  1018. }
  1019. result = calcNetworkSpeed(ifaceSanitized, rx_bytes, tx_bytes, operstate, rx_dropped, rx_errors, tx_dropped, tx_errors);
  1020. }
  1021. resolve(result);
  1022. });
  1023. }
  1024. if (_darwin) {
  1025. cmd = 'ifconfig ' + ifaceSanitized + ' | grep "status"';
  1026. exec(cmd, function (error, stdout) {
  1027. result.operstate = (stdout.toString().split(':')[1] || '').trim();
  1028. result.operstate = (result.operstate || '').toLowerCase();
  1029. result.operstate = (result.operstate === 'active' ? 'up' : (result.operstate === 'inactive' ? 'down' : 'unknown'));
  1030. cmd = 'netstat -bdI ' + ifaceSanitized;
  1031. exec(cmd, function (error, stdout) {
  1032. if (!error) {
  1033. lines = stdout.toString().split('\n');
  1034. // if there is less than 2 lines, no information for this interface was found
  1035. if (lines.length > 1 && lines[1].trim() !== '') {
  1036. // skip header line
  1037. // use the second line because it is tied to the NIC instead of the ipv4 or ipv6 address
  1038. stats = lines[1].replace(/ +/g, ' ').split(' ');
  1039. rx_bytes = parseInt(stats[6]);
  1040. rx_dropped = parseInt(stats[11]);
  1041. rx_errors = parseInt(stats[5]);
  1042. tx_bytes = parseInt(stats[9]);
  1043. tx_dropped = parseInt(stats[11]);
  1044. tx_errors = parseInt(stats[8]);
  1045. result = calcNetworkSpeed(ifaceSanitized, rx_bytes, tx_bytes, result.operstate, rx_dropped, rx_errors, tx_dropped, tx_errors);
  1046. }
  1047. }
  1048. resolve(result);
  1049. });
  1050. });
  1051. }
  1052. if (_windows) {
  1053. let perfData = [];
  1054. let ifaceName = ifaceSanitized;
  1055. // Performance Data
  1056. util.wmic('path Win32_PerfRawData_Tcpip_NetworkInterface Get name,BytesReceivedPersec,BytesSentPersec,BytesTotalPersec,PacketsOutboundDiscarded,PacketsOutboundErrors,PacketsReceivedDiscarded,PacketsReceivedErrors /value').then((stdout, error) => {
  1057. if (!error) {
  1058. const psections = stdout.toString().split(/\n\s*\n/);
  1059. perfData = parseLinesWindowsPerfData(psections);
  1060. }
  1061. // Network Interfaces
  1062. networkInterfaces(false).then(interfaces => {
  1063. // get bytes sent, received from perfData by name
  1064. rx_bytes = 0;
  1065. tx_bytes = 0;
  1066. perfData.forEach(detail => {
  1067. interfaces.forEach(det => {
  1068. if ((det.iface.toLowerCase() === ifaceSanitized.toLowerCase() ||
  1069. det.mac.toLowerCase() === ifaceSanitized.toLowerCase() ||
  1070. det.ip4.toLowerCase() === ifaceSanitized.toLowerCase() ||
  1071. det.ip6.toLowerCase() === ifaceSanitized.toLowerCase() ||
  1072. det.ifaceName.replace(/[()\[\] ]+/g, '').replace('#', '_').toLowerCase() === ifaceSanitized.replace(/[()\[\] ]+/g, '').replace('#', '_').toLowerCase()) &&
  1073. (det.ifaceName.replace(/[()\[\] ]+/g, '').replace('#', '_').toLowerCase() === detail.name)) {
  1074. ifaceName = det.iface;
  1075. rx_bytes = detail.rx_bytes;
  1076. rx_dropped = detail.rx_dropped;
  1077. rx_errors = detail.rx_errors;
  1078. tx_bytes = detail.tx_bytes;
  1079. tx_dropped = detail.tx_dropped;
  1080. tx_errors = detail.tx_errors;
  1081. operstate = det.operstate;
  1082. }
  1083. });
  1084. });
  1085. if (rx_bytes && tx_bytes) {
  1086. result = calcNetworkSpeed(ifaceName, parseInt(rx_bytes), parseInt(tx_bytes), operstate, rx_dropped, rx_errors, tx_dropped, tx_errors);
  1087. }
  1088. resolve(result);
  1089. });
  1090. });
  1091. }
  1092. } else {
  1093. result.rx_bytes = _network[ifaceSanitized].rx_bytes;
  1094. result.tx_bytes = _network[ifaceSanitized].tx_bytes;
  1095. result.rx_sec = _network[ifaceSanitized].rx_sec;
  1096. result.tx_sec = _network[ifaceSanitized].tx_sec;
  1097. result.ms = _network[ifaceSanitized].last_ms;
  1098. result.operstate = _network[ifaceSanitized].operstate;
  1099. resolve(result);
  1100. }
  1101. });
  1102. });
  1103. }
  1104. exports.networkStats = networkStats;
  1105. // --------------------------
  1106. // NET - connections (sockets)
  1107. function networkConnections(callback) {
  1108. return new Promise((resolve) => {
  1109. process.nextTick(() => {
  1110. let result = [];
  1111. if (_linux || _freebsd || _openbsd || _netbsd) {
  1112. 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';
  1113. 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';
  1114. exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
  1115. let lines = stdout.toString().split('\n');
  1116. if (!error && (lines.length > 1 || lines[0] != '')) {
  1117. lines.forEach(function (line) {
  1118. line = line.replace(/ +/g, ' ').split(' ');
  1119. if (line.length >= 7) {
  1120. let localip = line[3];
  1121. let localport = '';
  1122. let localaddress = line[3].split(':');
  1123. if (localaddress.length > 1) {
  1124. localport = localaddress[localaddress.length - 1];
  1125. localaddress.pop();
  1126. localip = localaddress.join(':');
  1127. }
  1128. let peerip = line[4];
  1129. let peerport = '';
  1130. let peeraddress = line[4].split(':');
  1131. if (peeraddress.length > 1) {
  1132. peerport = peeraddress[peeraddress.length - 1];
  1133. peeraddress.pop();
  1134. peerip = peeraddress.join(':');
  1135. }
  1136. let connstate = line[5];
  1137. // if (connstate === 'VERBUNDEN') connstate = 'ESTABLISHED';
  1138. let proc = line[6].split('/');
  1139. if (connstate) {
  1140. result.push({
  1141. protocol: line[0],
  1142. localaddress: localip,
  1143. localport: localport,
  1144. peeraddress: peerip,
  1145. peerport: peerport,
  1146. state: connstate,
  1147. pid: proc[0] && proc[0] !== '-' ? parseInt(proc[0], 10) : -1,
  1148. process: proc[1] ? proc[1].split(' ')[0] : ''
  1149. });
  1150. }
  1151. }
  1152. });
  1153. if (callback) {
  1154. callback(result);
  1155. }
  1156. resolve(result);
  1157. } else {
  1158. cmd = 'ss -tunap | grep "ESTAB\\|SYN-SENT\\|SYN-RECV\\|FIN-WAIT1\\|FIN-WAIT2\\|TIME-WAIT\\|CLOSE\\|CLOSE-WAIT\\|LAST-ACK\\|LISTEN\\|CLOSING"';
  1159. exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
  1160. if (!error) {
  1161. let lines = stdout.toString().split('\n');
  1162. lines.forEach(function (line) {
  1163. line = line.replace(/ +/g, ' ').split(' ');
  1164. if (line.length >= 6) {
  1165. let localip = line[4];
  1166. let localport = '';
  1167. let localaddress = line[4].split(':');
  1168. if (localaddress.length > 1) {
  1169. localport = localaddress[localaddress.length - 1];
  1170. localaddress.pop();
  1171. localip = localaddress.join(':');
  1172. }
  1173. let peerip = line[5];
  1174. let peerport = '';
  1175. let peeraddress = line[5].split(':');
  1176. if (peeraddress.length > 1) {
  1177. peerport = peeraddress[peeraddress.length - 1];
  1178. peeraddress.pop();
  1179. peerip = peeraddress.join(':');
  1180. }
  1181. let connstate = line[1];
  1182. if (connstate === 'ESTAB') connstate = 'ESTABLISHED';
  1183. if (connstate === 'TIME-WAIT') connstate = 'TIME_WAIT';
  1184. let pid = -1;
  1185. let process = '';
  1186. if (line.length >= 7 && line[6].indexOf('users:') > -1) {
  1187. let proc = line[6].replace('users:(("', '').replace(/"/g, '').split(',');
  1188. if (proc.length > 2) {
  1189. process = proc[0].split(' ')[0];
  1190. pid = parseInt(proc[1], 10);
  1191. }
  1192. }
  1193. if (connstate) {
  1194. result.push({
  1195. protocol: line[0],
  1196. localaddress: localip,
  1197. localport: localport,
  1198. peeraddress: peerip,
  1199. peerport: peerport,
  1200. state: connstate,
  1201. pid,
  1202. process
  1203. });
  1204. }
  1205. }
  1206. });
  1207. }
  1208. if (callback) {
  1209. callback(result);
  1210. }
  1211. resolve(result);
  1212. });
  1213. }
  1214. });
  1215. }
  1216. if (_darwin) {
  1217. let cmd = 'netstat -natv | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"';
  1218. exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
  1219. if (!error) {
  1220. let lines = stdout.toString().split('\n');
  1221. lines.forEach(function (line) {
  1222. line = line.replace(/ +/g, ' ').split(' ');
  1223. if (line.length >= 8) {
  1224. let localip = line[3];
  1225. let localport = '';
  1226. let localaddress = line[3].split('.');
  1227. if (localaddress.length > 1) {
  1228. localport = localaddress[localaddress.length - 1];
  1229. localaddress.pop();
  1230. localip = localaddress.join('.');
  1231. }
  1232. let peerip = line[4];
  1233. let peerport = '';
  1234. let peeraddress = line[4].split('.');
  1235. if (peeraddress.length > 1) {
  1236. peerport = peeraddress[peeraddress.length - 1];
  1237. peeraddress.pop();
  1238. peerip = peeraddress.join('.');
  1239. }
  1240. let connstate = line[5];
  1241. let pid = parseInt(line[8], 10);
  1242. if (connstate) {
  1243. result.push({
  1244. protocol: line[0],
  1245. localaddress: localip,
  1246. localport: localport,
  1247. peeraddress: peerip,
  1248. peerport: peerport,
  1249. state: connstate,
  1250. pid: pid,
  1251. process: ''
  1252. });
  1253. }
  1254. }
  1255. });
  1256. if (callback) {
  1257. callback(result);
  1258. }
  1259. resolve(result);
  1260. }
  1261. });
  1262. }
  1263. if (_windows) {
  1264. let cmd = 'netstat -nao';
  1265. try {
  1266. exec(cmd, util.execOptsWin, function (error, stdout) {
  1267. if (!error) {
  1268. let lines = stdout.toString().split('\r\n');
  1269. lines.forEach(function (line) {
  1270. line = line.trim().replace(/ +/g, ' ').split(' ');
  1271. if (line.length >= 4) {
  1272. let localip = line[1];
  1273. let localport = '';
  1274. let localaddress = line[1].split(':');
  1275. if (localaddress.length > 1) {
  1276. localport = localaddress[localaddress.length - 1];
  1277. localaddress.pop();
  1278. localip = localaddress.join(':');
  1279. }
  1280. let peerip = line[2];
  1281. let peerport = '';
  1282. let peeraddress = line[2].split(':');
  1283. if (peeraddress.length > 1) {
  1284. peerport = peeraddress[peeraddress.length - 1];
  1285. peeraddress.pop();
  1286. peerip = peeraddress.join(':');
  1287. }
  1288. let pid = line[4];
  1289. let connstate = line[3];
  1290. if (connstate === 'HERGESTELLT') connstate = 'ESTABLISHED';
  1291. if (connstate.startsWith('ABH')) connstate = 'LISTEN';
  1292. if (connstate === 'SCHLIESSEN_WARTEN') connstate = 'CLOSE_WAIT';
  1293. if (connstate === 'WARTEND') connstate = 'TIME_WAIT';
  1294. if (connstate === 'SYN_GESENDET') connstate = 'SYN_SENT';
  1295. if (connstate === 'LISTENING') connstate = 'LISTEN';
  1296. if (connstate === 'SYN_RECEIVED') connstate = 'SYN_RECV';
  1297. if (connstate === 'FIN_WAIT_1') connstate = 'FIN_WAIT1';
  1298. if (connstate === 'FIN_WAIT_2') connstate = 'FIN_WAIT2';
  1299. if (connstate) {
  1300. result.push({
  1301. protocol: line[0].toLowerCase(),
  1302. localaddress: localip,
  1303. localport: localport,
  1304. peeraddress: peerip,
  1305. peerport: peerport,
  1306. state: connstate,
  1307. pid,
  1308. process: ''
  1309. });
  1310. }
  1311. }
  1312. });
  1313. if (callback) {
  1314. callback(result);
  1315. }
  1316. resolve(result);
  1317. }
  1318. });
  1319. } catch (e) {
  1320. if (callback) { callback(result); }
  1321. resolve(result);
  1322. }
  1323. }
  1324. });
  1325. });
  1326. }
  1327. exports.networkConnections = networkConnections;
  1328. function networkGatewayDefault(callback) {
  1329. return new Promise((resolve) => {
  1330. process.nextTick(() => {
  1331. let result = '';
  1332. if (_linux || _freebsd || _openbsd || _netbsd) {
  1333. let cmd = 'ip route get 1';
  1334. try {
  1335. exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
  1336. if (!error) {
  1337. let lines = stdout.toString().split('\n');
  1338. const line = lines && lines[0] ? lines[0] : '';
  1339. let parts = line.split(' via ');
  1340. if (parts && parts[1]) {
  1341. parts = parts[1].split(' ');
  1342. result = parts[0];
  1343. }
  1344. if (callback) {
  1345. callback(result);
  1346. }
  1347. resolve(result);
  1348. } else {
  1349. if (callback) {
  1350. callback(result);
  1351. }
  1352. resolve(result);
  1353. }
  1354. });
  1355. } catch (e) {
  1356. if (callback) { callback(result); }
  1357. resolve(result);
  1358. }
  1359. }
  1360. if (_darwin) {
  1361. let cmd = 'route -n get default';
  1362. try {
  1363. exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
  1364. if (!error) {
  1365. let lines = stdout.toString().split('\n').map(line => line.trim());
  1366. result = util.getValue(lines, 'gateway');
  1367. if (callback) {
  1368. callback(result);
  1369. }
  1370. resolve(result);
  1371. } else {
  1372. if (callback) {
  1373. callback(result);
  1374. }
  1375. resolve(result);
  1376. }
  1377. });
  1378. } catch (e) {
  1379. if (callback) { callback(result); }
  1380. resolve(result);
  1381. }
  1382. }
  1383. if (_windows) {
  1384. try {
  1385. exec('netstat -r', util.execOptsWin, function (error, stdout) {
  1386. const lines = stdout.toString().split(os.EOL);
  1387. lines.forEach(line => {
  1388. line = line.replace(/\s+/g, ' ').trim();
  1389. if (line.indexOf('0.0.0.0 0.0.0.0') > -1 && !(/[a-zA-Z]/.test(line))) {
  1390. const parts = line.split(' ');
  1391. if (parts.length >= 5 && (parts[parts.length - 3]).indexOf('.') > -1) {
  1392. result = parts[parts.length - 3];
  1393. }
  1394. }
  1395. });
  1396. if (!result) {
  1397. util.powerShell('Get-CimInstance -ClassName Win32_IP4RouteTable | Where-Object { $_.Destination -eq \'0.0.0.0\' -and $_.Mask -eq \'0.0.0.0\' }')
  1398. .then(data => {
  1399. let lines = data.toString().split('\r\n');
  1400. if (lines.length > 1 && !result) {
  1401. result = util.getValue(lines, 'NextHop');
  1402. if (callback) {
  1403. callback(result);
  1404. }
  1405. resolve(result);
  1406. // } else {
  1407. // exec('ipconfig', util.execOptsWin, function (error, stdout) {
  1408. // let lines = stdout.toString().split('\r\n');
  1409. // lines.forEach(function (line) {
  1410. // line = line.trim().replace(/\. /g, '');
  1411. // line = line.trim().replace(/ +/g, '');
  1412. // const parts = line.split(':');
  1413. // if ((parts[0].toLowerCase().startsWith('standardgate') || parts[0].toLowerCase().indexOf('gateway') > -1 || parts[0].toLowerCase().indexOf('enlace') > -1) && parts[1]) {
  1414. // result = parts[1];
  1415. // }
  1416. // });
  1417. // if (callback) { callback(result); }
  1418. // resolve(result);
  1419. // });
  1420. }
  1421. });
  1422. } else {
  1423. if (callback) {
  1424. callback(result);
  1425. }
  1426. resolve(result);
  1427. }
  1428. });
  1429. } catch (e) {
  1430. if (callback) { callback(result); }
  1431. resolve(result);
  1432. }
  1433. }
  1434. });
  1435. });
  1436. }
  1437. exports.networkGatewayDefault = networkGatewayDefault;