node_core_utils.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Created by Mingliang Chen on 17/8/23.
  3. // illuspas[a]gmail.com
  4. // Copyright (c) 2018 Nodemedia. All rights reserved.
  5. //
  6. const Crypto = require('crypto');
  7. const EventEmitter = require('events');
  8. const { spawn } = require('child_process');
  9. const readline = require('readline');
  10. const context = require('./node_core_ctx');
  11. function generateNewSessionID() {
  12. let sessionID = '';
  13. const possible = 'ABCDEFGHIJKLMNOPQRSTUVWKYZ0123456789';
  14. const numPossible = possible.length;
  15. do {
  16. for (let i = 0; i < 8; i++) {
  17. sessionID += possible.charAt((Math.random() * numPossible) | 0);
  18. }
  19. } while (context.sessions.has(sessionID))
  20. return sessionID;
  21. }
  22. function genRandomName() {
  23. let name = '';
  24. const possible = 'abcdefghijklmnopqrstuvwxyz0123456789';
  25. const numPossible = possible.length;
  26. for (let i = 0; i < 4; i++) {
  27. name += possible.charAt((Math.random() * numPossible) | 0);
  28. }
  29. return name;
  30. }
  31. function verifyAuth(signStr, streamId, secretKey) {
  32. if (signStr === undefined) {
  33. return false;
  34. }
  35. let now = Date.now() / 1000 | 0;
  36. let exp = parseInt(signStr.split('-')[0]);
  37. let shv = signStr.split('-')[1];
  38. let str = streamId + '-' + exp + '-' + secretKey;
  39. if (exp < now) {
  40. return false;
  41. }
  42. let md5 = Crypto.createHash('md5');
  43. let ohv = md5.update(str).digest('hex');
  44. return shv === ohv;
  45. }
  46. function getFFmpegVersion(ffpath) {
  47. return new Promise((resolve, reject) => {
  48. let ffmpeg_exec = spawn(ffpath, ['-version']);
  49. let version = '';
  50. ffmpeg_exec.on('error', (e) => {
  51. reject(e);
  52. });
  53. ffmpeg_exec.stdout.on('data', (data) => {
  54. try {
  55. version = data.toString().split(/(?:\r\n|\r|\n)/g)[0].split('\ ')[2];
  56. } catch (e) {
  57. }
  58. });
  59. ffmpeg_exec.on('close', (code) => {
  60. resolve(version);
  61. });
  62. });
  63. }
  64. function getFFmpegUrl() {
  65. let url = '';
  66. switch (process.platform) {
  67. case 'darwin':
  68. url = 'https://ffmpeg.zeranoe.com/builds/macos64/static/ffmpeg-latest-macos64-static.zip';
  69. break;
  70. case 'win32':
  71. url = 'https://ffmpeg.zeranoe.com/builds/win64/static/ffmpeg-latest-win64-static.zip | https://ffmpeg.zeranoe.com/builds/win32/static/ffmpeg-latest-win32-static.zip';
  72. break;
  73. case 'linux':
  74. url = 'https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz | https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-i686-static.tar.xz';
  75. break;
  76. default:
  77. url = 'http://ffmpeg.org/download.html';
  78. break;
  79. }
  80. return url;
  81. }
  82. module.exports = {
  83. generateNewSessionID,
  84. verifyAuth,
  85. genRandomName,
  86. getFFmpegVersion,
  87. getFFmpegUrl
  88. }