mirror of
https://github.com/janeczku/calibre-web
synced 2026-10-14 03:51:40 +00:00
106 lines
4.4 KiB
Python
106 lines
4.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
# Flask License
|
|
#
|
|
# Copyright © 2010 by the Pallets team, cervinko, janeczku, OzzieIsaacs
|
|
#
|
|
# Some rights reserved.
|
|
#
|
|
# Redistribution and use in source and binary forms of the software as
|
|
# well as documentation, with or without modification, are permitted
|
|
# provided that the following conditions are met:
|
|
#
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
# this list of conditions and the following disclaimer.
|
|
#
|
|
# * Redistributions in binary form must reproduce the above copyright
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
# documentation and/or other materials provided with the distribution.
|
|
#
|
|
# * Neither the name of the copyright holder nor the names of its
|
|
# contributors may be used to endorse or promote products derived from
|
|
# this software without specific prior written permission.
|
|
#
|
|
# THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
|
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
# THIS SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
# SUCH DAMAGE.
|
|
#
|
|
# Inspired by http://flask.pocoo.org/snippets/35/
|
|
|
|
from . import logger
|
|
from .reverse_proxy_auth import is_trusted_proxy_source, to_wsgi_header_key
|
|
|
|
log = logger.create()
|
|
|
|
|
|
class ReverseProxied(object):
|
|
"""Wrap the application in this middleware and configure the
|
|
front-end server to add these headers, to let you quietly bind
|
|
this to a URL other than / and to an HTTP scheme that is
|
|
different from what is used locally.
|
|
|
|
Code courtesy of: https://flask.pocoo.org/snippets/35/
|
|
|
|
In nginx:
|
|
location /myprefix {
|
|
proxy_pass http://127.0.0.1:8083;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Scheme $scheme;
|
|
proxy_set_header X-Script-Name /myprefix;
|
|
}
|
|
"""
|
|
|
|
def __init__(self, application):
|
|
self.app = application
|
|
self.proxied = False
|
|
|
|
def __call__(self, environ, start_response):
|
|
from . import config
|
|
|
|
rp_header_name = config.config_reverse_proxy_login_header_name or ""
|
|
rp_secret_header_name = config.config_reverse_proxy_login_secret_header_name or ""
|
|
header_names = [header for header in (rp_header_name, rp_secret_header_name) if header]
|
|
header_keys = [(header, to_wsgi_header_key(header)) for header in header_names]
|
|
trusted_proxy_config = config.config_reverse_proxy_trusted_ips or ""
|
|
if config.config_allow_reverse_proxy_header_login and any(key in environ for _, key in header_keys):
|
|
if not is_trusted_proxy_source(environ.get("REMOTE_ADDR"), trusted_proxy_config):
|
|
for header_name, header_key in header_keys:
|
|
if header_key in environ:
|
|
environ.pop(header_key, None)
|
|
log.warning("Discarded reverse proxy auth header '%s' from untrusted source IP-address: %s",
|
|
header_name, environ.get("REMOTE_ADDR"))
|
|
|
|
self.proxied = False
|
|
self.script_name = "/"
|
|
script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
|
|
if script_name:
|
|
self.proxied = True
|
|
environ['SCRIPT_NAME'] = script_name
|
|
path_info = environ.get('PATH_INFO', '')
|
|
self.script_name = script_name
|
|
if path_info and path_info.startswith(script_name):
|
|
environ['PATH_INFO'] = path_info[len(script_name):]
|
|
|
|
scheme = environ.get('HTTP_X_SCHEME', '')
|
|
if scheme:
|
|
environ['wsgi.url_scheme'] = scheme
|
|
servr = environ.get('HTTP_X_FORWARDED_HOST', '')
|
|
if servr:
|
|
environ['HTTP_HOST'] = servr
|
|
self.proxied = True
|
|
return self.app(environ, start_response)
|
|
|
|
@property
|
|
def is_proxied(self):
|
|
return self.proxied
|