From 185099f1831249278344130cf2c2145ccb83c55a Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Sun, 9 Sep 2018 10:57:14 -0700 Subject: [PATCH] Check for alternative reverse proxy headers --- plexpy/webstart.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/plexpy/webstart.py b/plexpy/webstart.py index 293dc9a8..72ddc4a5 100644 --- a/plexpy/webstart.py +++ b/plexpy/webstart.py @@ -67,6 +67,10 @@ def initialize(options): else: protocol = "http" + if options['http_proxy']: + # Overwrite cherrypy.tools.proxy with our own proxy handler + cherrypy.tools.proxy = cherrypy.Tool('before_handler', proxy) + if options['http_password']: login_allowed = ["Tautulli admin (username is '%s')" % options['http_username']] if plexpy.CONFIG.HTTP_PLEX_ADMIN: @@ -94,7 +98,7 @@ def initialize(options): conf = { '/': { 'tools.staticdir.root': os.path.join(plexpy.PROG_DIR, 'data'), - 'tools.proxy.on': options['http_proxy'], # pay attention to X-Forwarded-Proto header + 'tools.proxy.on': bool(options['http_proxy']), 'tools.gzip.on': True, 'tools.gzip.mime_types': ['text/html', 'text/plain', 'text/css', 'text/javascript', 'application/json', @@ -226,3 +230,26 @@ class BaseRedirect(object): @cherrypy.expose def index(self): raise cherrypy.HTTPRedirect(plexpy.HTTP_ROOT) + + +def proxy(): + # logger.debug(u"REQUEST URI: %s, HEADER [X-Forwarded-Host]: %s, [X-Host]: %s, [Origin]: %s, [Host]: %s", + # cherrypy.request.wsgi_environ['REQUEST_URI'], + # cherrypy.request.headers.get('X-Forwarded-Host'), + # cherrypy.request.headers.get('X-Host'), + # cherrypy.request.headers.get('Origin'), + # cherrypy.request.headers.get('Host')) + + # Change cherrpy.tools.proxy.local header if X-Forwarded-Host header is not present + local = 'X-Forwarded-Host' + if not cherrypy.request.headers.get('X-Forwarded-Host'): + if cherrypy.request.headers.get('X-Host'): # lighttpd + local = 'X-Host' + elif cherrypy.request.headers.get('Origin'): # Squid + local = 'Origin' + elif cherrypy.request.headers.get('Host'): # nginx + local = 'Host' + # logger.debug(u"cherrypy.tools.proxy.local set to [%s]", local) + + # Call original cherrypy proxy tool with the new local + cherrypy.lib.cptools.proxy(local=local)