bbb.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { Request, ParamsDictionary, createHash } from "./deps.ts";
  2. import type { server } from './deps.ts'
  3. export class BBB {
  4. call: string
  5. checksum_incoming: string
  6. query: string
  7. params: string
  8. meeting_id: string
  9. url: string
  10. constructor(req: Request<ParamsDictionary, any, any>) {
  11. this.call = req.params.call
  12. this.checksum_incoming = req.query.checksum
  13. this.query = req._parsedUrl?.query || ""
  14. this.params = this.query.replace(/[?&]?checksum.*$/, '')
  15. this.meeting_id = req.query.meetingID
  16. this.url = req.originalUrl
  17. }
  18. // generate a checksum for various calls
  19. generate_checksum = (secret: string, call: string = this.call, params: string = this.params) => {
  20. const hash = createHash("sha1");
  21. hash.update(`${call}${params}${secret}`)
  22. return hash.toString()
  23. }
  24. // generate a url to check if meeting is available
  25. check_for_meeting_query = (server: server) => {
  26. const checksum = this.generate_checksum(server.secret, 'getMeetingInfo', `meetingID=${this.meeting_id}`)
  27. return `${server.host}/bigbluebutton/api/getMeetingInfo?meetingID=${this.meeting_id}&checksum=${checksum}`
  28. }
  29. // write new query for target bbb server
  30. rewritten_query = (server: server) => {
  31. const checksum_outgoing = this.generate_checksum(server.secret)
  32. return `${server.host}${this.url.replace(this.checksum_incoming, checksum_outgoing)}`
  33. }
  34. // check if request is autheticated with correct checksum
  35. authenticated = (secret: string) => {
  36. const checksum = this.generate_checksum(secret)
  37. const ok = checksum === this.checksum_incoming
  38. return ok
  39. }
  40. find_meeting_id = (servers: server[]): Promise<server> => {
  41. if (!this.meeting_id) throw Error
  42. const promises = servers.map(async s => {
  43. const res = await fetch(this.check_for_meeting_query(s))
  44. if (!res.ok) throw Error
  45. const text = await res.text()
  46. if (text.includes(this.meeting_id)) return s
  47. else throw Error
  48. })
  49. return Promise.any(promises)
  50. }
  51. }