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))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.