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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. 'disableLocalVideoFlip',
  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. 'localRecording',
  116. 'minParticipants',
  117. 'nick',
  118. 'openBridgeChannel',
  119. 'p2p',
  120. 'preferH264',
  121. 'requireDisplayName',
  122. 'resolution',
  123. 'startAudioMuted',
  124. 'startAudioOnly',
  125. 'startBitrate',
  126. 'startScreenSharing',
  127. 'startVideoMuted',
  128. 'startWithAudioMuted',
  129. 'startWithVideoMuted',
  130. 'testing',
  131. 'useIPv6',
  132. 'useNicks',
  133. 'useStunTurn',
  134. 'webrtcIceTcpDisable',
  135. 'webrtcIceUdpDisable'
  136. ];
  137. const logger = require('jitsi-meet-logger').getLogger(__filename);
  138. // XXX The functions getRoomName and parseURLParams are split out of
  139. // functions.js because they are bundled in both app.bundle and
  140. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  141. // want all functions to be bundled in do_external_connect.
  142. export { default as getRoomName } from './getRoomName';
  143. export { parseURLParams };
  144. /**
  145. * Promise wrapper on obtain config method. When HttpConfigFetch will be moved
  146. * to React app it's better to use load config instead.
  147. *
  148. * @param {string} location - URL of the domain from which the config is to be
  149. * obtained.
  150. * @param {string} room - Room name.
  151. * @private
  152. * @returns {Promise<void>}
  153. */
  154. export function obtainConfig(location: string, room: string): Promise<void> {
  155. return new Promise((resolve, reject) =>
  156. _obtainConfig(location, room, (success, error) => {
  157. success ? resolve() : reject(error);
  158. })
  159. );
  160. }
  161. /**
  162. * Sends HTTP POST request to specified {@code endpoint}. In request the name
  163. * of the room is included in JSON format:
  164. * {
  165. * "rooomName": "someroom12345"
  166. * }.
  167. *
  168. * @param {string} endpoint - The name of HTTP endpoint to which to send
  169. * the HTTP POST request.
  170. * @param {string} roomName - The name of the conference room for which config
  171. * is requested.
  172. * @param {Function} complete - The callback to invoke upon success or failure.
  173. * @returns {void}
  174. */
  175. function _obtainConfig(endpoint: string, roomName: string, complete: Function) {
  176. logger.info(`Send config request to ${endpoint} for room: ${roomName}`);
  177. $.ajax(
  178. endpoint,
  179. {
  180. contentType: 'application/json',
  181. data: JSON.stringify({ roomName }),
  182. dataType: 'json',
  183. method: 'POST',
  184. error(jqXHR, textStatus, errorThrown) {
  185. logger.error('Get config error: ', jqXHR, errorThrown);
  186. complete(false, `Get config response status: ${textStatus}`);
  187. },
  188. success(data) {
  189. const { config, interfaceConfig, loggingConfig } = window;
  190. try {
  191. overrideConfigJSON(
  192. config, interfaceConfig, loggingConfig,
  193. data);
  194. complete(true);
  195. } catch (e) {
  196. logger.error('Parse config error: ', e);
  197. complete(false, e);
  198. }
  199. }
  200. }
  201. );
  202. }
  203. /* eslint-disable max-params, no-shadow */
  204. /**
  205. * Overrides JSON properties in {@code config} and
  206. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  207. * Overrides only the whitelisted keys.
  208. *
  209. * @param {Object} config - The config Object in which we'll be overriding
  210. * properties.
  211. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  212. * be overriding properties.
  213. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  214. * overriding properties.
  215. * @param {Object} json - Object containing configuration properties.
  216. * Destination object is selected based on root property name:
  217. * {
  218. * config: {
  219. * // config.js properties here
  220. * },
  221. * interfaceConfig: {
  222. * // interface_config.js properties here
  223. * },
  224. * loggingConfig: {
  225. * // logging_config.js properties here
  226. * }
  227. * }.
  228. * @returns {void}
  229. */
  230. export function overrideConfigJSON(
  231. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  232. json: Object) {
  233. for (const configName of Object.keys(json)) {
  234. let configObj;
  235. if (configName === 'config') {
  236. configObj = config;
  237. } else if (configName === 'interfaceConfig') {
  238. configObj = interfaceConfig;
  239. } else if (configName === 'loggingConfig') {
  240. configObj = loggingConfig;
  241. }
  242. if (configObj) {
  243. const configJSON
  244. = _getWhitelistedJSON(configName, json[configName]);
  245. if (!_.isEmpty(configJSON)) {
  246. logger.info(
  247. `Extending ${configName} with: ${
  248. JSON.stringify(configJSON)}`);
  249. // eslint-disable-next-line arrow-body-style
  250. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  251. // XXX We don't want to merge the arrays, we want to
  252. // overwrite them.
  253. return Array.isArray(oldValue) ? newValue : undefined;
  254. });
  255. }
  256. }
  257. }
  258. }
  259. /* eslint-enable max-params, no-shadow */
  260. /**
  261. * Whitelist only config.js, skips this for others configs
  262. * (interfaceConfig, loggingConfig).
  263. * Only extracts overridden values for keys we allow to be overridden.
  264. *
  265. * @param {string} configName - The config name, one of config,
  266. * interfaceConfig, loggingConfig.
  267. * @param {Object} configJSON - The object with keys and values to override.
  268. * @private
  269. * @returns {Object} - The result object only with the keys
  270. * that are whitelisted.
  271. */
  272. function _getWhitelistedJSON(configName, configJSON) {
  273. if (configName !== 'config') {
  274. return configJSON;
  275. }
  276. return _.pick(configJSON, WHITELISTED_KEYS);
  277. }
  278. /**
  279. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  280. * previously downloaded from a specific {@code baseURL} and stored with
  281. * {@link storeConfig}.
  282. *
  283. * @param {string} baseURL - The base URL from which the config.js was
  284. * previously downloaded and stored with {@code storeConfig}.
  285. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  286. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  287. * otherwise, {@code undefined}.
  288. */
  289. export function restoreConfig(baseURL: string): ?Object {
  290. let storage;
  291. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  292. try {
  293. // XXX Even reading the property localStorage of window may throw an
  294. // error (which is user agent-specific behavior).
  295. storage = window.localStorage;
  296. const config = storage.getItem(key);
  297. if (config) {
  298. return JSON.parse(config) || undefined;
  299. }
  300. } catch (e) {
  301. // Somehow incorrect data ended up in the storage. Clean it up.
  302. storage && storage.removeItem(key);
  303. }
  304. return undefined;
  305. }
  306. /* eslint-disable max-params */
  307. /**
  308. * Inspects the hash part of the location URI and overrides values specified
  309. * there in the corresponding config objects given as the arguments. The syntax
  310. * is: {@code https://server.com/room#config.debug=true
  311. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  312. *
  313. * In the hash part each parameter will be parsed to JSON and then the root
  314. * object will be matched with the corresponding config object given as the
  315. * argument to this function.
  316. *
  317. * @param {Object} config - This is the general config.
  318. * @param {Object} interfaceConfig - This is the interface config.
  319. * @param {Object} loggingConfig - The logging config.
  320. * @param {URI} location - The new location to which the app is navigating to.
  321. * @returns {void}
  322. */
  323. export function setConfigFromURLParams(
  324. config: ?Object,
  325. interfaceConfig: ?Object,
  326. loggingConfig: ?Object,
  327. location: Object) {
  328. const params = parseURLParams(location);
  329. const json = {};
  330. // At this point we have:
  331. // params = {
  332. // "config.disableAudioLevels": false,
  333. // "config.channelLastN": -1,
  334. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  335. // }
  336. // We want to have:
  337. // json = {
  338. // config: {
  339. // "disableAudioLevels": false,
  340. // "channelLastN": -1
  341. // },
  342. // interfaceConfig: {
  343. // "APP_NAME": "Jitsi Meet"
  344. // }
  345. // }
  346. config && (json.config = {});
  347. interfaceConfig && (json.interfaceConfig = {});
  348. loggingConfig && (json.loggingConfig = {});
  349. for (const param of Object.keys(params)) {
  350. let base = json;
  351. const names = param.split('.');
  352. const last = names.pop();
  353. for (const name of names) {
  354. base = base[name] = base[name] || {};
  355. }
  356. base[last] = params[param];
  357. }
  358. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  359. }
  360. /* eslint-enable max-params */