1
0

jitsimeetbridge.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 = "<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>" % {
  169. "tojid": "%s@%s/%s" % (ROOMNAME, ROOMDOMAIN, self.shortJid),
  170. "nick": self.userId,
  171. "assrc": self.ssrcs["audio"],
  172. "vssrc": self.ssrcs["video"],
  173. }
  174. res = self.sendIq(ssrcMsg)
  175. print("reply from ssrc announce: ", res)
  176. time.sleep(10)
  177. def xmppLoop(self):
  178. self.matrixCallId = time.time()
  179. res = self.xmppPoke(
  180. "<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'/>"
  181. % (self.nextRid(), HOST)
  182. )
  183. print(res)
  184. self.sid = res.body["sid"]
  185. print("sid %s" % (self.sid))
  186. res = self.sendIq(
  187. "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>"
  188. )
  189. res = self.xmppPoke(
  190. "<body rid='%s' xmlns='http://jabber.org/protocol/httpbind' sid='%s' to='%s' xml:lang='en' xmpp:restart='true' xmlns:xmpp='urn:xmpp:xbosh'/>"
  191. % (self.nextRid(), self.sid, HOST)
  192. )
  193. res = self.sendIq(
  194. "<iq type='set' id='_bind_auth_2' xmlns='jabber:client'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>"
  195. )
  196. print(res)
  197. self.jid = res.body.iq.bind.jid.string
  198. print("jid: %s" % (self.jid))
  199. self.shortJid = self.jid.split("-")[0]
  200. res = self.sendIq(
  201. "<iq type='set' id='_session_auth_2' xmlns='jabber:client'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>"
  202. )
  203. # randomthing = res.body.iq['to']
  204. # whatsitpart = randomthing.split('-')[0]
  205. # print "other random bind thing: %s" % (randomthing)
  206. # advertise preence to the jitsi room, with our nick
  207. res = self.sendIq(
  208. "<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>"
  209. % (HOST, TURNSERVER, ROOMNAME, ROOMDOMAIN, self.userId)
  210. )
  211. self.muc = {"users": []}
  212. for p in res.body.findAll("presence"):
  213. u = {}
  214. u["shortJid"] = p["from"].split("/")[1]
  215. if p.c and p.c.nick:
  216. u["nick"] = p.c.nick.string
  217. self.muc["users"].append(u)
  218. print("muc: ", self.muc)
  219. # wait for stuff
  220. while True:
  221. print("waiting...")
  222. res = self.sendIq("")
  223. print("got from stream: ", res)
  224. if res.body.iq:
  225. jingles = res.body.iq.findAll("jingle")
  226. if len(jingles):
  227. self.callfrom = res.body.iq["from"]
  228. self.handleInvite(jingles[0])
  229. elif "type" in res.body and res.body["type"] == "terminate":
  230. self.running = False
  231. del xmppClients[self.matrixRoom]
  232. return
  233. def handleInvite(self, jingle):
  234. self.initiator = jingle["initiator"]
  235. self.callsid = jingle["sid"]
  236. p = subprocess.Popen(
  237. ["node", "unjingle/unjingle.js", "--jingle"],
  238. stdin=subprocess.PIPE,
  239. stdout=subprocess.PIPE,
  240. )
  241. print("raw jingle invite", str(jingle))
  242. sdp, out_err = p.communicate(str(jingle))
  243. print("transformed remote offer sdp", sdp)
  244. inviteEvent = {
  245. "offer": {"type": "offer", "sdp": sdp},
  246. "call_id": self.matrixCallId,
  247. "version": 0,
  248. "lifetime": 30000,
  249. }
  250. matrixCli.sendEvent(self.matrixRoom, "m.call.invite", inviteEvent)
  251. matrixCli = TrivialMatrixClient(ACCESS_TOKEN) # Undefined name
  252. gevent.joinall([gevent.spawn(matrixLoop)])