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.

xmpp.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /* global $, APP, config, Strophe, Base64, $msg */
  2. /* jshint -W101 */
  3. var Moderator = require("./moderator");
  4. var EventEmitter = require("events");
  5. var Recording = require("./recording");
  6. var SDP = require("./SDP");
  7. var SDPUtil = require("./SDPUtil");
  8. var Settings = require("../settings/Settings");
  9. var Pako = require("pako");
  10. var StreamEventTypes = require("../../service/RTC/StreamEventTypes");
  11. var RTCEvents = require("../../service/RTC/RTCEvents");
  12. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  13. var retry = require('retry');
  14. var RandomUtil = require("../util/RandomUtil");
  15. var eventEmitter = new EventEmitter();
  16. var connection = null;
  17. var authenticatedUser = false;
  18. /**
  19. * Utility method that generates user name based on random hex values.
  20. * Eg. 12345678-1234-1234-12345678
  21. * @returns {string}
  22. */
  23. function generateUserName() {
  24. return RandomUtil.randomHexString(8) + "-" + RandomUtil.randomHexString(4)
  25. + "-" + RandomUtil.randomHexString(4) + "-"
  26. + RandomUtil.randomHexString(8);
  27. }
  28. function connect(jid, password) {
  29. var faultTolerantConnect = retry.operation({
  30. retries: 3
  31. });
  32. // fault tolerant connect
  33. faultTolerantConnect.attempt(function () {
  34. connection = XMPP.createConnection();
  35. Moderator.setConnection(connection);
  36. connection.jingle.pc_constraints = APP.RTC.getPCConstraints();
  37. if (config.useIPv6) {
  38. // https://code.google.com/p/webrtc/issues/detail?id=2828
  39. if (!connection.jingle.pc_constraints.optional)
  40. connection.jingle.pc_constraints.optional = [];
  41. connection.jingle.pc_constraints.optional.push({googIPv6: true});
  42. }
  43. // Include user info in MUC presence
  44. var settings = Settings.getSettings();
  45. if (settings.email) {
  46. connection.emuc.addEmailToPresence(settings.email);
  47. }
  48. if (settings.uid) {
  49. connection.emuc.addUserIdToPresence(settings.uid);
  50. }
  51. if (settings.displayName) {
  52. connection.emuc.addDisplayNameToPresence(settings.displayName);
  53. }
  54. // connection.connect() starts the connection process.
  55. //
  56. // As the connection process proceeds, the user supplied callback will
  57. // be triggered multiple times with status updates. The callback should
  58. // take two arguments - the status code and the error condition.
  59. //
  60. // The status code will be one of the values in the Strophe.Status
  61. // constants. The error condition will be one of the conditions defined
  62. // in RFC 3920 or the condition ‘strophe-parsererror’.
  63. //
  64. // The Parameters wait, hold and route are optional and only relevant
  65. // for BOSH connections. Please see XEP 124 for a more detailed
  66. // explanation of the optional parameters.
  67. //
  68. // Connection status constants for use by the connection handler
  69. // callback.
  70. //
  71. // Status.ERROR - An error has occurred (websockets specific)
  72. // Status.CONNECTING - The connection is currently being made
  73. // Status.CONNFAIL - The connection attempt failed
  74. // Status.AUTHENTICATING - The connection is authenticating
  75. // Status.AUTHFAIL - The authentication attempt failed
  76. // Status.CONNECTED - The connection has succeeded
  77. // Status.DISCONNECTED - The connection has been terminated
  78. // Status.DISCONNECTING - The connection is currently being terminated
  79. // Status.ATTACHED - The connection has been attached
  80. var anonymousConnectionFailed = false;
  81. var connectionFailed = false;
  82. var lastErrorMsg;
  83. connection.connect(jid, password, function (status, msg) {
  84. console.log("(TIME) Strophe " + Strophe.getStatusString(status) +
  85. (msg ? "[" + msg + "]" : "") +
  86. "\t:" + window.performance.now());
  87. if (status === Strophe.Status.CONNECTED) {
  88. if (config.useStunTurn) {
  89. connection.jingle.getStunAndTurnCredentials();
  90. }
  91. console.info("My Jabber ID: " + connection.jid);
  92. // Schedule ping ?
  93. var pingJid = connection.domain;
  94. connection.ping.hasPingSupport(
  95. pingJid,
  96. function (hasPing) {
  97. if (hasPing)
  98. connection.ping.startInterval(pingJid);
  99. else
  100. console.warn("Ping NOT supported by " + pingJid);
  101. }
  102. );
  103. if (password)
  104. authenticatedUser = true;
  105. maybeDoJoin();
  106. } else if (status === Strophe.Status.CONNFAIL) {
  107. if (msg === 'x-strophe-bad-non-anon-jid') {
  108. anonymousConnectionFailed = true;
  109. } else {
  110. connectionFailed = true;
  111. }
  112. lastErrorMsg = msg;
  113. } else if (status === Strophe.Status.DISCONNECTED) {
  114. // Stop ping interval
  115. connection.ping.stopInterval();
  116. if (anonymousConnectionFailed) {
  117. // prompt user for username and password
  118. XMPP.promptLogin();
  119. } else {
  120. // Strophe already has built-in HTTP/BOSH error handling and
  121. // request retry logic. Requests are resent automatically
  122. // until their error count reaches 5. Strophe.js disconnects
  123. // if the error count is > 5. We are not replicating this
  124. // here.
  125. //
  126. // The "problem" is that failed HTTP/BOSH requests don't
  127. // trigger a callback with a status update, so when a
  128. // callback with status Strophe.Status.DISCONNECTED arrives,
  129. // we can't be sure if it's a graceful disconnect or if it's
  130. // triggered by some HTTP/BOSH error.
  131. //
  132. // But that's a minor issue in Jitsi Meet as we never
  133. // disconnect anyway, not even when the user closes the
  134. // browser window (which is kind of wrong, but the point is
  135. // that we should never ever get disconnected).
  136. //
  137. // On the other hand, failed connections due to XMPP layer
  138. // errors, trigger a callback with status Strophe.Status.CONNFAIL.
  139. //
  140. // Here we implement retry logic for failed connections due
  141. // to XMPP layer errors and we display an error to the user
  142. // if we get disconnected from the XMPP server permanently.
  143. // If the connection failed, retry.
  144. if (connectionFailed &&
  145. faultTolerantConnect.retry("connection-failed")) {
  146. return;
  147. }
  148. // If we failed to connect to the XMPP server, fire an event
  149. // to let all the interested module now about it.
  150. eventEmitter.emit(XMPPEvents.CONNECTION_FAILED,
  151. msg ? msg : lastErrorMsg);
  152. }
  153. } else if (status === Strophe.Status.AUTHFAIL) {
  154. // wrong password or username, prompt user
  155. XMPP.promptLogin();
  156. }
  157. });
  158. });
  159. }
  160. function maybeDoJoin() {
  161. if (connection && connection.connected &&
  162. Strophe.getResourceFromJid(connection.jid) &&
  163. (APP.RTC.localAudio || APP.RTC.localVideo)) {
  164. // .connected is true while connecting?
  165. doJoin();
  166. }
  167. }
  168. function doJoin() {
  169. eventEmitter.emit(XMPPEvents.READY_TO_JOIN);
  170. }
  171. function initStrophePlugins()
  172. {
  173. require("./strophe.emuc")(XMPP, eventEmitter);
  174. require("./strophe.jingle")(XMPP, eventEmitter);
  175. require("./strophe.moderate")(XMPP, eventEmitter);
  176. require("./strophe.util")();
  177. require("./strophe.ping")(XMPP, eventEmitter);
  178. require("./strophe.rayo")();
  179. require("./strophe.logger")();
  180. }
  181. /**
  182. * If given <tt>localStream</tt> is video one this method will advertise it's
  183. * video type in MUC presence.
  184. * @param localStream new or modified <tt>LocalStream</tt>.
  185. */
  186. function broadcastLocalVideoType(localStream) {
  187. if (localStream.videoType)
  188. XMPP.addToPresence('videoType', localStream.videoType);
  189. }
  190. function registerListeners() {
  191. APP.RTC.addStreamListener(
  192. function (localStream) {
  193. maybeDoJoin();
  194. broadcastLocalVideoType(localStream);
  195. },
  196. StreamEventTypes.EVENT_TYPE_LOCAL_CREATED
  197. );
  198. APP.RTC.addStreamListener(
  199. broadcastLocalVideoType,
  200. StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED
  201. );
  202. APP.RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  203. XMPP.addToPresence("devices", devices);
  204. });
  205. }
  206. var unload = (function () {
  207. var unloaded = false;
  208. return function () {
  209. if (unloaded) { return; }
  210. unloaded = true;
  211. if (connection && connection.connected) {
  212. // ensure signout
  213. $.ajax({
  214. type: 'POST',
  215. url: config.bosh,
  216. async: false,
  217. cache: false,
  218. contentType: 'application/xml',
  219. data: "<body rid='" +
  220. (connection.rid || connection._proto.rid) +
  221. "' xmlns='http://jabber.org/protocol/httpbind' sid='" +
  222. (connection.sid || connection._proto.sid) +
  223. "' type='terminate'>" +
  224. "<presence xmlns='jabber:client' type='unavailable'/>" +
  225. "</body>",
  226. success: function (data) {
  227. console.log('signed out');
  228. console.log(data);
  229. },
  230. error: function (XMLHttpRequest, textStatus, errorThrown) {
  231. console.log('signout error',
  232. textStatus + ' (' + errorThrown + ')');
  233. }
  234. });
  235. }
  236. XMPP.disposeConference(true);
  237. };
  238. })();
  239. function setupEvents() {
  240. // In recent versions of FF the 'beforeunload' event is not fired when the
  241. // window or the tab is closed. It is only fired when we leave the page
  242. // (change URL). If this participant doesn't unload properly, then it
  243. // becomes a ghost for the rest of the participants that stay in the
  244. // conference. Thankfully handling the 'unload' event in addition to the
  245. // 'beforeunload' event seems to guarantee the execution of the 'unload'
  246. // method at least once.
  247. //
  248. // The 'unload' method can safely be run multiple times, it will actually do
  249. // something only the first time that it's run, so we're don't have to worry
  250. // about browsers that fire both events.
  251. $(window).bind('beforeunload', unload);
  252. $(window).bind('unload', unload);
  253. }
  254. var XMPP = {
  255. getConnection: function(){ return connection; },
  256. sessionTerminated: false,
  257. /**
  258. * XMPP connection status
  259. */
  260. Status: Strophe.Status,
  261. /**
  262. * Remembers if we were muted by the focus.
  263. * @type {boolean}
  264. */
  265. forceMuted: false,
  266. start: function () {
  267. setupEvents();
  268. initStrophePlugins();
  269. registerListeners();
  270. Moderator.init(this, eventEmitter);
  271. Recording.init();
  272. var configDomain = config.hosts.anonymousdomain || config.hosts.domain;
  273. // Force authenticated domain if room is appended with '?login=true'
  274. if (config.hosts.anonymousdomain &&
  275. window.location.search.indexOf("login=true") !== -1) {
  276. configDomain = config.hosts.domain;
  277. }
  278. var jid = configDomain || window.location.hostname;
  279. var password = null;
  280. if (config.token) {
  281. password = config.token;
  282. if (config.id) {
  283. jid = config.id + "@" + jid;
  284. } else {
  285. jid = generateUserName() + "@" + jid;
  286. }
  287. }
  288. connect(jid, password);
  289. },
  290. createConnection: function () {
  291. var bosh = config.bosh || '/http-bind';
  292. // adds the room name used to the bosh connection
  293. return new Strophe.Connection(bosh + '?room=' + APP.UI.getRoomNode());
  294. },
  295. getStatusString: function (status) {
  296. return Strophe.getStatusString(status);
  297. },
  298. promptLogin: function () {
  299. eventEmitter.emit(XMPPEvents.PROMPT_FOR_LOGIN, connect);
  300. },
  301. joinRoom: function(roomName, useNicks, nick) {
  302. var roomjid = roomName;
  303. if (useNicks) {
  304. if (nick) {
  305. roomjid += '/' + nick;
  306. } else {
  307. roomjid += '/' + Strophe.getNodeFromJid(connection.jid);
  308. }
  309. } else {
  310. var tmpJid = Strophe.getNodeFromJid(connection.jid);
  311. if(!authenticatedUser)
  312. tmpJid = tmpJid.substr(0, 8);
  313. roomjid += '/' + tmpJid;
  314. }
  315. connection.emuc.doJoin(roomjid);
  316. },
  317. myJid: function () {
  318. if(!connection)
  319. return null;
  320. return connection.emuc.myroomjid;
  321. },
  322. myResource: function () {
  323. if(!connection || ! connection.emuc.myroomjid)
  324. return null;
  325. return Strophe.getResourceFromJid(connection.emuc.myroomjid);
  326. },
  327. getLastPresence: function (from) {
  328. if(!connection)
  329. return null;
  330. return connection.emuc.lastPresenceMap[from];
  331. },
  332. disposeConference: function (onUnload) {
  333. var handler = connection.jingle.activecall;
  334. if (handler && handler.peerconnection) {
  335. // FIXME: probably removing streams is not required and close() should
  336. // be enough
  337. if (APP.RTC.localAudio) {
  338. handler.peerconnection.removeStream(
  339. APP.RTC.localAudio.getOriginalStream(), onUnload);
  340. }
  341. if (APP.RTC.localVideo) {
  342. handler.peerconnection.removeStream(
  343. APP.RTC.localVideo.getOriginalStream(), onUnload);
  344. }
  345. handler.peerconnection.close();
  346. }
  347. eventEmitter.emit(XMPPEvents.DISPOSE_CONFERENCE, onUnload);
  348. connection.jingle.activecall = null;
  349. if (!onUnload) {
  350. this.sessionTerminated = true;
  351. connection.emuc.doLeave();
  352. }
  353. },
  354. addListener: function(type, listener) {
  355. eventEmitter.on(type, listener);
  356. },
  357. removeListener: function (type, listener) {
  358. eventEmitter.removeListener(type, listener);
  359. },
  360. allocateConferenceFocus: function(roomName, callback) {
  361. Moderator.allocateConferenceFocus(roomName, callback);
  362. },
  363. getLoginUrl: function (roomName, callback) {
  364. Moderator.getLoginUrl(roomName, callback);
  365. },
  366. getPopupLoginUrl: function (roomName, callback) {
  367. Moderator.getPopupLoginUrl(roomName, callback);
  368. },
  369. isModerator: function () {
  370. return Moderator.isModerator();
  371. },
  372. isSipGatewayEnabled: function () {
  373. return Moderator.isSipGatewayEnabled();
  374. },
  375. isExternalAuthEnabled: function () {
  376. return Moderator.isExternalAuthEnabled();
  377. },
  378. isConferenceInProgress: function () {
  379. return connection && connection.jingle.activecall &&
  380. connection.jingle.activecall.peerconnection;
  381. },
  382. switchStreams: function (stream, oldStream, callback, isAudio) {
  383. if (this.isConferenceInProgress()) {
  384. // FIXME: will block switchInProgress on true value in case of exception
  385. connection.jingle.activecall.switchStreams(stream, oldStream, callback, isAudio);
  386. } else {
  387. // We are done immediately
  388. console.warn("No conference handler or conference not started yet");
  389. callback();
  390. }
  391. },
  392. sendVideoInfoPresence: function (mute) {
  393. if(!connection)
  394. return;
  395. connection.emuc.addVideoInfoToPresence(mute);
  396. connection.emuc.sendPresence();
  397. },
  398. setVideoMute: function (mute, callback, options) {
  399. if(!connection)
  400. return;
  401. var self = this;
  402. var localCallback = function (mute) {
  403. self.sendVideoInfoPresence(mute);
  404. return callback(mute);
  405. };
  406. if(connection.jingle.activecall)
  407. {
  408. connection.jingle.activecall.setVideoMute(
  409. mute, localCallback, options);
  410. }
  411. else {
  412. localCallback(mute);
  413. }
  414. },
  415. setAudioMute: function (mute, callback) {
  416. if (!(connection && APP.RTC.localAudio)) {
  417. return false;
  418. }
  419. if (this.forceMuted && !mute) {
  420. console.info("Asking focus for unmute");
  421. connection.moderate.setMute(connection.emuc.myroomjid, mute);
  422. // FIXME: wait for result before resetting muted status
  423. this.forceMuted = false;
  424. }
  425. if (mute == APP.RTC.localAudio.isMuted()) {
  426. // Nothing to do
  427. return true;
  428. }
  429. APP.RTC.localAudio.setMute(mute);
  430. this.sendAudioInfoPresence(mute, callback);
  431. return true;
  432. },
  433. sendAudioInfoPresence: function(mute, callback) {
  434. if(connection) {
  435. connection.emuc.addAudioInfoToPresence(mute);
  436. connection.emuc.sendPresence();
  437. }
  438. callback();
  439. return true;
  440. },
  441. toggleRecording: function (tokenEmptyCallback,
  442. recordingStateChangeCallback) {
  443. Recording.toggleRecording(tokenEmptyCallback,
  444. recordingStateChangeCallback, connection);
  445. },
  446. addToPresence: function (name, value, dontSend) {
  447. switch (name) {
  448. case "displayName":
  449. connection.emuc.addDisplayNameToPresence(value);
  450. break;
  451. case "prezi":
  452. connection.emuc.addPreziToPresence(value, 0);
  453. break;
  454. case "preziSlide":
  455. connection.emuc.addCurrentSlideToPresence(value);
  456. break;
  457. case "connectionQuality":
  458. connection.emuc.addConnectionInfoToPresence(value);
  459. break;
  460. case "email":
  461. connection.emuc.addEmailToPresence(value);
  462. break;
  463. case "devices":
  464. connection.emuc.addDevicesToPresence(value);
  465. break;
  466. case "videoType":
  467. connection.emuc.addVideoTypeToPresence(value);
  468. break;
  469. case "startMuted":
  470. if(!Moderator.isModerator())
  471. return;
  472. connection.emuc.addStartMutedToPresence(value[0],
  473. value[1]);
  474. break;
  475. default :
  476. console.log("Unknown tag for presence: " + name);
  477. return;
  478. }
  479. if (!dontSend)
  480. connection.emuc.sendPresence();
  481. },
  482. /**
  483. * Sends 'data' as a log message to the focus. Returns true iff a message
  484. * was sent.
  485. * @param data
  486. * @returns {boolean} true iff a message was sent.
  487. */
  488. sendLogs: function (data) {
  489. if(!connection.emuc.focusMucJid)
  490. return false;
  491. var deflate = true;
  492. var content = JSON.stringify(data);
  493. if (deflate) {
  494. content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
  495. }
  496. content = Base64.encode(content);
  497. // XEP-0337-ish
  498. var message = $msg({to: connection.emuc.focusMucJid, type: 'normal'});
  499. message.c('log', { xmlns: 'urn:xmpp:eventlog',
  500. id: 'PeerConnectionStats'});
  501. message.c('message').t(content).up();
  502. if (deflate) {
  503. message.c('tag', {name: "deflated", value: "true"}).up();
  504. }
  505. message.up();
  506. connection.send(message);
  507. return true;
  508. },
  509. // Gets the logs from strophe.jingle.
  510. getJingleLog: function () {
  511. return connection.jingle ? connection.jingle.getLog() : {};
  512. },
  513. // Gets the logs from strophe.
  514. getXmppLog: function () {
  515. return connection.logger ? connection.logger.log : null;
  516. },
  517. getPrezi: function () {
  518. return connection.emuc.getPrezi(this.myJid());
  519. },
  520. removePreziFromPresence: function () {
  521. connection.emuc.removePreziFromPresence();
  522. connection.emuc.sendPresence();
  523. },
  524. sendChatMessage: function (message, nickname) {
  525. connection.emuc.sendMessage(message, nickname);
  526. },
  527. setSubject: function (topic) {
  528. connection.emuc.setSubject(topic);
  529. },
  530. lockRoom: function (key, onSuccess, onError, onNotSupported) {
  531. connection.emuc.lockRoom(key, onSuccess, onError, onNotSupported);
  532. },
  533. dial: function (to, from, roomName,roomPass) {
  534. connection.rayo.dial(to, from, roomName,roomPass);
  535. },
  536. setMute: function (jid, mute) {
  537. connection.moderate.setMute(jid, mute);
  538. },
  539. eject: function (jid) {
  540. connection.moderate.eject(jid);
  541. },
  542. logout: function (callback) {
  543. Moderator.logout(callback);
  544. },
  545. findJidFromResource: function (resource) {
  546. return connection.emuc.findJidFromResource(resource);
  547. },
  548. getMembers: function () {
  549. return connection.emuc.members;
  550. },
  551. getJidFromSSRC: function (ssrc) {
  552. if (!this.isConferenceInProgress())
  553. return null;
  554. return connection.jingle.activecall.getSsrcOwner(ssrc);
  555. },
  556. // Returns true iff we have joined the MUC.
  557. isMUCJoined: function () {
  558. return connection === null ? false : connection.emuc.joined;
  559. },
  560. getSessions: function () {
  561. return connection.jingle.sessions;
  562. },
  563. removeStream: function (stream) {
  564. if (!this.isConferenceInProgress())
  565. return;
  566. connection.jingle.activecall.peerconnection.removeStream(stream);
  567. },
  568. filter_special_chars: function (text) {
  569. return SDPUtil.filter_special_chars(text);
  570. }
  571. };
  572. module.exports = XMPP;