You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

moderator.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* global $, $iq, APP, config, messageHandler,
  2. roomName, sessionTerminated, Strophe, Util */
  3. var logger = require("jitsi-meet-logger").getLogger(__filename);
  4. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  5. var Settings = require("../settings/Settings");
  6. var AuthenticationEvents
  7. = require("../../service/authentication/AuthenticationEvents");
  8. function createExpBackoffTimer(step) {
  9. var count = 1;
  10. return function (reset) {
  11. // Reset call
  12. if (reset) {
  13. count = 1;
  14. return;
  15. }
  16. // Calculate next timeout
  17. var timeout = Math.pow(2, count - 1);
  18. count += 1;
  19. return timeout * step;
  20. };
  21. }
  22. function Moderator(roomName, xmpp, emitter) {
  23. this.roomName = roomName;
  24. this.xmppService = xmpp;
  25. this.getNextTimeout = createExpBackoffTimer(1000);
  26. this.getNextErrorTimeout = createExpBackoffTimer(1000);
  27. // External authentication stuff
  28. this.externalAuthEnabled = false;
  29. this.settings = new Settings(roomName);
  30. // Sip gateway can be enabled by configuring Jigasi host in config.js or
  31. // it will be enabled automatically if focus detects the component through
  32. // service discovery.
  33. this.sipGatewayEnabled = this.xmppService.options.hosts &&
  34. this.xmppService.options.hosts.call_control !== undefined;
  35. this.eventEmitter = emitter;
  36. this.connection = this.xmppService.connection;
  37. this.focusUserJid;
  38. //FIXME:
  39. // Message listener that talks to POPUP window
  40. function listener(event) {
  41. if (event.data && event.data.sessionId) {
  42. if (event.origin !== window.location.origin) {
  43. logger.warn("Ignoring sessionId from different origin: " +
  44. event.origin);
  45. return;
  46. }
  47. localStorage.setItem('sessionId', event.data.sessionId);
  48. // After popup is closed we will authenticate
  49. }
  50. }
  51. // Register
  52. if (window.addEventListener) {
  53. window.addEventListener("message", listener, false);
  54. } else {
  55. window.attachEvent("onmessage", listener);
  56. }
  57. }
  58. Moderator.prototype.isExternalAuthEnabled = function () {
  59. return this.externalAuthEnabled;
  60. };
  61. Moderator.prototype.isSipGatewayEnabled = function () {
  62. return this.sipGatewayEnabled;
  63. };
  64. Moderator.prototype.onMucMemberLeft = function (jid) {
  65. logger.info("Someone left is it focus ? " + jid);
  66. var resource = Strophe.getResourceFromJid(jid);
  67. if (resource === 'focus' && !this.xmppService.sessionTerminated) {
  68. logger.info(
  69. "Focus has left the room - leaving conference");
  70. //hangUp();
  71. // We'd rather reload to have everything re-initialized
  72. //FIXME: show some message before reload
  73. this.eventEmitter.emit(XMPPEvents.FOCUS_LEFT);
  74. }
  75. };
  76. Moderator.prototype.setFocusUserJid = function (focusJid) {
  77. if (!this.focusUserJid) {
  78. this.focusUserJid = focusJid;
  79. logger.info("Focus jid set to: " + this.focusUserJid);
  80. }
  81. };
  82. Moderator.prototype.getFocusUserJid = function () {
  83. return this.focusUserJid;
  84. };
  85. Moderator.prototype.getFocusComponent = function () {
  86. // Get focus component address
  87. var focusComponent = this.xmppService.options.hosts.focus;
  88. // If not specified use default: 'focus.domain'
  89. if (!focusComponent) {
  90. focusComponent = 'focus.' + this.xmppService.options.hosts.domain;
  91. }
  92. return focusComponent;
  93. };
  94. Moderator.prototype.createConferenceIq = function () {
  95. // Generate create conference IQ
  96. var elem = $iq({to: this.getFocusComponent(), type: 'set'});
  97. // Session Id used for authentication
  98. var sessionId = localStorage.getItem('sessionId');
  99. var machineUID = this.settings.getSettings().uid;
  100. logger.info(
  101. "Session ID: " + sessionId + " machine UID: " + machineUID);
  102. elem.c('conference', {
  103. xmlns: 'http://jitsi.org/protocol/focus',
  104. room: this.roomName,
  105. 'machine-uid': machineUID
  106. });
  107. if (sessionId) {
  108. elem.attrs({ 'session-id': sessionId});
  109. }
  110. if (this.xmppService.options.hosts.bridge !== undefined) {
  111. elem.c(
  112. 'property', {
  113. name: 'bridge',
  114. value: this.xmppService.options.hosts.bridge
  115. }).up();
  116. }
  117. if (this.xmppService.options.enforcedBridge !== undefined) {
  118. elem.c(
  119. 'property', {
  120. name: 'enforcedBridge',
  121. value: this.xmppService.options.enforcedBridge
  122. }).up();
  123. }
  124. // Tell the focus we have Jigasi configured
  125. if (this.xmppService.options.hosts.call_control !== undefined) {
  126. elem.c(
  127. 'property', {
  128. name: 'call_control',
  129. value: this.xmppService.options.hosts.call_control
  130. }).up();
  131. }
  132. if (this.xmppService.options.channelLastN !== undefined) {
  133. elem.c(
  134. 'property', {
  135. name: 'channelLastN',
  136. value: this.xmppService.options.channelLastN
  137. }).up();
  138. }
  139. if (this.xmppService.options.adaptiveLastN !== undefined) {
  140. elem.c(
  141. 'property', {
  142. name: 'adaptiveLastN',
  143. value: this.xmppService.options.adaptiveLastN
  144. }).up();
  145. }
  146. if (this.xmppService.options.adaptiveSimulcast !== undefined) {
  147. elem.c(
  148. 'property', {
  149. name: 'adaptiveSimulcast',
  150. value: this.xmppService.options.adaptiveSimulcast
  151. }).up();
  152. }
  153. if (this.xmppService.options.openSctp !== undefined) {
  154. elem.c(
  155. 'property', {
  156. name: 'openSctp',
  157. value: this.xmppService.options.openSctp
  158. }).up();
  159. }
  160. if (this.xmppService.options.startAudioMuted !== undefined)
  161. {
  162. elem.c(
  163. 'property', {
  164. name: 'startAudioMuted',
  165. value: this.xmppService.options.startAudioMuted
  166. }).up();
  167. }
  168. if (this.xmppService.options.startVideoMuted !== undefined)
  169. {
  170. elem.c(
  171. 'property', {
  172. name: 'startVideoMuted',
  173. value: this.xmppService.options.startVideoMuted
  174. }).up();
  175. }
  176. elem.c(
  177. 'property', {
  178. name: 'simulcastMode',
  179. value: 'rewriting'
  180. }).up();
  181. elem.up();
  182. return elem;
  183. };
  184. Moderator.prototype.parseSessionId = function (resultIq) {
  185. var sessionId = $(resultIq).find('conference').attr('session-id');
  186. if (sessionId) {
  187. logger.info('Received sessionId: ' + sessionId);
  188. localStorage.setItem('sessionId', sessionId);
  189. }
  190. };
  191. Moderator.prototype.parseConfigOptions = function (resultIq) {
  192. this.setFocusUserJid(
  193. $(resultIq).find('conference').attr('focusjid'));
  194. var authenticationEnabled
  195. = $(resultIq).find(
  196. '>conference>property' +
  197. '[name=\'authentication\'][value=\'true\']').length > 0;
  198. logger.info("Authentication enabled: " + authenticationEnabled);
  199. this.externalAuthEnabled = $(resultIq).find(
  200. '>conference>property' +
  201. '[name=\'externalAuth\'][value=\'true\']').length > 0;
  202. console.info(
  203. 'External authentication enabled: ' + this.externalAuthEnabled);
  204. if (!this.externalAuthEnabled) {
  205. // We expect to receive sessionId in 'internal' authentication mode
  206. this.parseSessionId(resultIq);
  207. }
  208. var authIdentity = $(resultIq).find('>conference').attr('identity');
  209. this.eventEmitter.emit(AuthenticationEvents.IDENTITY_UPDATED,
  210. authenticationEnabled, authIdentity);
  211. // Check if focus has auto-detected Jigasi component(this will be also
  212. // included if we have passed our host from the config)
  213. if ($(resultIq).find(
  214. '>conference>property' +
  215. '[name=\'sipGatewayEnabled\'][value=\'true\']').length) {
  216. this.sipGatewayEnabled = true;
  217. }
  218. logger.info("Sip gateway enabled: " + this.sipGatewayEnabled);
  219. };
  220. // FIXME = we need to show the fact that we're waiting for the focus
  221. // to the user(or that focus is not available)
  222. Moderator.prototype.allocateConferenceFocus = function (callback) {
  223. // Try to use focus user JID from the config
  224. this.setFocusUserJid(this.xmppService.options.focusUserJid);
  225. // Send create conference IQ
  226. var iq = this.createConferenceIq();
  227. var self = this;
  228. this.connection.sendIQ(
  229. iq,
  230. function (result) {
  231. // Setup config options
  232. self.parseConfigOptions(result);
  233. if ('true' === $(result).find('conference').attr('ready')) {
  234. // Reset both timers
  235. self.getNextTimeout(true);
  236. self.getNextErrorTimeout(true);
  237. // Exec callback
  238. callback();
  239. } else {
  240. var waitMs = self.getNextTimeout();
  241. logger.info("Waiting for the focus... " + waitMs);
  242. // Reset error timeout
  243. self.getNextErrorTimeout(true);
  244. window.setTimeout(
  245. function () {
  246. self.allocateConferenceFocus(callback);
  247. }, waitMs);
  248. }
  249. },
  250. function (error) {
  251. // Invalid session ? remove and try again
  252. // without session ID to get a new one
  253. var invalidSession
  254. = $(error).find('>error>session-invalid').length;
  255. if (invalidSession) {
  256. logger.info("Session expired! - removing");
  257. localStorage.removeItem("sessionId");
  258. }
  259. if ($(error).find('>error>graceful-shutdown').length) {
  260. self.eventEmitter.emit(XMPPEvents.GRACEFUL_SHUTDOWN);
  261. return;
  262. }
  263. // Check for error returned by the reservation system
  264. var reservationErr = $(error).find('>error>reservation-error');
  265. if (reservationErr.length) {
  266. // Trigger error event
  267. var errorCode = reservationErr.attr('error-code');
  268. var errorMsg;
  269. if ($(error).find('>error>text')) {
  270. errorMsg = $(error).find('>error>text').text();
  271. }
  272. self.eventEmitter.emit(
  273. XMPPEvents.RESERVATION_ERROR, errorCode, errorMsg);
  274. return;
  275. }
  276. // Not authorized to create new room
  277. if ($(error).find('>error>not-authorized').length) {
  278. logger.warn("Unauthorized to start the conference", error);
  279. var toDomain
  280. = Strophe.getDomainFromJid(error.getAttribute('to'));
  281. if (toDomain !==
  282. this.xmppService.options.hosts.anonymousdomain) {
  283. //FIXME: "is external" should come either from
  284. // the focus or config.js
  285. self.externalAuthEnabled = true;
  286. }
  287. self.eventEmitter.emit(
  288. XMPPEvents.AUTHENTICATION_REQUIRED,
  289. function () {
  290. self.allocateConferenceFocus(
  291. callback);
  292. });
  293. return;
  294. }
  295. var waitMs = self.getNextErrorTimeout();
  296. logger.error("Focus error, retry after " + waitMs, error);
  297. // Show message
  298. var focusComponent = self.getFocusComponent();
  299. var retrySec = waitMs / 1000;
  300. //FIXME: message is duplicated ?
  301. // Do not show in case of session invalid
  302. // which means just a retry
  303. if (!invalidSession) {
  304. self.eventEmitter.emit(XMPPEvents.FOCUS_DISCONNECTED,
  305. focusComponent, retrySec);
  306. }
  307. // Reset response timeout
  308. self.getNextTimeout(true);
  309. window.setTimeout(
  310. function () {
  311. self.allocateConferenceFocus(callback);
  312. }, waitMs);
  313. }
  314. );
  315. };
  316. Moderator.prototype.getLoginUrl = function (urlCallback, failureCallback) {
  317. var iq = $iq({to: this.getFocusComponent(), type: 'get'});
  318. iq.c('login-url', {
  319. xmlns: 'http://jitsi.org/protocol/focus',
  320. room: this.roomName,
  321. 'machine-uid': this.settings.getSettings().uid
  322. });
  323. this.connection.sendIQ(
  324. iq,
  325. function (result) {
  326. var url = $(result).find('login-url').attr('url');
  327. url = url = decodeURIComponent(url);
  328. if (url) {
  329. logger.info("Got auth url: " + url);
  330. urlCallback(url);
  331. } else {
  332. logger.error(
  333. "Failed to get auth url from the focus", result);
  334. failureCallback(result);
  335. }
  336. },
  337. function (error) {
  338. logger.error("Get auth url error", error);
  339. failureCallback(error);
  340. }
  341. );
  342. };
  343. Moderator.prototype.getPopupLoginUrl = function (urlCallback, failureCallback) {
  344. var iq = $iq({to: this.getFocusComponent(), type: 'get'});
  345. iq.c('login-url', {
  346. xmlns: 'http://jitsi.org/protocol/focus',
  347. room: this.roomName,
  348. 'machine-uid': this.settings.getSettings().uid,
  349. popup: true
  350. });
  351. this.connection.sendIQ(
  352. iq,
  353. function (result) {
  354. var url = $(result).find('login-url').attr('url');
  355. url = url = decodeURIComponent(url);
  356. if (url) {
  357. logger.info("Got POPUP auth url: " + url);
  358. urlCallback(url);
  359. } else {
  360. logger.error(
  361. "Failed to get POPUP auth url from the focus", result);
  362. failureCallback(result);
  363. }
  364. },
  365. function (error) {
  366. logger.error('Get POPUP auth url error', error);
  367. failureCallback(error);
  368. }
  369. );
  370. };
  371. Moderator.prototype.logout = function (callback) {
  372. var iq = $iq({to: this.getFocusComponent(), type: 'set'});
  373. var sessionId = localStorage.getItem('sessionId');
  374. if (!sessionId) {
  375. callback();
  376. return;
  377. }
  378. iq.c('logout', {
  379. xmlns: 'http://jitsi.org/protocol/focus',
  380. 'session-id': sessionId
  381. });
  382. this.connection.sendIQ(
  383. iq,
  384. function (result) {
  385. var logoutUrl = $(result).find('logout').attr('logout-url');
  386. if (logoutUrl) {
  387. logoutUrl = decodeURIComponent(logoutUrl);
  388. }
  389. logger.info("Log out OK, url: " + logoutUrl, result);
  390. localStorage.removeItem('sessionId');
  391. callback(logoutUrl);
  392. },
  393. function (error) {
  394. logger.error("Logout error", error);
  395. }
  396. );
  397. };
  398. module.exports = Moderator;