fix wrong ip address behind proxy

This commit is contained in:
wangjiacai 2023-04-18 01:28:58 +08:00
parent 9c309dafc2
commit 6f3df5fc51

View File

@ -60,6 +60,13 @@ logger = logging.getLogger('waitress')
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')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
@ -69,14 +76,14 @@ def favicon():
@app.before_request
def before_request():
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)
@app.after_request
def after_request(response):
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)
return response
@ -86,5 +93,5 @@ def exceptions(e):
tb = traceback.format_exc()
timestamp = time.strftime('[%Y-%b-%d %H:%M]')
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