forked from emojipeach/whatismyip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
54 lines (43 loc) · 1.46 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from flask import Flask, render_template, request, jsonify
import socket
app = Flask(__name__)
@app.route("/", methods=["GET"])
def get_my_ip():
if request.environ.get('HTTP_CF_CONNECTING_IPV6'):
real_ip = request.environ.get('HTTP_CF_CONNECTING_IPV6')
else:
real_ip = request.environ['HTTP_X_FORWARDED_FOR'].split(',')[0]
try:
ip_hostname = socket.gethostbyaddr(real_ip)[0]
except:
ip_hostname = "No reverse DNS for IP"
return render_template(
'ip_new.html', ip=real_ip, hostname=ip_hostname
)
@app.route("/api", methods=["GET"])
def get_my_ip_api():
return_data = {}
if request.environ.get('HTTP_CF_CONNECTING_IPV6'):
real_ip = request.environ.get('HTTP_CF_CONNECTING_IPV6')
else:
real_ip = request.environ['HTTP_X_FORWARDED_FOR'].split(',')[0]
try:
ip_hostname = socket.gethostbyaddr(real_ip)[0]
except:
ip_hostname = "no_rdns_for_ip"
return_data['ip'] = real_ip
return_data['hostname'] = ip_hostname
try:
if request.environ['HTTP_DNT']:
return_data['do_not_track'] = 'true'
else:
return_data['do_not_track'] = 'false'
except:
return_data['do_not_track'] = 'unknown'
try:
return_data['user_agent'] = request.environ['HTTP_USER_AGENT']
except:
return_data['user_agent'] = "unknown"
return jsonify(return_data),200
if __name__ == '__main__':
app.run()