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.any.js 13KB

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