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

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