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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. 'use strict';
  2. // @ts-check
  3. // ==================================================================================
  4. // internet.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. // 12. Internet
  14. // ----------------------------------------------------------------------------------
  15. const exec = require('child_process').exec;
  16. const util = require('./util');
  17. let _platform = process.platform;
  18. const _linux = (_platform === 'linux');
  19. const _darwin = (_platform === 'darwin');
  20. const _windows = (_platform === 'win32');
  21. const _freebsd = (_platform === 'freebsd');
  22. const _openbsd = (_platform === 'openbsd');
  23. const _netbsd = (_platform === 'netbsd');
  24. const _sunos = (_platform === 'sunos');
  25. // --------------------------
  26. // check if external site is available
  27. function inetChecksite(url, callback) {
  28. return new Promise((resolve) => {
  29. process.nextTick(() => {
  30. let urlSanitized = '';
  31. const s = util.sanitizeShellString(url);
  32. for (let i = 0; i <= 2000; i++) {
  33. if (!(s[i] === undefined ||
  34. s[i] === ' ' ||
  35. s[i] === '{' ||
  36. s[i] === '}')) {
  37. s[i].__proto__.toLowerCase = util.stringToLower;
  38. const sl = s[i].toLowerCase();
  39. if (sl && sl[0] && !sl[1]) {
  40. urlSanitized = urlSanitized + sl[0];
  41. }
  42. }
  43. }
  44. let result = {
  45. url: urlSanitized,
  46. ok: false,
  47. status: 404,
  48. ms: -1
  49. };
  50. try {
  51. if (urlSanitized && !util.isPrototypePolluted()) {
  52. let t = Date.now();
  53. if (_linux || _freebsd || _openbsd || _netbsd || _darwin || _sunos) {
  54. let args = ' -I --connect-timeout 5 -m 5 ' + urlSanitized + ' 2>/dev/null | head -n 1 | cut -d " " -f2';
  55. let cmd = 'curl';
  56. exec(cmd + args, function (error, stdout) {
  57. let statusCode = parseInt(stdout.toString());
  58. result.status = statusCode || 404;
  59. result.ok = !error && (statusCode === 200 || statusCode === 301 || statusCode === 302 || statusCode === 304);
  60. result.ms = (result.ok ? Date.now() - t : -1);
  61. if (callback) { callback(result); }
  62. resolve(result);
  63. });
  64. }
  65. if (_windows) { // if this is stable, this can be used for all OS types
  66. const http = (urlSanitized.startsWith('https:') ? require('https') : require('http'));
  67. try {
  68. http.get(urlSanitized, (res) => {
  69. const statusCode = res.statusCode;
  70. result.status = statusCode || 404;
  71. result.ok = (statusCode === 200 || statusCode === 301 || statusCode === 302 || statusCode === 304);
  72. if (statusCode !== 200) {
  73. res.resume();
  74. result.ms = (result.ok ? Date.now() - t : -1);
  75. if (callback) { callback(result); }
  76. resolve(result);
  77. } else {
  78. res.on('data', () => { });
  79. res.on('end', () => {
  80. result.ms = (result.ok ? Date.now() - t : -1);
  81. if (callback) { callback(result); }
  82. resolve(result);
  83. });
  84. }
  85. }).on('error', () => {
  86. if (callback) { callback(result); }
  87. resolve(result);
  88. });
  89. } catch (err) {
  90. if (callback) { callback(result); }
  91. resolve(result);
  92. }
  93. }
  94. } else {
  95. if (callback) { callback(result); }
  96. resolve(result);
  97. }
  98. } catch (err) {
  99. if (callback) { callback(result); }
  100. resolve(result);
  101. }
  102. });
  103. });
  104. }
  105. exports.inetChecksite = inetChecksite;
  106. // --------------------------
  107. // check inet latency
  108. function inetLatency(host, callback) {
  109. // fallback - if only callback is given
  110. if (util.isFunction(host) && !callback) {
  111. callback = host;
  112. host = '';
  113. }
  114. host = host || '8.8.8.8';
  115. const hostSanitized = util.isPrototypePolluted() ? '8.8.8.8' : util.sanitizeShellString(host);
  116. return new Promise((resolve) => {
  117. process.nextTick(() => {
  118. let cmd;
  119. if (_linux || _freebsd || _openbsd || _netbsd || _darwin) {
  120. if (_linux) {
  121. cmd = 'ping -c 2 -w 3 ' + hostSanitized + ' | grep rtt';
  122. }
  123. if (_freebsd || _openbsd || _netbsd) {
  124. cmd = 'ping -c 2 -t 3 ' + hostSanitized + ' | grep round-trip';
  125. }
  126. if (_darwin) {
  127. cmd = 'ping -c 2 -t 3 ' + hostSanitized + ' | grep avg';
  128. }
  129. exec(cmd, function (error, stdout) {
  130. let result = -1;
  131. if (!error) {
  132. const line = stdout.toString().split('=');
  133. if (line.length > 1) {
  134. const parts = line[1].split('/');
  135. if (parts.length > 1) {
  136. result = parseFloat(parts[1]);
  137. }
  138. }
  139. }
  140. if (callback) { callback(result); }
  141. resolve(result);
  142. });
  143. }
  144. if (_sunos) {
  145. exec('ping -s -a ' + hostSanitized + ' 56 2 | grep avg', { timeout: 3000 }, function (error, stdout) {
  146. let result = -1;
  147. if (!error) {
  148. const line = stdout.toString().split('=');
  149. if (line.length > 1) {
  150. const parts = line[1].split('/');
  151. if (parts.length > 1) {
  152. result = parseFloat(parts[1].replace(',', '.'));
  153. }
  154. }
  155. }
  156. if (callback) { callback(result); }
  157. resolve(result);
  158. });
  159. }
  160. if (_windows) {
  161. let result = -1;
  162. try {
  163. exec('ping ' + hostSanitized + ' -n 1', util.execOptsWin, function (error, stdout) {
  164. if (!error) {
  165. let lines = stdout.toString().split('\r\n');
  166. lines.shift();
  167. lines.forEach(function (line) {
  168. if ((line.toLowerCase().match(/ms/g) || []).length === 3) {
  169. let l = line.replace(/ +/g, ' ').split(' ');
  170. if (l.length > 6) {
  171. result = parseFloat(l[l.length - 1]);
  172. }
  173. }
  174. });
  175. }
  176. if (callback) { callback(result); }
  177. resolve(result);
  178. });
  179. } catch (e) {
  180. if (callback) { callback(result); }
  181. resolve(result);
  182. }
  183. }
  184. });
  185. });
  186. }
  187. exports.inetLatency = inetLatency;