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

users.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. 'use strict';
  2. // @ts-check
  3. // ==================================================================================
  4. // users.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. // 11. Users/Sessions
  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. let _winDateFormat = {
  26. dateFormat: '',
  27. dateSeperator: '',
  28. timeFormat: '',
  29. timeSeperator: '',
  30. amDesignator: '',
  31. pmDesignator: ''
  32. };
  33. // --------------------------
  34. // array of users online = sessions
  35. function getWinCulture() {
  36. return new Promise((resolve) => {
  37. process.nextTick(() => {
  38. if (!_winDateFormat.dateFormat) {
  39. util.powerShell('(get-culture).DateTimeFormat')
  40. .then(data => {
  41. let lines = data.toString().split('\r\n');
  42. _winDateFormat.dateFormat = util.getValue(lines, 'ShortDatePattern', ':');
  43. _winDateFormat.dateSeperator = util.getValue(lines, 'DateSeparator', ':');
  44. _winDateFormat.timeFormat = util.getValue(lines, 'ShortTimePattern', ':');
  45. _winDateFormat.timeSeperator = util.getValue(lines, 'TimeSeparator', ':');
  46. _winDateFormat.amDesignator = util.getValue(lines, 'AMDesignator', ':');
  47. _winDateFormat.pmDesignator = util.getValue(lines, 'PMDesignator', ':');
  48. resolve(_winDateFormat);
  49. })
  50. .catch(() => {
  51. resolve(_winDateFormat);
  52. });
  53. } else {
  54. resolve(_winDateFormat);
  55. }
  56. });
  57. });
  58. }
  59. function parseUsersLinux(lines, phase) {
  60. let result = [];
  61. let result_who = [];
  62. let result_w = {};
  63. let w_first = true;
  64. let w_header = [];
  65. let w_pos = [];
  66. let who_line = {};
  67. let is_whopart = true;
  68. lines.forEach(function (line) {
  69. if (line === '---') {
  70. is_whopart = false;
  71. } else {
  72. let l = line.replace(/ +/g, ' ').split(' ');
  73. // who part
  74. if (is_whopart) {
  75. result_who.push({
  76. user: l[0],
  77. tty: l[1],
  78. date: l[2],
  79. time: l[3],
  80. ip: (l && l.length > 4) ? l[4].replace(/\(/g, '').replace(/\)/g, '') : ''
  81. });
  82. } else {
  83. // w part
  84. if (w_first) { // header
  85. w_header = l;
  86. w_header.forEach(function (item) {
  87. w_pos.push(line.indexOf(item));
  88. });
  89. w_first = false;
  90. } else {
  91. // split by w_pos
  92. result_w.user = line.substring(w_pos[0], w_pos[1] - 1).trim();
  93. result_w.tty = line.substring(w_pos[1], w_pos[2] - 1).trim();
  94. result_w.ip = line.substring(w_pos[2], w_pos[3] - 1).replace(/\(/g, '').replace(/\)/g, '').trim();
  95. result_w.command = line.substring(w_pos[7], 1000).trim();
  96. // find corresponding 'who' line
  97. who_line = result_who.filter(function (obj) {
  98. return (obj.user.substring(0, 8).trim() === result_w.user && obj.tty === result_w.tty);
  99. });
  100. if (who_line.length === 1) {
  101. result.push({
  102. user: who_line[0].user,
  103. tty: who_line[0].tty,
  104. date: who_line[0].date,
  105. time: who_line[0].time,
  106. ip: who_line[0].ip,
  107. command: result_w.command
  108. });
  109. }
  110. }
  111. }
  112. }
  113. });
  114. if (result.length === 0 && phase === 2) {
  115. return result_who;
  116. } else {
  117. return result;
  118. }
  119. }
  120. function parseUsersDarwin(lines) {
  121. let result = [];
  122. let result_who = [];
  123. let result_w = {};
  124. let who_line = {};
  125. let is_whopart = true;
  126. lines.forEach(function (line) {
  127. if (line === '---') {
  128. is_whopart = false;
  129. } else {
  130. let l = line.replace(/ +/g, ' ').split(' ');
  131. // who part
  132. if (is_whopart) {
  133. result_who.push({
  134. user: l[0],
  135. tty: l[1],
  136. date: ('' + new Date().getFullYear()) + '-' + ('0' + ('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.indexOf(l[2].toUpperCase()) / 3 + 1)).slice(-2) + '-' + ('0' + l[3]).slice(-2),
  137. time: l[4],
  138. });
  139. } else {
  140. // w part
  141. // split by w_pos
  142. result_w.user = l[0];
  143. result_w.tty = l[1];
  144. result_w.ip = (l[2] !== '-') ? l[2] : '';
  145. result_w.command = l.slice(5, 1000).join(' ');
  146. // find corresponding 'who' line
  147. who_line = result_who.filter(function (obj) {
  148. return (obj.user === result_w.user && (obj.tty.substring(3, 1000) === result_w.tty || obj.tty === result_w.tty));
  149. });
  150. if (who_line.length === 1) {
  151. result.push({
  152. user: who_line[0].user,
  153. tty: who_line[0].tty,
  154. date: who_line[0].date,
  155. time: who_line[0].time,
  156. ip: result_w.ip,
  157. command: result_w.command
  158. });
  159. }
  160. }
  161. }
  162. });
  163. return result;
  164. }
  165. function parseUsersWin(lines, culture) {
  166. let result = [];
  167. const header = lines[0];
  168. const headerDelimiter = [];
  169. if (header) {
  170. const start = (header[0] === ' ') ? 1 : 0;
  171. headerDelimiter.push(start - 1);
  172. let nextSpace = 0;
  173. for (let i = start + 1; i < header.length; i++) {
  174. if (header[i] === ' ' && ((header[i - 1] === ' ') || (header[i - 1] === '.'))) {
  175. nextSpace = i;
  176. } else {
  177. if (nextSpace) {
  178. headerDelimiter.push(nextSpace);
  179. nextSpace = 0;
  180. }
  181. }
  182. }
  183. for (let i = 1; i < lines.length; i++) {
  184. if (lines[i].trim()) {
  185. const user = lines[i].substring(headerDelimiter[0] + 1, headerDelimiter[1]).trim() || '';
  186. const tty = lines[i].substring(headerDelimiter[1] + 1, headerDelimiter[2] - 2).trim() || '';
  187. const dateTime = util.parseDateTime(lines[i].substring(headerDelimiter[5] + 1, 2000).trim(), culture) || '';
  188. result.push({
  189. user: user,
  190. tty: tty,
  191. date: dateTime.date,
  192. time: dateTime.time,
  193. ip: '',
  194. command: ''
  195. });
  196. }
  197. }
  198. }
  199. return result;
  200. }
  201. function users(callback) {
  202. return new Promise((resolve) => {
  203. process.nextTick(() => {
  204. let result = [];
  205. // linux
  206. if (_linux) {
  207. exec('who --ips; echo "---"; w | tail -n +2', function (error, stdout) {
  208. if (!error) {
  209. // lines / split
  210. let lines = stdout.toString().split('\n');
  211. result = parseUsersLinux(lines, 1);
  212. if (result.length === 0) {
  213. exec('who; echo "---"; w | tail -n +2', function (error, stdout) {
  214. if (!error) {
  215. // lines / split
  216. lines = stdout.toString().split('\n');
  217. result = parseUsersLinux(lines, 2);
  218. }
  219. if (callback) { callback(result); }
  220. resolve(result);
  221. });
  222. } else {
  223. if (callback) { callback(result); }
  224. resolve(result);
  225. }
  226. } else {
  227. if (callback) { callback(result); }
  228. resolve(result);
  229. }
  230. });
  231. }
  232. if (_freebsd || _openbsd || _netbsd) {
  233. exec('who; echo "---"; w -ih', function (error, stdout) {
  234. if (!error) {
  235. // lines / split
  236. let lines = stdout.toString().split('\n');
  237. result = parseUsersDarwin(lines);
  238. }
  239. if (callback) { callback(result); }
  240. resolve(result);
  241. });
  242. }
  243. if (_sunos) {
  244. exec('who; echo "---"; w -h', function (error, stdout) {
  245. if (!error) {
  246. // lines / split
  247. let lines = stdout.toString().split('\n');
  248. result = parseUsersDarwin(lines);
  249. }
  250. if (callback) { callback(result); }
  251. resolve(result);
  252. });
  253. }
  254. if (_darwin) {
  255. exec('who; echo "---"; w -ih', function (error, stdout) {
  256. if (!error) {
  257. // lines / split
  258. let lines = stdout.toString().split('\n');
  259. result = parseUsersDarwin(lines);
  260. }
  261. if (callback) { callback(result); }
  262. resolve(result);
  263. });
  264. }
  265. if (_windows) {
  266. try {
  267. exec('query user', util.execOptsWin, function (error, stdout) {
  268. if (stdout) {
  269. // lines / split
  270. let lines = stdout.toString().split('\r\n');
  271. getWinCulture()
  272. .then(culture => {
  273. result = parseUsersWin(lines, culture);
  274. if (callback) { callback(result); }
  275. resolve(result);
  276. });
  277. } else {
  278. if (callback) { callback(result); }
  279. resolve(result);
  280. }
  281. });
  282. } catch (e) {
  283. if (callback) { callback(result); }
  284. resolve(result);
  285. }
  286. }
  287. });
  288. });
  289. }
  290. exports.users = users;