diff options
| -rw-r--r-- | app.py | 30 |
1 files changed, 24 insertions, 6 deletions
@@ -3,15 +3,33 @@ from os import environ app = Flask(__name__) +env = 'PROD' if 'PROD' in environ else 'DEV' + +TEMPLATE = ''' +/----------------------------------------\\ +| {ip:39}| +| | +| Created by Yuval Adam | +| https://github.com/yuvadm/ipcli.app | +| Comments welcome at hello@ipcli.app | +\\----------------------------------------/ +''' + def get_ip(request): - if 'PROD' in environ: - # Heroku routing layers masks the original IP, fetch it from the custom header - # And make sure we just take the first IP in case other proxies were in the way - ips = request.headers.get('X-Forwarded-For') + FOWARDED_FOR_HEADER = 'X-Forwarded-For' + if FOWARDED_FOR_HEADER in request.headers: + # Routing layers and proxies along the way might mask the original IP + # So fetch it from the custom header + # And make sure we just take the first (source) IP address + ips = request.headers.get(FOWARDED_FOR_HEADER) return ips.split(',')[0] - else: - return request.remote_addr + return request.remote_addr @app.route('/') def index(): + ip = get_ip(request) + return TEMPLATE.format(ip=ip) + +@app.route('/p') +def plain(): return get_ip(request) |
