Commit ff86dcc1 authored by Antoine Millet's avatar Antoine Millet
Browse files

Added a first server internal job: KillOldCli.

parent c2c27dc1
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ DEFAULT_CONFIGURATION = {
    'interface': '127.0.0.1',
    'ssl_cert': None,
    'ssl_key': None,
    'maxcon': 600,
    'maxidle': 30,
}

def run_server(options):
@@ -62,8 +64,12 @@ def run_server(options):
    handler.setFormatter(fmt)
    logger.addHandler(handler)

    server = CCServer(conf_dir=options['account_db'], port=int(options['port']),
                      address=options['interface'], keyfile=options['ssl_key'],
    server = CCServer(conf_dir=options['account_db'],
                      maxcon=int(options['maxcon']),
                      maxidle=int(options['maxidle']),
                      port=int(options['port']),
                      address=options['interface'],
                      keyfile=options['ssl_key'],
                      certfile=options['ssl_cert'])

    def shutdown_handler(signum, frame):
+4 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ class CCServer(object):
    # These tags are reserved and cannot be setted by an user:
    RESERVED_TAGS = ('id', 'a', 'r', 'close', 'con', 'ip')

    def __init__(self, conf_dir, certfile=None, keyfile=None,
    def __init__(self, conf_dir, maxcon, maxidle, certfile=None, keyfile=None,
                 address='0.0.0.0', port=1984):

        # Dict containing all connected accounts, the key is the login of
@@ -72,6 +72,9 @@ class CCServer(object):
        # Register accounts on the database:
        self._update_accounts()

        # Running server internal jobs:
        self.jobs.create('kill_oldcli', maxcon=maxcon, maxidle=maxidle)

    def _update_accounts(self):
        '''
        Update the database with accounts.
+20 −0
Original line number Diff line number Diff line
@@ -26,6 +26,9 @@ class CCClient(object):
        # The date of connection of the client:
        self._connection_date = datetime.now()

        # The date of the last action:
        self._last_action = datetime.now()

        # Jobs lock for this client:
        self.lock = RLock()

@@ -39,6 +42,23 @@ class CCClient(object):
        dt = datetime.now() - self._connection_date
        return dt.seconds + dt.days * 86400

    def get_idle(self):
        '''
        Get the idle time of the client connection in seconds.

        :return: idle of the client
        '''
        
        dt = datetime.now() - self._last_action
        return dt.seconds + dt.days * 86400

    def top(self):
        '''
        Reset the last_action date to now.
        '''

        self._last_action = datetime.now()

    def get_ip(self):
        peer = self.connection.getpeername()
        return ':'.join(peer.split(':')[:-1])
+33 −0
Original line number Diff line number Diff line
@@ -169,6 +169,38 @@ class KillClientJob(BaseJob):
        self.manager.server.kill(account)


class KillOldCliJob(BaseJob):

    '''
    Typically an hidden job used to kill clients who are connected/idle since
    too much time.

    Mandatory items:
     * maxcon: maximum connection time in minutes
     * maxidle: maximum idle time in minutes

    Optional items:
     * delay: delay in secondes between two checks (default 1m)
    '''

    DEFAULT_DELAY = 60

    def job(self):
        maxcon = self.get('maxcon')
        assert maxcon is not None, 'maxcon is None'
        maxidle = self.get('maxidle')
        assert maxidle is not None, 'maxidle is None'
        delay = self.get('delay', self.DEFAULT_DELAY)
        
        while True:
            self.checkpoint()
            for client in self.manager.server.iter_connected_role('cli'):
                if client.get_uptime() > (maxcon * 60):
                    self.manager.server.kill(client.login)
                #TODO: handle idleing.
            time.sleep(delay)


class JobsManager(object):

    '''
@@ -179,6 +211,7 @@ class JobsManager(object):

    JOBS_TYPES = {
        'kill': KillClientJob,
        'kill_oldcli': KillOldCliJob,
    }

    def __init__(self, server):
+6 −0
Original line number Diff line number Diff line
@@ -28,3 +28,9 @@ interface = 0.0.0.0

# The path to the account database (mandatory):
account_db = /var/lib/cc-server/

# Max connection time for cli:
#maxcon=600

# Max idle time for cli:
#maxcon=30