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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. '_desktopSharingSourceDevice',
  17. '_peerConnStatusOutOfLastNTimeout',
  18. '_peerConnStatusRtcMuteTimeout',
  19. 'abTesting',
  20. 'autoRecord',
  21. 'autoRecordToken',
  22. 'avgRtpStatsN',
  23. 'callFlowsEnabled',
  24. 'callStatsConfIDNamespace',
  25. 'callStatsID',
  26. 'callStatsSecret',
  27. /**
  28. * The display name of the CallKit call representing the conference/meeting
  29. * associated with this config.js including while the call is ongoing in the
  30. * UI presented by CallKit and in the system-wide call history. The property
  31. * is meant for use cases in which the room name is not desirable as a
  32. * display name for CallKit purposes and the desired display name is not
  33. * provided in the form of a JWT callee. As the value is associated with a
  34. * conference/meeting, the value makes sense not as a deployment-wide
  35. * configuration, only as a runtime configuration override/overwrite
  36. * provided by, for example, Jitsi Meet SDK for iOS.
  37. *
  38. * @type string
  39. */
  40. 'callDisplayName',
  41. /**
  42. * The handle
  43. * ({@link https://developer.apple.com/documentation/callkit/cxhandle}) of
  44. * the CallKit call representing the conference/meeting associated with this
  45. * config.js. The property is meant for use cases in which the room URL is
  46. * not desirable as the handle for CallKit purposes. As the value is
  47. * associated with a conference/meeting, the value makes sense not as a
  48. * deployment-wide configuration, only as a runtime configuration
  49. * override/overwrite provided by, for example, Jitsi Meet SDK for iOS.
  50. *
  51. * @type string
  52. */
  53. 'callHandle',
  54. /**
  55. * The UUID of the CallKit call representing the conference/meeting
  56. * associated with this config.js. The property is meant for use cases in
  57. * which Jitsi Meet is to work with a CallKit call created outside of Jitsi
  58. * Meet and to be adopted by Jitsi Meet such as, for example, an incoming
  59. * and/or outgoing CallKit call created by Jitsi Meet SDK for iOS
  60. * clients/consumers prior to giving control to Jitsi Meet. As the value is
  61. * associated with a conference/meeting, the value makes sense not as a
  62. * deployment-wide configuration, only as a runtime configuration
  63. * override/overwrite provided by, for example, Jitsi Meet SDK for iOS.
  64. *
  65. * @type string
  66. */
  67. 'callUUID',
  68. 'channelLastN',
  69. 'constraints',
  70. 'debug',
  71. 'debugAudioLevels',
  72. 'defaultLanguage',
  73. 'desktopSharingChromeDisabled',
  74. 'desktopSharingChromeExtId',
  75. 'desktopSharingChromeMinExtVersion',
  76. 'desktopSharingChromeSources',
  77. 'desktopSharingFrameRate',
  78. 'desktopSharingFirefoxDisabled',
  79. 'desktopSharingSources',
  80. 'disable1On1Mode',
  81. 'disableAEC',
  82. 'disableAGC',
  83. 'disableAP',
  84. 'disableAudioLevels',
  85. 'disableH264',
  86. 'disableHPF',
  87. 'disableNS',
  88. 'disableRemoteControl',
  89. 'disableRtx',
  90. 'disableSuspendVideo',
  91. 'displayJids',
  92. 'e2eping',
  93. 'enableDisplayNameInStats',
  94. 'enableLayerSuspension',
  95. 'enableLipSync',
  96. 'enableLocalVideoFlip',
  97. 'enableRemb',
  98. 'enableStatsID',
  99. 'enableTalkWhileMuted',
  100. 'enableTcc',
  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. * Promise wrapper on obtain config method. When HttpConfigFetch will be moved
  145. * to React app it's better to use load config instead.
  146. *
  147. * @param {string} location - URL of the domain from which the config is to be
  148. * obtained.
  149. * @param {string} room - Room name.
  150. * @private
  151. * @returns {Promise<void>}
  152. */
  153. export function obtainConfig(location: string, room: string): Promise<void> {
  154. return new Promise((resolve, reject) =>
  155. _obtainConfig(location, room, (success, error) => {
  156. success ? resolve() : reject(error);
  157. })
  158. );
  159. }
  160. /**
  161. * Sends HTTP POST request to specified {@code endpoint}. In request the name
  162. * of the room is included in JSON format:
  163. * {
  164. * "rooomName": "someroom12345"
  165. * }.
  166. *
  167. * @param {string} endpoint - The name of HTTP endpoint to which to send
  168. * the HTTP POST request.
  169. * @param {string} roomName - The name of the conference room for which config
  170. * is requested.
  171. * @param {Function} complete - The callback to invoke upon success or failure.
  172. * @returns {void}
  173. */
  174. function _obtainConfig(endpoint: string, roomName: string, complete: Function) {
  175. logger.info(`Send config request to ${endpoint} for room: ${roomName}`);
  176. $.ajax(
  177. endpoint,
  178. {
  179. contentType: 'application/json',
  180. data: JSON.stringify({ roomName }),
  181. dataType: 'json',
  182. method: 'POST',
  183. error(jqXHR, textStatus, errorThrown) {
  184. logger.error('Get config error: ', jqXHR, errorThrown);
  185. complete(false, `Get config response status: ${textStatus}`);
  186. },
  187. success(data) {
  188. const { config, interfaceConfig, loggingConfig } = window;
  189. try {
  190. overrideConfigJSON(
  191. config, interfaceConfig, loggingConfig,
  192. data);
  193. complete(true);
  194. } catch (e) {
  195. logger.error('Parse config error: ', e);
  196. complete(false, e);
  197. }
  198. }
  199. }
  200. );
  201. }
  202. /* eslint-disable max-params, no-shadow */
  203. /**
  204. * Overrides JSON properties in {@code config} and
  205. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  206. * Overrides only the whitelisted keys.
  207. *
  208. * @param {Object} config - The config Object in which we'll be overriding
  209. * properties.
  210. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  211. * be overriding properties.
  212. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  213. * overriding properties.
  214. * @param {Object} json - Object containing configuration properties.
  215. * Destination object is selected based on root property name:
  216. * {
  217. * config: {
  218. * // config.js properties here
  219. * },
  220. * interfaceConfig: {
  221. * // interface_config.js properties here
  222. * },
  223. * loggingConfig: {
  224. * // logging_config.js properties here
  225. * }
  226. * }.
  227. * @returns {void}
  228. */
  229. export function overrideConfigJSON(
  230. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  231. json: Object) {
  232. for (const configName of Object.keys(json)) {
  233. let configObj;
  234. if (configName === 'config') {
  235. configObj = config;
  236. } else if (configName === 'interfaceConfig') {
  237. configObj = interfaceConfig;
  238. } else if (configName === 'loggingConfig') {
  239. configObj = loggingConfig;
  240. }
  241. if (configObj) {
  242. const configJSON
  243. = _getWhitelistedJSON(configName, json[configName]);
  244. if (!_.isEmpty(configJSON)) {
  245. logger.info(
  246. `Extending ${configName} with: ${
  247. JSON.stringify(configJSON)}`);
  248. // eslint-disable-next-line arrow-body-style
  249. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  250. // XXX We don't want to merge the arrays, we want to
  251. // overwrite them.
  252. return Array.isArray(oldValue) ? newValue : undefined;
  253. });
  254. }
  255. }
  256. }
  257. }
  258. /* eslint-enable max-params, no-shadow */
  259. /**
  260. * Whitelist only config.js, skips this for others configs
  261. * (interfaceConfig, loggingConfig).
  262. * Only extracts overridden values for keys we allow to be overridden.
  263. *
  264. * @param {string} configName - The config name, one of config,
  265. * interfaceConfig, loggingConfig.
  266. * @param {Object} configJSON - The object with keys and values to override.
  267. * @private
  268. * @returns {Object} - The result object only with the keys
  269. * that are whitelisted.
  270. */
  271. function _getWhitelistedJSON(configName, configJSON) {
  272. if (configName !== 'config') {
  273. return configJSON;
  274. }
  275. return _.pick(configJSON, WHITELISTED_KEYS);
  276. }
  277. /**
  278. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  279. * previously downloaded from a specific {@code baseURL} and stored with
  280. * {@link storeConfig}.
  281. *
  282. * @param {string} baseURL - The base URL from which the config.js was
  283. * previously downloaded and stored with {@code storeConfig}.
  284. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  285. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  286. * otherwise, {@code undefined}.
  287. */
  288. export function restoreConfig(baseURL: string): ?Object {
  289. let storage;
  290. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  291. try {
  292. // XXX Even reading the property localStorage of window may throw an
  293. // error (which is user agent-specific behavior).
  294. storage = window.localStorage;
  295. const config = storage.getItem(key);
  296. if (config) {
  297. return JSON.parse(config) || undefined;
  298. }
  299. } catch (e) {
  300. // Somehow incorrect data ended up in the storage. Clean it up.
  301. storage && storage.removeItem(key);
  302. }
  303. return undefined;
  304. }
  305. /* eslint-disable max-params */
  306. /**
  307. * Inspects the hash part of the location URI and overrides values specified
  308. * there in the corresponding config objects given as the arguments. The syntax
  309. * is: {@code https://server.com/room#config.debug=true
  310. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  311. *
  312. * In the hash part each parameter will be parsed to JSON and then the root
  313. * object will be matched with the corresponding config object given as the
  314. * argument to this function.
  315. *
  316. * @param {Object} config - This is the general config.
  317. * @param {Object} interfaceConfig - This is the interface config.
  318. * @param {Object} loggingConfig - The logging config.
  319. * @param {URI} location - The new location to which the app is navigating to.
  320. * @returns {void}
  321. */
  322. export function setConfigFromURLParams(
  323. config: ?Object,
  324. interfaceConfig: ?Object,
  325. loggingConfig: ?Object,
  326. location: Object) {
  327. const params = parseURLParams(location);
  328. const json = {};
  329. // At this point we have:
  330. // params = {
  331. // "config.disableAudioLevels": false,
  332. // "config.channelLastN": -1,
  333. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  334. // }
  335. // We want to have:
  336. // json = {
  337. // config: {
  338. // "disableAudioLevels": false,
  339. // "channelLastN": -1
  340. // },
  341. // interfaceConfig: {
  342. // "APP_NAME": "Jitsi Meet"
  343. // }
  344. // }
  345. config && (json.config = {});
  346. interfaceConfig && (json.interfaceConfig = {});
  347. loggingConfig && (json.loggingConfig = {});
  348. for (const param of Object.keys(params)) {
  349. let base = json;
  350. const names = param.split('.');
  351. const last = names.pop();
  352. for (const name of names) {
  353. base = base[name] = base[name] || {};
  354. }
  355. base[last] = params[param];
  356. }
  357. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  358. }
  359. /* eslint-enable max-params */