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.

participants.ts 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import fs from 'fs';
  2. import jwt from 'jsonwebtoken';
  3. import process from 'node:process';
  4. import { v4 as uuidv4 } from 'uuid';
  5. import { Participant } from './Participant';
  6. import WebhookProxy from './WebhookProxy';
  7. import { IContext } from './types';
  8. /**
  9. * Generate a random room name.
  10. * Everytime we generate a name and iframeAPI is enabled and there is a configured
  11. * webhooks proxy we connect to it with the new room name.
  12. *
  13. * @returns {string} - The random room name.
  14. */
  15. function generateRandomRoomName(): string {
  16. const roomName = `jitsimeettorture-${crypto.randomUUID()}`;
  17. if (context.iframeAPI && !context.webhooksProxy
  18. && process.env.WEBHOOKS_PROXY_URL && process.env.WEBHOOKS_PROXY_SHARED_SECRET) {
  19. context.webhooksProxy = new WebhookProxy(`${process.env.WEBHOOKS_PROXY_URL}&room=${roomName}`,
  20. process.env.WEBHOOKS_PROXY_SHARED_SECRET);
  21. context.webhooksProxy.connect();
  22. }
  23. return roomName;
  24. }
  25. /**
  26. * Ensure that there is on participant.
  27. *
  28. * @param {IContext} context - The context.
  29. * @returns {Promise<void>}
  30. */
  31. export async function ensureOneParticipant(context: IContext): Promise<void> {
  32. if (!context.roomName) {
  33. context.roomName = generateRandomRoomName();
  34. }
  35. context.p1 = new Participant('participant1');
  36. await context.p1.joinConference(context, true);
  37. }
  38. /**
  39. * Ensure that there are three participants.
  40. *
  41. * @param {Object} context - The context.
  42. * @returns {Promise<void>}
  43. */
  44. export async function ensureThreeParticipants(context: IContext): Promise<void> {
  45. if (!context.roomName) {
  46. context.roomName = generateRandomRoomName();
  47. }
  48. const p1 = new Participant('participant1');
  49. const p2 = new Participant('participant2');
  50. const p3 = new Participant('participant3');
  51. context.p1 = p1;
  52. context.p2 = p2;
  53. context.p3 = p3;
  54. // these need to be all, so we get the error when one fails
  55. await Promise.all([
  56. p1.joinConference(context),
  57. p2.joinConference(context),
  58. p3.joinConference(context)
  59. ]);
  60. await Promise.all([
  61. p1.waitForRemoteStreams(2),
  62. p2.waitForRemoteStreams(2),
  63. p3.waitForRemoteStreams(2)
  64. ]);
  65. }
  66. /**
  67. * Ensure that there are two participants.
  68. *
  69. * @param {Object} context - The context.
  70. * @returns {Promise<void>}
  71. */
  72. export async function ensureTwoParticipants(context: IContext): Promise<void> {
  73. if (!context.roomName) {
  74. context.roomName = generateRandomRoomName();
  75. }
  76. const p1DisplayName = 'participant1';
  77. let token;
  78. // if it is jaas create the first one to be moderator and second not moderator
  79. if (context.jwtPrivateKeyPath) {
  80. token = getModeratorToken(p1DisplayName);
  81. }
  82. // make sure the first participant is moderator, if supported by deployment
  83. await _joinParticipant(p1DisplayName, context.p1, p => {
  84. context.p1 = p;
  85. }, true, token);
  86. await Promise.all([
  87. _joinParticipant('participant2', context.p2, p => {
  88. context.p2 = p;
  89. }),
  90. context.p1.waitForRemoteStreams(1),
  91. context.p2.waitForRemoteStreams(1)
  92. ]);
  93. }
  94. /**
  95. * Creates a participant instance or prepares one for re-joining.
  96. * @param name - The name of the participant.
  97. * @param p - The participant instance to prepare or undefined if new one is needed.
  98. * @param setter - The setter to use for setting the new participant instance into the context if needed.
  99. * @param {boolean} skipInMeetingChecks - Whether to skip in meeting checks.
  100. * @param {string?} jwtToken - The token to use if any.
  101. */
  102. async function _joinParticipant( // eslint-disable-line max-params
  103. name: string,
  104. p: Participant,
  105. setter: (p: Participant) => void,
  106. skipInMeetingChecks = false,
  107. jwtToken?: string) {
  108. if (p) {
  109. await p.switchInPage();
  110. if (await p.isInMuc()) {
  111. return;
  112. }
  113. // when loading url make sure we are on the top page context or strange errors may occur
  114. await p.switchToAPI();
  115. // Change the page so we can reload same url if we need to, base.html is supposed to be empty or close to empty
  116. await p.driver.url('/base.html');
  117. // we want the participant instance re-recreated so we clear any kept state, like endpoint ID
  118. }
  119. const newParticipant = new Participant(name, jwtToken);
  120. // set the new participant instance, pass it to setter
  121. setter(newParticipant);
  122. return newParticipant.joinConference(context, skipInMeetingChecks);
  123. }
  124. /**
  125. * Toggles the mute state of a specific Meet conference participant and verifies that a specific other Meet
  126. * conference participants sees a specific mute state for the former.
  127. *
  128. * @param {Participant} testee - The {@code Participant} which represents the Meet conference participant whose
  129. * mute state is to be toggled.
  130. * @param {Participant} observer - The {@code Participant} which represents the Meet conference participant to verify
  131. * the mute state of {@code testee}.
  132. * @returns {Promise<void>}
  133. */
  134. export async function toggleMuteAndCheck(testee: Participant, observer: Participant): Promise<void> {
  135. await testee.getToolbar().clickAudioMuteButton();
  136. await observer.getFilmstrip().assertAudioMuteIconIsDisplayed(testee);
  137. await testee.getFilmstrip().assertAudioMuteIconIsDisplayed(testee);
  138. }
  139. /**
  140. * Get a JWT token for a moderator.
  141. */
  142. function getModeratorToken(displayName: string) {
  143. const keyid = process.env.JWT_KID;
  144. const headers = {
  145. algorithm: 'RS256',
  146. noTimestamp: true,
  147. expiresIn: '24h',
  148. keyid
  149. };
  150. if (!keyid) {
  151. console.error('JWT_KID is not set');
  152. return;
  153. }
  154. const key = fs.readFileSync(context.jwtPrivateKeyPath);
  155. const payload = {
  156. 'aud': 'jitsi',
  157. 'iss': 'chat',
  158. 'sub': keyid.substring(0, keyid.indexOf('/')),
  159. 'context': {
  160. 'user': {
  161. 'name': displayName,
  162. 'id': uuidv4(),
  163. 'avatar': 'https://avatars0.githubusercontent.com/u/3671647',
  164. 'email': 'john.doe@jitsi.org'
  165. }
  166. },
  167. 'room': '*'
  168. };
  169. // @ts-ignore
  170. payload.context.user.moderator = true;
  171. // @ts-ignore
  172. return jwt.sign(payload, key, headers);
  173. }
  174. /**
  175. * Parse a JID string.
  176. * @param str the string to parse.
  177. */
  178. export function parseJid(str: string): {
  179. domain: string;
  180. node: string;
  181. resource: string | undefined;
  182. } {
  183. const parts = str.split('@');
  184. const domainParts = parts[1].split('/');
  185. return {
  186. node: parts[0],
  187. domain: domainParts[0],
  188. resource: domainParts.length > 0 ? domainParts[1] : undefined
  189. };
  190. }