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

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