responder_json_con_informacion_de_encabezados_ip_user-agent_etc
This is an old revision of the document!
Table of Contents
WhoAmI Flask App (with Cloudflare and Reverse Proxy Support)
This Python Flask application provides detailed information about the requester, such as their IP address, browser, and operating system. It also supports Cloudflare tunnels and reverse proxies by checking the relevant headers to determine the actual IP address of the client.
Features:
- Detects the requester's real IP address, even behind proxies or Cloudflare.
- Returns additional details such as browser, platform, Accept-Language, and cookies.
- Provides HTTP headers, query parameters, and request method details.
Code
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/')
def whoami():
user_agent = request.headers.get('User-Agent')
# First, check for the X-Forwarded-For header (used by most proxies)
real_ip = request.headers.get('X-Forwarded-For')
# Cloudflare provides this header for the actual client's IP
cf_ip = request.headers.get('CF-Connecting-IP')
# Fall back to remote_addr if no proxy headers are found
ip_address = cf_ip or real_ip or request.remote_addr
# Other details
accept_language = request.headers.get('Accept-Language')
referer = request.referrer
scheme = request.scheme
remote_port = request.environ['REMOTE_PORT']
headers = dict(request.headers)
info = {
'ip_address': ip_address,
'browser': request.user_agent.browser,
'platform': request.user_agent.platform,
'user_agent': user_agent,
'accept_language': accept_language,
'referer': referer,
'scheme': scheme,
'remote_port': remote_port,
'headers': headers,
'method': request.method,
'query_params': request.args,
'cookies': request.cookies
}
return jsonify(info)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5500)
Usage
- Install Flask using `pip`:
pip install Flask
- Run the app:
python main.py
- Ensure that your app is compatible with your reverse proxy (e.g., NGINX) or Cloudflare tunnel by properly forwarding headers like `X-Forwarded-For` and `CF-Connecting-IP`. You can configure a domain (e.g., `whoami.mydomain.com`) to access the app, and it will return detailed JSON information about the requester.
Example JSON Response:
{
"ip_address": "192.168.1.101",
"browser": "chrome",
"platform": "windows",
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
"accept_language": "en-US,en;q=0.9",
"referer": null,
"scheme": "https",
"remote_port": "50896",
"headers": {
"Host": "whoami.mydomain.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)...",
"Accept-Language": "en-US,en;q=0.9",
"X-Forwarded-For": "192.168.1.101",
"CF-Connecting-IP": "192.168.1.101",
...
},
"method": "GET",
"query_params": {},
"cookies": {}
}
responder_json_con_informacion_de_encabezados_ip_user-agent_etc.1726338724.txt.gz · Last modified: 2024/10/17 21:42 (external edit)
