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.

middleware.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // @flow
  2. import { batch } from 'react-redux';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  4. import { CONFERENCE_JOINED, getCurrentConference } from '../base/conference';
  5. import {
  6. PARTICIPANT_JOINED,
  7. PARTICIPANT_LEFT,
  8. PARTICIPANT_UPDATED,
  9. getLocalParticipant,
  10. getParticipantById,
  11. getParticipantCount,
  12. getRemoteParticipants,
  13. isScreenShareParticipant,
  14. participantUpdated
  15. } from '../base/participants';
  16. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  17. import { playSound, registerSound, unregisterSound } from '../base/sounds';
  18. import { SET_MEDIA_ENCRYPTION_KEY, TOGGLE_E2EE } from './actionTypes';
  19. import { setE2EEMaxMode, setEveryoneEnabledE2EE, setEveryoneSupportE2EE, toggleE2EE } from './actions';
  20. import { E2EE_OFF_SOUND_ID, E2EE_ON_SOUND_ID, MAX_MODE } from './constants';
  21. import { isMaxModeReached, isMaxModeThresholdReached } from './functions';
  22. import logger from './logger';
  23. import { E2EE_OFF_SOUND_FILE, E2EE_ON_SOUND_FILE } from './sounds';
  24. /**
  25. * Middleware that captures actions related to E2EE.
  26. *
  27. * @param {Store} store - The redux store.
  28. * @returns {Function}
  29. */
  30. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  31. const conference = getCurrentConference(getState);
  32. switch (action.type) {
  33. case APP_WILL_MOUNT:
  34. dispatch(registerSound(
  35. E2EE_OFF_SOUND_ID,
  36. E2EE_OFF_SOUND_FILE));
  37. dispatch(registerSound(
  38. E2EE_ON_SOUND_ID,
  39. E2EE_ON_SOUND_FILE));
  40. break;
  41. case APP_WILL_UNMOUNT:
  42. dispatch(unregisterSound(E2EE_OFF_SOUND_ID));
  43. dispatch(unregisterSound(E2EE_ON_SOUND_ID));
  44. break;
  45. case CONFERENCE_JOINED:
  46. _updateMaxMode(dispatch, getState);
  47. break;
  48. case PARTICIPANT_UPDATED: {
  49. const { id, e2eeEnabled, e2eeSupported } = action.participant;
  50. const oldParticipant = getParticipantById(getState(), id);
  51. const result = next(action);
  52. if (e2eeEnabled !== oldParticipant?.e2eeEnabled
  53. || e2eeSupported !== oldParticipant?.e2eeSupported) {
  54. const state = getState();
  55. let newEveryoneSupportE2EE = true;
  56. let newEveryoneEnabledE2EE = true;
  57. // eslint-disable-next-line no-unused-vars
  58. for (const [ key, p ] of getRemoteParticipants(state)) {
  59. if (!p.e2eeEnabled) {
  60. newEveryoneEnabledE2EE = false;
  61. }
  62. if (!p.e2eeSupported) {
  63. newEveryoneSupportE2EE = false;
  64. }
  65. if (!newEveryoneEnabledE2EE && !newEveryoneSupportE2EE) {
  66. break;
  67. }
  68. }
  69. if (!getLocalParticipant(state)?.e2eeEnabled) {
  70. newEveryoneEnabledE2EE = false;
  71. }
  72. batch(() => {
  73. dispatch(setEveryoneEnabledE2EE(newEveryoneEnabledE2EE));
  74. dispatch(setEveryoneSupportE2EE(newEveryoneSupportE2EE));
  75. });
  76. }
  77. return result;
  78. }
  79. case PARTICIPANT_JOINED: {
  80. const result = next(action);
  81. const { e2eeEnabled, e2eeSupported, local } = action.participant;
  82. const { everyoneEnabledE2EE } = getState()['features/e2ee'];
  83. const participantCount = getParticipantCount(getState);
  84. if (isScreenShareParticipant(action.participant)) {
  85. return result;
  86. }
  87. // the initial values
  88. if (participantCount === 1) {
  89. batch(() => {
  90. dispatch(setEveryoneEnabledE2EE(e2eeEnabled));
  91. dispatch(setEveryoneSupportE2EE(e2eeSupported));
  92. });
  93. }
  94. // if all had it enabled and this one disabled it, change value in store
  95. // otherwise there is no change in the value we store
  96. if (everyoneEnabledE2EE && !e2eeEnabled) {
  97. dispatch(setEveryoneEnabledE2EE(false));
  98. }
  99. if (local) {
  100. return result;
  101. }
  102. const { everyoneSupportE2EE } = getState()['features/e2ee'];
  103. // if all supported it and this one does not, change value in store
  104. // otherwise there is no change in the value we store
  105. if (everyoneSupportE2EE && !e2eeSupported) {
  106. dispatch(setEveryoneSupportE2EE(false));
  107. }
  108. _updateMaxMode(dispatch, getState);
  109. return result;
  110. }
  111. case PARTICIPANT_LEFT: {
  112. const previosState = getState();
  113. const participant = getParticipantById(previosState, action.participant?.id) || {};
  114. const result = next(action);
  115. const newState = getState();
  116. const { e2eeEnabled = false, e2eeSupported = false } = participant;
  117. if (isScreenShareParticipant(participant)) {
  118. return result;
  119. }
  120. const { everyoneEnabledE2EE, everyoneSupportE2EE } = newState['features/e2ee'];
  121. // if it was not enabled by everyone, and the participant leaving had it disabled, or if it was not supported
  122. // by everyone, and the participant leaving had it not supported let's check is it enabled for all that stay
  123. if ((!everyoneEnabledE2EE && !e2eeEnabled) || (!everyoneSupportE2EE && !e2eeSupported)) {
  124. let latestEveryoneEnabledE2EE = true;
  125. let latestEveryoneSupportE2EE = true;
  126. // eslint-disable-next-line no-unused-vars
  127. for (const [ key, p ] of getRemoteParticipants(newState)) {
  128. if (!p.e2eeEnabled) {
  129. latestEveryoneEnabledE2EE = false;
  130. }
  131. if (!p.e2eeSupported) {
  132. latestEveryoneSupportE2EE = false;
  133. }
  134. if (!latestEveryoneEnabledE2EE && !latestEveryoneSupportE2EE) {
  135. break;
  136. }
  137. }
  138. if (!getLocalParticipant(newState)?.e2eeEnabled) {
  139. latestEveryoneEnabledE2EE = false;
  140. }
  141. batch(() => {
  142. if (!everyoneEnabledE2EE && latestEveryoneEnabledE2EE) {
  143. dispatch(setEveryoneEnabledE2EE(true));
  144. }
  145. if (!everyoneSupportE2EE && latestEveryoneSupportE2EE) {
  146. dispatch(setEveryoneSupportE2EE(true));
  147. }
  148. });
  149. }
  150. _updateMaxMode(dispatch, getState);
  151. return result;
  152. }
  153. case TOGGLE_E2EE: {
  154. if (conference && conference.isE2EESupported() && conference.isE2EEEnabled() !== action.enabled) {
  155. logger.debug(`E2EE will be ${action.enabled ? 'enabled' : 'disabled'}`);
  156. conference.toggleE2EE(action.enabled);
  157. // Broadcast that we enabled / disabled E2EE.
  158. const participant = getLocalParticipant(getState);
  159. dispatch(participantUpdated({
  160. e2eeEnabled: action.enabled,
  161. id: participant.id,
  162. local: true
  163. }));
  164. const soundID = action.enabled ? E2EE_ON_SOUND_ID : E2EE_OFF_SOUND_ID;
  165. dispatch(playSound(soundID));
  166. }
  167. break;
  168. }
  169. case SET_MEDIA_ENCRYPTION_KEY: {
  170. if (conference && conference.isE2EESupported()) {
  171. const { exportedKey, index } = action.keyInfo;
  172. if (exportedKey) {
  173. window.crypto.subtle.importKey(
  174. 'raw',
  175. new Uint8Array(exportedKey),
  176. 'AES-GCM',
  177. false,
  178. [ 'encrypt', 'decrypt' ])
  179. .then(
  180. encryptionKey => {
  181. conference.setMediaEncryptionKey({
  182. encryptionKey,
  183. index
  184. });
  185. })
  186. .catch(error => logger.error('SET_MEDIA_ENCRYPTION_KEY error', error));
  187. } else {
  188. conference.setMediaEncryptionKey({
  189. encryptionKey: false,
  190. index
  191. });
  192. }
  193. }
  194. break;
  195. }
  196. }
  197. return next(action);
  198. });
  199. /**
  200. * Set up state change listener to perform maintenance tasks when the conference
  201. * is left or failed.
  202. */
  203. StateListenerRegistry.register(
  204. state => getCurrentConference(state),
  205. (conference, { dispatch }, previousConference) => {
  206. if (previousConference) {
  207. dispatch(toggleE2EE(false));
  208. }
  209. });
  210. /**
  211. * Sets the maxMode based on the number of participants in the conference.
  212. *
  213. * @param { Dispatch<any>} dispatch - The redux dispatch function.
  214. * @param {Function|Object} getState - The {@code getState} function.
  215. * @private
  216. * @returns {void}
  217. */
  218. function _updateMaxMode(dispatch, getState) {
  219. const state = getState();
  220. const { e2ee = {} } = state['features/base/config'];
  221. if (e2ee.externallyManagedKey) {
  222. return;
  223. }
  224. if (isMaxModeThresholdReached(state)) {
  225. dispatch(setE2EEMaxMode(MAX_MODE.THRESHOLD_EXCEEDED));
  226. dispatch(toggleE2EE(false));
  227. } else if (isMaxModeReached(state)) {
  228. dispatch(setE2EEMaxMode(MAX_MODE.ENABLED));
  229. } else {
  230. dispatch(setE2EEMaxMode(MAX_MODE.DISABLED));
  231. }
  232. }