-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvishttp.py
156 lines (115 loc) · 3.37 KB
/
vishttp.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from flask import Flask, request, send_from_directory, jsonify, render_template, Response
import folium
import threading
import random
import sys,os,signal
import config
import math
import geojson
from geojson import Feature, Point, FeatureCollection
app = Flask(__name__,
static_url_path='',
static_folder='static',
template_folder='templates')
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
)
@app.route('/kill')
def kill_server():
response = Response('Killing server')
@response.call_on_close
def process_after_request():
os.kill(os.getpid(), signal.SIGINT)
return response
@app.route('/flush')
def flush_conntrack():
import subprocess
return subprocess.check_output(['conntrack','-F'],stderr=subprocess.STDOUT).decode('utf-8')
@app.route('/folium')
def index():
start_coords = (46.9540700, 142.7360300)
folium_map = folium.Map(location=start_coords, zoom_start=14)
return folium_map._repr_html_()
import config
import connections
@app.route('/deviceactivity.json')
def ipinfo():
acts = {}
activity_local = {}
for ip,last_activity in connections.getIPActivity().items():
if ip in config.homenetwork:
activity_local[str(ip)]=last_activity
else:
acts[str(ip)]=last_activity
return jsonify({
"activity": acts,
"activity_local": activity_local,
})
@app.route('/config.json')
def configjson():
return jsonify({ "homenetwork": str(config.homenetwork) })
import monitor_domains
@app.route('/ipdomainquerymappingshort.json')
def ipdomainquerymappingshort():
return jsonify({ "mapping": {str(k):v for k,v in monitor_domains.getIPQueryMappingShort().items()} })
# Geo visualization
def GeoProvider():
"Example geoprovider"
features = []
features.append(Feature(properties={
"id": "1",
"ip": "10.0.0.0",
"asnname": "localhost",
},geometry=Point((random.random() * 360 - 180, random.random() * 360 - 180))))
features.append(Feature(properties={
"id": "2",
"ip": "10.0.0.1",
"asnname": "localhost2",
},geometry=Point((random.random() * 360 - 180, random.random() * 360 - 180))))
data = FeatureCollection(features)
return data
#TODO less ugly
def setGeoProvider(p):
global GeoProvider
GeoProvider=p
@app.route('/geo.json')
def geodata():
return Response(
response=geojson.dumps(GeoProvider(), sort_keys=False),
mimetype='application/json',
status=200)
def dnsBarProvider():
return {}
def setDnsBarProvider(p):
global dnsBarProvider
dnsBarProvider=p
@app.route('/dnsbar.json')
def dnsbar():
return jsonify(dnsBarProvider())
@app.route('/dnsbar_unshort.json')
def dnsbar_unshort():
return jsonify(dnsBarProvider(short=False))
# Sankey visualization
def SankeyProvider():
return jsonify({})
def setSankeyProvider(p):
global SankeyProvider
SankeyProvider=p
@app.route('/sankey.json')
def sankeyjson():
return jsonify(SankeyProvider())
import ipaddress,utils
@app.route("/")
def main():
yourip=ipaddress.ip_address(request.remote_addr)
yourname=request.remote_addr
if yourip in config.homenetwork:
yourname=utils.shortenHomeIPMemorableUniq(yourip,True)
return render_template("hello.html",yourip=yourip,yourname=yourname)
def run(use_reloader=False):
app.run(debug=True, host='0.0.0.0', use_reloader=use_reloader)
def run_threaded():
threading.Thread(target=run).start()
if __name__ == "__main__":
run(use_reloader=True)