ssl connection feature added

This commit is contained in:
Volkan Şahin 2016-08-24 18:02:16 +03:00
parent d358bc87fa
commit 9875638e6c
2 changed files with 20 additions and 15 deletions

View file

@ -6,7 +6,7 @@ import json
import sys import sys
from sleekxmpp import ClientXMPP from sleekxmpp import ClientXMPP
from base.Scope import Scope from base.scope import Scope
sys.path.append('../..') sys.path.append('../..')
@ -35,6 +35,11 @@ class AnonymousMessenger(ClientXMPP):
if self.receiver_resource: if self.receiver_resource:
self.receiver += '/' + self.receiver_resource self.receiver += '/' + self.receiver_resource
if self.configuration_manager.get('CONNECTION', 'use_tls').strip().lower() == 'true':
self.use_tls = True
else:
self.use_tls = False
self.logger.debug('[AnonymousMessenger] XMPP Receiver parameters were set') self.logger.debug('[AnonymousMessenger] XMPP Receiver parameters were set')
self.add_listeners() self.add_listeners()
@ -61,24 +66,24 @@ class AnonymousMessenger(ClientXMPP):
self.logger.debug('[AnonymousMessenger] Extension were registered: xep_0030,xep_0199') self.logger.debug('[AnonymousMessenger] Extension were registered: xep_0030,xep_0199')
return True return True
except Exception as e: except Exception as e:
self.logger.error('[AnonymousMessenger] Extension registration is failed! Error Message: {}'.format(str(e))) self.logger.error('[AnonymousMessenger] Extension registration is failed! Error Message: {0}'.format(str(e)))
return False return False
def connect_to_server(self): def connect_to_server(self):
try: try:
self.logger.debug('[AnonymousMessenger] Connecting to server...') self.logger.debug('[AnonymousMessenger] Connecting to server...')
self['feature_mechanisms'].unencrypted_plain = True self['feature_mechanisms'].unencrypted_plain = True
self.connect((self.host, self.port), use_tls=False) self.connect((self.host, self.port), use_tls=self.use_tls)
self.process(block=True) self.process(block=True)
self.logger.debug('[AnonymousMessenger] Connection were established successfully') self.logger.debug('[AnonymousMessenger] Connection were established successfully')
return True return True
except Exception as e: except Exception as e:
self.logger.error('[AnonymousMessenger] Connection to server is failed! Error Message: {}'.format(str(e))) self.logger.error('[AnonymousMessenger] Connection to server is failed! Error Message: {0}'.format(str(e)))
return False return False
def recv_direct_message(self, msg): def recv_direct_message(self, msg):
if msg['type'] in ['normal']: if msg['type'] in ['normal']:
self.logger.debug('[AnonymousMessenger] ---------->Received message: {}'.format(str(msg['body']))) self.logger.debug('[AnonymousMessenger] ---------->Received message: {0}'.format(str(msg['body'])))
self.logger.debug('[AnonymousMessenger] Disconnecting...') self.logger.debug('[AnonymousMessenger] Disconnecting...')
self.disconnect() self.disconnect()
j = json.loads(str(msg['body'])) j = json.loads(str(msg['body']))
@ -86,5 +91,5 @@ class AnonymousMessenger(ClientXMPP):
self.event_manager.fireEvent(message_type, str(msg['body'])) self.event_manager.fireEvent(message_type, str(msg['body']))
def send_direct_message(self, msg): def send_direct_message(self, msg):
self.logger.debug('[AnonymousMessenger] <<--------Sending message: {}'.format(msg)) self.logger.debug('[AnonymousMessenger] <<--------Sending message: {0}'.format(msg))
self.send_message(mto=self.receiver, mbody=msg, mtype='normal') self.send_message(mto=self.receiver, mbody=msg, mtype='normal')

View file

@ -7,7 +7,7 @@ import sys
from sleekxmpp import ClientXMPP from sleekxmpp import ClientXMPP
from base.Scope import Scope from base.scope import Scope
sys.path.append('../..') sys.path.append('../..')
@ -34,7 +34,7 @@ class Messenger(ClientXMPP):
self.hostname = self.configuration_manager.get('CONNECTION', 'host') self.hostname = self.configuration_manager.get('CONNECTION', 'host')
self.receiver_resource = self.configuration_manager.get('CONNECTION', 'receiverresource') self.receiver_resource = self.configuration_manager.get('CONNECTION', 'receiverresource')
if self.configuration_manager.get('CONNECTION', 'use_tls').split().lower() == 'true': if self.configuration_manager.get('CONNECTION', 'use_tls').strip().lower() == 'true':
self.use_tls = True self.use_tls = True
else: else:
self.use_tls = False self.use_tls = False
@ -59,7 +59,7 @@ class Messenger(ClientXMPP):
self.logger.debug('[Messenger]Extension were registered: xep_0030,xep_0199') self.logger.debug('[Messenger]Extension were registered: xep_0030,xep_0199')
return True return True
except Exception as e: except Exception as e:
self.logger.error('[Messenger]Extension registration is failed! Error Message: {}'.format(str(e))) self.logger.error('[Messenger]Extension registration is failed! Error Message: {0}'.format(str(e)))
return False return False
def add_listeners(self): def add_listeners(self):
@ -78,7 +78,7 @@ class Messenger(ClientXMPP):
self.logger.debug('[Messenger] Connection were established successfully') self.logger.debug('[Messenger] Connection were established successfully')
return True return True
except Exception as e: except Exception as e:
self.logger.error('[Messenger] Connection to server is failed! Error Message: {}'.format(str(e))) self.logger.error('[Messenger] Connection to server is failed! Error Message: {0}'.format(str(e)))
return False return False
def session_end(self): def session_end(self):
@ -91,20 +91,20 @@ class Messenger(ClientXMPP):
def send_direct_message(self, msg): def send_direct_message(self, msg):
try: try:
self.logger.debug('[Messenger] <<--------Sending message: {}'.format(msg)) self.logger.debug('[Messenger] <<--------Sending message: {0}'.format(msg))
self.send_message(mto=self.receiver, mbody=msg, mtype='normal') self.send_message(mto=self.receiver, mbody=msg, mtype='normal')
except Exception as e: except Exception as e:
self.logger.error( self.logger.error(
'[Messenger] A problem occurred while sending direct message. Error Message: {}'.format(str(e))) '[Messenger] A problem occurred while sending direct message. Error Message: {0}'.format(str(e)))
def recv_direct_message(self, msg): def recv_direct_message(self, msg):
if msg['type'] in ['normal']: if msg['type'] in ['normal']:
self.logger.debug('[Messenger] ---------->Received message: {}'.format(str(msg['body']))) self.logger.debug('[Messenger] ---------->Received message: {0}'.format(str(msg['body'])))
try: try:
j = json.loads(str(msg['body'])) j = json.loads(str(msg['body']))
message_type = j['type'] message_type = j['type']
self.event_manger.fireEvent(message_type, str(msg['body'])) self.event_manger.fireEvent(message_type, str(msg['body']))
self.logger.debug('[Messenger] Fired event is: {}'.format(message_type)) self.logger.debug('[Messenger] Fired event is: {0}'.format(message_type))
except Exception as e: except Exception as e:
self.logger.error( self.logger.error(
'[Messenger] A problem occurred while keeping message. Error Message: {}'.format(str(e))) '[Messenger] A problem occurred while keeping message. Error Message: {0}'.format(str(e)))