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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. 'use strict';
  2. // @ts-check
  3. // ==================================================================================
  4. // wifi.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. wifi
  14. // ----------------------------------------------------------------------------------
  15. const os = require('os');
  16. const exec = require('child_process').exec;
  17. const util = require('./util');
  18. let _platform = process.platform;
  19. const _linux = (_platform === 'linux');
  20. const _darwin = (_platform === 'darwin');
  21. const _windows = (_platform === 'win32');
  22. function wifiDBFromQuality(quality) {
  23. return (parseFloat(quality) / 2 - 100);
  24. }
  25. function wifiQualityFromDB(db) {
  26. const result = 2 * (parseFloat(db) + 100);
  27. return result <= 100 ? result : 100;
  28. }
  29. function wifiFrequencyFromChannel(channel) {
  30. const frequencies = {
  31. 1: 2412,
  32. 2: 2417,
  33. 3: 2422,
  34. 4: 2427,
  35. 5: 2432,
  36. 6: 2437,
  37. 7: 2442,
  38. 8: 2447,
  39. 9: 2452,
  40. 10: 2457,
  41. 11: 2462,
  42. 12: 2467,
  43. 13: 2472,
  44. 14: 2484,
  45. 32: 5160,
  46. 34: 5170,
  47. 36: 5180,
  48. 38: 5190,
  49. 40: 5200,
  50. 42: 5210,
  51. 44: 5220,
  52. 46: 5230,
  53. 48: 5240,
  54. 50: 5250,
  55. 52: 5260,
  56. 54: 5270,
  57. 56: 5280,
  58. 58: 5290,
  59. 60: 5300,
  60. 62: 5310,
  61. 64: 5320,
  62. 68: 5340,
  63. 96: 5480,
  64. 100: 5500,
  65. 102: 5510,
  66. 104: 5520,
  67. 106: 5530,
  68. 108: 5540,
  69. 110: 5550,
  70. 112: 5560,
  71. 114: 5570,
  72. 116: 5580,
  73. 118: 5590,
  74. 120: 5600,
  75. 122: 5610,
  76. 124: 5620,
  77. 126: 5630,
  78. 128: 5640,
  79. 132: 5660,
  80. 134: 5670,
  81. 136: 5680,
  82. 138: 5690,
  83. 140: 5700,
  84. 142: 5710,
  85. 144: 5720,
  86. 149: 5745,
  87. 151: 5755,
  88. 153: 5765,
  89. 155: 5775,
  90. 157: 5785,
  91. 159: 5795,
  92. 161: 5805,
  93. 165: 5825,
  94. 169: 5845,
  95. 173: 5865,
  96. 183: 4915,
  97. 184: 4920,
  98. 185: 4925,
  99. 187: 4935,
  100. 188: 4940,
  101. 189: 4945,
  102. 192: 4960,
  103. 196: 4980
  104. };
  105. return {}.hasOwnProperty.call(frequencies, channel) ? frequencies[channel] : -1;
  106. }
  107. function wifiNetworks(callback) {
  108. return new Promise((resolve) => {
  109. process.nextTick(() => {
  110. let result = [];
  111. if (_linux) {
  112. let cmd = 'nmcli --terse --fields active,ssid,bssid,mode,chan,freq,signal,security,wpa-flags,rsn-flags device wifi list 2>/dev/null';
  113. exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
  114. const parts = stdout.toString().split('ACTIVE:');
  115. parts.shift();
  116. parts.forEach(part => {
  117. part = 'ACTIVE:' + part;
  118. const lines = part.split(os.EOL);
  119. const channel = util.getValue(lines, 'CHAN');
  120. const frequency = util.getValue(lines, 'FREQ').toLowerCase().replace('mhz', '').trim();
  121. const security = util.getValue(lines, 'SECURITY').replace('(', '').replace(')', '');
  122. const wpaFlags = util.getValue(lines, 'WPA-FLAGS').replace('(', '').replace(')', '');
  123. const rsnFlags = util.getValue(lines, 'RSN-FLAGS').replace('(', '').replace(')', '');
  124. result.push({
  125. ssid: util.getValue(lines, 'SSID'),
  126. bssid: util.getValue(lines, 'BSSID'),
  127. mode: util.getValue(lines, 'MODE'),
  128. channel: channel ? parseInt(channel, 10) : -1,
  129. frequency: frequency ? parseInt(frequency, 10) : -1,
  130. signalLevel: wifiDBFromQuality(util.getValue(lines, 'SIGNAL')),
  131. quality: parseFloat(util.getValue(lines, 'SIGNAL')),
  132. security: security && security !== 'none' ? security.split(' ') : [],
  133. wpaFlags: wpaFlags && wpaFlags !== 'none' ? wpaFlags.split(' ') : [],
  134. rsnFlags: rsnFlags && rsnFlags !== 'none' ? rsnFlags.split(' ') : []
  135. });
  136. });
  137. if (callback) {
  138. callback(result);
  139. }
  140. resolve(result);
  141. });
  142. } else if (_darwin) {
  143. let cmd = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s';
  144. exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
  145. const lines = stdout.toString().split(os.EOL);
  146. if (lines && lines.length > 1) {
  147. const parsedhead = util.parseHead(lines[0], 1);
  148. if (parsedhead.length >= 7) {
  149. lines.shift();
  150. lines.forEach(line => {
  151. if (line.trim()) {
  152. const channelStr = line.substring(parsedhead[3].from, parsedhead[3].to).trim();
  153. const channel = channelStr ? parseInt(channelStr, 10) : -1;
  154. const signalLevel = line.substring(parsedhead[2].from, parsedhead[2].to).trim();
  155. const securityAll = line.substring(parsedhead[6].from, 1000).trim().split(' ');
  156. let security = [];
  157. let wpaFlags = [];
  158. securityAll.forEach(securitySingle => {
  159. if (securitySingle.indexOf('(') > 0) {
  160. const parts = securitySingle.split('(');
  161. security.push(parts[0]);
  162. wpaFlags = wpaFlags.concat(parts[1].replace(')', '').split(','));
  163. }
  164. });
  165. wpaFlags = Array.from(new Set(wpaFlags));
  166. result.push({
  167. ssid: line.substring(parsedhead[0].from, parsedhead[0].to).trim(),
  168. bssid: line.substring(parsedhead[1].from, parsedhead[1].to).trim(),
  169. mode: '',
  170. channel,
  171. frequency: wifiFrequencyFromChannel(channel),
  172. signalLevel: signalLevel ? parseInt(signalLevel, 10) : -1,
  173. quality: wifiQualityFromDB(signalLevel),
  174. security,
  175. wpaFlags,
  176. rsnFlags: []
  177. });
  178. }
  179. });
  180. }
  181. }
  182. if (callback) {
  183. callback(result);
  184. }
  185. resolve(result);
  186. });
  187. } else if (_windows) {
  188. let cmd = 'chcp 65001 && netsh wlan show networks mode=Bssid';
  189. exec(cmd, util.execOptsWin, function (error, stdout) {
  190. const parts = stdout.toString('utf8').split(os.EOL + os.EOL + 'SSID ');
  191. parts.shift();
  192. parts.forEach(part => {
  193. const lines = part.split(os.EOL);
  194. if (lines && lines.length >= 8 && lines[0].indexOf(':') >= 0) {
  195. let bssid = lines[4].split(':');
  196. bssid.shift();
  197. bssid = bssid.join(':').trim();
  198. const channel = lines[7].split(':').pop().trim();
  199. const quality = lines[5].split(':').pop().trim();
  200. result.push({
  201. ssid: lines[0].split(':').pop().trim(),
  202. bssid,
  203. mode: '',
  204. channel: channel ? parseInt(channel, 10) : -1,
  205. frequency: wifiFrequencyFromChannel(channel),
  206. signalLevel: wifiDBFromQuality(quality),
  207. quality: quality ? parseInt(quality, 10) : -1,
  208. security: [lines[2].split(':').pop().trim()],
  209. wpaFlags: [lines[3].split(':').pop().trim()],
  210. rsnFlags: []
  211. });
  212. }
  213. });
  214. if (callback) {
  215. callback(result);
  216. }
  217. resolve(result);
  218. });
  219. } else {
  220. if (callback) {
  221. callback(result);
  222. }
  223. resolve(result);
  224. }
  225. });
  226. });
  227. }
  228. exports.wifiNetworks = wifiNetworks;