chg: [server UI v0.2] add ip info + ip list

pull/8/head
Terrtia 2019-01-25 16:17:46 +01:00
parent d84ca76736
commit 5cc00b93d5
No known key found for this signature in database
GPG Key ID: 1E1B1F50D84613D0
3 changed files with 100 additions and 2 deletions

View File

@ -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']))

View File

@ -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)

View File

@ -6,6 +6,7 @@
<link rel="icon" href="{{ url_for('static', filename='img/d4-logo.png')}}">
<!-- Core CSS -->
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='font-awesome/css/font-awesome.css') }}" rel="stylesheet">
<!-- JS -->
<script src="{{ url_for('static', filename='js/jquery.js')}}"></script>
@ -75,7 +76,7 @@
</div>
</div>
<div class="card-deck justify-content-center">
<div class="card-deck justify-content-center ml-0 mr-0">
<div class="card border-dark mt-3" style="max-width: 18rem;">
<div class="card-body text-dark">
<h5 class="card-title">Change Stream Max Size</h5>
@ -102,6 +103,20 @@
{% endif %}
</div>
</div>
<div class="card text-center border-danger mt-3" style="max-width: 20rem;">
<div class="card-body text-danger">
<h5 class="card-title">Blacklist Last IP</h5>
{% if not data_uuid['blacklisted_ip_by_uuid'] %}
<a href="{{ url_for('blacklist_ip_by_uuid') }}?uuid={{uuid_sensor}}&redirect=1">
<button type="button" class="btn btn-danger">Blacklist IP</button>
</a>
{% else %}
<a href="{{ url_for('unblacklist_ip_by_uuid') }}?uuid={{uuid_sensor}}&redirect=1">
<button type="button" class="btn btn-warning">UnBlacklist IP</button>
</a>
{% endif %}
</div>
</div>
<div class="card text-center border-danger mt-3" style="max-width: 20rem;">
<div class="card-body text-danger">
<h5 class="card-title">Blacklist IP Using This UUID</h5>
@ -118,4 +133,54 @@
</div>
</div>
<div class="row ml-0 mr-0">
<div class="col-6">
<div class="card text-center mt-3">
<div class="card-header bg-dark text-white">
Last IP Used:
</div>
<ul class="list-group list-group-flush">
{%for row in all_ip%}
<li class="list-group-item">
{{row['ip']}} - {{row['datetime']}} <button class="fa fa-info-circle btn text-secondary" onclick="get_whois_data('{{row['ip']}}');"></button>
</li>
{%endfor%}
</ul>
</div>
</div>
<div class="col-6">
<div class="d-none card mt-3 mb-3" id="whois_data">
<div class="card-header bg-dark text-center text-white">
Whois Info:
</div>
<pre class="ml-2" id="whois_output">
</pre>
</div>
</div>
</div>
</body>
<script>
function get_whois_data(ip){
$.getJSON( "{{url_for('whois_data')}}?ip="+ip, function( data ) {
console.log(data)
$( "#whois_data" ).removeClass( "d-none" );
$( "#whois_output" ).text(data);
});
}
</script>