jitsimeetbridge.py 11 KB

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