forked from iBaa/PlexConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebServer.py
executable file
·403 lines (324 loc) · 15.2 KB
/
WebServer.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
#!/usr/bin/env python
"""
Sources:
http://fragments.turtlemeat.com/pythonwebserver.php
http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python
...stackoverflow.com and such
after 27Aug - Apple's switch to https:
- added https WebServer with SSL encryption - needs valid (private) vertificate on aTV and server
- for additional information see http://langui.sh/2013/08/27/appletv-ssl-plexconnect/
Thanks to reaperhulk for showing this solution!
"""
import sys
import string, cgi, time
from os import sep, path
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn
import ssl
from multiprocessing import Pipe # inter process communication
import urllib, StringIO, gzip
import signal
import traceback
import datetime
import Settings, ATVSettings
from Debug import * # dprint()
import XMLConverter # XML_PMS2aTV, XML_PlayVideo
import re
import Localize
import Subtitle
g_param = {}
def setParams(param):
global g_param
g_param = param
def JSConverter(file, options):
f = open(sys.path[0] + "/assets/js/" + file)
JS = f.read()
f.close()
# PlexConnect {{URL()}}->baseURL
for path in set(re.findall(r'\{\{URL\((.*?)\)\}\}', JS)):
JS = JS.replace('{{URL(%s)}}' % path, g_param['baseURL']+path)
# localization
JS = Localize.replaceTEXT(JS, options['aTVLanguage']).encode('utf-8')
return JS
class MyHandler(BaseHTTPRequestHandler):
# Fixes slow serving speed under Windows
def address_string(self):
host, port = self.client_address[:2]
#return socket.getfqdn(host)
return host
def log_message(self, format, *args):
pass
def compress(self, data):
buf = StringIO.StringIO()
zfile = gzip.GzipFile(mode='wb', fileobj=buf, compresslevel=9)
zfile.write(data)
zfile.close()
return buf.getvalue()
def sendResponse(self, data, type, enableGzip):
self.send_response(200)
self.send_header('Server', 'PlexConnect')
self.send_header('Content-type', type)
try:
accept_encoding = map(string.strip, string.split(self.headers["accept-encoding"], ","))
except KeyError:
accept_encoding = []
if enableGzip and \
g_param['CSettings'].getSetting('allow_gzip_atv')=='True' and \
'gzip' in accept_encoding:
self.send_header('Content-encoding', 'gzip')
self.end_headers()
self.wfile.write(self.compress(data))
else:
self.end_headers()
self.wfile.write(data)
def do_GET(self):
global g_param
try:
dprint(__name__, 2, "http request header:\n{0}", self.headers)
dprint(__name__, 2, "http request path:\n{0}", self.path)
# check for PMS address
PMSaddress = ''
pms_end = self.path.find(')')
if self.path.startswith('/PMS(') and pms_end>-1:
PMSaddress = urllib.unquote_plus(self.path[5:pms_end])
self.path = self.path[pms_end+1:]
# break up path, separate PlexConnect options
# clean path needed for filetype decoding
parts = re.split(r'[?&]', self.path, 1) # should be '?' only, but we do some things different :-)
if len(parts)==1:
self.path = parts[0]
options = {}
query = ''
else:
self.path = parts[0]
# break up query string
options = {}
query = ''
parts = parts[1].split('&')
for part in parts:
if part.startswith('PlexConnect'):
# get options[]
opt = part.split('=', 1)
if len(opt)==1:
options[opt[0]] = ''
else:
options[opt[0]] = urllib.unquote(opt[1])
else:
# recreate query string (non-PlexConnect) - has to be merged back when forwarded
if query=='':
query = '?' + part
else:
query += '&' + part
# get aTV language setting
options['aTVLanguage'] = Localize.pickLanguage(self.headers.get('Accept-Language', 'en'))
query = query.replace("yyltyy", "<").replace("yygtyy", ">")
# add client address - to be used in case UDID is unknown
if 'X-Forwarded-For' in self.headers:
options['aTVAddress'] = self.headers['X-Forwarded-For'].split(',', 1)[0]
else:
options['aTVAddress'] = self.client_address[0]
# get aTV hard-/software parameters
options['aTVFirmwareVersion'] = self.headers.get('X-Apple-TV-Version', '5.1')
options['aTVScreenResolution'] = self.headers.get('X-Apple-TV-Resolution', '720')
dprint(__name__, 2, "pms address:\n{0}", PMSaddress)
dprint(__name__, 2, "cleaned path:\n{0}", self.path)
dprint(__name__, 2, "PlexConnect options:\n{0}", options)
dprint(__name__, 2, "additional arguments:\n{0}", query)
if 'User-Agent' in self.headers and \
'AppleTV' in self.headers['User-Agent']:
# recieve simple logging messages from the ATV
if 'PlexConnectATVLogLevel' in options:
dprint('ATVLogger', int(options['PlexConnectATVLogLevel']), options['PlexConnectLog'])
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
return
# serve "*.cer" - Serve up certificate file to atv
if self.path.endswith(".cer"):
dprint(__name__, 1, "serving *.cer: "+self.path)
if g_param['CSettings'].getSetting('certfile').startswith('.'):
# relative to current path
cfg_certfile = sys.path[0] + sep + g_param['CSettings'].getSetting('certfile')
else:
# absolute path
cfg_certfile = g_param['CSettings'].getSetting('certfile')
cfg_certfile = path.normpath(cfg_certfile)
cfg_certfile = path.splitext(cfg_certfile)[0] + '.cer'
try:
f = open(cfg_certfile, "rb")
except:
dprint(__name__, 0, "Failed to access certificate: {0}", cfg_certfile)
return
self.sendResponse(f.read(), 'text/xml', False)
f.close()
return
# serve .js files to aTV
# application, main: ignore path, send /assets/js/application.js
# otherwise: path should be '/js', send /assets/js/*.js
dirname = path.dirname(self.path)
basename = path.basename(self.path)
if basename in ("application.js", "main.js", "javascript-packed.js", "bootstrap.js") or \
basename.endswith(".js") and dirname == '/js':
if basename in ("main.js", "javascript-packed.js", "bootstrap.js"):
basename = "application.js"
dprint(__name__, 1, "serving /js/{0}", basename)
JS = JSConverter(basename, options)
self.sendResponse(JS, 'text/javascript', True)
return
# proxy phobos.apple.com to support PlexConnect main icon
if "a1.phobos.apple.com" in self.headers['Host']:
resource = self.headers['Host']+self.path
icon = g_param['CSettings'].getSetting('icon')
if basename.startswith(icon):
icon_res = basename[len(icon):] # cut string from settings, keeps @720.png/@1080.png
resource = sys.path[0] + '/assets/icons/icon'+icon_res
dprint(__name__, 1, "serving "+self.headers['Host']+self.path+" with "+resource)
r = open(resource, "rb")
else:
r = urllib.urlopen('http://'+resource)
self.sendResponse(r.read(), 'image/png', False)
r.close()
return
# serve "*.jpg" - thumbnails for old-style mainpage
if self.path.endswith(".jpg"):
dprint(__name__, 1, "serving *.jpg: "+self.path)
f = open(sys.path[0] + sep + "assets" + self.path, "rb")
self.sendResponse(f.read(), 'image/jpeg', False)
f.close()
return
# serve "*.png" - only png's support transparent colors
if self.path.endswith(".png"):
dprint(__name__, 1, "serving *.png: "+self.path)
f = open(sys.path[0] + sep + "assets" + self.path, "rb")
self.sendResponse(f.read(), 'image/png', False)
f.close()
return
# serve subtitle file - transcoded to aTV subtitle json
if 'PlexConnect' in options and \
options['PlexConnect']=='Subtitle':
dprint(__name__, 1, "serving subtitle: "+self.path)
XML = Subtitle.getSubtitleJSON(PMSaddress, self.path + query, options)
self.sendResponse(XML, 'application/json', True)
return
# get everything else from XMLConverter - formerly limited to trailing "/" and &PlexConnect Cmds
if True:
dprint(__name__, 1, "serving .xml: "+self.path)
XML = XMLConverter.XML_PMS2aTV(PMSaddress, self.path + query, options)
self.sendResponse(XML, 'text/xml', True)
return
"""
# unexpected request
self.send_error(403,"Access denied: %s" % self.path)
"""
else:
"""
Added Up Page for docker helthcheck
self.send_error(403,"Not Serving Client %s" % self.client_address[0])
"""
dprint(__name__, 1, "serving *.html: "+self.path)
f = open(sys.path[0] + sep + "assets/templates/up.html")
self.sendResponse(f.read(), 'text/html', False)
f.close()
except IOError:
dprint(__name__, 0, 'File Not Found:\n{0}', traceback.format_exc())
self.send_error(404,"File Not Found: %s" % self.path)
except:
dprint(__name__, 0, 'Internal Server Error:\n{0}', traceback.format_exc())
self.send_error(500,"Internal Server Error: %s" % self.path)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
def Run(cmdPipe, param):
if not __name__ == '__main__':
signal.signal(signal.SIGINT, signal.SIG_IGN)
dinit(__name__, param) # init logging, WebServer process
cfg_IP_WebServer = param['IP_self']
cfg_Port_WebServer = param['CSettings'].getSetting('port_webserver')
try:
server = ThreadedHTTPServer((cfg_IP_WebServer,int(cfg_Port_WebServer)), MyHandler)
server.timeout = 1
except Exception, e:
dprint(__name__, 0, "Failed to connect to HTTP on {0} port {1}: {2}", cfg_IP_WebServer, cfg_Port_WebServer, e)
sys.exit(1)
socketinfo = server.socket.getsockname()
dprint(__name__, 0, "***")
dprint(__name__, 0, "WebServer: Serving HTTP on {0} port {1}.", socketinfo[0], socketinfo[1])
dprint(__name__, 0, "***")
setParams(param)
XMLConverter.setParams(param)
XMLConverter.setATVSettings(param['CATVSettings'])
try:
while True:
# check command
if cmdPipe.poll():
cmd = cmdPipe.recv()
if cmd=='shutdown':
break
# do your work (with timeout)
server.handle_request()
except KeyboardInterrupt:
signal.signal(signal.SIGINT, signal.SIG_IGN) # we heard you!
dprint(__name__, 0,"^C received.")
finally:
dprint(__name__, 0, "Shutting down (HTTP).")
server.socket.close()
def Run_SSL(cmdPipe, param):
if not __name__ == '__main__':
signal.signal(signal.SIGINT, signal.SIG_IGN)
dinit(__name__, param) # init logging, WebServer process
cfg_IP_WebServer = param['IP_self']
cfg_Port_SSL = param['CSettings'].getSetting('port_ssl')
if param['CSettings'].getSetting('certfile').startswith('.'):
# relative to current path
cfg_certfile = sys.path[0] + sep + param['CSettings'].getSetting('certfile')
else:
# absolute path
cfg_certfile = param['CSettings'].getSetting('certfile')
cfg_certfile = path.normpath(cfg_certfile)
try:
certfile = open(cfg_certfile, 'r')
except:
dprint(__name__, 0, "Failed to access certificate: {0}", cfg_certfile)
sys.exit(1)
certfile.close()
try:
server = ThreadedHTTPServer((cfg_IP_WebServer,int(cfg_Port_SSL)), MyHandler)
server.socket = ssl.wrap_socket(server.socket, certfile=cfg_certfile, server_side=True)
server.timeout = 1
except Exception, e:
dprint(__name__, 0, "Failed to connect to HTTPS on {0} port {1}: {2}", cfg_IP_WebServer, cfg_Port_SSL, e)
sys.exit(1)
socketinfo = server.socket.getsockname()
dprint(__name__, 0, "***")
dprint(__name__, 0, "WebServer: Serving HTTPS on {0} port {1}.", socketinfo[0], socketinfo[1])
dprint(__name__, 0, "***")
setParams(param)
XMLConverter.setParams(param)
XMLConverter.setATVSettings(param['CATVSettings'])
try:
while True:
# check command
if cmdPipe.poll():
cmd = cmdPipe.recv()
if cmd=='shutdown':
break
# do your work (with timeout)
server.handle_request()
except KeyboardInterrupt:
signal.signal(signal.SIGINT, signal.SIG_IGN) # we heard you!
dprint(__name__, 0,"^C received.")
finally:
dprint(__name__, 0, "Shutting down (HTTPS).")
server.socket.close()
if __name__=="__main__":
cmdPipe = Pipe()
cfg = Settings.CSettings()
param = {}
param['CSettings'] = cfg
param['CATVSettings'] = ATVSettings.CATVSettings()
param['IP_self'] = '192.168.178.20' # IP_self?
param['baseURL'] = 'http://'+ param['IP_self'] +':'+ cfg.getSetting('port_webserver')
param['HostToIntercept'] = cfg.getSetting('hosttointercept')
if len(sys.argv)==1:
Run(cmdPipe[1], param)
elif len(sys.argv)==2 and sys.argv[1]=='SSL':
Run_SSL(cmdPipe[1], param)