-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
141 lines (108 loc) · 3.74 KB
/
server.py
File metadata and controls
141 lines (108 loc) · 3.74 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#
# Flask server for frontend.
# Author: N. Beckstead
# TODO: Implement API
# TODO: Interactive search.
# TODO: Implement render_template instead of send_from_directory
#
from flask import Flask, send_from_directory
#from flask.ext.jsonpify import jsonify
import folium
import server_vars
import db_helper as database
import geo_helper as geo
import shodan_helper as sh
app = Flask(__name__)
##################### API #####################
@app.route('/api/ip/<ip>')
def api_ip(ip):
return "{}".format(ip)
@app.route('/api/country/<country>')
def api_country(country):
return "{}".format(country)
@app.route('/api/service/<service>')
def api_service(service):
return "{}".format(service)
@app.route('/api/port/<port>')
def api_port(port):
return "{}".format(port)
@app.route('/api/recent/<num>')
def api_recent(num):
db, curs = database.connect()
recents = database.get_recent(db, curs, num)
ret_str = ""
for row in recents:
ret_str += str(row)
ret_str += '<br>'
db.close()
return ret_str
@app.route('/api/stats/totals')
def api_totals():
db, curs = database.connect()
total_attempts = database.get_total_attempts(db, curs)
total_ips = database.get_total_ips(db, curs)
db.close()
return "Total Attempts: {}<br>Total IPs: {}".format(total_attempts, total_ips)
@app.route('/api/stats/total-attempts')
def api_totalattempts():
db, curs = database.connect()
ret_str = str(database.get_total_attempts(db, curs))
db.close()
return ret_str
@app.route('/api/stats/total-ips')
def apt_totalips():
db, curs = database.connect()
ret_str = str(database.get_total_ips(db, curs))
db.close()
return ret_str
##################### PAGES #####################
@app.route('/')
def index():
return send_from_directory('res/html', 'index.html')
@app.route('/ip/<ip>')
def address(ip):
# Get host info from Shodan
page = sh.lookup_host(str(ip))
# Add interactive map
lat, lon = geo.get_coordinates(str(ip))
ip_map = folium.Map(location=[lat, lon], zoom_start=7, tiles='CartoDB positron')
folium.Marker(location=(lat, lon)).add_to(ip_map)
page += "<div style=\"height:30%; width:30%;\">"
page += str(ip_map.get_root().render())
page += "</div>"
return page
@app.route('/color/<ip>')
def starred(ip):
db, curs = database.connect()
marker_color = database.get_color(db, curs, ip)
db.close()
return '<html><body style="background:{};"></body></html>'.format(marker_color)
@app.route('/recent/<num>')
def recent(num):
db, curs = database.connect()
recents = database.get_recent(db, curs, num)
db.close()
recent_map = folium.Map(location=[24.635246, 2.616971], zoom_start=2, tiles='CartoDB positron')
for row in recents:
coords = geo.get_coordinates(row[2])
if coords is None:
continue
folium.Marker(location=coords).add_to(recent_map)
recent_page = "<div style=\"height:50%; width:60%;\">" + str(recent_map.get_root().render()) + "</div>"
return recent_page #send_from_directory('res/html', 'recent.html')
##################### RESOURCES #####################
@app.route('/favicon.ico')
def favicon():
return send_from_directory('res/img', 'favicon.ico')
@app.route('/res/img/<image>')
def return_img(image):
return send_from_directory('res/img', image)
@app.route('/res/html/maps/<getmap>')
def return_map(getmap):
return send_from_directory(server_vars.MAP_DIRECTORY, getmap)
##################### ERRORS #####################
@app.errorhandler(404)
def page_not_found(e):
return send_from_directory('res/html/errors', '404.html'), 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)