-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
119 lines (96 loc) · 3.18 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# by Tobias Karuth (TKAMING)
import folium
import requests
import os
import glob
from flask import Flask, render_template, redirect, request
import json
class CountCalls:
def __init__(self, func):
self._count = 0
self._func = func
def __call__( self, *args, **kwargs):
self._count += 1
return self._func(*args,**kwargs)
@property
def call_count(self):
return self._count
# gets ip cordinates
def ip_cordinates():
global response
response = requests.get("http://ip-api.com/json/" + ip).json()
if response["status"] == "fail":
print(f"[*] Error: Couldn´t find IP address ({ip}) location")
return NameError
else:
global lat
lat = response['lat']
global lon
lon = response['lon']
@CountCalls
# makes map
def built_map():
if response["status"] == "fail":
print(f"[*] Error: Couldn´t generate map (No cordinates)")
return NameError
else:
map = folium.Map([lat, lon], zoom_start=12, control_scale=True)
folium.CircleMarker([lat, lon], radius=50, popup="The radius the IP could be in").add_to(map)
folium.Marker([lat, lon], popup='The IP you searched for', icon=folium.Icon(color="black")).add_to(map)
# create new map file
with open(f"templates/maps/map{built_map.call_count}.html", 'w') as fp:
# save and display the map
map.save(f'templates/maps/map{built_map.call_count}.html')
# flask processes
app = Flask(__name__)
@app.after_request
def after_request(response):
"""Ensure responses aren't cached"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
# --------------------------------- USERFORM PAGE -----------------------------------
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
try:
# deletes the old map
print(f"[*] Removes old map.html files")
all_files = glob.glob('templates/maps/*.html', recursive=True)
for f in all_files:
os.remove(f)
except:
FileExistsError
global ip
ip = request.form.get("ip")
if ip == "":
return redirect("/")
else:
return redirect("/map")
else:
response = ""
return render_template("index.html")
# --------------------------------- MAP PAGE -----------------------------------
@app.route("/map")
def map():
if request.method == "GET":
try:
if ip != NotImplemented:
ip_cordinates()
built_map()
if built_map() == NameError or ip_cordinates == NameError:
return redirect("/")
else:
return redirect("/")
except:
NameError
return redirect("/")
try:
if ip == "":
return redirect("/")
except:
NameError
return render_template(f"maps/map{built_map.call_count}.html")
if __name__ == "__main__":
app.run(debug=True)