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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* @flow */
  2. import _ from 'lodash';
  3. import { _CONFIG_STORE_PREFIX } from './constants';
  4. import parseURLParams from './parseURLParams';
  5. declare var $: Object;
  6. /**
  7. * The config keys to whitelist, the keys that can be overridden.
  8. * Currently we can only whitelist the first part of the properties, like
  9. * 'p2p.useStunTurn' and 'p2p.enabled' we whitelist all p2p options.
  10. * The whitelist is used only for config.js.
  11. *
  12. * @private
  13. * @type Array
  14. */
  15. const WHITELISTED_KEYS = [
  16. '_peerConnStatusOutOfLastNTimeout',
  17. '_peerConnStatusRtcMuteTimeout',
  18. 'abTesting',
  19. 'autoRecord',
  20. 'autoRecordToken',
  21. 'avgRtpStatsN',
  22. 'callFlowsEnabled',
  23. 'callStatsConfIDNamespace',
  24. 'callStatsID',
  25. 'callStatsSecret',
  26. /**
  27. * The display name of the CallKit call representing the conference/meeting
  28. * associated with this config.js including while the call is ongoing in the
  29. * UI presented by CallKit and in the system-wide call history. The property
  30. * is meant for use cases in which the room name is not desirable as a
  31. * display name for CallKit purposes and the desired display name is not
  32. * provided in the form of a JWT callee. As the value is associated with a
  33. * conference/meeting, the value makes sense not as a deployment-wide
  34. * configuration, only as a runtime configuration override/overwrite
  35. * provided by, for example, Jitsi Meet SDK for iOS.
  36. *
  37. * @type string
  38. */
  39. 'callDisplayName',
  40. /**
  41. * The handle
  42. * ({@link https://developer.apple.com/documentation/callkit/cxhandle}) of
  43. * the CallKit call representing the conference/meeting associated with this
  44. * config.js. The property is meant for use cases in which the room URL is
  45. * not desirable as the handle for CallKit purposes. As the value is
  46. * associated with a conference/meeting, the value makes sense not as a
  47. * deployment-wide configuration, only as a runtime configuration
  48. * override/overwrite provided by, for example, Jitsi Meet SDK for iOS.
  49. *
  50. * @type string
  51. */
  52. 'callHandle',
  53. /**
  54. * The UUID of the CallKit call representing the conference/meeting
  55. * associated with this config.js. The property is meant for use cases in
  56. * which Jitsi Meet is to work with a CallKit call created outside of Jitsi
  57. * Meet and to be adopted by Jitsi Meet such as, for example, an incoming
  58. * and/or outgoing CallKit call created by Jitsi Meet SDK for iOS
  59. * clients/consumers prior to giving control to Jitsi Meet. As the value is
  60. * associated with a conference/meeting, the value makes sense not as a
  61. * deployment-wide configuration, only as a runtime configuration
  62. * override/overwrite provided by, for example, Jitsi Meet SDK for iOS.
  63. *
  64. * @type string
  65. */
  66. 'callUUID',
  67. 'channelLastN',
  68. 'constraints',
  69. 'debug',
  70. 'debugAudioLevels',
  71. 'defaultLanguage',
  72. 'desktopSharingChromeDisabled',
  73. 'desktopSharingChromeExtId',
  74. 'desktopSharingChromeMinExtVersion',
  75. 'desktopSharingChromeSources',
  76. 'desktopSharingFrameRate',
  77. 'desktopSharingFirefoxDisabled',
  78. 'desktopSharingSources',
  79. 'disable1On1Mode',
  80. 'disableAEC',
  81. 'disableAGC',
  82. 'disableAP',
  83. 'disableAudioLevels',
  84. 'disableDesktopSharing',
  85. 'disableDesktopSharing',
  86. 'disableH264',
  87. 'disableHPF',
  88. 'disableNS',
  89. 'disableRemoteControl',
  90. 'disableRtx',
  91. 'disableSuspendVideo',
  92. 'displayJids',
  93. 'enableDisplayNameInStats',
  94. 'enableLipSync',
  95. 'enableLocalVideoFlip',
  96. 'enableRemb',
  97. 'enableStatsID',
  98. 'enableTalkWhileMuted',
  99. 'enableTcc',
  100. 'enableUserRolesBasedOnToken',
  101. 'etherpad_base',
  102. 'failICE',
  103. 'fileRecordingsEnabled',
  104. 'firefox_fake_device',
  105. 'forceJVB121Ratio',
  106. 'gatherStats',
  107. 'googleApiApplicationClientID',
  108. 'hiddenDomain',
  109. 'hosts',
  110. 'iAmRecorder',
  111. 'iAmSipGateway',
  112. 'iceTransportPolicy',
  113. 'ignoreStartMuted',
  114. 'liveStreamingEnabled',
  115. 'minParticipants',
  116. 'nick',
  117. 'openBridgeChannel',
  118. 'p2p',
  119. 'preferH264',
  120. 'requireDisplayName',
  121. 'resolution',
  122. 'startAudioMuted',
  123. 'startAudioOnly',
  124. 'startBitrate',
  125. 'startScreenSharing',
  126. 'startVideoMuted',
  127. 'startWithAudioMuted',
  128. 'startWithVideoMuted',
  129. 'testing',
  130. 'useIPv6',
  131. 'useNicks',
  132. 'useStunTurn',
  133. 'webrtcIceTcpDisable',
  134. 'webrtcIceUdpDisable'
  135. ];
  136. const logger = require('jitsi-meet-logger').getLogger(__filename);
  137. // XXX The functions getRoomName and parseURLParams are split out of
  138. // functions.js because they are bundled in both app.bundle and
  139. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  140. // want all functions to be bundled in do_external_connect.
  141. export { default as getRoomName } from './getRoomName';
  142. export { parseURLParams };
  143. /**
  144. * Sends HTTP POST request to specified {@code endpoint}. In request the name
  145. * of the room is included in JSON format:
  146. * {
  147. * "rooomName": "someroom12345"
  148. * }.
  149. *
  150. * @param {string} endpoint - The name of HTTP endpoint to which to send
  151. * the HTTP POST request.
  152. * @param {string} roomName - The name of the conference room for which config
  153. * is requested.
  154. * @param {Function} complete - The callback to invoke upon success or failure.
  155. * @returns {void}
  156. */
  157. export function obtainConfig(
  158. endpoint: string,
  159. roomName: string,
  160. complete: Function) {
  161. logger.info(`Send config request to ${endpoint} for room: ${roomName}`);
  162. $.ajax(
  163. endpoint,
  164. {
  165. contentType: 'application/json',
  166. data: JSON.stringify({ roomName }),
  167. dataType: 'json',
  168. method: 'POST',
  169. error(jqXHR, textStatus, errorThrown) {
  170. logger.error('Get config error: ', jqXHR, errorThrown);
  171. complete(false, `Get config response status: ${textStatus}`);
  172. },
  173. success(data) {
  174. const { config, interfaceConfig, loggingConfig } = window;
  175. try {
  176. overrideConfigJSON(
  177. config, interfaceConfig, loggingConfig,
  178. data);
  179. complete(true);
  180. } catch (e) {
  181. logger.error('Parse config error: ', e);
  182. complete(false, e);
  183. }
  184. }
  185. }
  186. );
  187. }
  188. /* eslint-disable max-params, no-shadow */
  189. /**
  190. * Overrides JSON properties in {@code config} and
  191. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  192. * Overrides only the whitelisted keys.
  193. *
  194. * @param {Object} config - The config Object in which we'll be overriding
  195. * properties.
  196. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  197. * be overriding properties.
  198. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  199. * overriding properties.
  200. * @param {Object} json - Object containing configuration properties.
  201. * Destination object is selected based on root property name:
  202. * {
  203. * config: {
  204. * // config.js properties here
  205. * },
  206. * interfaceConfig: {
  207. * // interface_config.js properties here
  208. * },
  209. * loggingConfig: {
  210. * // logging_config.js properties here
  211. * }
  212. * }.
  213. * @returns {void}
  214. */
  215. export function overrideConfigJSON(
  216. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  217. json: Object) {
  218. for (const configName of Object.keys(json)) {
  219. let configObj;
  220. if (configName === 'config') {
  221. configObj = config;
  222. } else if (configName === 'interfaceConfig') {
  223. configObj = interfaceConfig;
  224. } else if (configName === 'loggingConfig') {
  225. configObj = loggingConfig;
  226. }
  227. if (configObj) {
  228. const configJSON
  229. = _getWhitelistedJSON(configName, json[configName]);
  230. if (!_.isEmpty(configJSON)) {
  231. logger.info(
  232. `Extending ${configName} with: ${
  233. JSON.stringify(configJSON)}`);
  234. // eslint-disable-next-line arrow-body-style
  235. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  236. // XXX We don't want to merge the arrays, we want to
  237. // overwrite them.
  238. return Array.isArray(oldValue) ? newValue : undefined;
  239. });
  240. }
  241. }
  242. }
  243. }
  244. /* eslint-enable max-params, no-shadow */
  245. /**
  246. * Whitelist only config.js, skips this for others configs
  247. * (interfaceConfig, loggingConfig).
  248. * Only extracts overridden values for keys we allow to be overridden.
  249. *
  250. * @param {string} configName - The config name, one of config,
  251. * interfaceConfig, loggingConfig.
  252. * @param {Object} configJSON - The object with keys and values to override.
  253. * @private
  254. * @returns {Object} - The result object only with the keys
  255. * that are whitelisted.
  256. */
  257. function _getWhitelistedJSON(configName, configJSON) {
  258. if (configName !== 'config') {
  259. return configJSON;
  260. }
  261. return _.pick(configJSON, WHITELISTED_KEYS);
  262. }
  263. /**
  264. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  265. * previously downloaded from a specific {@code baseURL} and stored with
  266. * {@link storeConfig}.
  267. *
  268. * @param {string} baseURL - The base URL from which the config.js was
  269. * previously downloaded and stored with {@code storeConfig}.
  270. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  271. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  272. * otherwise, {@code undefined}.
  273. */
  274. export function restoreConfig(baseURL: string): ?Object {
  275. let storage;
  276. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  277. try {
  278. // XXX Even reading the property localStorage of window may throw an
  279. // error (which is user agent-specific behavior).
  280. storage = window.localStorage;
  281. const config = storage.getItem(key);
  282. if (config) {
  283. return JSON.parse(config) || undefined;
  284. }
  285. } catch (e) {
  286. // Somehow incorrect data ended up in the storage. Clean it up.
  287. storage && storage.removeItem(key);
  288. }
  289. return undefined;
  290. }
  291. /* eslint-disable max-params */
  292. /**
  293. * Inspects the hash part of the location URI and overrides values specified
  294. * there in the corresponding config objects given as the arguments. The syntax
  295. * is: {@code https://server.com/room#config.debug=true
  296. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  297. *
  298. * In the hash part each parameter will be parsed to JSON and then the root
  299. * object will be matched with the corresponding config object given as the
  300. * argument to this function.
  301. *
  302. * @param {Object} config - This is the general config.
  303. * @param {Object} interfaceConfig - This is the interface config.
  304. * @param {Object} loggingConfig - The logging config.
  305. * @param {URI} location - The new location to which the app is navigating to.
  306. * @returns {void}
  307. */
  308. export function setConfigFromURLParams(
  309. config: ?Object,
  310. interfaceConfig: ?Object,
  311. loggingConfig: ?Object,
  312. location: Object) {
  313. const params = parseURLParams(location);
  314. const json = {};
  315. // At this point we have:
  316. // params = {
  317. // "config.disableAudioLevels": false,
  318. // "config.channelLastN": -1,
  319. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  320. // }
  321. // We want to have:
  322. // json = {
  323. // config: {
  324. // "disableAudioLevels": false,
  325. // "channelLastN": -1
  326. // },
  327. // interfaceConfig: {
  328. // "APP_NAME": "Jitsi Meet"
  329. // }
  330. // }
  331. config && (json.config = {});
  332. interfaceConfig && (json.interfaceConfig = {});
  333. loggingConfig && (json.loggingConfig = {});
  334. for (const param of Object.keys(params)) {
  335. let base = json;
  336. const names = param.split('.');
  337. const last = names.pop();
  338. for (const name of names) {
  339. base = base[name] = base[name] || {};
  340. }
  341. base[last] = params[param];
  342. }
  343. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  344. }
  345. /* eslint-enable max-params */