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.

functions.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* @flow */
  2. import _ from 'lodash';
  3. import parseURLParams from './parseURLParams';
  4. declare var $: Object;
  5. /**
  6. * The config keys to whitelist, the keys that can be overridden.
  7. * Currently we can only whitelist the first part of the properties, like
  8. * 'p2p.useStunTurn' and 'p2p.enabled' we whitelist all p2p options.
  9. * The whitelist is used only for config.js.
  10. *
  11. * @private
  12. * @type Array
  13. */
  14. const WHITELISTED_KEYS = [
  15. '_peerConnStatusOutOfLastNTimeout',
  16. '_peerConnStatusRtcMuteTimeout',
  17. 'abTesting',
  18. 'alwaysVisibleToolbar',
  19. 'autoEnableDesktopSharing',
  20. 'autoRecord',
  21. 'autoRecordToken',
  22. 'avgRtpStatsN',
  23. 'callStatsConfIDNamespace',
  24. 'callStatsID',
  25. 'callStatsSecret',
  26. 'channelLastN',
  27. 'constraints',
  28. 'debug',
  29. 'debugAudioLevels',
  30. 'defaultLanguage',
  31. 'desktopSharingChromeDisabled',
  32. 'desktopSharingChromeExtId',
  33. 'desktopSharingChromeMinExtVersion',
  34. 'desktopSharingChromeSources',
  35. 'desktopSharingFirefoxDisabled',
  36. 'desktopSharingSources',
  37. 'disable1On1Mode',
  38. 'disableAEC',
  39. 'disableAGC',
  40. 'disableAP',
  41. 'disableAudioLevels',
  42. 'disableDesktopSharing',
  43. 'disableDesktopSharing',
  44. 'disableH264',
  45. 'disableHPF',
  46. 'disableNS',
  47. 'disableRemoteControl',
  48. 'disableRtx',
  49. 'disableSuspendVideo',
  50. 'displayJids',
  51. 'enableDisplayNameInStats',
  52. 'enableLipSync',
  53. 'enableLocalVideoFlip',
  54. 'enableRecording',
  55. 'enableStatsID',
  56. 'enableTalkWhileMuted',
  57. 'enableUserRolesBasedOnToken',
  58. 'etherpad_base',
  59. 'failICE',
  60. 'firefox_fake_device',
  61. 'forceJVB121Ratio',
  62. 'hiddenDomain',
  63. 'hosts',
  64. 'iAmRecorder',
  65. 'iAmSipGateway',
  66. 'ignoreStartMuted',
  67. 'nick',
  68. 'openBridgeChannel',
  69. 'p2p',
  70. 'preferH264',
  71. 'recordingType',
  72. 'requireDisplayName',
  73. 'resolution',
  74. 'startAudioMuted',
  75. 'startAudioOnly',
  76. 'startBitrate',
  77. 'startScreenSharing',
  78. 'startVideoMuted',
  79. 'startWithAudioMuted',
  80. 'startWithVideoMuted',
  81. 'testing',
  82. 'useIPv6',
  83. 'useNicks',
  84. 'useStunTurn',
  85. 'webrtcIceTcpDisable',
  86. 'webrtcIceUdpDisable'
  87. ];
  88. const logger = require('jitsi-meet-logger').getLogger(__filename);
  89. // XXX The functions getRoomName and parseURLParams are split out of
  90. // functions.js because they are bundled in both app.bundle and
  91. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  92. // want all functions to be bundled in do_external_connect.
  93. export { default as getRoomName } from './getRoomName';
  94. export { parseURLParams };
  95. /**
  96. * Sends HTTP POST request to specified {@code endpoint}. In request the name
  97. * of the room is included in JSON format:
  98. * {
  99. * "rooomName": "someroom12345"
  100. * }.
  101. *
  102. * @param {string} endpoint - The name of HTTP endpoint to which to send
  103. * the HTTP POST request.
  104. * @param {string} roomName - The name of the conference room for which config
  105. * is requested.
  106. * @param {Function} complete - The callback to invoke upon success or failure.
  107. * @returns {void}
  108. */
  109. export function obtainConfig(
  110. endpoint: string,
  111. roomName: string,
  112. complete: Function) {
  113. logger.info(`Send config request to ${endpoint} for room: ${roomName}`);
  114. $.ajax(
  115. endpoint,
  116. {
  117. contentType: 'application/json',
  118. data: JSON.stringify({ roomName }),
  119. dataType: 'json',
  120. method: 'POST',
  121. error(jqXHR, textStatus, errorThrown) {
  122. logger.error('Get config error: ', jqXHR, errorThrown);
  123. complete(false, `Get config response status: ${textStatus}`);
  124. },
  125. success(data) {
  126. const { config, interfaceConfig, loggingConfig } = window;
  127. try {
  128. overrideConfigJSON(
  129. config, interfaceConfig, loggingConfig,
  130. data);
  131. complete(true);
  132. } catch (e) {
  133. logger.error('Parse config error: ', e);
  134. complete(false, e);
  135. }
  136. }
  137. }
  138. );
  139. }
  140. /* eslint-disable max-params, no-shadow */
  141. /**
  142. * Overrides JSON properties in {@code config} and
  143. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  144. * Overrides only the whitelisted keys.
  145. *
  146. * @param {Object} config - The config Object in which we'll be overriding
  147. * properties.
  148. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  149. * be overriding properties.
  150. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  151. * overriding properties.
  152. * @param {Object} json - Object containing configuration properties.
  153. * Destination object is selected based on root property name:
  154. * {
  155. * config: {
  156. * // config.js properties here
  157. * },
  158. * interfaceConfig: {
  159. * // interface_config.js properties here
  160. * },
  161. * loggingConfig: {
  162. * // logging_config.js properties here
  163. * }
  164. * }.
  165. * @returns {void}
  166. */
  167. export function overrideConfigJSON(
  168. config: Object, interfaceConfig: Object, loggingConfig: Object,
  169. json: Object) {
  170. for (const configName of Object.keys(json)) {
  171. let configObj;
  172. if (configName === 'config') {
  173. configObj = config;
  174. } else if (configName === 'interfaceConfig') {
  175. configObj = interfaceConfig;
  176. } else if (configName === 'loggingConfig') {
  177. configObj = loggingConfig;
  178. }
  179. if (configObj) {
  180. const configJSON
  181. = _getWhitelistedJSON(configName, json[configName]);
  182. if (!_.isEmpty(configJSON)) {
  183. logger.info(
  184. `Extending ${configName} with: ${
  185. JSON.stringify(configJSON)}`);
  186. // eslint-disable-next-line arrow-body-style
  187. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  188. // XXX We don't want to merge the arrays, we want to
  189. // overwrite them.
  190. return Array.isArray(oldValue) ? newValue : undefined;
  191. });
  192. }
  193. }
  194. }
  195. }
  196. /**
  197. * Whitelist only config.js, skips this for others configs
  198. * (interfaceConfig, loggingConfig).
  199. * Only extracts overridden values for keys we allow to be overridden.
  200. *
  201. * @param {string} configName - The config name, one of config,
  202. * interfaceConfig, loggingConfig.
  203. * @param {Object} configJSON - The object with keys and values to override.
  204. * @returns {Object} - The result object only with the keys
  205. * that are whitelisted.
  206. * @private
  207. */
  208. function _getWhitelistedJSON(configName, configJSON) {
  209. if (configName !== 'config') {
  210. return configJSON;
  211. }
  212. return _.pick(configJSON, WHITELISTED_KEYS);
  213. }
  214. /* eslint-enable max-params, no-shadow */
  215. /**
  216. * Converts 'URL_PARAMS' to JSON object.
  217. * We have:
  218. * {
  219. * "config.disableAudioLevels": false,
  220. * "config.channelLastN": -1,
  221. * "interfaceConfig.APP_NAME": "Jitsi Meet"
  222. * }.
  223. * We want to have:
  224. * {
  225. * "config": {
  226. * "disableAudioLevels": false,
  227. * "channelLastN": -1
  228. * },
  229. * interfaceConfig: {
  230. * "APP_NAME": "Jitsi Meet"
  231. * }
  232. * }.
  233. *
  234. * @returns {void}
  235. */
  236. export function setConfigFromURLParams() {
  237. const params = parseURLParams(window.location);
  238. const { config, interfaceConfig, loggingConfig } = window;
  239. const json = {};
  240. // TODO We're still in the middle ground between old Web with config,
  241. // interfaceConfig, and loggingConfig used via global variables and new Web
  242. // and mobile reading the respective values from the redux store. On React
  243. // Native there's no interfaceConfig at all yet and loggingConfig is not
  244. // loaded but there's a default value in the redux store.
  245. config && (json.config = {});
  246. interfaceConfig && (json.interfaceConfig = {});
  247. loggingConfig && (json.loggingConfig = {});
  248. for (const param of Object.keys(params)) {
  249. let base = json;
  250. const names = param.split('.');
  251. const last = names.pop();
  252. for (const name of names) {
  253. base = base[name] = base[name] || {};
  254. }
  255. base[last] = params[param];
  256. }
  257. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  258. }