# 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/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'


import uuid
import logging
import json
import zmq.green as zmq
import beeswarm
from datetime import datetime
from gevent.lock import BoundedSemaphore
from beeswarm.shared.socket_enum import SocketNames
from beeswarm.shared.message_enum import Messages
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.




logger = logging.getLogger(__name__)


class BaseSession(object):

    socket = None
    socketLock = BoundedSemaphore(1)

    def __init__(self, protocol, source_ip=None, source_port=None, destination_ip=None, destination_port=None):
        self.id = uuid.uuid4()
        self.source_ip = source_ip
        self.source_port = source_port
        self.protocol = protocol
        self.destination_ip = destination_ip
        self.destination_port = destination_port

# 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/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/models/base_session.py
import uuid
import logging
import json
import zmq.green as zmq
import beeswarm
from datetime import datetime
from gevent.lock import BoundedSemaphore
from beeswarm.shared.socket_enum import SocketNames
from beeswarm.shared.message_enum import Messages

# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.




logger = logging.getLogger(__name__)


class BaseSession(object):

    socket = None
    socketLock = BoundedSemaphore(1)

    def __init__(self, protocol, source_ip=None, source_port=None, destination_ip=None, destination_port=None):
        self.id = uuid.uuid4()
        self.source_ip = source_ip
        self.source_port = source_port
        self.protocol = protocol
        self.destination_ip = destination_ip
        self.destination_port = destination_port

        self.timestamp = datetime.utcnow()