2024-06-26 13:30:47 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2024-07-02 09:41:17 +00:00
|
|
|
import functools
|
2024-06-26 13:30:47 +00:00
|
|
|
import json
|
|
|
|
import sys
|
2024-07-02 09:41:17 +00:00
|
|
|
import time
|
2024-06-26 13:30:47 +00:00
|
|
|
import zmq
|
|
|
|
import socketio
|
|
|
|
import eventlet
|
|
|
|
from pprint import pprint
|
|
|
|
from eventlet.green import zmq as gzmq
|
|
|
|
|
|
|
|
import exercise as exercise_model
|
|
|
|
import notification as notification_model
|
|
|
|
import db
|
2024-07-01 13:02:19 +00:00
|
|
|
import config
|
2024-07-02 11:46:15 +00:00
|
|
|
from config import logger
|
2024-07-01 11:12:23 +00:00
|
|
|
import misp_api
|
2024-06-26 13:30:47 +00:00
|
|
|
|
|
|
|
|
2024-07-01 12:54:33 +00:00
|
|
|
ZMQ_MESSAGE_COUNT = 0
|
|
|
|
|
|
|
|
|
2024-07-02 09:41:17 +00:00
|
|
|
def debounce(debounce_seconds: int = 1):
|
|
|
|
func_last_execution_time = {}
|
|
|
|
def decorator(func):
|
|
|
|
@functools.wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
now = time.time()
|
|
|
|
key = func.__name__
|
|
|
|
if key not in func_last_execution_time:
|
|
|
|
func_last_execution_time[key] = now
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
elif now >= func_last_execution_time[key] + debounce_seconds:
|
|
|
|
func_last_execution_time[key] = now
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-06-26 13:30:47 +00:00
|
|
|
# Initialize ZeroMQ context and subscriber socket
|
|
|
|
context = gzmq.Context()
|
|
|
|
zsocket = context.socket(gzmq.SUB)
|
2024-07-01 13:02:19 +00:00
|
|
|
zmq_url = config.zmq_url
|
2024-06-26 13:30:47 +00:00
|
|
|
zsocket.connect(zmq_url)
|
|
|
|
zsocket.setsockopt_string(gzmq.SUBSCRIBE, '')
|
|
|
|
|
|
|
|
|
|
|
|
# Initialize Socket.IO server
|
|
|
|
sio = socketio.Server(cors_allowed_origins='*', async_mode='eventlet')
|
2024-06-26 13:46:40 +00:00
|
|
|
app = socketio.WSGIApp(sio, static_files={
|
|
|
|
'/': {'content_type': 'text/html', 'filename': 'dist/index.html'},
|
|
|
|
'/assets': './dist/assets',
|
|
|
|
})
|
2024-06-26 13:30:47 +00:00
|
|
|
|
|
|
|
@sio.event
|
|
|
|
def connect(sid, environ):
|
2024-07-02 11:46:15 +00:00
|
|
|
logger.debug("Client connected: " + sid)
|
2024-06-26 13:30:47 +00:00
|
|
|
|
|
|
|
@sio.event
|
|
|
|
def disconnect(sid):
|
2024-07-02 11:46:15 +00:00
|
|
|
logger.debug("Client disconnected: " + sid)
|
2024-06-26 13:30:47 +00:00
|
|
|
|
|
|
|
@sio.event
|
|
|
|
def get_exercises(sid):
|
|
|
|
return exercise_model.get_exercises()
|
|
|
|
|
2024-07-01 09:21:01 +00:00
|
|
|
@sio.event
|
|
|
|
def get_selected_exercises(sid):
|
|
|
|
return exercise_model.get_selected_exercises()
|
|
|
|
|
|
|
|
@sio.event
|
|
|
|
def change_exercise_selection(sid, payload):
|
|
|
|
return exercise_model.change_exercise_selection(payload['exercise_uuid'], payload['selected'])
|
|
|
|
|
2024-06-26 13:30:47 +00:00
|
|
|
@sio.event
|
|
|
|
def get_progress(sid):
|
|
|
|
return exercise_model.get_progress()
|
|
|
|
|
|
|
|
@sio.event
|
|
|
|
def get_notifications(sid):
|
|
|
|
return notification_model.get_notifications()
|
|
|
|
|
|
|
|
@sio.event
|
|
|
|
def mark_task_completed(sid, payload):
|
2024-07-01 09:21:01 +00:00
|
|
|
return exercise_model.mark_task_completed(int(payload['user_id']), payload['exercise_uuid'], payload['task_uuid'])
|
2024-06-26 13:30:47 +00:00
|
|
|
|
|
|
|
@sio.event
|
|
|
|
def mark_task_incomplete(sid, payload):
|
2024-07-01 09:21:01 +00:00
|
|
|
return exercise_model.mark_task_incomplete(int(payload['user_id']), payload['exercise_uuid'], payload['task_uuid'])
|
|
|
|
|
|
|
|
@sio.event
|
|
|
|
def reset_all_exercise_progress(sid):
|
|
|
|
return exercise_model.resetAllExerciseProgress()
|
2024-06-26 13:30:47 +00:00
|
|
|
|
2024-07-01 13:10:18 +00:00
|
|
|
@sio.event
|
|
|
|
def reset_notifications(sid):
|
|
|
|
return notification_model.reset_notifications()
|
|
|
|
|
2024-07-01 11:12:23 +00:00
|
|
|
@sio.event
|
|
|
|
def get_diagnostic(sid):
|
|
|
|
return getDiagnostic()
|
|
|
|
|
2024-07-01 11:44:05 +00:00
|
|
|
@sio.event
|
|
|
|
def toggle_verbose_mode(sid, payload):
|
|
|
|
return notification_model.set_verbose_mode(payload['verbose'])
|
|
|
|
|
2024-06-26 13:30:47 +00:00
|
|
|
@sio.on('*')
|
|
|
|
def any_event(event, sid, data={}):
|
2024-07-02 11:46:15 +00:00
|
|
|
logger.info('>> Unhandled event', event)
|
2024-06-26 13:30:47 +00:00
|
|
|
|
|
|
|
def handleMessage(topic, s, message):
|
|
|
|
data = json.loads(message)
|
|
|
|
|
|
|
|
if topic == 'misp_json_audit':
|
|
|
|
user_id, email = notification_model.get_user_email_id_pair(data)
|
2024-06-28 15:14:05 +00:00
|
|
|
if user_id is not None and '@' in email:
|
2024-06-26 13:30:47 +00:00
|
|
|
if user_id not in db.USER_ID_TO_EMAIL_MAPPING:
|
|
|
|
db.USER_ID_TO_EMAIL_MAPPING[user_id] = email
|
|
|
|
sio.emit('new_user', email)
|
|
|
|
|
|
|
|
user_id, authkey = notification_model.get_user_authkey_id_pair(data)
|
|
|
|
if user_id is not None:
|
|
|
|
if authkey not in db.USER_ID_TO_AUTHKEY_MAPPING:
|
|
|
|
db.USER_ID_TO_AUTHKEY_MAPPING[user_id] = authkey
|
|
|
|
return
|
|
|
|
|
|
|
|
if notification_model.is_http_request(data):
|
|
|
|
notification = notification_model.get_notification_message(data)
|
|
|
|
if notification_model.is_accepted_notification(notification):
|
|
|
|
notification_model.record_notification(notification)
|
|
|
|
sio.emit('notification', notification)
|
|
|
|
|
|
|
|
user_id = notification_model.get_user_id(data)
|
|
|
|
if user_id is not None:
|
|
|
|
if exercise_model.is_accepted_query(data):
|
|
|
|
context = get_context(data)
|
|
|
|
succeeded_once = exercise_model.check_active_tasks(user_id, data, context)
|
|
|
|
if succeeded_once:
|
2024-07-02 09:41:17 +00:00
|
|
|
sendRefreshScore()
|
|
|
|
|
|
|
|
|
|
|
|
@debounce(debounce_seconds=1)
|
|
|
|
def sendRefreshScore():
|
|
|
|
sio.emit('refresh_score')
|
2024-06-26 13:30:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_context(data: dict) -> dict:
|
|
|
|
context = {}
|
|
|
|
if 'Log' in data:
|
|
|
|
if 'request_is_rest' in data['Log']:
|
|
|
|
context['request_is_rest'] = data['Log']['request_is_rest']
|
|
|
|
elif 'authkey_id' in data:
|
|
|
|
context['request_is_rest'] = True
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2024-07-01 11:12:23 +00:00
|
|
|
def getDiagnostic() -> dict:
|
2024-07-01 12:54:33 +00:00
|
|
|
global ZMQ_MESSAGE_COUNT
|
|
|
|
|
2024-07-01 12:31:29 +00:00
|
|
|
diagnostic = {}
|
|
|
|
misp_version = misp_api.getVersion()
|
|
|
|
if misp_version is None:
|
|
|
|
diagnostic['online'] = False
|
|
|
|
return diagnostic
|
|
|
|
diagnostic['version'] = misp_version
|
2024-07-01 11:12:23 +00:00
|
|
|
misp_settings = misp_api.getSettings()
|
2024-07-01 12:31:29 +00:00
|
|
|
diagnostic['settings'] = misp_settings
|
2024-07-01 12:54:33 +00:00
|
|
|
diagnostic['zmq_message_count'] = ZMQ_MESSAGE_COUNT
|
2024-07-01 12:31:29 +00:00
|
|
|
return diagnostic
|
2024-07-01 11:12:23 +00:00
|
|
|
|
|
|
|
|
2024-06-26 13:30:47 +00:00
|
|
|
# Function to forward zmq messages to Socket.IO
|
|
|
|
def forward_zmq_to_socketio():
|
2024-07-01 12:54:33 +00:00
|
|
|
global ZMQ_MESSAGE_COUNT
|
|
|
|
|
2024-06-26 13:30:47 +00:00
|
|
|
while True:
|
|
|
|
message = zsocket.recv_string()
|
|
|
|
topic, s, m = message.partition(" ")
|
|
|
|
try:
|
2024-07-01 12:54:33 +00:00
|
|
|
ZMQ_MESSAGE_COUNT += 1
|
2024-07-02 11:46:15 +00:00
|
|
|
handleMessage(topic, s, m)
|
2024-06-26 13:30:47 +00:00
|
|
|
except Exception as e:
|
2024-07-02 11:46:15 +00:00
|
|
|
logger.error('Error handling message', e)
|
2024-06-26 13:30:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
exercises_loaded = exercise_model.load_exercises()
|
|
|
|
if not exercises_loaded:
|
2024-07-02 11:46:15 +00:00
|
|
|
logger.critical('Could not load exercises')
|
2024-06-26 13:30:47 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Start the forwarding in a separate thread
|
|
|
|
eventlet.spawn_n(forward_zmq_to_socketio)
|
|
|
|
|
|
|
|
# Run the Socket.IO server
|
2024-07-01 13:02:19 +00:00
|
|
|
eventlet.wsgi.server(eventlet.listen((config.server_host, config.server_port)), app)
|