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

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