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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. 'autoRecord',
  20. 'autoRecordToken',
  21. 'avgRtpStatsN',
  22. 'callStatsConfIDNamespace',
  23. 'callStatsID',
  24. 'callStatsSecret',
  25. 'channelLastN',
  26. 'constraints',
  27. 'debug',
  28. 'debugAudioLevels',
  29. 'defaultLanguage',
  30. 'desktopSharingChromeDisabled',
  31. 'desktopSharingChromeExtId',
  32. 'desktopSharingChromeMinExtVersion',
  33. 'desktopSharingChromeSources',
  34. 'desktopSharingFrameRate',
  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. 'enableRemb',
  56. 'enableStatsID',
  57. 'enableTalkWhileMuted',
  58. 'enableTcc',
  59. 'enableUserRolesBasedOnToken',
  60. 'etherpad_base',
  61. 'failICE',
  62. 'firefox_fake_device',
  63. 'forceJVB121Ratio',
  64. 'gatherStats',
  65. 'googleApiApplicationClientID',
  66. 'hiddenDomain',
  67. 'hosts',
  68. 'iAmRecorder',
  69. 'iAmSipGateway',
  70. 'iceTransportPolicy',
  71. 'ignoreStartMuted',
  72. 'minParticipants',
  73. 'nick',
  74. 'openBridgeChannel',
  75. 'p2p',
  76. 'preferH264',
  77. 'recordingType',
  78. 'requireDisplayName',
  79. 'resolution',
  80. 'startAudioMuted',
  81. 'startAudioOnly',
  82. 'startBitrate',
  83. 'startScreenSharing',
  84. 'startVideoMuted',
  85. 'startWithAudioMuted',
  86. 'startWithVideoMuted',
  87. 'testing',
  88. 'useIPv6',
  89. 'useNicks',
  90. 'useStunTurn',
  91. 'webrtcIceTcpDisable',
  92. 'webrtcIceUdpDisable'
  93. ];
  94. const logger = require('jitsi-meet-logger').getLogger(__filename);
  95. // XXX The functions getRoomName and parseURLParams are split out of
  96. // functions.js because they are bundled in both app.bundle and
  97. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  98. // want all functions to be bundled in do_external_connect.
  99. export { default as getRoomName } from './getRoomName';
  100. export { parseURLParams };
  101. /**
  102. * Sends HTTP POST request to specified {@code endpoint}. In request the name
  103. * of the room is included in JSON format:
  104. * {
  105. * "rooomName": "someroom12345"
  106. * }.
  107. *
  108. * @param {string} endpoint - The name of HTTP endpoint to which to send
  109. * the HTTP POST request.
  110. * @param {string} roomName - The name of the conference room for which config
  111. * is requested.
  112. * @param {Function} complete - The callback to invoke upon success or failure.
  113. * @returns {void}
  114. */
  115. export function obtainConfig(
  116. endpoint: string,
  117. roomName: string,
  118. complete: Function) {
  119. logger.info(`Send config request to ${endpoint} for room: ${roomName}`);
  120. $.ajax(
  121. endpoint,
  122. {
  123. contentType: 'application/json',
  124. data: JSON.stringify({ roomName }),
  125. dataType: 'json',
  126. method: 'POST',
  127. error(jqXHR, textStatus, errorThrown) {
  128. logger.error('Get config error: ', jqXHR, errorThrown);
  129. complete(false, `Get config response status: ${textStatus}`);
  130. },
  131. success(data) {
  132. const { config, interfaceConfig, loggingConfig } = window;
  133. try {
  134. overrideConfigJSON(
  135. config, interfaceConfig, loggingConfig,
  136. data);
  137. complete(true);
  138. } catch (e) {
  139. logger.error('Parse config error: ', e);
  140. complete(false, e);
  141. }
  142. }
  143. }
  144. );
  145. }
  146. /* eslint-disable max-params, no-shadow */
  147. /**
  148. * Overrides JSON properties in {@code config} and
  149. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  150. * Overrides only the whitelisted keys.
  151. *
  152. * @param {Object} config - The config Object in which we'll be overriding
  153. * properties.
  154. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  155. * be overriding properties.
  156. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  157. * overriding properties.
  158. * @param {Object} json - Object containing configuration properties.
  159. * Destination object is selected based on root property name:
  160. * {
  161. * config: {
  162. * // config.js properties here
  163. * },
  164. * interfaceConfig: {
  165. * // interface_config.js properties here
  166. * },
  167. * loggingConfig: {
  168. * // logging_config.js properties here
  169. * }
  170. * }.
  171. * @returns {void}
  172. */
  173. export function overrideConfigJSON(
  174. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  175. json: Object) {
  176. for (const configName of Object.keys(json)) {
  177. let configObj;
  178. if (configName === 'config') {
  179. configObj = config;
  180. } else if (configName === 'interfaceConfig') {
  181. configObj = interfaceConfig;
  182. } else if (configName === 'loggingConfig') {
  183. configObj = loggingConfig;
  184. }
  185. if (configObj) {
  186. const configJSON
  187. = _getWhitelistedJSON(configName, json[configName]);
  188. if (!_.isEmpty(configJSON)) {
  189. logger.info(
  190. `Extending ${configName} with: ${
  191. JSON.stringify(configJSON)}`);
  192. // eslint-disable-next-line arrow-body-style
  193. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  194. // XXX We don't want to merge the arrays, we want to
  195. // overwrite them.
  196. return Array.isArray(oldValue) ? newValue : undefined;
  197. });
  198. }
  199. }
  200. }
  201. }
  202. /* eslint-enable max-params, no-shadow */
  203. /**
  204. * Whitelist only config.js, skips this for others configs
  205. * (interfaceConfig, loggingConfig).
  206. * Only extracts overridden values for keys we allow to be overridden.
  207. *
  208. * @param {string} configName - The config name, one of config,
  209. * interfaceConfig, loggingConfig.
  210. * @param {Object} configJSON - The object with keys and values to override.
  211. * @private
  212. * @returns {Object} - The result object only with the keys
  213. * that are whitelisted.
  214. */
  215. function _getWhitelistedJSON(configName, configJSON) {
  216. if (configName !== 'config') {
  217. return configJSON;
  218. }
  219. return _.pick(configJSON, WHITELISTED_KEYS);
  220. }
  221. /* eslint-disable max-params */
  222. /**
  223. * Inspects the hash part of the location URI and overrides values specified
  224. * there in the corresponding config objects given as the arguments. The syntax
  225. * is: {@code https://server.com/room#config.debug=true
  226. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  227. *
  228. * In the hash part each parameter will be parsed to JSON and then the root
  229. * object will be matched with the corresponding config object given as the
  230. * argument to this function.
  231. *
  232. * @param {Object} config - This is the general config.
  233. * @param {Object} interfaceConfig - This is the interface config.
  234. * @param {Object} loggingConfig - The logging config.
  235. * @param {URI} location - The new location to which the app is navigating to.
  236. * @returns {void}
  237. */
  238. export function setConfigFromURLParams(
  239. config: ?Object,
  240. interfaceConfig: ?Object,
  241. loggingConfig: ?Object,
  242. location: Object) {
  243. const params = parseURLParams(location);
  244. const json = {};
  245. // At this point we have:
  246. // params = {
  247. // "config.disableAudioLevels": false,
  248. // "config.channelLastN": -1,
  249. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  250. // }
  251. // We want to have:
  252. // json = {
  253. // config: {
  254. // "disableAudioLevels": false,
  255. // "channelLastN": -1
  256. // },
  257. // interfaceConfig: {
  258. // "APP_NAME": "Jitsi Meet"
  259. // }
  260. // }
  261. config && (json.config = {});
  262. interfaceConfig && (json.interfaceConfig = {});
  263. loggingConfig && (json.loggingConfig = {});
  264. for (const param of Object.keys(params)) {
  265. let base = json;
  266. const names = param.split('.');
  267. const last = names.pop();
  268. for (const name of names) {
  269. base = base[name] = base[name] || {};
  270. }
  271. base[last] = params[param];
  272. }
  273. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  274. }
  275. /* eslint-enable max-params */