# Path: beeswarm/shared/message_enum.py
# class Messages(Enum):
#     STOP = 'STOP'
#     START = 'START'
#     CONFIG = 'CONFIG'
#     # dump of all configuration elements known to the sender
#     CONFIG_FULL = 'CONFIG_FULL'
#     # mapping between clients, honeypots, capabilities and bait users
#     CONFIG_ARCHITECTURE = 'CONFIG_ARCHITECTURE'
#     BROADCAST = 'BROADCAST'
# 
#     OK = 'OK'
#     FAIL = 'FAIL'
#     # KEY DRONE_ID DRONE_PRIVATE_KEY
#     KEY = 'KEY'
#     # CERT DRONE_ID DRONE_CERT
#     CERT = 'CERT'
# 
#     # All data on an entire session, this also indicates session end.
#     SESSION_HONEYPOT = 'SESSION_HONEYPOT'
#     # Network connect to a honeypot, data format is as in SESSION_HONEYPOT, but not all fields has data.
#     SESSION_PART_HONEYPOT_SESSION_START = 'SESSION_PART_HONEYPOT_SESSION_START'
#     # Authentication attempt, format is as in self.login_attempts list SESSION_HONEYPOT
#     SESSION_PART_HONEYPOT_AUTH = 'SESSION_PART_HONEYPOT_AUTH'
#     SESSION_CLIENT = 'SESSION_CLIENT'
# 
#     SET_CONFIG_ITEM = 'SET'
#     GET_CONFIG_ITEM = 'GET'
#     GET_ZMQ_KEYS = 'GET_ZMQ_KEYS'
#     DELETE_ZMQ_KEYS = 'DELETE_ZMQ_KEYS'
#     PING = 'PING'
#     PONG = 'PING'
#     IP = 'IP'
# 
#     DRONE_WANT_CONFIG = 'DRONE_WANT_CONFIG'
#     DRONE_CONFIG = 'DRONE_CONFIG'
#     # ID USERNAME PASSWORD
#     BAIT_USER_ADD = 'BAIT_USER_ADD'
#     BAIT_USER_DELETE = 'BAIT_USER_DELETE'
#     GET_BAIT_USERS = 'GET_BAIT_USERS'
#     DRONE_DELETE = 'DRONE_DELETE'
#     DRONE_ADD = 'DRONE_ADD'
# 
#     # Session was deleted because it was matched with an existing session
#     # This happens when a bait session is matched with a honeypot session.
#     # ID_OF_DELETED_SESSION ID_OF_THE_MERGED_SESSION
#     DELETED_DUE_TO_MERGE = "DELETED_DUE_TO_MERGE"
# 
#     # A classified session is transmitted
#     # Parameters is data in json.
#     SESSION = "SESSION"
# 
#     # Database requests
#     GET_DB_STATS = 'GET_DB_STATS'
#     # TODO: Following three should have to/from params to facilitate pagination
#     GET_SESSIONS_ALL = 'GET_SESSIONS_ALL'
#     GET_SESSIONS_BAIT = 'GET_SESSIONS_BAIT'
#     GET_SESSIONS_ATTACKS = 'GET_SESSIONS_ATTACKS'
# 
#     # Param: session id
#     GET_SESSION_TRANSCRIPT = 'GET_SESSION_TRANSCRIPT'
#     # Param: session id
#     GET_SESSION_CREDENTIALS = 'GET_SESSION_CREDENTIALS'
#     # Param dronetype
#     GET_DRONE_LIST = 'GET_DRONE_LIST'
#     # Param: clientId config
#     CONFIG_DRONE = 'CONFIG_DRONE'
# 
#     # Param: clientId config
#     PING_ALL_DRONES = 'PING_ALL_DRONES'
# 
# Path: beeswarm/shared/socket_enum.py
# class SocketNames(Enum):
#     #### Sockets used on server ####
#     # All data received from drones will be published on this socket
#     DRONE_DATA = 'inproc://droneData'
#     # After sessions has been classified they will get retransmitted on this socket.
#     # TODO: Does not actually happen yet
#     PROCESSED_SESSIONS = 'inproc://processedSessionPublisher'
#     # Request / Reply to config actor
#     CONFIG_COMMANDS = 'inproc://configCommands'
#     # Data sent on this socket will be retransmitted to the correct drone, the data must be prefixed with
#     # the id of the drone.
#     DRONE_COMMANDS = 'inproc://droneCommands'
# 
# 
#     # Requests to and from the databsae
#     DATABASE_REQUESTS = 'inproc://databaseRequests'
# 
#     #### Sockets used on drones ####
#     # Drone commands received from the server will be retransmitted  on this socket.
#     SERVER_COMMANDS = 'inproc://serverCommands'
#     # All messages transmitted on this socket will get retransmitted to the server
#     SERVER_RELAY = 'inproc://serverRelay'


import json
import logging
import random
import string
import ast
import gevent
import gevent.lock
import os
import uuid
import zmq.green as zmq
import beeswarm
from flask import Flask, render_template, redirect, flash, Response, abort
from flask.ext.login import LoginManager, login_user, current_user, login_required, logout_user, UserMixin
from werkzeug.security import check_password_hash
from werkzeug.security import generate_password_hash
from wtforms import HiddenField
from flask import request
from forms import HoneypotConfigurationForm, ClientConfigurationForm, LoginForm, SettingsForm
from beeswarm.shared.message_enum import Messages
from beeswarm.shared.socket_enum import SocketNames
    bait_users = json.loads(request.data)
    for bait_user in bait_users:
        # TODO: Also validate client side
        if bait_user['username'] == '':
            continue
        send_database_request(
            '{0} {1} {2}'.format(Messages.BAIT_USER_ADD.value, bait_user['username'], bait_user['password']))
    return ''


# deletes a single bait user or a list of users
@app.route('/ws/bait_users/delete', methods=['POST'])
@login_required
def delete_bait_user():
    # list of bait user id's
    bait_users = json.loads(request.data)
    for _id in bait_users:
        send_database_request('{0} {1}'.format(Messages.BAIT_USER_DELETE.value, _id))
    return ''


@app.route('/data/sessions/<_type>', methods=['GET'])
@login_required
def data_sessions_attacks(_type):
    sessions = []
    if _type == 'all':
        sessions = send_database_request('{0}'.format(Messages.GET_SESSIONS_ALL.value))
    elif _type == 'bait_sessions':
        sessions = send_database_request('{0}'.format(Messages.GET_SESSIONS_BAIT.value))
    elif _type == 'attacks':

# Path: beeswarm/shared/message_enum.py
# class Messages(Enum):
#     STOP = 'STOP'
#     START = 'START'
#     CONFIG = 'CONFIG'
#     # dump of all configuration elements known to the sender
#     CONFIG_FULL = 'CONFIG_FULL'
#     # mapping between clients, honeypots, capabilities and bait users
#     CONFIG_ARCHITECTURE = 'CONFIG_ARCHITECTURE'
#     BROADCAST = 'BROADCAST'
# 
#     OK = 'OK'
#     FAIL = 'FAIL'
#     # KEY DRONE_ID DRONE_PRIVATE_KEY
#     KEY = 'KEY'
#     # CERT DRONE_ID DRONE_CERT
#     CERT = 'CERT'
# 
#     # All data on an entire session, this also indicates session end.
#     SESSION_HONEYPOT = 'SESSION_HONEYPOT'
#     # Network connect to a honeypot, data format is as in SESSION_HONEYPOT, but not all fields has data.
#     SESSION_PART_HONEYPOT_SESSION_START = 'SESSION_PART_HONEYPOT_SESSION_START'
#     # Authentication attempt, format is as in self.login_attempts list SESSION_HONEYPOT
#     SESSION_PART_HONEYPOT_AUTH = 'SESSION_PART_HONEYPOT_AUTH'
#     SESSION_CLIENT = 'SESSION_CLIENT'
# 
#     SET_CONFIG_ITEM = 'SET'
#     GET_CONFIG_ITEM = 'GET'
#     GET_ZMQ_KEYS = 'GET_ZMQ_KEYS'
#     DELETE_ZMQ_KEYS = 'DELETE_ZMQ_KEYS'
#     PING = 'PING'
#     PONG = 'PING'
#     IP = 'IP'
# 
#     DRONE_WANT_CONFIG = 'DRONE_WANT_CONFIG'
#     DRONE_CONFIG = 'DRONE_CONFIG'
#     # ID USERNAME PASSWORD
#     BAIT_USER_ADD = 'BAIT_USER_ADD'
#     BAIT_USER_DELETE = 'BAIT_USER_DELETE'
#     GET_BAIT_USERS = 'GET_BAIT_USERS'
#     DRONE_DELETE = 'DRONE_DELETE'
#     DRONE_ADD = 'DRONE_ADD'
# 
#     # Session was deleted because it was matched with an existing session
#     # This happens when a bait session is matched with a honeypot session.
#     # ID_OF_DELETED_SESSION ID_OF_THE_MERGED_SESSION
#     DELETED_DUE_TO_MERGE = "DELETED_DUE_TO_MERGE"
# 
#     # A classified session is transmitted
#     # Parameters is data in json.
#     SESSION = "SESSION"
# 
#     # Database requests
#     GET_DB_STATS = 'GET_DB_STATS'
#     # TODO: Following three should have to/from params to facilitate pagination
#     GET_SESSIONS_ALL = 'GET_SESSIONS_ALL'
#     GET_SESSIONS_BAIT = 'GET_SESSIONS_BAIT'
#     GET_SESSIONS_ATTACKS = 'GET_SESSIONS_ATTACKS'
# 
#     # Param: session id
#     GET_SESSION_TRANSCRIPT = 'GET_SESSION_TRANSCRIPT'
#     # Param: session id
#     GET_SESSION_CREDENTIALS = 'GET_SESSION_CREDENTIALS'
#     # Param dronetype
#     GET_DRONE_LIST = 'GET_DRONE_LIST'
#     # Param: clientId config
#     CONFIG_DRONE = 'CONFIG_DRONE'
# 
#     # Param: clientId config
#     PING_ALL_DRONES = 'PING_ALL_DRONES'
# 
# Path: beeswarm/shared/socket_enum.py
# class SocketNames(Enum):
#     #### Sockets used on server ####
#     # All data received from drones will be published on this socket
#     DRONE_DATA = 'inproc://droneData'
#     # After sessions has been classified they will get retransmitted on this socket.
#     # TODO: Does not actually happen yet
#     PROCESSED_SESSIONS = 'inproc://processedSessionPublisher'
#     # Request / Reply to config actor
#     CONFIG_COMMANDS = 'inproc://configCommands'
#     # Data sent on this socket will be retransmitted to the correct drone, the data must be prefixed with
#     # the id of the drone.
#     DRONE_COMMANDS = 'inproc://droneCommands'
# 
# 
#     # Requests to and from the databsae
#     DATABASE_REQUESTS = 'inproc://databaseRequests'
# 
#     #### Sockets used on drones ####
#     # Drone commands received from the server will be retransmitted  on this socket.
#     SERVER_COMMANDS = 'inproc://serverCommands'
#     # All messages transmitted on this socket will get retransmitted to the server
#     SERVER_RELAY = 'inproc://serverRelay'


# Path: beeswarm/server/webapp/app.py
import json
import logging
import random
import string
import ast
import gevent
import gevent.lock
import os
import uuid
import zmq.green as zmq
import beeswarm
from flask import Flask, render_template, redirect, flash, Response, abort
from flask.ext.login import LoginManager, login_user, current_user, login_required, logout_user, UserMixin
from werkzeug.security import check_password_hash
from werkzeug.security import generate_password_hash
from wtforms import HiddenField
from flask import request
from forms import HoneypotConfigurationForm, ClientConfigurationForm, LoginForm, SettingsForm
from beeswarm.shared.message_enum import Messages
from beeswarm.shared.socket_enum import SocketNames

    bait_users = json.loads(request.data)
    for bait_user in bait_users:
        # TODO: Also validate client side
        if bait_user['username'] == '':
            continue
        send_database_request(
            '{0} {1} {2}'.format(Messages.BAIT_USER_ADD.value, bait_user['username'], bait_user['password']))
    return ''


# deletes a single bait user or a list of users
@app.route('/ws/bait_users/delete', methods=['POST'])
@login_required
def delete_bait_user():
    # list of bait user id's
    bait_users = json.loads(request.data)
    for _id in bait_users:
        send_database_request('{0} {1}'.format(Messages.BAIT_USER_DELETE.value, _id))
    return ''


@app.route('/data/sessions/<_type>', methods=['GET'])
@login_required
def data_sessions_attacks(_type):
    sessions = []
    if _type == 'all':
        sessions = send_database_request('{0}'.format(Messages.GET_SESSIONS_ALL.value))
    elif _type == 'bait_sessions':
        sessions = send_database_request('{0}'.format(Messages.GET_SESSIONS_BAIT.value))
    elif _type == 'attacks':

        sessions = send_database_request('{0}'.format(Messages.GET_SESSIONS_ATTACKS.value))