fix wrong ip address behind proxy
This commit is contained in:
parent
9c309dafc2
commit
6f3df5fc51
@ -60,6 +60,13 @@ logger = logging.getLogger('waitress')
|
|||||||
logger.setLevel(app.config['LOGGING_LEVEL'])
|
logger.setLevel(app.config['LOGGING_LEVEL'])
|
||||||
|
|
||||||
|
|
||||||
|
def get_actual_addr(request):
|
||||||
|
ip = request.remote_addr
|
||||||
|
if request.headers.getlist("X-Forwarded-For"):
|
||||||
|
ip = request.headers.getlist("X-Forwarded-For")[0]
|
||||||
|
return ip
|
||||||
|
|
||||||
|
|
||||||
@app.route('/favicon.ico')
|
@app.route('/favicon.ico')
|
||||||
def favicon():
|
def favicon():
|
||||||
return send_from_directory(os.path.join(app.root_path, 'static'),
|
return send_from_directory(os.path.join(app.root_path, 'static'),
|
||||||
@ -69,14 +76,14 @@ def favicon():
|
|||||||
@app.before_request
|
@app.before_request
|
||||||
def before_request():
|
def before_request():
|
||||||
timestamp = time.strftime('[%Y-%b-%d %H:%M]')
|
timestamp = time.strftime('[%Y-%b-%d %H:%M]')
|
||||||
logger.info('%s > %s %s %s %s', timestamp, request.remote_addr,
|
logger.info('%s > %s %s %s %s', timestamp, get_actual_addr(request),
|
||||||
request.method, request.scheme, request.full_path)
|
request.method, request.scheme, request.full_path)
|
||||||
|
|
||||||
|
|
||||||
@app.after_request
|
@app.after_request
|
||||||
def after_request(response):
|
def after_request(response):
|
||||||
timestamp = time.strftime('[%Y-%b-%d %H:%M]')
|
timestamp = time.strftime('[%Y-%b-%d %H:%M]')
|
||||||
logger.info('%s < %s %s %s %s %s', timestamp, request.remote_addr,
|
logger.info('%s < %s %s %s %s %s', timestamp, get_actual_addr(request),
|
||||||
request.method, request.scheme, request.full_path, response.status)
|
request.method, request.scheme, request.full_path, response.status)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -86,5 +93,5 @@ def exceptions(e):
|
|||||||
tb = traceback.format_exc()
|
tb = traceback.format_exc()
|
||||||
timestamp = time.strftime('[%Y-%b-%d %H:%M]')
|
timestamp = time.strftime('[%Y-%b-%d %H:%M]')
|
||||||
logger.error('%s %s %s %s %s 5xx INTERNAL SERVER ERROR\n%s', timestamp,
|
logger.error('%s %s %s %s %s 5xx INTERNAL SERVER ERROR\n%s', timestamp,
|
||||||
request.remote_addr, request.method, request.scheme, request.full_path, tb)
|
get_actual_addr(request), request.method, request.scheme, request.full_path, tb)
|
||||||
return e.status_code
|
return e.status_code
|
||||||
|
Reference in New Issue
Block a user