mirror of https://github.com/D4-project/d4-core
chg: [server UI v0.1] add management by uuid + blacklist ip by uuid
parent
7ff0f9ccdb
commit
63a447d588
|
@ -105,6 +105,12 @@ class Echo(Protocol, TimeoutMixin):
|
|||
data_header['hmac_header'] = data[26:58]
|
||||
data_header['size'] = struct.unpack('I', data[58:62])[0]
|
||||
|
||||
# blacklist ip by uuid
|
||||
if redis_server_metadata.sismember('blacklist_ip_by_uuid', data_header['uuid_header']):
|
||||
redis_server_metadata.sadd('blacklist_ip', self.transport.client[0])
|
||||
self.transport.abortConnection()
|
||||
logger.warning('Blacklisted IP by UUID={}, connection closed'.format(data_header['uuid_header']))
|
||||
|
||||
# uuid blacklist
|
||||
if redis_server_metadata.sismember('blacklist_uuid', data_header['uuid_header']):
|
||||
self.transport.abortConnection()
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
import os
|
||||
import sys
|
||||
import uuid
|
||||
import time
|
||||
import redis
|
||||
import flask
|
||||
|
@ -17,6 +18,8 @@ if baseUrl != '':
|
|||
host_redis_stream = "localhost"
|
||||
port_redis_stream = 6379
|
||||
|
||||
default_max_entries_by_stream = 10000
|
||||
|
||||
redis_server_stream = redis.StrictRedis(
|
||||
host=host_redis_stream,
|
||||
port=port_redis_stream,
|
||||
|
@ -34,6 +37,14 @@ redis_server_metadata = redis.StrictRedis(
|
|||
app = Flask(__name__, static_url_path=baseUrl+'/static/')
|
||||
app.config['MAX_CONTENT_LENGTH'] = 900 * 1024 * 1024
|
||||
|
||||
# ========== FUNCTIONS ============
|
||||
def is_valid_uuid_v4(header_uuid):
|
||||
try:
|
||||
uuid_test = uuid.UUID(hex=header_uuid, version=4)
|
||||
return uuid_test.hex == header_uuid
|
||||
except:
|
||||
return False
|
||||
|
||||
# ========== ROUTES ============
|
||||
@app.route('/')
|
||||
def index():
|
||||
|
@ -73,13 +84,101 @@ def sensors_status():
|
|||
first_seen_gmt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(first_seen)))
|
||||
last_seen = redis_server_metadata.hget('metadata_uuid:{}'.format(result), 'last_seen')
|
||||
last_seen_gmt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(last_seen)))
|
||||
Error = redis_server_metadata.hget('metadata_uuid:{}'.format(result), 'Error')
|
||||
if redis_server_metadata.sismember('blacklist_ip_by_uuid', result):
|
||||
Error = "All IP using this UUID are Blacklisted"
|
||||
elif redis_server_metadata.sismember('blacklist_uuid', result):
|
||||
Error = "Blacklisted UUID"
|
||||
else:
|
||||
Error = redis_server_metadata.hget('metadata_uuid:{}'.format(result), 'Error')
|
||||
|
||||
if first_seen is not None and last_seen is not None:
|
||||
status_daily_uuid.append({"uuid": result,"first_seen": first_seen, "last_seen": last_seen,
|
||||
"first_seen_gmt": first_seen_gmt, "last_seen_gmt": last_seen_gmt, "Error": Error})
|
||||
|
||||
return render_template("sensors_status.html", status_daily_uuid=status_daily_uuid)
|
||||
|
||||
@app.route('/server_management')
|
||||
def server_management():
|
||||
return render_template("server_management.html")
|
||||
|
||||
@app.route('/uuid_management')
|
||||
def uuid_management():
|
||||
uuid_sensor = request.args.get('uuid')
|
||||
if is_valid_uuid_v4(uuid_sensor):
|
||||
|
||||
first_seen = redis_server_metadata.hget('metadata_uuid:{}'.format(uuid_sensor), 'first_seen')
|
||||
first_seen_gmt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(first_seen)))
|
||||
last_seen = redis_server_metadata.hget('metadata_uuid:{}'.format(uuid_sensor), 'last_seen')
|
||||
last_seen_gmt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(last_seen)))
|
||||
Error = redis_server_metadata.hget('metadata_uuid:{}'.format(uuid_sensor), 'Error')
|
||||
if redis_server_metadata.sismember('blacklist_uuid', uuid_sensor):
|
||||
blacklisted_uuid = True
|
||||
Error = "Blacklisted UUID"
|
||||
else:
|
||||
blacklisted_uuid = False
|
||||
if redis_server_metadata.sismember('blacklist_ip_by_uuid', uuid_sensor):
|
||||
blacklisted_ip_by_uuid = True
|
||||
Error = "All IP using this UUID are Blacklisted"
|
||||
else:
|
||||
blacklisted_ip_by_uuid = False
|
||||
data_uuid= {"first_seen": first_seen, "last_seen": last_seen,
|
||||
"blacklisted_uuid": blacklisted_uuid, "blacklisted_ip_by_uuid": blacklisted_ip_by_uuid,
|
||||
"first_seen_gmt": first_seen_gmt, "last_seen_gmt": last_seen_gmt, "Error": Error}
|
||||
|
||||
max_uuid_stream = redis_server_metadata.hget('stream_max_size_by_uuid', uuid_sensor)
|
||||
if max_uuid_stream is not None:
|
||||
max_uuid_stream = int(max_uuid_stream)
|
||||
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)
|
||||
else:
|
||||
return 'Invalid uuid'
|
||||
|
||||
@app.route('/blacklist_uuid')
|
||||
def blacklist_uuid():
|
||||
uuid_sensor = request.args.get('uuid')
|
||||
user = request.args.get('redirect')
|
||||
if is_valid_uuid_v4(uuid_sensor):
|
||||
redis_server_metadata.sadd('blacklist_uuid', uuid_sensor)
|
||||
if user:
|
||||
return redirect(url_for('uuid_management', uuid=uuid_sensor))
|
||||
else:
|
||||
return 'Invalid uuid'
|
||||
|
||||
@app.route('/unblacklist_uuid')
|
||||
def unblacklist_uuid():
|
||||
uuid_sensor = request.args.get('uuid')
|
||||
user = request.args.get('redirect')
|
||||
if is_valid_uuid_v4(uuid_sensor):
|
||||
redis_server_metadata.srem('blacklist_uuid', uuid_sensor)
|
||||
if user:
|
||||
return redirect(url_for('uuid_management', uuid=uuid_sensor))
|
||||
else:
|
||||
return 'Invalid uuid'
|
||||
|
||||
@app.route('/blacklist_ip_by_uuid')
|
||||
def blacklist_ip_by_uuid():
|
||||
uuid_sensor = request.args.get('uuid')
|
||||
user = request.args.get('redirect')
|
||||
if is_valid_uuid_v4(uuid_sensor):
|
||||
redis_server_metadata.sadd('blacklist_ip_by_uuid', uuid_sensor)
|
||||
if user:
|
||||
return redirect(url_for('uuid_management', uuid=uuid_sensor))
|
||||
else:
|
||||
return 'Invalid uuid'
|
||||
|
||||
@app.route('/unblacklist_ip_by_uuid')
|
||||
def unblacklist_ip_by_uuid():
|
||||
uuid_sensor = request.args.get('uuid')
|
||||
user = request.args.get('redirect')
|
||||
if is_valid_uuid_v4(uuid_sensor):
|
||||
redis_server_metadata.srem('blacklist_ip_by_uuid', uuid_sensor)
|
||||
if user:
|
||||
return redirect(url_for('uuid_management', uuid=uuid_sensor))
|
||||
else:
|
||||
return 'Invalid uuid'
|
||||
|
||||
# demo function
|
||||
@app.route('/delete_data')
|
||||
def delete_data():
|
||||
|
|
|
@ -60,7 +60,7 @@
|
|||
<a class="nav-link mr-3" href="{{ url_for('sensors_status') }}">Sensors Status</a>
|
||||
</li>
|
||||
<li class="nav-item mr-3">
|
||||
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Server Management</a>
|
||||
<a class="nav-link" href="{{ url_for('server_management') }}" tabindex="-1" aria-disabled="true">Server Management</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
|
|
@ -32,16 +32,18 @@
|
|||
<a class="nav-link active mr-3" href="{{ url_for('sensors_status') }}">Sensors Status</a>
|
||||
</li>
|
||||
<li class="nav-item mr-3">
|
||||
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Server Management</a>
|
||||
<a class="nav-link" href="{{ url_for('server_management') }}" tabindex="-1" aria-disabled="true">Server Management</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
{% for row_uuid in status_daily_uuid %}
|
||||
<div class="card text-center mt-3 ml-2 mr-2">
|
||||
<div class="card-header bg-dark text-white">
|
||||
UUID: {{row_uuid['uuid']}}
|
||||
</div>
|
||||
<a href="{{ url_for('uuid_management') }}?uuid={{row_uuid['uuid']}}">
|
||||
<div class="card-header bg-dark text-white">
|
||||
UUID: {{row_uuid['uuid']}}
|
||||
</div>
|
||||
</a>
|
||||
<div class="card-body">
|
||||
<div class="card-group">
|
||||
<div class="card">
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>D4-Project</title>
|
||||
<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">
|
||||
|
||||
<!-- JS -->
|
||||
<script src="{{ url_for('static', filename='js/jquery.js')}}"></script>
|
||||
<script src="{{ url_for('static', filename='js/bootstrap.min.js')}}"></script>
|
||||
|
||||
<style>
|
||||
|
||||
</style>
|
||||
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
|
||||
<a class="navbar-brand" href="{{ url_for('index') }}">
|
||||
<img src="{{ url_for('static', filename='img/d4-logo.png')}}" alt="D4 Project" style="width:80px;">
|
||||
</a>
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link mr-3" href="{{ url_for('index') }}">Home <span class="sr-only">(current)</span></a>
|
||||
</li>
|
||||
<li class="nav-item" mr-3>
|
||||
<a class="nav-link mr-3" href="{{ url_for('sensors_status') }}">Sensors Status</a>
|
||||
</li>
|
||||
<li class="nav-item mr-3">
|
||||
<a class="nav-link active" href="{{ url_for('server_management') }}" tabindex="-1" aria-disabled="true">Server Management</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
|
||||
|
||||
</body>
|
|
@ -0,0 +1,121 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>D4-Project</title>
|
||||
<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">
|
||||
|
||||
<!-- JS -->
|
||||
<script src="{{ url_for('static', filename='js/jquery.js')}}"></script>
|
||||
<script src="{{ url_for('static', filename='js/bootstrap.min.js')}}"></script>
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
|
||||
<a class="navbar-brand" href="{{ url_for('index') }}">
|
||||
<img src="{{ url_for('static', filename='img/d4-logo.png')}}" alt="D4 Project" style="width:80px;">
|
||||
</a>
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link mr-3" href="{{ url_for('index') }}">Home <span class="sr-only">(current)</span></a>
|
||||
</li>
|
||||
<li class="nav-item" mr-3>
|
||||
<a class="nav-link mr-3" href="{{ url_for('sensors_status') }}">Sensors Status</a>
|
||||
</li>
|
||||
<li class="nav-item mr-3">
|
||||
<a class="nav-link" href="{{ url_for('server_management') }}" tabindex="-1" aria-disabled="true">Server Management</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
<div class="card text-center mt-3 ml-2 mr-2">
|
||||
<div class="card-header bg-dark text-white">
|
||||
UUID: {{uuid_sensor}}
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="card-group">
|
||||
<div class="card">
|
||||
<div class="card-header bg-info text-white">
|
||||
First Seen
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<p class="card-text">{{data_uuid['first_seen_gmt']}} - ({{data_uuid['first_seen']}})</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="card-header bg-info text-white">
|
||||
Last Seen
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<p class="card-text">{{data_uuid['last_seen_gmt']}} - ({{data_uuid['last_seen']}})</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
{% if not data_uuid['Error'] %}
|
||||
<div class="card-header bg-success text-white">
|
||||
Status
|
||||
</div>
|
||||
<div class="card-body text-success">
|
||||
<p class="card-text">OK</p>
|
||||
{% else %}
|
||||
<div class="card-header bg-danger text-white">
|
||||
Status
|
||||
</div>
|
||||
<div class="card-body text-danger">
|
||||
<p class="card-text">{{data_uuid['Error']}}</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card-deck justify-content-center">
|
||||
<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>
|
||||
{% if not data_uuid['blacklisted_uuid'] and not data_uuid['blacklisted_ip_by_uuid'] %}
|
||||
<input class="form-control" type="number" id="max_stream_input" value="{{max_uuid_stream}}" min="0" required>
|
||||
<button type="button" class="btn btn-outline-secondary mt-1">Change Max Size</button>
|
||||
{% else %}
|
||||
<input class="form-control" type="number" id="max_stream_input" value="{{max_uuid_stream}}" min="0" required disabled>
|
||||
<button type="button" class="btn btn-outline-secondary mt-1" disabled>Change Max Size</button>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="card text-center border-danger mt-3" style="max-width: 14rem;">
|
||||
<div class="card-body text-danger">
|
||||
<h5 class="card-title">UUID Blacklist</h5>
|
||||
{% if not data_uuid['blacklisted_uuid'] %}
|
||||
<a href="{{ url_for('blacklist_uuid') }}?uuid={{uuid_sensor}}&redirect=1" {% if data_uuid['blacklisted_ip_by_uuid'] %}style="pointer-events: none;"{% endif %}>
|
||||
<button type="button" class="btn btn-danger" {% if data_uuid['blacklisted_ip_by_uuid'] %}disabled{% endif %}>Blacklist UUID</button>
|
||||
</a>
|
||||
{% else %}
|
||||
<a href="{{ url_for('unblacklist_uuid') }}?uuid={{uuid_sensor}}&redirect=1" {% if data_uuid['blacklisted_ip_by_uuid'] %}style="pointer-events: none;"{% endif %}>
|
||||
<button type="button" class="btn btn-warning" {% if data_uuid['blacklisted_ip_by_uuid'] %}disabled{% endif %}>UnBlacklist UUID</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>
|
||||
{% 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>
|
||||
|
||||
</body>
|
Loading…
Reference in New Issue