added db service

This commit is contained in:
İsmail Başaran 2016-03-14 10:58:18 +02:00
commit 66dfb8e5fc
7 changed files with 321 additions and 256 deletions

View file

@ -17,106 +17,124 @@ from base.event.EventManager import EventManager
from base.plugin.PluginManager import PluginManager
from base.task.TaskManager import TaskManager
from multiprocessing import Process
from threading import Thread
import sys,logging,queue,time,os
import sys
class AhenkDeamon(BaseDaemon):
"""docstring for AhenkDeamon"""
"""docstring for AhenkDeamon"""
def reload(self,msg):
# reload service here
pass
def reload(self,msg):
# reload service here
pass
def run(self):
print ("merhaba dunya")
def run(self):
print ("Ahenk running...")
globalscope = Scope()
globalscope.setInstance(globalscope)
globalscope = Scope()
globalscope.setInstance(globalscope)
configFilePath='/etc/ahenk/ahenk.conf'
configfileFolderPath='/etc/ahenk/config.d/'
configFilePath='/etc/ahenk/ahenk.conf'
configfileFolderPath='/etc/ahenk/config.d/'
#configuration manager must be first load
configManager = ConfigManager(configFilePath,configfileFolderPath)
config = configManager.read()
globalscope.setConfigurationManager(config)
#configuration manager must be first load
configManager = ConfigManager(configFilePath,configfileFolderPath)
config = configManager.read()
globalscope.setConfigurationManager(config)
# Logger must be second
logger = Logger()
logger.info("this is info log")
globalscope.setLogger(logger)
# Logger must be second
logger = Logger()
logger.info("[AhenkDeamon] Log was set")
globalscope.setLogger(logger)
eventManager = EventManager()
globalscope.setEventManager(eventManager)
eventManager = EventManager()
globalscope.setEventManager(eventManager)
logger.info("[AhenkDeamon] Event Manager was set")
messageManager = Messaging()
globalscope.setMessageManager(messageManager)
messageManager = Messaging()
globalscope.setMessageManager(messageManager)
logger.info("[AhenkDeamon] Message Manager was set")
pluginManager = PluginManager()
pluginManager.loadPlugins()
globalscope.setPluginManager(pluginManager)
pluginManager = PluginManager()
pluginManager.loadPlugins()
globalscope.setPluginManager(pluginManager)
logger.info("[AhenkDeamon] Plugin Manager was set")
taskManger = TaskManager()
globalscope.setTaskManager(taskManger)
taskManger = TaskManager()
globalscope.setTaskManager(taskManger)
logger.info("[AhenkDeamon] Task Manager was set")
registration=Registration()
globalscope.setRegistration(registration)
registration=Registration()
globalscope.setRegistration(registration)
logger.info("[AhenkDeamon] Registration was set")
execution_manager=ExecutionManager()
execution_manager=ExecutionManager()
globalscope.setExecutionManager(execution_manager)
logger.info("[AhenkDeamon] Execution Manager was set")
while registration.is_registered() is False:
registration.registration_request()
print("Receiver OnAir")
message_receiver = MessageReceiver()
rec_process = Process(target=message_receiver.connect_to_server)
rec_process.start()
while registration.is_registered() is False:
logger.debug("[AhenkDeamon] Attempting to register")
registration.registration_request()
logger.info("[AhenkDeamon] Ahenk is registered")
message_receiver = MessageReceiver()
rec_process = Process(target=message_receiver.connect_to_server)
rec_process.start()
logger.info("[AhenkDeamon] Receiver was set")
if registration.is_ldap_registered() is False:
logger.debug("[AhenkDeamon] Attempting to registering ldap")
registration.ldap_registration_request() #TODO bu mesaj daha kısa olabilir
logger.info("[AhenkDeamon] LDAP registration of Ahenk is completed")
#login
#message_sender=MessageSender(messageManager.login_msg(),None)
#message_sender.connect_to_server()
logger.info("[AhenkDeamon] Logining...")
message_sender=MessageSender(messageManager.login_msg(),None)
message_sender.connect_to_server()
#logout
#message_sender=MessageSender(messageManager.logout_msg(),None)
#message_sender.connect_to_server()
#rec_process.terminate()
#logout
#message_sender=MessageSender(messageManager.logout_msg(),None)
#message_sender.connect_to_server()
"""
this is must be created after message services
responseQueue = queue.Queue()
messageResponseQueue = MessageResponseQueue(responseQueue)
messageResponseQueue.setDaemon(True)
messageResponseQueue.start()
globalscope.setResponseQueue(responseQueue)
"""
#rec_process.terminate()
"""
this is must be created after message services
responseQueue = queue.Queue()
messageResponseQueue = MessageResponseQueue(responseQueue)
messageResponseQueue.setDaemon(True)
messageResponseQueue.start()
globalscope.setResponseQueue(responseQueue)
"""
if __name__ == '__main__':
pidfilePath='/var/run/ahenk.pid'
pidfilePath='/var/run/ahenk.pid'
ahenkdaemon = AhenkDeamon(pidfilePath)
ahenkdaemon = AhenkDeamon(pidfilePath)
print (sys.argv)
print (sys.argv)
if len(sys.argv) == 2:
if sys.argv[1] == "start":
print ("starting")
ahenkdaemon.run()
#print (ahenkdaemon.get_pid())
elif sys.argv[1] == 'stop':
ahenkdaemon.stop()
elif sys.argv[1] == 'restart':
ahenkdaemon.restart()
elif sys.argv[1] == 'status':
# print (status)
pass
else:
print ('Unknown command. Usage : %s start|stop|restart|status' % sys.argv[0])
sys.exit(2)
sys.exit(0)
else:
print ('Usage : %s start|stop|restart|status' % sys.argv[0])
sys.exit(2)
if len(sys.argv) == 2:
if sys.argv[1] == "start":
print ("starting")
ahenkdaemon.run()
#print (ahenkdaemon.get_pid())
elif sys.argv[1] == 'stop':
ahenkdaemon.stop()
elif sys.argv[1] == 'restart':
ahenkdaemon.restart()
elif sys.argv[1] == 'status':
# print (status)
pass
else:
print ('Unknown command. Usage : %s start|stop|restart|status' % sys.argv[0])
sys.exit(2)
sys.exit(0)
else:
print ('Usage : %s start|stop|restart|status' % sys.argv[0])
sys.exit(2)

View file

@ -19,6 +19,7 @@ class Scope(object):
self.responseQueue=None
self.registration=None
self.eventManager=None
self.executionManager=None
self.dbService=None
@ -88,8 +89,14 @@ class Scope(object):
def setEventManager(self,eventManager):
self.eventManager=eventManager
def getExecutionManager(self):
return self.executionManager
def setExecutionManager(self,executionManager):
self.executionManager=executionManager
def getDbService(self):
return self.dbService
def serDbService(self,dbService):
def setDbService(self,dbService):
self.dbService = dbService

View file

@ -1,43 +1,64 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
import sys
import logging,subprocess
import logging.config
import subprocess
from base.Scope import Scope
from base.messaging.MessageSender import MessageSender
import hashlib,json,os,stat,shutil
class ExecutionManager(object):
"""docstring for FileTransferManager"""
"""docstring for FileTransferManager"""
def __init__(self):
super(ExecutionManager, self).__init__()
def __init__(self):
super(ExecutionManager, self).__init__()
scope = Scope.getInstance()
self.config_manager = scope.getConfigurationManager()
self.event_manager = scope.getEventManager()
scope = Scope.getInstance()
self.config_manager = scope.getConfigurationManager()
self.event_manager = scope.getEventManager()
self.event_manager.register_event('EXECUTE_TASK',self.execute_task)
self.event_manager.register_event('EXECUTE_SCRIPT',self.execute_script)
self.event_manager.register_event('SEND_FILE',self.send_file)
self.event_manager.register_event('EXECUTE_TASK',self.execute_task)
self.event_manager.register_event('EXECUTE_SCRIPT',self.execute_script)
self.event_manager.register_event('REQUEST_FILE',self.request_file)
self.event_manager.register_event('MOVE_FILE',self.move_file)
def execute_task(self,arg):
print("execute_task")
def execute_task(self,arg):
#TODO
self.logger.debug('[ExecutionManager] Executing task...')
def execute_script(self,arg):
print("execute_script")
j = json.loads(arg)
msg_id =str(j['id']).lower()
file_name =str(j['filePath']).lower()
time_stamp=str(j['timestamp']).lower()
subprocess.call("/bin/sh "+self.conf_manager.get('CONNECTION', 'receivefileparam')+file_name, shell=True)
def move_file(self,arg):
default_file_path=self.config_manager.get('CONNECTION', 'receiveFileParam')
j = json.loads(arg)
#msg_id =str(j['id']).lower()
target_file_path =str(j['filepath']).lower()
file_name =str(j['filename']).lower()
self.logger.debug('[ExecutionManager] %s will be moved to %s' % file_name,target_file_path)
shutil.move(default_file_path+file_name,target_file_path+file_name)
#need to move somewhere else
def send_file(self,arg):
print("send_file")
j = json.loads(arg)
msg_id =str(j['id']).lower()
file_path =str(j['filePath']).lower()
time_stamp=str(j['timestamp']).lower()
def execute_script(self,arg):
j = json.loads(arg)
#msg_id =str(j['id']).lower()
file_path =str(j['filepath']).lower()
time_stamp=str(j['timestamp']).lower()
self.logger.debug('[ExecutionManager] Making executable file (%s) for execution' % file_path)
st = os.stat(file_path)
os.chmod(file_path, st.st_mode | stat.S_IEXEC)
subprocess.call("/bin/sh "+file_path, shell=True)
message_sender=MessageSender(None,file_path)
message_sender.connect_to_server()
#need to move somewhere else
def request_file(self,arg):
j = json.loads(arg)
#msg_id =str(j['id']).lower()
file_path =str(j['filepath']).lower()
time_stamp=str(j['timestamp']).lower()
self.logger.debug('[ExecutionManager] Requested file is '+file_path)
message_sender=MessageSender(None,file_path)
message_sender.connect_to_server()
def get_md5_file(self,fname):
self.logger.debug('[ExecutionManager] md5 hashing')
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return str(hash_md5.hexdigest())

View file

@ -2,29 +2,22 @@
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
import sys
import json, os, asyncio, slixmpp, sys
sys.path.append('../..')
import slixmpp
import asyncio
import threading
import json
from threading import Thread
from multiprocessing import Process
from slixmpp.exceptions import IqError, IqTimeout
from base.Scope import Scope
class MessageReceiver(slixmpp.ClientXMPP):
def __init__(self):
# global scope of ahenk
scope = Scope().getInstance()
# configuration_manager and logger comes from ahenk deamon
self.logger = scope.getLogger()
self.configurationManager = scope.getConfigurationManager()
self.configuration_manager = scope.getConfigurationManager()
self.event_manger=scope.getEventManager()
self.execution_manager=scope.getExecutionManager()
self.my_jid=self.get_jid_id()
self.my_pass=self.get_password()
@ -32,32 +25,33 @@ class MessageReceiver(slixmpp.ClientXMPP):
slixmpp.ClientXMPP.__init__(self, self.my_jid,self.my_pass)
self.room=None
self.receiver=self.configurationManager.get('CONNECTION', 'receiverjid')+'@'+self.configurationManager.get('CONNECTION', 'host')+'/Smack'
self.nick = self.configurationManager.get('CONNECTION', 'nick')
self.receive_file_path=self.configurationManager.get('CONNECTION', 'receiveFileParam')
#TODO get default folder path from receivefile
#self.file = open('/home/volkan/Desktop/yaz.txt', 'rb')
self.receiver=self.configuration_manager.get('CONNECTION', 'receiverjid')+'@'+self.configuration_manager.get('CONNECTION', 'host')+'/Smack'
self.nick = self.configuration_manager.get('CONNECTION', 'nick')
self.receive_file_path=self.configuration_manager.get('CONNECTION', 'receiveFileParam')
self.logger.debug('[MessageReceiver] XMPP Receiver parameters were set')
self.register_extensions()
self.add_listeners()
self.connect()
def get_jid_id(self):
if self.configurationManager.get('CONNECTION', 'uid') == "" or self.configurationManager.get('CONNECTION', 'uid') is None:
return str(self.configurationManager.get('CONNECTION', 'host')) #is user want to create connection as anonymous
if self.configuration_manager.get('CONNECTION', 'uid') == "" or self.configuration_manager.get('CONNECTION', 'uid') is None:
self.logger.debug('[MessageReceiver] Parameters were set as anonymous account')
return str(self.configuration_manager.get('CONNECTION', 'host'))
else:
return str(self.configurationManager.get('CONNECTION', 'uid')+'@'+self.configurationManager.get('CONNECTION', 'host')+'/receiver')
self.logger.debug('[MessageReceiver] Parameters were set as defined account')
return str(self.configuration_manager.get('CONNECTION', 'uid')+'@'+self.configuration_manager.get('CONNECTION', 'host')+'/receiver')
def get_password(self):
if self.configurationManager.get('CONNECTION', 'password') == "" or self.configurationManager.get('CONNECTION', 'password') is None:
if self.configuration_manager.get('CONNECTION', 'password') == "" or self.configuration_manager.get('CONNECTION', 'password') is None:
return None
else:
return str(self.configurationManager.get('CONNECTION', 'password'))
return str(self.configuration_manager.get('CONNECTION', 'password'))
def add_listeners(self):
self.add_event_handler("session_start", self.session_start)
self.add_event_handler("message", self.recv_direct_message)
self.add_event_handler("socks5_connected", self.stream_opened)
self.add_event_handler("socks5_data", self.stream_data)
self.add_event_handler("socks5_closed", self.stream_closed)
@ -66,68 +60,68 @@ class MessageReceiver(slixmpp.ClientXMPP):
self.add_event_handler("ibb_stream_data", self.stream_data)
self.add_event_handler("ibb_stream_end", self.stream_closed)
def stream_opened(self, sid):
print('stream opened')
print('So stream_id:'+str(self.stream_id))
self.logger.debug('[MessageReceiver] Event handlers were added')
def stream_opened(self, sid):
self.logger.debug('[MessageReceiver] Stream was opened. Stream id: '+str(self.stream_id))
self.file = open(self.receive_file_path+self.stream_id, 'wb')
return self.file
def stream_data(self, data):
print('stream data')
self.logger.info('Sd'+self.get_id())
#self.logger.info('Stream data.')
self.logger.debug('[MessageReceiver] Receiving file...')
self.file.write(data)
def stream_closed(self, exception):
print('stream close')
#self.logger.info('Stream closed. %s', exception)
self.logger.debug('[MessageReceiver] Stream was closed')
self.file.close()
self.set_file_name_to_md5()
def session_start(self, event):
self.logger.debug('[MessageReceiver] Session was started')
self.get_roster()
self.send_presence()
def send_direct_message(self,msg):
#need connection control
print("sending...\n"+msg)
self.logger.debug('[MessageReceiver] Sending message: '+msg)
self.send_message(mto=self.receiver,mbody=msg,mtype='normal')
def invite_auto_accept(self, inv):
self.room=inv['from']
print("(%s) invite is accepted" % str(self.room))
self.logger.debug('[MessageReceiver] (%s) invite is accepted' % str(self.room))
self.plugin['xep_0045'].joinMUC(self.room,self.nick,wait=True)
self.send_message(mto=self.room.bare,mbody="Hi all!",mtype='groupchat')
return self.room
def recv_muc_message(self, msg):#auto reply
def recv_muc_message(self, msg):
if msg['mucnick'] != self.nick:
print("%s : %s" % (str(msg['from']),str(msg['body'])) )
self.logger.debug('[MessageReceiver] %s : %s' % (str(msg['from']),str(msg['body'])) )
self.send_message(mto=msg['from'].bare,mbody="I got it, %s." % msg['mucnick'],mtype='groupchat')
else:
print("%s : %s" % (str(msg['mucnick']),str(msg['body'])))
self.logger.debug('[MessageReceiver] %s : %s' % (str(msg['mucnick']),str(msg['body'])))
def recv_direct_message(self, msg): #TODO burada mesajın type ını event olarak fırlat
def recv_direct_message(self, msg):
if msg['type'] in ('chat', 'normal'):
j = json.loads(str(msg['body']))
type =j['type']
print ("event will be fired:"+type)
self.logger.debug('[MessageReceiver] Fired event is: '+type)
self.event_manger.fireEvent(type,str(msg['body']).lower())
def connect_to_server(self):# Connect to the XMPP server and start processing XMPP stanzas.
try:
self.logger.debug('[MessageReceiver] Connecting to server as thread')
loop = asyncio.get_event_loop()
loop.run_until_complete(self.process())
#self.logger.info('Connection were established successfully')
self.logger.debug('[MessageReceiver] Connection were established successfully')
return True
except Exception as e:
print('Connection to server is failed (%s)\n' % (e.strerror))
#self.logger.error('Connection to server is failed! '+e)
self.logger.error('[MessageReceiver] Connection to server is failed! '+e)
return False
def set_file_name_to_md5(self):
self.logger.debug('[MessageReceiver] Renaming file as md5 hash')
md5_hash=self.execution_manager.get_md5_file(self.file.name)
os.rename(self.file.name,self.receive_file_path+md5_hash)
def register_extensions(self):
try:
self.register_plugin('xep_0030') # Service Discovery
@ -136,8 +130,8 @@ class MessageReceiver(slixmpp.ClientXMPP):
self.register_plugin('xep_0065', {'auto_accept': True}) # SOCKS5 Bytestreams
self.register_plugin('xep_0047', {'auto_accept': True}) # In-band Bytestreams
#self.logger.info('Extension were registered: xep_0030,xep_0045,xep_0199,xep_0065')
self.logger.debug('Extension were registered: xep_0030,xep_0045,xep_0199,xep_0065,xep_0047')
return True
except Exception as e:
#self.logger.error('Extension registration is failed!(%s)\n' % (e.errno, e.strerror))
self.logger.error('Extension registration is failed!(%s)\n' % (e.errno, e.strerror))
return False

View file

@ -2,27 +2,21 @@
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
import sys
import slixmpp, asyncio, sys
sys.path.append('../..')
import slixmpp
import asyncio
import threading
from threading import Thread
from multiprocessing import Process
from slixmpp.exceptions import IqError, IqTimeout
from base.Scope import Scope
class MessageSender(slixmpp.ClientXMPP):
def __init__(self,message,file_path):
def __init__(self,message,file_path):
# global scope of ahenk
scope = Scope().getInstance()
# logger comes from ahenk deamon
#configurationManager comes from ahenk deamon
self.logger = scope.getLogger()
self.configurationManager = scope.getConfigurationManager()
self.configuration_manager = scope.getConfigurationManager()
self.registration=scope.getRegistration()
self.event_manager = scope.getEventManager()
@ -34,15 +28,17 @@ class MessageSender(slixmpp.ClientXMPP):
self.message=None
self.file=None
self.room=None
self.receiver=self.configurationManager.get('CONNECTION', 'receiverjid')+'@'+self.configurationManager.get('CONNECTION', 'host')+'/Smack'
self.nick = self.configurationManager.get('CONNECTION', 'nick')
self.receivefile=self.configurationManager.get('CONNECTION', 'receiveFileParam')
self.receiver=self.configuration_manager.get('CONNECTION', 'receiverjid')+'@'+self.configuration_manager.get('CONNECTION', 'host')+'/Smack'
self.nick = self.configuration_manager.get('CONNECTION', 'nick')
self.receivefile=self.configuration_manager.get('CONNECTION', 'receiveFileParam')
if file_path is not None and file_path!='':
self.file=open(file_path, 'rb')
if message is not None:
self.message=message
self.logger.debug('[MessageSender] XMPP Receiver parameters were set')
self.add_listeners()
self.register_extensions()
@ -52,41 +48,48 @@ class MessageSender(slixmpp.ClientXMPP):
self.add_event_handler("socks5_connected", self.stream_opened)
self.add_event_handler("socks5_data", self.stream_data)
self.add_event_handler("socks5_closed", self.stream_closed)
self.logger.debug('[MessageSender] Event handlers were added')
def get_jid_id(self):
if self.configurationManager.get('CONNECTION', 'uid') == "" or self.configurationManager.get('CONNECTION', 'uid') is None:
return str(self.configurationManager.get('CONNECTION', 'host')) #is user want to create connection as anonymous
if self.is_anonymous()==True:
self.logger.debug('[MessageSender] Parameters were set as anonymous account')
return str(self.configuration_manager.get('CONNECTION', 'host'))
else:
return str(self.configurationManager.get('CONNECTION', 'uid')+'@'+self.configurationManager.get('CONNECTION', 'host')+'/sender')
self.logger.debug('[MessageSender] Parameters were set as defined account')
return str(self.configuration_manager.get('CONNECTION', 'uid')+'@'+self.configuration_manager.get('CONNECTION', 'host')+'/sender')
def get_password(self):
if self.configurationManager.get('CONNECTION', 'password') == "" or self.configurationManager.get('CONNECTION', 'password') is None:
if self.configuration_manager.get('CONNECTION', 'password') == "" or self.configuration_manager.get('CONNECTION', 'password') is None:
return None
else:
return str(self.configurationManager.get('CONNECTION', 'password'))
return str(self.configuration_manager.get('CONNECTION', 'password'))
def recv_direct_message(self, msg):
if msg['type'] in ('chat', 'normal'):
print ("%s : %s" % (msg['from'], msg['body']))
self.logger.debug("[MessageSender] Received message: %s -> %s" % (msg['from'], msg['body']))
self.disconnect()
self.event_manager.fireEvent('confirm_registration',str(msg['body'])) #only anonymous account can fire confirm_registration
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
@asyncio.coroutine
def session_start(self, event):
print("session start")
self.logger.debug('[MessageSender] Session was started')
self.get_roster()
self.send_presence()
if self.message is not None:
self.send_direct_message(self.message)
print("sent")
if self.file is not None:
self.logger.debug('[MessageSender] Sending file: '+self.file.name)
try:
self.logger.debug('[MessageSender] Handshaking for file transfering...')
# Open the S5B stream in which to write to.
proxy = yield from self['xep_0065'].handshake(self.receiver)
# Send the entire file.
self.logger.debug('[MessageSender] Started to streaming file...')
while True:
data = self.file.read(1048576)
if not data:
@ -95,43 +98,54 @@ class MessageSender(slixmpp.ClientXMPP):
# And finally close the stream.
proxy.transport.write_eof()
except (IqError, IqTimeout):
print('File transfer errored')
self.logger.error('[MessageSender] File transfer errored')
else:
print('File transfer finished')
self.logger.debug('[MessageSender] File transfer finished successfully')
finally:
self.file.close()
if (self.message is None and self.file is None) or self.get_password() is not None:
if (self.message is None and self.file is None) or self.get_password() is not None:#TODO password yerine ???
self.logger.debug('[MessageSender] Disconnecting...')
self.disconnect()
def stream_opened(self, sid):
#self.logger.info('Stream opened. %s', sid)
self.logger.debug('[MessageSender] Stream was opened. Stream id: '+str(self.stream_id))
self.file = open(self.receive_file_path+self.stream_id, 'wb')
return self.file
def stream_data(self, data):
#self.logger.info('Stream data.')
self.logger.debug('[MessageSender] Receiving file...')
self.file.write(data)
def stream_closed(self, exception):
#self.logger.info('Stream closed. %s', exception)
self.logger.debug('[MessageSender] Stream was closed')
self.file.close()
self.logger.debug('[MessageSender] Disconnecting...')
self.disconnect()
def send_direct_message(self,msg):
#need connection control
print("sending...\n"+msg)
self.logger.debug('[MessageSender] Sending message: '+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.
try:
self.logger.debug('[MessageSender] Connecting to server...')
self.connect()
self.process(forever=True)
#self.logger.info('Connection were established successfully')
if self.is_anonymous()==True:
self.process(forever=False)
else:
self.process(forever=True)
self.logger.debug('[MessageSender] Connection were established successfully')
return True
except Exception as e:
print('Connection to server is failed (%s)\n' % (e.strerror))
#self.logger.error('Connection to server is failed! '+e)
self.logger.error('[MessageSender] Connection to server is failed! '+e)
return False
def is_anonymous(self):
if self.configuration_manager.get('CONNECTION', 'uid') == "" or self.configuration_manager.get('CONNECTION', 'uid') is None:
return True
else:
return False
def register_extensions(self):
@ -142,8 +156,8 @@ class MessageSender(slixmpp.ClientXMPP):
self.register_plugin('xep_0065', {'auto_accept': True}) # SOCKS5 Bytestreams
self.register_plugin('xep_0047', {'auto_accept': True}) # In-band Bytestreams
#self.logger.info('Extension were registered: xep_0030,xep_0045,xep_0199,xep_0065')
self.logger.debug('[MessageSender] Extension were registered: xep_0030,xep_0045,xep_0199,xep_0065,xep_0047')
return True
except Exception as e:
#self.logger.error('Extension registration is failed!(%s)\n' % (e.errno, e.strerror))
self.logger.error('[MessageSender] Extension registration is failed!(%s)\n' % (e.errno, e.strerror))
return False

View file

@ -1,51 +1,84 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
import sys, pwd, os, datetime, json
import sys,pwd,os
import datetime,json
sys.path.append('../..')
from base.Scope import Scope
class Messaging(object):
class Messaging(object):
def __init__(self):
scope = Scope().getInstance()
self.logger = scope.getLogger()
self.configurationManager = scope.getConfigurationManager()
self.event_manger=scope.getEventManager()
self.conf_manager = scope.getConfigurationManager()
self.event_manger = scope.getEventManager()
#TODO can use sh commands for getting username and timestamp
# TODO can use sh commands or api for getting username and timestamp
def login_msg(self):
data = {}
data['type'] = 'LOGIN'
data['username'] = str(pwd.getpwuid( os.getuid() )[ 0 ])
data['username'] = str(pwd.getpwuid(os.getuid())[0])
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
json_data = json.dumps(data)
self.logger.debug('[Messaging] Login message was created')
return json_data
def logout_msg(self):
data = {}
data['type'] = 'LOGOUT'
data['username'] = str(pwd.getpwuid( os.getuid() )[ 0 ])
data['username'] = str(pwd.getpwuid(os.getuid())[0])
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
json_data = json.dumps(data)
self.logger.debug('[Messaging] Logout message was created')
return json_data
def get_policies_msg(self):
def policies_msg(self):
data = {}
data['type'] = 'GET_POLICIES'
data['username'] = str(pwd.getpwuid( os.getuid() )[ 0 ])
data['username'] = str(pwd.getpwuid(os.getuid())[0])
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
json_data = json.dumps(data)
self.logger.debug('[Messaging] Get Policies message was created')
return json_data
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"))
json_data = json.dumps(data)
self.logger.debug('[Messaging] Registration message was created')
return json_data
def ldap_registration_msg(self):
data = {}
data['type'] = 'REGISTER_LDAP'
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"))
json_data = json.dumps(data)
self.logger.debug('[Messaging] LDAP Registration message was created')
return json_data
def unregister_msg(self):
data = {}
data['type'] = 'UNREGISTER'
data['username'] = str(pwd.getpwuid( os.getuid() )[ 0 ])
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['username'] = str(pwd.getpwuid( os.getuid() )[ 0 ])
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
json_data = json.dumps(data)
self.logger.debug('[Messaging] Unregister message was created')
return json_data

View file

@ -2,19 +2,16 @@
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
from base.config.ConfigManager import ConfigManager
from base.logger.AhenkLogger import Logger
from base.Scope import Scope
from base.messaging.MessageSender import MessageSender
from uuid import getnode as get_mac
import sys,logging,json,uuid
import datetime,time,configparser
import netifaces,socket,re
import json, uuid, netifaces, socket, datetime
class Registration():
#TODO try catches
#TODO keep catches and set logs
def __init__(self):
scope = Scope().getInstance()
self.conf_manager = scope.getConfigurationManager()
@ -28,35 +25,42 @@ class Registration():
if self.conf_manager.get('REGISTRATION', 'registered')=='false':
self.re_register()
else:
self.logger.debug('[Registration] already registered')
self.logger.debug('[Registration] Ahenk already registered')
else:
self.register(True)
self.logger.debug('[Registration] ')
def registration_request(self):
self.logger.debug('[Registration] Requesting registration')
message_sender=MessageSender(self.get_registration_request_message(),None)
message_sender.connect_to_server()
def ldap_registration_request(self):
self.logger.debug('[Registration] Requesting LDAP registration')
message_sender=MessageSender(self.get_ldap_registration_request_message(),None)
message_sender.connect_to_server()
def confirm_registration(self,reg_reply): #event fire and keep here
def confirm_registration(self,reg_reply):
self.logger.debug('[Registration] Reading registration reply')
j = json.loads(reg_reply)
self.logger.info('[REGISTRATION] register reply: '+j['message'])
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 str(status)=='registered' or str(status)=='registered_without_ldap':
self.update_conf_file()
self.logger.debug('dn:'+dn)
self.update_conf_file(dn)
elif str(status)=='registration_error':
self.logger.info('[REGISTRATION] registration error')
elif str(status)=='already_registered':
self.logger.info('[REGISTRATION]already registered')
self.logger.info('[Registration] Registration is failed. New registration request will send')
self.re_register()
self.registration_request()
elif str(status)=='already_exists':
self.update_conf_file(dn)
self.logger.info('[Registration] Ahenk already registered')
def update_conf_file(self):
def update_conf_file(self,dn=None):
self.logger.debug('[Registration] Registration configuration is updating...')
if self.conf_manager.has_section('CONNECTION') and self.conf_manager.get('REGISTRATION', 'from') is not None:
self.conf_manager.set('CONNECTION', 'uid',self.conf_manager.get('REGISTRATION', 'from'))
self.conf_manager.set('CONNECTION', 'password',self.conf_manager.get('REGISTRATION', 'password'))
@ -64,11 +68,14 @@ class Registration():
self.conf_manager.set('REGISTRATION', 'registered','true')
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
self.conf_manager.write(configfile)
self.logger.debug('[Registration] Registration configuration file is updated')
def is_registered(self):
if self.conf_manager.has_section('REGISTRATION') and (self.conf_manager.get('REGISTRATION', 'registered')=='true'):
self.logger.debug('registered')
return True
else:
self.logger.debug('not registered')
return False
def is_ldap_registered(self):
@ -78,54 +85,24 @@ class Registration():
return False
def get_registration_request_message(self):
self.logger.debug('[Registration] creating registration message according to parameters of registration')
if self.conf_manager.has_section('REGISTRATION'):
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(self.conf_manager.get('REGISTRATION','timestamp'))
self.logger.debug('[Registration] json of registration message was created')
json_data = json.dumps(data)
self.logger.debug('[Registration] json converted to str')
return json_data
return self.message_manager.registration_msg()
else:
print("Registration section not found")
self.logger.error('[Registration] Registration section not found while trying to registration request')
return None
def get_ldap_registration_request_message(self):
self.logger.debug('[Registration] creating ldap registration message according to parameters of registration')
if self.conf_manager.has_section('REGISTRATION'):
data = {}
data['type'] = 'REGISTER_LDAP'
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(self.conf_manager.get('REGISTRATION','timestamp'))
self.logger.debug('[Registration] json of registration message was created')
json_data = json.dumps(data)
self.logger.debug('[Registration] json converted to str')
return json_data
return self.message_manager.ldap_registration_msg()
else:
print("Registration section not found")
self.logger.error('[Registration] Registration section not found while trying to ldap registration request')
return None
def register(self,uuid_depend_mac):
self.logger.debug('[Registration] configuration parameters of registration is checking')
if self.conf_manager.has_section('REGISTRATION'):
self.logger.debug('[Registration] REGISTRATION section is already created')
self.logger.info('[Registration] Registration section is already created')
else:
self.logger.debug('[Registration] creating REGISTRATION section')
self.logger.debug('[Registration] Creating Registration section')
self.conf_manager.add_section('REGISTRATION')
self.conf_manager.set('REGISTRATION', 'from',str(self.generate_uuid(uuid_depend_mac)))
self.conf_manager.set('REGISTRATION', 'macAddresses',str(':'.join(("%012X" % get_mac())[i:i+2] for i in range(0, 12, 2))))
@ -137,12 +114,13 @@ class Registration():
self.conf_manager.set('REGISTRATION', 'registered','false')
#TODO self.conf_manager.configurationFilePath attribute error ? READ olacak
self.logger.debug('[Registration] parameters were set up, section will write to configuration file')
self.logger.debug('[Registration] Parameters were set up, section will write to configuration file')
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
self.conf_manager.write(configfile)
self.logger.debug('[Registration] REGISTRATION section wrote to configuration file successfully')
def unregister(self):
self.logger.debug('[Registration] Ahenk is unregistering...')
if self.conf_manager.has_section('REGISTRATION'):
#TODO open this block if you want to be aware about unregistration
#message_sender=MessageSender(self.message_manager.unregister_msg(),None)
@ -153,26 +131,26 @@ class Registration():
self.conf_manager.set('CONNECTION', 'password','')
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
self.conf_manager.write(configfile)
self.logger.debug('[Registration] Ahenk is unregistered')
def re_register(self):
self.logger.debug('[Registration] Reregistrating...')
self.unregister()
self.register(False)
def generate_uuid(self,depend_mac=True):
self.logger.debug('[Registration] universal user id will be created')
if depend_mac is False:
self.logger.debug('[Registration] uuid creating randomly')
return uuid.uuid4() # make a random UUID
else:
self.logger.debug('[Registration] uuid creating depends to mac address')
self.logger.debug('[Registration] uuid creating according to mac address')
return uuid.uuid3(uuid.NAMESPACE_DNS, str(get_mac()))# make a UUID using an MD5 hash of a namespace UUID and a mac address
def generate_password(self):
return uuid.uuid4()
def get_ipAddresses(self):
self.logger.debug('[Registration] looking for network interces')
self.logger.debug('[Registration] looking for network interfaces')
ip_address=""
for interface in netifaces.interfaces():
if(str(interface) != "lo"):