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

internet.js 6.0KB

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