From 5cc00b93d59c7e5e2882430475ede9bd35ff4ba6 Mon Sep 17 00:00:00 2001 From: Terrtia Date: Fri, 25 Jan 2019 16:17:46 +0100 Subject: [PATCH] chg: [server UI v0.2] add ip info + ip list --- server/server.py | 7 +++ server/web/Flask_server.py | 28 +++++++++- server/web/templates/uuid_management.html | 67 ++++++++++++++++++++++- 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/server/server.py b/server/server.py index 358ecbd..ce30d4b 100755 --- a/server/server.py +++ b/server/server.py @@ -78,6 +78,8 @@ class Echo(Protocol, TimeoutMixin): def dataReceived(self, data): self.resetTimeout() ip, source_port = self.transport.client + if self.data_saved == False: + logger.debug('New connection, ip={}, port={} session_uuid={}'.format(ip, source_port, self.session_uuid)) # check blacklisted_ip if redis_server_metadata.sismember('blacklist_ip', ip): self.transport.abortConnection() @@ -260,6 +262,11 @@ class Echo(Protocol, TimeoutMixin): redis_server_stream.sadd('session_uuid:{}'.format(data_header['type']), self.session_uuid.encode()) redis_server_stream.hset('map-type:session_uuid-uuid:{}'.format(data_header['type']), self.session_uuid, data_header['uuid_header']) redis_server_metadata.hdel('metadata_uuid:{}'.format(data_header['uuid_header']), 'Error') + + #UUID IP: ## TODO: use d4 timestamp ? + redis_server_metadata.lpush('list_uuid_ip:{}'.format(data_header['uuid_header']), '{}-{}'.format(ip, datetime.datetime.now().strftime("%Y%m%d%H%M%S"))) + redis_server_metadata.ltrim('list_uuid_ip:{}'.format(data_header['uuid_header']), 0, 15) + self.data_saved = True else: logger.warning("stream exceed max entries limit, uuid={}, session_uuid={}, type={}".format(data_header['uuid_header'], self.session_uuid, data_header['type'])) diff --git a/server/web/Flask_server.py b/server/web/Flask_server.py index d85adae..76bdbbc 100755 --- a/server/web/Flask_server.py +++ b/server/web/Flask_server.py @@ -2,6 +2,7 @@ # -*-coding:UTF-8 -* import os +import re import sys import uuid import time @@ -11,6 +12,8 @@ import flask import datetime import ipaddress +import subprocess + from flask import Flask, render_template, jsonify, request, Blueprint, redirect, url_for baseUrl = '' @@ -73,6 +76,13 @@ def get_json_type_description(): json_type_description = json.loads(f.read()) return json_type_description +def get_whois_ouput(ip): + if is_valid_ip(ip): + process = subprocess.run(["whois", ip], stdout=subprocess.PIPE) + return re.sub(r"#.*\n?", '', process.stdout.decode()).lstrip('\n').rstrip('\n') + else: + return '' + # ========== ROUTES ============ @app.route('/') def index(): @@ -177,7 +187,14 @@ def uuid_management(): else: max_uuid_stream = default_max_entries_by_stream - return render_template("uuid_management.html", uuid_sensor=uuid_sensor, data_uuid=data_uuid, max_uuid_stream=max_uuid_stream) + list_ip = redis_server_metadata.lrange('list_uuid_ip:{}'.format(uuid_sensor), 0, -1) + all_ip = [] + for elem in list_ip: + ip, d_time = elem.split('-') + all_ip.append({'ip': ip,'datetime': '{}/{}/{} - {}:{}.{}'.format(d_time[0:4], d_time[5:6], d_time[6:8], d_time[8:10], d_time[10:12], d_time[12:14])}) + print(all_ip) + + return render_template("uuid_management.html", uuid_sensor=uuid_sensor, data_uuid=data_uuid, max_uuid_stream=max_uuid_stream, all_ip=all_ip) else: return 'Invalid uuid' @@ -325,5 +342,14 @@ def delete_data(): redis_server_metadata.delete('daily_uuid:{}'.format(date)) return render_template("index.html") +# demo function +@app.route('/whois_data') +def whois_data(): + ip = request.args.get('ip') + if is_valid_ip: + return jsonify(get_whois_ouput(ip)) + else: + return 'Invalid IP' + if __name__ == "__main__": app.run(host='0.0.0.0', port=7000, threaded=True) diff --git a/server/web/templates/uuid_management.html b/server/web/templates/uuid_management.html index b14f863..22226cc 100644 --- a/server/web/templates/uuid_management.html +++ b/server/web/templates/uuid_management.html @@ -6,6 +6,7 @@ + @@ -75,7 +76,7 @@ -
+
Change Stream Max Size
@@ -102,6 +103,20 @@ {% endif %}
+
+
+
Blacklist Last IP
+ {% if not data_uuid['blacklisted_ip_by_uuid'] %} + + + + {% else %} + + + + {% endif %} +
+
Blacklist IP Using This UUID
@@ -118,4 +133,54 @@
+
+
+
+
+ Last IP Used: +
+
    + {%for row in all_ip%} +
  • + {{row['ip']}} - {{row['datetime']}} +
  • + {%endfor%} +
+
+
+
+
+
+ Whois Info: +
+
+				
+
+
+
+ + +