From 85395033d497bb204171b59b9d46a90d81b8f224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Fri, 8 Apr 2016 15:54:26 +0300 Subject: [PATCH] registartion process fixed --- opt/ahenk/ahenkd.py | 17 +++++++++----- opt/ahenk/base/messaging/AnonymousMessager.py | 9 +++++--- opt/ahenk/base/messaging/Messaging.py | 14 ++++++----- opt/ahenk/base/registration/Registration.py | 23 +++++++++++-------- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index 9988aae..3573bab 100755 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -118,10 +118,15 @@ class AhenkDeamon(BaseDaemon): def check_registration(self): # TODO restrict number of attemption - while Scope.getInstance().getRegistration().is_registered() is False: - print('registration need') - Scope.getInstance().getLogger().debug('[AhenkDeamon] Attempting to register') - Scope.getInstance().getRegistration().registration_request() + try: + while Scope.getInstance().getRegistration().is_registered() is False: + print('registration need') + Scope.getInstance().getLogger().debug('[AhenkDeamon] Attempting to register') + Scope.getInstance().getRegistration().registration_request() + except Exception as e: + print(str(e)) + raise + def reload_plugins(self): @@ -176,13 +181,13 @@ class AhenkDeamon(BaseDaemon): self.init_task_manager() logger.info('[AhenkDeamon] Task Manager was set') - #self.init_registration() + self.init_registration() logger.info('[AhenkDeamon] Registration was set') self.init_execution_manager() logger.info('[AhenkDeamon] Execution Manager was set') - #self.check_registration() + self.check_registration() logger.info('[AhenkDeamon] Ahenk is registered') messager = self.init_messager() diff --git a/opt/ahenk/base/messaging/AnonymousMessager.py b/opt/ahenk/base/messaging/AnonymousMessager.py index 59763bb..7d5876a 100644 --- a/opt/ahenk/base/messaging/AnonymousMessager.py +++ b/opt/ahenk/base/messaging/AnonymousMessager.py @@ -5,6 +5,7 @@ import asyncio import slixmpp import sys +import json sys.path.append('../..') from slixmpp.exceptions import IqError, IqTimeout @@ -49,12 +50,13 @@ class AnonymousMessager(slixmpp.ClientXMPP): def recv_direct_message(self, msg): if msg['type'] in ('chat', 'normal'): + print('ANON<---'+ msg['body']) self.logger.debug("[MessageSender] Received message: %s -> %s" % (msg['from'], msg['body'])) self.disconnect() self.logger.debug('[MessageSender] Disconnecting...') - self.logger.debug('[MessageSender] Fired event is: confirm_registration') - self.event_manager.fireEvent('confirm_registration', str(msg['body'])) - ##TODO type fire -- only anonymous account can fire confirm_registration + j = json.loads(str(msg['body'])) + message_type = j['type'] + self.event_manager.fireEvent(message_type, str(msg['body'])) @asyncio.coroutine def session_start(self, event): @@ -104,6 +106,7 @@ class AnonymousMessager(slixmpp.ClientXMPP): def send_direct_message(self, msg): self.logger.debug('[MessageSender] Sending message: ' + msg) + print('ANON-->'+msg) self.send_message(mto=self.receiver, mbody=msg, mtype='normal') def connect_to_server(self): # Connect to the XMPP server and start processing XMPP stanzas. diff --git a/opt/ahenk/base/messaging/Messaging.py b/opt/ahenk/base/messaging/Messaging.py index 4f7dfd7..f934b90 100644 --- a/opt/ahenk/base/messaging/Messaging.py +++ b/opt/ahenk/base/messaging/Messaging.py @@ -100,12 +100,14 @@ class Messaging(object): def registration_msg(self): data = {} data['type'] = 'REGISTER' - data['from'] = str(self.conf_manager.get('REGISTRATION', 'from')) - data['password'] = str(self.conf_manager.get('REGISTRATION', 'password')) - data['macAddresses'] = str(self.conf_manager.get('REGISTRATION', 'macAddresses')) - data['ipAddresses'] = str(self.conf_manager.get('REGISTRATION', 'ipAddresses')) - data['hostname'] = str(self.conf_manager.get('REGISTRATION', 'hostname')) - data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M")) + data['from'] = self.db_service.select_one_result('registration','jid',' 1=1')#str(self.conf_manager.get('REGISTRATION', 'from')) + data['password'] = self.db_service.select_one_result('registration','password',' 1=1') + params = self.db_service.select_one_result('registration','params',' 1=1') + json_params = json.loads(str(params)) + data['macAddresses'] = json_params['macAddresses'] + data['ipAddresses'] = json_params['ipAddresses'] + data['hostname'] = json_params['hostname'] + data['timestamp'] = self.db_service.select_one_result('registration','timestamp',' 1=1') json_data = json.dumps(data) self.logger.debug('[Messaging] Registration message was created') return json_data diff --git a/opt/ahenk/base/registration/Registration.py b/opt/ahenk/base/registration/Registration.py index 7dff7f9..369841d 100644 --- a/opt/ahenk/base/registration/Registration.py +++ b/opt/ahenk/base/registration/Registration.py @@ -20,8 +20,11 @@ class Registration(): self.message_manager = scope.getMessageManager() self.event_manager = scope.getEventManager() self.messager = scope.getMessager() + self.conf_manager = scope.getConfigurationManager() self.db_service = scope.getDbService() + self.event_manager.register_event('REGISTRATION_RESPONSE', self.registration_process) + if self.is_registered(): self.logger.debug('[Registration] Ahenk already registered') else: @@ -36,33 +39,33 @@ class Registration(): self.logger.debug('[Registration] Requesting LDAP registration') self.messager.send_Direct_message(self.message_manager.ldap_registration_msg()) - def confirm_registration(self, reg_reply): + def registration_process(self, reg_reply): self.logger.debug('[Registration] Reading registration reply') j = json.loads(reg_reply) self.logger.debug('[Registration]' + j['message']) status = str(j['status']).lower() dn = str(j['agentDn']).lower() + self.logger.debug('[Registration] Registration status: ' + str(status)) - if 'registered' == str(status) or 'registered_without_ldap' == str(status): + if 'already_exists' == str(status) or 'registered' == str(status) or 'registered_without_ldap' == str(status): self.logger.debug('dn:' + dn) - self.update_conf_file(dn) + self.update_registration_attrs(dn) elif 'registration_error' == str(status): self.logger.info('[Registration] Registration is failed. New registration request will send') self.re_register() self.registration_request() - elif 'already_exists' == str(status): - self.update_conf_file(dn) - self.logger.info('[Registration] Ahenk already registered') + else: + self.logger.error('[Registration] Bad message type of registration response ') - def update_conf_file(self, dn=None): + def update_registration_attrs(self, dn=None): self.logger.debug('[Registration] Registration configuration is updating...') self.db_service.update('registration', ['dn', 'registered'], [dn, 1], ' registered = 0') if self.conf_manager.has_section('CONNECTION'): - self.conf_manager.set('CONNECTION', 'uid',self.conf_manager.get('REGISTRATION', 'from')) - self.conf_manager.set('CONNECTION', 'password',self.conf_manager.get('REGISTRATION', 'password')) - #TODO get file path? + self.conf_manager.set('CONNECTION', 'uid', self.db_service.select_one_result('registration', 'jid', ' registered=1')) + self.conf_manager.set('CONNECTION', 'password', self.db_service.select_one_result('registration', 'password', ' registered=1')) + # TODO get file path? with open('/etc/ahenk/ahenk.conf', 'w') as configfile: self.conf_manager.write(configfile) self.logger.debug('[Registration] Registration configuration file is updated')