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

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