forked from dz0ny/leapcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·467 lines (368 loc) · 14.1 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/python
# Python program that emulates ChromeCast device
from twisted.internet import reactor
from twisted.internet.protocol import DatagramProtocol
import tornado.ioloop
import tornado.web
import tornado.websocket
import socket
import threading
import string
import argparse
import signal
import logging
from textwrap import dedent
import shlex
import subprocess
import json
import copy
import uuid
global_status = dict()
friendlyName = "Mopidy"
user_agent = "Mozilla/5.0 (CrKey - 0.9.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1573.2 Safari/537.36"
chrome = "/opt/google/chrome/chrome"
fullscreen = False
class SSDP(DatagramProtocol):
SSDP_ADDR = '239.255.255.250'
SSDP_PORT = 1900
MS = """HTTP/1.1 200 OK\r
LOCATION: http://$ip:8008/ssdp/device-desc.xml\r
CACHE-CONTROL: max-age=1800\r
CONFIGID.UPNP.ORG: 7337\r
BOOTID.UPNP.ORG: 7337\r
USN: uuid:$uuid\r
ST: urn:dial-multiscreen-org:service:dial:1\r
\r
"""
def __init__(self, iface):
self.iface = iface
self.transport = reactor.listenMulticast(
self.SSDP_PORT, self, listenMultiple=True)
self.transport.setLoopbackMode(1)
self.transport.joinGroup(self.SSDP_ADDR, interface=iface)
def stop(self):
self.transport.leaveGroup(self.SSDP_ADDR, interface=self.iface)
self.transport.stopListening()
def datagramReceived(self, datagram, address):
if "urn:dial-multiscreen-org:service:dial:1" in datagram and "M-SEARCH" in datagram:
iface = self.iface
if not iface:
# Create a socket to determine what address the client should
# use
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(address)
iface = s.getsockname()[0]
s.close()
data = string.Template(dedent(self.MS)).substitute(
ip=iface, uuid=uuid.uuid5(uuid.NAMESPACE_DNS, friendlyName))
self.transport.write(data, address)
class LEAP(tornado.web.RequestHandler):
application_status = dict(
name="",
state="stopped",
link="",
pid=None,
connectionSvcURL="",
protocols="",
)
service = """<?xml version="1.0" encoding="UTF-8"?>
<service xmlns="urn:dial-multiscreen-org:schemas:dial">
<name>$name</name>
<options allowStop="true"/>
<activity-status xmlns="urn:chrome.google.com:cast">
<description>Legacy</description>
</activity-status>
<servicedata xmlns="urn:chrome.google.com:cast">
<connectionSvcURL>$connectionSvcURL</connectionSvcURL>
<protocols>$protocols</protocols>
</servicedata>
<state>$state</state>
$link
</service>
"""
ip = None
url = "$query"
protocols = ""
def get_name(self):
return self.__class__.__name__
def get_status_dict(self):
status = copy.deepcopy(self.application_status)
status["name"] = self.get_name()
return status
def prepare(self):
self.ip = self.request.host
def get_app_status(self):
return global_status.get(self.get_name(), self.get_status_dict())
def set_app_status(self, app_status):
global global_status
app_status["name"] = self.get_name()
global_status[self.get_name()] = app_status
def _response(self):
self.set_header("Content-Type", "application/xml")
self.set_header(
"Access-Control-Allow-Method", "GET, POST, DELETE, OPTIONS")
self.set_header("Access-Control-Expose-Headers", "Location")
self.set_header("Cache-control", "no-cache, must-revalidate, no-store")
self.finish(self._toXML(self.get_app_status()))
@tornado.web.asynchronous
def post(self, sec):
"""Start app"""
self.clear()
self.set_status(201)
self.set_header("Location", self._getLocation(self.get_name()))
status = self.get_status_dict()
status["state"] = "running"
status["link"] = """<link rel="run" href="web-1"/>"""
status["pid"] = self.launch(self.request.body)
status["connectionSvcURL"] = "http://%s/connection/%s" % (
self.ip, self.get_name())
status["protocols"] = self.protocols
self.set_app_status(status)
self.finish()
@tornado.web.asynchronous
def get(self, sec):
"""Status of an app"""
self.clear()
if self.get_app_status()["pid"]:
# app crashed or closed
if self.get_app_status()["pid"].poll() is not None:
status = self.get_status_dict()
status["state"] = "stopped"
status["link"] = ""
status["pid"] = None
self.set_app_status(status)
self._response()
@tornado.web.asynchronous
def delete(self, sec):
"""Close app"""
self.clear()
self.destroy(self.get_app_status()["pid"])
status = self.get_status_dict()
status["state"] = "stopped"
status["link"] = ""
status["pid"] = None
self.set_app_status(status)
self._response()
def _getLocation(self, app):
return "http://%s/apps/%s/web-1" % (self.ip, app)
def launch(self, data):
appurl = string.Template(self.url).substitute(query=data)
if not fullscreen:
appurl = '--app="%s"' % appurl
command_line = """%s --incognito --kiosk --user-agent="%s" %s""" % (
chrome, user_agent, appurl)
args = shlex.split(command_line)
return subprocess.Popen(args)
def destroy(self, pid):
if pid is not None:
pid.terminate()
def _toXML(self, data):
return string.Template(dedent(self.service)).substitute(data)
@classmethod
def toInfo(cls):
data = copy.deepcopy(cls.application_status)
data["name"] = cls.__name__
data = global_status.get(cls.__name__, data)
return string.Template(dedent(cls.service)).substitute(data)
class ChromeCast(LEAP):
url = "https://www.gstatic.com/cv/receiver.html?$query"
protocols = "<protocol>ramp</protocol>"
class YouTube(LEAP):
url = "https://www.youtube.com/tv?$query"
protocols = "<protocol>ramp</protocol>"
class PlayMovies(LEAP):
url = "https://play.google.com/video/avi/eureka?$query"
protocols = "<protocol>ramp</protocol><protocol>play-movies</protocol>"
class GoogleMusic(LEAP):
url = "https://play.google.com/music/cast/player"
protocols = "<protocol>ramp</protocol>"
class GoogleCastSampleApp(LEAP):
url = "http://anzymrcvr.appspot.com/receiver/anzymrcvr.html"
protocols = "<protocol>ramp</protocol>"
class GoogleCastPlayer(LEAP):
url = "https://www.gstatic.com/eureka/html/gcp.html"
protocols = "<protocol>ramp</protocol>"
class Fling(LEAP):
url = "https://www.gstatic.com/eureka/html/gcp.html"
protocols = "<protocol>ramp</protocol>"
class TicTacToe(LEAP):
url = "http://www.gstatic.com/eureka/sample/tictactoe/tictactoe.html"
protocols = "<protocol>com.google.chromecast.demo.tictactoe</protocol>"
class DeviceHandler(tornado.web.RequestHandler):
device = """<?xml version="1.0" encoding="utf-8"?>
<root xmlns="urn:schemas-upnp-org:device-1-0" xmlns:r="urn:restful-tv-org:schemas:upnp-dd">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<URLBase>$path</URLBase>
<device>
<deviceType>urn:schemas-upnp-org:device:dail:1</deviceType>
<friendlyName>$friendlyName</friendlyName>
<manufacturer>Google Inc.</manufacturer>
<modelName>Eureka Dongle</modelName>
<UDN>uuid:$uuid</UDN>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:dail:1</serviceType>
<serviceId>urn:upnp-org:serviceId:dail</serviceId>
<controlURL>/ssdp/notfound</controlURL>
<eventSubURL>/ssdp/notfound</eventSubURL>
<SCPDURL>/ssdp/notfound</SCPDURL>
</service>
</serviceList>
</device>
</root>"""
def get(self):
if self.request.uri == "/apps":
for app, astatus in global_status.items():
if astatus["state"] == "running":
self.redirect("/apps/%s" % app)
self.set_status(204)
self.set_header(
"Access-Control-Allow-Method", "GET, POST, DELETE, OPTIONS")
self.set_header("Access-Control-Expose-Headers", "Location")
self.finish()
else:
self.set_header(
"Access-Control-Allow-Method", "GET, POST, DELETE, OPTIONS")
self.set_header("Access-Control-Expose-Headers", "Location")
self.add_header(
"Application-URL", "http://%s/apps" % self.request.host)
self.set_header("Content-Type", "application/xml")
self.write(string.Template(dedent(self.device)).substitute(
dict(
uuid=uuid.uuid5(uuid.NAMESPACE_DNS, friendlyName),
friendlyName=friendlyName,
path="http://%s" % self.request.host)
)
)
class WS(tornado.websocket.WebSocketHandler):
def open(self, app=None):
self.app = app
logging.info("%s opened %s" %
(self.__class__.__name__, self.request.uri))
self.cmd_id = 0
def on_message(self, message):
cmd = json.loads(message)
self.on_cmd(cmd)
def on_cmd(self, cmd):
print(cmd)
def on_close(self):
logging.info("%s closed %s" %
(self.__class__.__name__, self.request.uri))
def reply(self, msg):
msg["cmd_id"] = self.cmd_id
self.write_message((json.dumps(msg)))
self.cmd_id += 1
class CastChannel(WS):
"""
RAMP over WebSocket. It acts like proxy between receiver app(1st screen) and remote app(2nd screen)
"""
def on_cmd(self, cmd):
if cmd["type"] == "REGISTER":
self.info = cmd
self.new_request()
if cmd["type"] == "CHANNELRESPONSE":
self.new_chanell()
def new_chanell(self):
ws = "ws://localhost:8008/connection/%s" % self.info["name"]
logging.info("New channel for app %s %s" % (self.info["name"], ws))
self.reply(
{"type": "NEWCHANNEL", "senderId": "1", "requestId": "123456", "URL": ws})
def new_request(self):
logging.info("New CHANNELREQUEST for app %s" % (self.info["name"]))
self.reply(
{"type": "CHANNELREQUEST", "requestId": "123456", "senderId": "1"})
class CastPlatform(WS):
"""
Remote control over WebSocket.
Commands are:
{u'type': u'GET_VOLUME', u'cmd_id': 1}
{u'type': u'GET_MUTED', u'cmd_id': 2}
{u'type': u'VOLUME_CHANGED', u'cmd_id': 3}
{u'type': u'SET_VOLUME', u'cmd_id': 4}
{u'type': u'SET_MUTED', u'cmd_id': 5}
Device control:
"""
class CastRAMP(WS):
"""
Remote proxy over WebSocket.
"""
def reply(self, msg):
self.write_message((json.dumps(msg)))
def on_cmd(self, cmd):
if cmd[0] == "cm":
self.on_cm_command(cmd[1])
if cmd[0] == "ramp":
self.on_ramp_command(cmd[1])
def on_cm_command(self, cmd):
print cmd
self.reply(['cm', {'type': 'pong'}])
def on_ramp_command(self, cmd):
print cmd
class HTTPThread(object):
def __init__(self, iface):
self.iface = iface
def register_app(self, app):
name = app.__name__
return (r"(/apps/" + name + "|/apps/" + name + "/run)", app)
def run(self):
self.application = tornado.web.Application([
(r"/ssdp/device-desc.xml", DeviceHandler),
(r"/apps", DeviceHandler),
self.register_app(ChromeCast),
self.register_app(YouTube),
self.register_app(PlayMovies),
self.register_app(GoogleMusic),
self.register_app(GoogleCastSampleApp),
self.register_app(GoogleCastPlayer),
self.register_app(TicTacToe),
self.register_app(Fling),
(r"/connection", CastChannel),
(r"/connection/([^\/]+)", CastRAMP),
(r"/system/control", CastPlatform),
])
self.application.listen(8008, address=self.iface)
tornado.ioloop.IOLoop.instance().start()
def start(self):
threading.Thread(target=self.run).start()
def shutdown(self, ):
logging.info('Stopping HTTP server')
reactor.callFromThread(reactor.stop)
logging.info('Stopping DIAL server')
tornado.ioloop.IOLoop.instance().stop()
def sig_handler(self, sig, frame):
tornado.ioloop.IOLoop.instance().add_callback(self.shutdown)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument(
'--iface', help='Interface you want to bind to (for example 192.168.1.22)', default='')
parser.add_argument('--name', help='Friendly name for this device')
parser.add_argument('--user_agent', help='Custom user agent')
parser.add_argument('--chrome', help='Path to Google Chrome executable')
parser.add_argument('--fullscreen', action='store_true',
default=False, help='Start in full-screen mode')
args = parser.parse_args()
if args.name:
friendlyName = args.name
logging.info("Service name is %s" % friendlyName)
if args.user_agent:
user_agent = args.user_agent
logging.info("User agent is %s" % user_agent)
if args.chrome:
chrome = args.chrome
logging.info("Chrome path is %s" % chrome)
if args.fullscreen:
fullscreen = True
server = HTTPThread(args.iface)
server.start()
signal.signal(signal.SIGTERM, server.sig_handler)
signal.signal(signal.SIGINT, server.sig_handler)
def LeapUPNPServer():
logging.info("Listening on %s" % (args.iface or 'all'))
sobj = SSDP(args.iface)
reactor.addSystemEventTrigger('before', 'shutdown', sobj.stop)
reactor.callWhenRunning(LeapUPNPServer)
reactor.run()