1. imalse
  2. Untitled project
  3. imalse

Source

imalse / scenario / spamming / server_cmd.py

"""

This file contains the Server Command Meta Description for the spamming
scenario.

"""

import jsonpickle

from core.nodes.server import ServerCMD as SCMD
from core.cmd.command import NewAddressCmd, EchoCmd
from scenario.spamming.config import ServerDescription as Description

class ServerCMD(SCMD):
    
    """This class is a subclass of ServerCMD."""
    
    def __init__(self, desc=None):        
        """Initialize"""
        
        if desc == None:
            desc = Description()
        
        SCMD.__init__(self, desc)
        
    def recv_request_conn(self, client_sock, address):
        """Callback when connection request from a new bot is received."""
        self.logger.info('receive request from addr: %s'%(str(address)))
        self.node.send(client_sock, 'connect_ack')        
        self.node.recv(client_sock, 512, self.node.dispatcher, threaded=True)
        
        if self.node.master_sock != None:            
            self.send_new_address(self.node.master_sock, address)
     
    def verify_master(self, sock, data):
        """Verify the identity of the bot master."""
        if data.password == self.desc.master_password and self.node.master_sock == None:
            self.logger.info( 'bot master password verified' )
            self.node.set_master_sock(sock)
            self.node.send(sock, jsonpickle.encode(EchoCmd()))
            
            self.node.sleep(5)
            
            self.send_known_addresses(sock)            
    
    def send_known_addresses(self, socket):
        """Send known addresses of bots to the botmaster."""        
        if len(self.node.client_socks) > 0:
            command = NewAddressCmd()
            values = self.node.sockets.values()        
            command.addresses = [val['addr'] for val in values if val['type'] == 'client']            
            self.node.send(socket, jsonpickle.encode(command))
    
    def send_new_address(self, socket, address): 
        """Send a single address of a new bot to the botmaster."""
        command = NewAddressCmd()
        command.addresses = [address]        
        self.node.send(socket, jsonpickle.encode(command))