Check for alternative reverse proxy headers

This commit is contained in:
JonnyWong16 2018-09-09 10:57:14 -07:00
parent cd6289046e
commit 185099f183

View file

@ -67,6 +67,10 @@ def initialize(options):
else: else:
protocol = "http" 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']: if options['http_password']:
login_allowed = ["Tautulli admin (username is '%s')" % options['http_username']] login_allowed = ["Tautulli admin (username is '%s')" % options['http_username']]
if plexpy.CONFIG.HTTP_PLEX_ADMIN: if plexpy.CONFIG.HTTP_PLEX_ADMIN:
@ -94,7 +98,7 @@ def initialize(options):
conf = { conf = {
'/': { '/': {
'tools.staticdir.root': os.path.join(plexpy.PROG_DIR, 'data'), '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.on': True,
'tools.gzip.mime_types': ['text/html', 'text/plain', 'text/css', 'tools.gzip.mime_types': ['text/html', 'text/plain', 'text/css',
'text/javascript', 'application/json', 'text/javascript', 'application/json',
@ -226,3 +230,26 @@ class BaseRedirect(object):
@cherrypy.expose @cherrypy.expose
def index(self): def index(self):
raise cherrypy.HTTPRedirect(plexpy.HTTP_ROOT) 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)