jitsimeetbridge.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #!/usr/bin/env python
  2. """
  3. This is an attempt at bridging matrix clients into a Jitis meet room via Matrix
  4. video call. It uses hard-coded xml strings overg XMPP BOSH. It can display one
  5. of the streams from the Jitsi bridge until the second lot of SDP comes down and
  6. we set the remote SDP at which point the stream ends. Our video never gets to
  7. the bridge.
  8. Requires:
  9. npm install jquery jsdom
  10. """
  11. import json
  12. import subprocess
  13. import time
  14. import gevent
  15. import grequests
  16. from BeautifulSoup import BeautifulSoup
  17. ACCESS_TOKEN = ""
  18. MATRIXBASE = "https://matrix.org/_matrix/client/api/v1/"
  19. MYUSERNAME = "@davetest:matrix.org"
  20. HTTPBIND = "https://meet.jit.si/http-bind"
  21. # HTTPBIND = 'https://jitsi.vuc.me/http-bind'
  22. # ROOMNAME = "matrix"
  23. ROOMNAME = "pibble"
  24. HOST = "guest.jit.si"
  25. # HOST="jitsi.vuc.me"
  26. TURNSERVER = "turn.guest.jit.si"
  27. # TURNSERVER="turn.jitsi.vuc.me"
  28. ROOMDOMAIN = "meet.jit.si"
  29. # ROOMDOMAIN="conference.jitsi.vuc.me"
  30. class TrivialMatrixClient:
  31. def __init__(self, access_token):
  32. self.token = None
  33. self.access_token = access_token
  34. def getEvent(self):
  35. while True:
  36. url = (
  37. MATRIXBASE
  38. + "events?access_token="
  39. + self.access_token
  40. + "&timeout=60000"
  41. )
  42. if self.token:
  43. url += "&from=" + self.token
  44. req = grequests.get(url)
  45. resps = grequests.map([req])
  46. obj = json.loads(resps[0].content)
  47. print("incoming from matrix", obj)
  48. if "end" not in obj:
  49. continue
  50. self.token = obj["end"]
  51. if len(obj["chunk"]):
  52. return obj["chunk"][0]
  53. def joinRoom(self, roomId):
  54. url = MATRIXBASE + "rooms/" + roomId + "/join?access_token=" + self.access_token
  55. print(url)
  56. headers = {"Content-Type": "application/json"}
  57. req = grequests.post(url, headers=headers, data="{}")
  58. resps = grequests.map([req])
  59. obj = json.loads(resps[0].content)
  60. print("response: ", obj)
  61. def sendEvent(self, roomId, evType, event):
  62. url = (
  63. MATRIXBASE
  64. + "rooms/"
  65. + roomId
  66. + "/send/"
  67. + evType
  68. + "?access_token="
  69. + self.access_token
  70. )
  71. print(url)
  72. print(json.dumps(event))
  73. headers = {"Content-Type": "application/json"}
  74. req = grequests.post(url, headers=headers, data=json.dumps(event))
  75. resps = grequests.map([req])
  76. obj = json.loads(resps[0].content)
  77. print("response: ", obj)
  78. xmppClients = {}
  79. def matrixLoop():
  80. while True:
  81. ev = matrixCli.getEvent()
  82. print(ev)
  83. if ev["type"] == "m.room.member":
  84. print("membership event")
  85. if ev["membership"] == "invite" and ev["state_key"] == MYUSERNAME:
  86. roomId = ev["room_id"]
  87. print("joining room %s" % (roomId))
  88. matrixCli.joinRoom(roomId)
  89. elif ev["type"] == "m.room.message":
  90. if ev["room_id"] in xmppClients:
  91. print("already have a bridge for that user, ignoring")
  92. continue
  93. print("got message, connecting")
  94. xmppClients[ev["room_id"]] = TrivialXmppClient(ev["room_id"], ev["user_id"])
  95. gevent.spawn(xmppClients[ev["room_id"]].xmppLoop)
  96. elif ev["type"] == "m.call.invite":
  97. print("Incoming call")
  98. # sdp = ev['content']['offer']['sdp']
  99. # print "sdp: %s" % (sdp)
  100. # xmppClients[ev['room_id']] = TrivialXmppClient(ev['room_id'], ev['user_id'])
  101. # gevent.spawn(xmppClients[ev['room_id']].xmppLoop)
  102. elif ev["type"] == "m.call.answer":
  103. print("Call answered")
  104. sdp = ev["content"]["answer"]["sdp"]
  105. if ev["room_id"] not in xmppClients:
  106. print("We didn't have a call for that room")
  107. continue
  108. # should probably check call ID too
  109. xmppCli = xmppClients[ev["room_id"]]
  110. xmppCli.sendAnswer(sdp)
  111. elif ev["type"] == "m.call.hangup":
  112. if ev["room_id"] in xmppClients:
  113. xmppClients[ev["room_id"]].stop()
  114. del xmppClients[ev["room_id"]]
  115. class TrivialXmppClient:
  116. def __init__(self, matrixRoom, userId):
  117. self.rid = 0
  118. self.matrixRoom = matrixRoom
  119. self.userId = userId
  120. self.running = True
  121. def stop(self):
  122. self.running = False
  123. def nextRid(self):
  124. self.rid += 1
  125. return "%d" % (self.rid)
  126. def sendIq(self, xml):
  127. fullXml = (
  128. "<body rid='%s' xmlns='http://jabber.org/protocol/httpbind' sid='%s'>%s</body>"
  129. % (self.nextRid(), self.sid, xml)
  130. )
  131. # print "\t>>>%s" % (fullXml)
  132. return self.xmppPoke(fullXml)
  133. def xmppPoke(self, xml):
  134. headers = {"Content-Type": "application/xml"}
  135. req = grequests.post(HTTPBIND, verify=False, headers=headers, data=xml)
  136. resps = grequests.map([req])
  137. obj = BeautifulSoup(resps[0].content)
  138. return obj
  139. def sendAnswer(self, answer):
  140. print("sdp from matrix client", answer)
  141. p = subprocess.Popen(
  142. ["node", "unjingle/unjingle.js", "--sdp"],
  143. stdin=subprocess.PIPE,
  144. stdout=subprocess.PIPE,
  145. )
  146. jingle, out_err = p.communicate(answer)
  147. jingle = jingle % {
  148. "tojid": self.callfrom,
  149. "action": "session-accept",
  150. "initiator": self.callfrom,
  151. "responder": self.jid,
  152. "sid": self.callsid,
  153. }
  154. print("answer jingle from sdp", jingle)
  155. res = self.sendIq(jingle)
  156. print("reply from answer: ", res)
  157. self.ssrcs = {}
  158. jingleSoup = BeautifulSoup(jingle)
  159. for cont in jingleSoup.iq.jingle.findAll("content"):
  160. if cont.description:
  161. self.ssrcs[cont["name"]] = cont.description["ssrc"]
  162. print("my ssrcs:", self.ssrcs)
  163. gevent.joinall([gevent.spawn(self.advertiseSsrcs)])
  164. def advertiseSsrcs(self):
  165. time.sleep(7)
  166. print("SSRC spammer started")
  167. while self.running:
  168. ssrcMsg = (
  169. "<presence to='%(tojid)s' xmlns='jabber:client'><x xmlns='http://jabber.org/protocol/muc'/><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://jitsi.org/jitsimeet' ver='0WkSdhFnAUxrz4ImQQLdB80GFlE='/><nick xmlns='http://jabber.org/protocol/nick'>%(nick)s</nick><stats xmlns='http://jitsi.org/jitmeet/stats'><stat name='bitrate_download' value='175'/><stat name='bitrate_upload' value='176'/><stat name='packetLoss_total' value='0'/><stat name='packetLoss_download' value='0'/><stat name='packetLoss_upload' value='0'/></stats><media xmlns='http://estos.de/ns/mjs'><source type='audio' ssrc='%(assrc)s' direction='sendre'/><source type='video' ssrc='%(vssrc)s' direction='sendre'/></media></presence>"
  170. % {
  171. "tojid": "%s@%s/%s" % (ROOMNAME, ROOMDOMAIN, self.shortJid),
  172. "nick": self.userId,
  173. "assrc": self.ssrcs["audio"],
  174. "vssrc": self.ssrcs["video"],
  175. }
  176. )
  177. res = self.sendIq(ssrcMsg)
  178. print("reply from ssrc announce: ", res)
  179. time.sleep(10)
  180. def xmppLoop(self):
  181. self.matrixCallId = time.time()
  182. res = self.xmppPoke(
  183. "<body rid='%s' xmlns='http://jabber.org/protocol/httpbind' to='%s' xml:lang='en' wait='60' hold='1' content='text/xml; charset=utf-8' ver='1.6' xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'/>"
  184. % (self.nextRid(), HOST)
  185. )
  186. print(res)
  187. self.sid = res.body["sid"]
  188. print("sid %s" % (self.sid))
  189. res = self.sendIq(
  190. "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>"
  191. )
  192. res = self.xmppPoke(
  193. "<body rid='%s' xmlns='http://jabber.org/protocol/httpbind' sid='%s' to='%s' xml:lang='en' xmpp:restart='true' xmlns:xmpp='urn:xmpp:xbosh'/>"
  194. % (self.nextRid(), self.sid, HOST)
  195. )
  196. res = self.sendIq(
  197. "<iq type='set' id='_bind_auth_2' xmlns='jabber:client'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>"
  198. )
  199. print(res)
  200. self.jid = res.body.iq.bind.jid.string
  201. print("jid: %s" % (self.jid))
  202. self.shortJid = self.jid.split("-")[0]
  203. res = self.sendIq(
  204. "<iq type='set' id='_session_auth_2' xmlns='jabber:client'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>"
  205. )
  206. # randomthing = res.body.iq['to']
  207. # whatsitpart = randomthing.split('-')[0]
  208. # print "other random bind thing: %s" % (randomthing)
  209. # advertise preence to the jitsi room, with our nick
  210. res = self.sendIq(
  211. "<iq type='get' to='%s' xmlns='jabber:client' id='1:sendIQ'><services xmlns='urn:xmpp:extdisco:1'><service host='%s'/></services></iq><presence to='%s@%s/d98f6c40' xmlns='jabber:client'><x xmlns='http://jabber.org/protocol/muc'/><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://jitsi.org/jitsimeet' ver='0WkSdhFnAUxrz4ImQQLdB80GFlE='/><nick xmlns='http://jabber.org/protocol/nick'>%s</nick></presence>"
  212. % (HOST, TURNSERVER, ROOMNAME, ROOMDOMAIN, self.userId)
  213. )
  214. self.muc = {"users": []}
  215. for p in res.body.findAll("presence"):
  216. u = {}
  217. u["shortJid"] = p["from"].split("/")[1]
  218. if p.c and p.c.nick:
  219. u["nick"] = p.c.nick.string
  220. self.muc["users"].append(u)
  221. print("muc: ", self.muc)
  222. # wait for stuff
  223. while True:
  224. print("waiting...")
  225. res = self.sendIq("")
  226. print("got from stream: ", res)
  227. if res.body.iq:
  228. jingles = res.body.iq.findAll("jingle")
  229. if len(jingles):
  230. self.callfrom = res.body.iq["from"]
  231. self.handleInvite(jingles[0])
  232. elif "type" in res.body and res.body["type"] == "terminate":
  233. self.running = False
  234. del xmppClients[self.matrixRoom]
  235. return
  236. def handleInvite(self, jingle):
  237. self.initiator = jingle["initiator"]
  238. self.callsid = jingle["sid"]
  239. p = subprocess.Popen(
  240. ["node", "unjingle/unjingle.js", "--jingle"],
  241. stdin=subprocess.PIPE,
  242. stdout=subprocess.PIPE,
  243. )
  244. print("raw jingle invite", str(jingle))
  245. sdp, out_err = p.communicate(str(jingle))
  246. print("transformed remote offer sdp", sdp)
  247. inviteEvent = {
  248. "offer": {"type": "offer", "sdp": sdp},
  249. "call_id": self.matrixCallId,
  250. "version": 0,
  251. "lifetime": 30000,
  252. }
  253. matrixCli.sendEvent(self.matrixRoom, "m.call.invite", inviteEvent)
  254. matrixCli = TrivialMatrixClient(ACCESS_TOKEN) # Undefined name
  255. gevent.joinall([gevent.spawn(matrixLoop)])