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

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