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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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, local } = action.participant;
  81. const { everyoneEnabledE2EE } = getState()['features/e2ee'];
  82. const participantCount = getParticipantCount(getState);
  83. // the initial values
  84. if (participantCount === 1) {
  85. batch(() => {
  86. dispatch(setEveryoneEnabledE2EE(e2eeEnabled));
  87. dispatch(setEveryoneSupportE2EE(e2eeSupported));
  88. });
  89. }
  90. // if all had it enabled and this one disabled it, change value in store
  91. // otherwise there is no change in the value we store
  92. if (everyoneEnabledE2EE && !e2eeEnabled) {
  93. dispatch(setEveryoneEnabledE2EE(false));
  94. }
  95. if (local) {
  96. return result;
  97. }
  98. const { everyoneSupportE2EE } = getState()['features/e2ee'];
  99. // if all supported it and this one does not, change value in store
  100. // otherwise there is no change in the value we store
  101. if (everyoneSupportE2EE && !e2eeSupported) {
  102. dispatch(setEveryoneSupportE2EE(false));
  103. }
  104. _updateMaxMode(dispatch, getState);
  105. return result;
  106. }
  107. case PARTICIPANT_LEFT: {
  108. const previosState = getState();
  109. const participant = getParticipantById(previosState, action.participant?.id) || {};
  110. const result = next(action);
  111. const newState = getState();
  112. const { e2eeEnabled = false, e2eeSupported = false } = participant;
  113. const { everyoneEnabledE2EE, everyoneSupportE2EE } = newState['features/e2ee'];
  114. // if it was not enabled by everyone, and the participant leaving had it disabled, or if it was not supported
  115. // by everyone, and the participant leaving had it not supported let's check is it enabled for all that stay
  116. if ((!everyoneEnabledE2EE && !e2eeEnabled) || (!everyoneSupportE2EE && !e2eeSupported)) {
  117. let latestEveryoneEnabledE2EE = true;
  118. let latestEveryoneSupportE2EE = true;
  119. // eslint-disable-next-line no-unused-vars
  120. for (const [ key, p ] of getRemoteParticipants(newState)) {
  121. if (!p.e2eeEnabled) {
  122. latestEveryoneEnabledE2EE = false;
  123. }
  124. if (!p.e2eeSupported) {
  125. latestEveryoneSupportE2EE = false;
  126. }
  127. if (!latestEveryoneEnabledE2EE && !latestEveryoneSupportE2EE) {
  128. break;
  129. }
  130. }
  131. if (!getLocalParticipant(newState)?.e2eeEnabled) {
  132. latestEveryoneEnabledE2EE = false;
  133. }
  134. batch(() => {
  135. if (!everyoneEnabledE2EE && latestEveryoneEnabledE2EE) {
  136. dispatch(setEveryoneEnabledE2EE(true));
  137. }
  138. if (!everyoneSupportE2EE && latestEveryoneSupportE2EE) {
  139. dispatch(setEveryoneSupportE2EE(true));
  140. }
  141. });
  142. }
  143. _updateMaxMode(dispatch, getState);
  144. return result;
  145. }
  146. case TOGGLE_E2EE: {
  147. if (conference && conference.isE2EEEnabled() !== action.enabled) {
  148. logger.debug(`E2EE will be ${action.enabled ? 'enabled' : 'disabled'}`);
  149. conference.toggleE2EE(action.enabled);
  150. // Broadcast that we enabled / disabled E2EE.
  151. const participant = getLocalParticipant(getState);
  152. dispatch(participantUpdated({
  153. e2eeEnabled: action.enabled,
  154. id: participant.id,
  155. local: true
  156. }));
  157. const soundID = action.enabled ? E2EE_ON_SOUND_ID : E2EE_OFF_SOUND_ID;
  158. dispatch(playSound(soundID));
  159. }
  160. break;
  161. }
  162. case SET_MEDIA_ENCRYPTION_KEY: {
  163. if (conference && conference.isE2EESupported()) {
  164. const { exportedKey, index } = action.keyInfo;
  165. if (exportedKey) {
  166. window.crypto.subtle.importKey(
  167. 'raw',
  168. new Uint8Array(exportedKey),
  169. 'AES-GCM',
  170. false,
  171. [ 'encrypt', 'decrypt' ])
  172. .then(
  173. encryptionKey => {
  174. conference.setMediaEncryptionKey({
  175. encryptionKey,
  176. index
  177. });
  178. })
  179. .catch(error => logger.error('SET_MEDIA_ENCRYPTION_KEY error', error));
  180. } else {
  181. conference.setMediaEncryptionKey({
  182. encryptionKey: false,
  183. index
  184. });
  185. }
  186. }
  187. break;
  188. }
  189. }
  190. return next(action);
  191. });
  192. /**
  193. * Set up state change listener to perform maintenance tasks when the conference
  194. * is left or failed.
  195. */
  196. StateListenerRegistry.register(
  197. state => getCurrentConference(state),
  198. (conference, { dispatch }, previousConference) => {
  199. if (previousConference) {
  200. dispatch(toggleE2EE(false));
  201. }
  202. });
  203. /**
  204. * Sets the maxMode based on the number of participants in the conference.
  205. *
  206. * @param { Dispatch<any>} dispatch - The redux dispatch function.
  207. * @param {Function|Object} getState - The {@code getState} function.
  208. * @private
  209. * @returns {void}
  210. */
  211. function _updateMaxMode(dispatch, getState) {
  212. const state = getState();
  213. const { e2ee = {} } = state['features/base/config'];
  214. if (e2ee.externallyManagedKey) {
  215. return;
  216. }
  217. if (isMaxModeThresholdReached(state)) {
  218. dispatch(setE2EEMaxMode(MAX_MODE.THRESHOLD_EXCEEDED));
  219. dispatch(toggleE2EE(false));
  220. } else if (isMaxModeReached(state)) {
  221. dispatch(setE2EEMaxMode(MAX_MODE.ENABLED));
  222. } else {
  223. dispatch(setE2EEMaxMode(MAX_MODE.DISABLED));
  224. }
  225. }