From 65f4b757ea7f330e019dc3520b514820d6de1445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 23 Mar 2016 17:15:27 +0200 Subject: [PATCH] PEP8 Standard -> https://www.python.org/dev/peps/pep-0008/ --- opt/ahenk/ahenkd.py | 111 +++++---- opt/ahenk/base/Scope.py | 63 +++-- opt/ahenk/base/config/ConfigManager.py | 53 ++-- opt/ahenk/base/database/AhenkDbService.py | 66 ++--- opt/ahenk/base/deamon/BaseDeamon.py | 227 +++++++++--------- opt/ahenk/base/event/EventBase.py | 4 +- opt/ahenk/base/event/EventManager.py | 9 +- opt/ahenk/base/execution/ExecutionManager.py | 104 ++++---- opt/ahenk/base/logger/AhenkLogger.py | 38 +-- opt/ahenk/base/logging/AhenkLogger.py | 38 +-- opt/ahenk/base/messaging/AnonymousMessager.py | 56 ++--- .../base/messaging/MessageResponseQueue.py | 9 +- opt/ahenk/base/messaging/Messager.py | 108 +++++---- opt/ahenk/base/messaging/Messaging.py | 32 +-- opt/ahenk/base/model/MessageFactory.py | 5 +- opt/ahenk/base/model/MessageType.py | 15 +- opt/ahenk/base/model/Plugin.py | 4 +- opt/ahenk/base/model/Policy.py | 11 +- opt/ahenk/base/model/Profile.py | 8 +- opt/ahenk/base/model/Task.py | 11 +- opt/ahenk/base/plugin/AbstractCommand.py | 2 + opt/ahenk/base/plugin/AbstractPlugin.py | 11 +- opt/ahenk/base/plugin/Plugin.py | 13 +- opt/ahenk/base/plugin/PluginManager.py | 33 +-- opt/ahenk/base/plugin/PluginQueue.py | 1 + opt/ahenk/base/registration/Registration.py | 13 +- opt/ahenk/base/task/TaskInQueue.py | 5 +- opt/ahenk/base/task/TaskJob.py | 3 + opt/ahenk/base/task/TaskManager.py | 4 +- 29 files changed, 554 insertions(+), 503 deletions(-) diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index afd85e1..672ed4d 100755 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -3,28 +3,35 @@ # Author: İsmail BAŞARAN # Author: Volkan Şahin -from base.config.ConfigManager import ConfigManager -from base.deamon.BaseDeamon import BaseDaemon -from base.logger.AhenkLogger import Logger +import configparser +import os +import queue +import signal +import sys +import threading +import time + from base.Scope import Scope -from base.messaging.Messaging import Messaging -from base.messaging.Messager import Messager -from base.execution.ExecutionManager import ExecutionManager -#from base.registration.Registration import Registration -from base.messaging.MessageResponseQueue import MessageResponseQueue -from base.event.EventManager import EventManager -from base.plugin.PluginManager import PluginManager -from base.task.TaskManager import TaskManager +from base.config.ConfigManager import ConfigManager from base.database.AhenkDbService import AhenkDbService -import threading, time, sys, os, signal, configparser,queue +from base.deamon.BaseDeamon import BaseDaemon +from base.event.EventManager import EventManager +from base.execution.ExecutionManager import ExecutionManager +from base.logger.AhenkLogger import Logger +from base.messaging.MessageResponseQueue import MessageResponseQueue +from base.messaging.Messager import Messager +from base.messaging.Messaging import Messaging +from base.plugin.PluginManager import PluginManager +from base.registration.Registration import Registration +from base.task.TaskManager import TaskManager pidfilePath = '/var/run/ahenk.pid' + class AhenkDeamon(BaseDaemon): """docstring for AhenkDeamon""" - - def reload(self,msg): + def reload(self, msg): # reload service here pass @@ -34,11 +41,11 @@ class AhenkDeamon(BaseDaemon): 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) + # configuration manager must be first load + configManager = ConfigManager(configFilePath, configfileFolderPath) config = configManager.read() globalscope.setConfigurationManager(config) @@ -51,7 +58,7 @@ class AhenkDeamon(BaseDaemon): globalscope.setEventManager(eventManager) logger.info('[AhenkDeamon] Event Manager was set') - db_service=AhenkDbService() + db_service = AhenkDbService() db_service.connect() db_service.initialize_table() globalscope.setDbService(db_service) @@ -70,19 +77,18 @@ class AhenkDeamon(BaseDaemon): globalscope.setTaskManager(taskManager) logger.info('[AhenkDeamon] Task Manager was set') - #registration=Registration() - #globalscope.setRegistration(registration) - #logger.info('[AhenkDeamon] Registration was set') + 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') - - #TODO restrict number of attemption - #while registration.is_registered() is False: - # logger.debug('[AhenkDeamon] Attempting to register') - # registration.registration_request() + # TODO restrict number of attemption + while registration.is_registered() is False: + logger.debug('[AhenkDeamon] Attempting to register') + registration.registration_request() logger.info('[AhenkDeamon] Ahenk is registered') @@ -90,34 +96,34 @@ class AhenkDeamon(BaseDaemon): messanger_thread = threading.Thread(target=messager.connect_to_server) messanger_thread.start() - while(messager.is_connected() is False): + while (messager.is_connected() is False): time.sleep(1) globalscope.setMessager(messager) logger.info('[AhenkDeamon] Messager was set') - #if registration.is_ldap_registered() is False: + # if registration.is_ldap_registered() is False: # logger.debug('[AhenkDeamon] Attempting to registering ldap') # registration.ldap_registration_request() #TODO work on message logger.info('[AhenkDeamon] LDAP registration of Ahenk is completed') - #TODO############### + # TODO############### cnfg = configparser.ConfigParser() cnfg.add_section('PID') - cnfg.set('PID','pid_number',str(os.getpid())) + cnfg.set('PID', 'pid_number', str(os.getpid())) with open(pidfilePath, 'w') as config_file: cnfg.write(config_file) - #TODO############## + # TODO############## try: signal.signal(signal.SIGALRM, self.signal_handler) logger.info('[AhenkDeamon] Signal handler is set up') except Exception as e: - logger.error('[AhenkDeamon] Signal handler could not set up :'+e.errno+'-'+e.strerror) + logger.error('[AhenkDeamon] Signal handler could not set up :' + e.errno + '-' + e.strerror) - #messager.send_direct_message('test') + messager.send_direct_message('test') responseQueue = queue.Queue() messageResponseQueue = MessageResponseQueue(responseQueue) @@ -128,23 +134,21 @@ class AhenkDeamon(BaseDaemon): while True: time.sleep(1) - #request policies - #logger.info('[AhenkDeamon] Requesting policies...') - #messager.send_direct_message(messageManager.policy_request_msg()) + # request policies + # logger.info('[AhenkDeamon] Requesting policies...') + # messager.send_direct_message(messageManager.policy_request_msg()) - #this is must be created after message services + # this is must be created after message services + def signal_handler(self, num, stack): - - def signal_handler (self, num, stack): - - #TODO###### + # TODO###### config = configparser.ConfigParser() config._interpolation = configparser.ExtendedInterpolation() config.read(pidfilePath) - event=config.get('PID', 'event') - #TODO###### + event = config.get('PID', 'event') + # TODO###### params = event.split() scope = Scope().getInstance() @@ -153,16 +157,17 @@ class AhenkDeamon(BaseDaemon): if 'login' == str(params[0]): message = scope.getMessageManager().login_msg(params[1]) scope.getMessager().send_direct_message(message) - logger.debug('[AhenkDeamon] login event is handled for user:'+params[1]) + logger.debug('[AhenkDeamon] login event is handled for user:' + params[1]) elif 'logout' == str(params[0]): message = scope.getMessageManager().logout_msg(params[1]) scope.getMessager().send_direct_message(message) - logger.debug('[AhenkDeamon] logout event is handled for user:'+params[1]) + logger.debug('[AhenkDeamon] logout event is handled for user:' + params[1]) else: - logger.error('[AhenkDeamon] Unknown command error. Command:'+params[0]) + logger.error('[AhenkDeamon] Unknown command error. Command:' + params[0]) logger.debug('[AhenkDeamon] Processing of handled event is completed') + if __name__ == '__main__': ahenkdaemon = AhenkDeamon(pidfilePath) @@ -183,15 +188,15 @@ if __name__ == '__main__': elif len(sys.argv) == 3: if sys.argv[1] == 'login' or sys.argv[1] == 'logout': - print('event:'+str(sys.argv[1])) + print('event:' + str(sys.argv[1])) - #TODO############## + # TODO############## config = configparser.ConfigParser() config._interpolation = configparser.ExtendedInterpolation() config.read(pidfilePath) - pid_number = config.get('PID','pid_number') - config.set('PID', 'event', str(sys.argv[1])+' '+sys.argv[2]) - #TODO############## + pid_number = config.get('PID', 'pid_number') + config.set('PID', 'event', str(sys.argv[1]) + ' ' + sys.argv[2]) + # TODO############## with open(pidfilePath, 'w') as config_file: config.write(config_file) diff --git a/opt/ahenk/base/Scope.py b/opt/ahenk/base/Scope.py index f3c149a..f02e1f4 100644 --- a/opt/ahenk/base/Scope.py +++ b/opt/ahenk/base/Scope.py @@ -6,23 +6,22 @@ class Scope(object): """docstring for Scope""" - scopeInstance=None + scopeInstance = None def __init__(self): super(Scope, self).__init__() self.customMap = {} - self.configurationManager=None - self.messageManager=None - self.logger=None - self.pluginManager=None - self.taskManager=None - self.responseQueue=None - self.registration=None - self.eventManager=None - self.executionManager=None - self.dbService=None - self.messager=None - + self.configurationManager = None + self.messageManager = None + self.logger = None + self.pluginManager = None + self.taskManager = None + self.responseQueue = None + self.registration = None + self.eventManager = None + self.executionManager = None + self.dbService = None + self.messager = None @staticmethod def getInstance(): @@ -36,76 +35,74 @@ class Scope(object): def getCustomMap(self): return self.customMap - def putCustomMap(self,name,value): - self.custom[str(name)]=value + def putCustomMap(self, name, value): + self.custom[str(name)] = value - def getCustomParam(self,name): + def getCustomParam(self, name): return self.customMap[str(name)] def getConfigurationManager(self): return self.configurationManager - def setConfigurationManager(self,configurationManager): + def setConfigurationManager(self, configurationManager): self.configurationManager = configurationManager def getLogger(self): return self.logger - def setLogger(self,logger): + def setLogger(self, logger): self.logger = logger def getMessageManager(self): return self.messageManager - def setMessageManager(self,messageManager): + def setMessageManager(self, messageManager): self.messageManager = messageManager def getPluginManager(self): return self.pluginManager - def setPluginManager(self,pluginManager): + def setPluginManager(self, pluginManager): self.pluginManager = pluginManager def getTaskManager(self): return self.taskManager - def setTaskManager(self,taskManager): + def setTaskManager(self, taskManager): self.taskManager = taskManager def getResponseQueue(self): return self.responseQueue - def setResponseQueue(self,responseQueue): - self.responseQueue=responseQueue + def setResponseQueue(self, responseQueue): + self.responseQueue = responseQueue def getRegistration(self): return self.registration - def setRegistration(self,registration): - self.registration=registration + def setRegistration(self, registration): + self.registration = registration def getEventManager(self): return self.eventManager - def setEventManager(self,eventManager): - self.eventManager=eventManager + def setEventManager(self, eventManager): + self.eventManager = eventManager def getExecutionManager(self): return self.executionManager - def setExecutionManager(self,executionManager): - self.executionManager=executionManager + def setExecutionManager(self, executionManager): + self.executionManager = executionManager def getDbService(self): return self.dbService - def setDbService(self,dbService): + def setDbService(self, dbService): self.dbService = dbService def getMessager(self): return self.messager - def setMessager(self,messager): + def setMessager(self, messager): self.messager = messager - - diff --git a/opt/ahenk/base/config/ConfigManager.py b/opt/ahenk/base/config/ConfigManager.py index ca79415..9d26d12 100644 --- a/opt/ahenk/base/config/ConfigManager.py +++ b/opt/ahenk/base/config/ConfigManager.py @@ -2,39 +2,40 @@ # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN -import os,configparser +import os +from configparser import SafeConfigParser from os import listdir from os.path import isfile, join -from configparser import SafeConfigParser + class ConfigManager(object): - """ - This class written for configuration file management of ahenk and ahenk plugins - Sample ahenk configuration file path /etc/ahenk/ahenk.conf and sample ahenk plugins configuration folder path /etc/ahenk/config.d/ - Usage: Takes two argument, - both of them are optional - one of the is a configuration file path the other one - is configuration files folder path - """ - def __init__(self, configurationFilePath=None, configurationFolderPath=None): - self.configurationFilePath = configurationFilePath - self.configurationFolderPath = configurationFolderPath + """ + This class written for configuration file management of ahenk and ahenk plugins + Sample ahenk configuration file path /etc/ahenk/ahenk.conf and sample ahenk plugins configuration folder path /etc/ahenk/config.d/ + Usage: Takes two argument, - both of them are optional - one of the is a configuration file path the other one + is configuration files folder path + """ + def __init__(self, configurationFilePath=None, configurationFolderPath=None): + self.configurationFilePath = configurationFilePath + self.configurationFolderPath = configurationFolderPath - def read(self): - configFiles = [] + def read(self): + configFiles = [] - # Check if given ahenk configuration file exists - # If file exists add it to configFiles array. - # TODO must write config file validater !! - if self.configurationFilePath: - if os.path.exists(self.configurationFilePath): - configFiles.append(self.configurationFilePath) + # Check if given ahenk configuration file exists + # If file exists add it to configFiles array. + # TODO must write config file validater !! + if self.configurationFilePath: + if os.path.exists(self.configurationFilePath): + configFiles.append(self.configurationFilePath) - if self.configurationFolderPath and os.path.exists(self.configurationFolderPath): - files = [f for f in listdir(self.configurationFolderPath) if isfile(join(self.configurationFolderPath, f))] - for f in files: - configFiles.append(join(self.configurationFolderPath, f)) + if self.configurationFolderPath and os.path.exists(self.configurationFolderPath): + files = [f for f in listdir(self.configurationFolderPath) if isfile(join(self.configurationFolderPath, f))] + for f in files: + configFiles.append(join(self.configurationFolderPath, f)) - parser = SafeConfigParser() - configValues = parser.read(configFiles) + parser = SafeConfigParser() + configValues = parser.read(configFiles) - return parser + return parser diff --git a/opt/ahenk/base/database/AhenkDbService.py b/opt/ahenk/base/database/AhenkDbService.py index 63201d4..abd0cd2 100644 --- a/opt/ahenk/base/database/AhenkDbService.py +++ b/opt/ahenk/base/database/AhenkDbService.py @@ -2,45 +2,47 @@ # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN # Author: Volkan Şahin -from base.Scope import Scope import sqlite3 +from base.Scope import Scope + + class AhenkDbService(object): """ Sqlite manager for ahenk """ + def __init__(self): scope = Scope.getInstance() self.logger = scope.getLogger() self.configurationManager = scope.getConfigurationManager() - self.db_path=self.configurationManager.get("BASE","dbPath") - self.connection=None + self.db_path = self.configurationManager.get('BASE', 'dbPath') + self.connection = None self.cursor = None def initialize_table(self): - self.check_and_create_table('task',['id INTEGER','create_date TEXT','modify_date TEXT','command_cls_id TEXT','parameter_map BLOB','deleted INTEGER','plugin TEXT']) - self.check_and_create_table('policy',['id INTEGER PRIMARY KEY AUTOINCREMENT','type TEXT','version TEXT','name TEXT']) - self.check_and_create_table('profile',['id INTEGER','create_date TEXT','label TEXT','description TEXT','overridable INTEGER','active INTEGER','deleted INTEGER','profile_data BLOB','modify_date TEXT']) - self.check_and_create_table('plugin',['version TEXT','name TEXT','description TEXT']) + self.check_and_create_table('task', ['id INTEGER', 'create_date TEXT', 'modify_date TEXT', 'command_cls_id TEXT', 'parameter_map BLOB', 'deleted INTEGER', 'plugin TEXT']) + self.check_and_create_table('policy', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'type TEXT', 'version TEXT', 'name TEXT']) + self.check_and_create_table('profile', ['id INTEGER', 'create_date TEXT', 'label TEXT', 'description TEXT', 'overridable INTEGER', 'active INTEGER', 'deleted INTEGER', 'profile_data BLOB', 'modify_date TEXT']) + self.check_and_create_table('plugin', ['version TEXT', 'name TEXT', 'description TEXT']) self.check_and_create_table('registration', ['jid TEXT', 'password TEXT', 'registered INTEGER', 'dn TEXT', 'params TEXT', 'timestamp TEXT']) - def connect(self): try: - self.connection=sqlite3.connect(self.db_path, check_same_thread=False) + self.connection = sqlite3.connect(self.db_path, check_same_thread=False) self.cursor = self.connection.cursor() except Exception as e: self.logger.error('Database connection error ' + str(e)) - def check_and_create_table(self,table_name,cols): + def check_and_create_table(self, table_name, cols): if self.cursor: cols = ', '.join([str(x) for x in cols]) - self.cursor.execute('create table if not exists '+table_name+' ('+cols+')') + self.cursor.execute('create table if not exists ' + table_name + ' (' + cols + ')') else: - self.logger.warning("Could not create table cursor is None! Table Name : " + str(table_name)) + self.logger.warning('Could not create table cursor is None! Table Name : ' + str(table_name)) - def drop_table(self,table_name): - sql = "DROP TABLE "+table_name + def drop_table(self, table_name): + sql = 'DROP TABLE ' + table_name self.cursor.execute(sql) self.connection.commit() @@ -49,27 +51,26 @@ class AhenkDbService(object): if self.connection: if criteria == None: cols = ', '.join([str(x) for x in cols]) - params = ', '.join(["?" for x in args]) - sql = "INSERT INTO "+table_name+" ("+cols+") VALUES ("+params+")" + params = ', '.join(['?' for x in args]) + sql = 'INSERT INTO ' + table_name + ' (' + cols + ') VALUES (' + params + ')' else: - update_list = "" - params = ', '.join(["?" for x in args]) + update_list = '' for index in range(len(cols)): - update_list = update_list + " " + cols[index] +" = ?," + update_list = update_list + ' ' + cols[index] + ' = ?,' update_list = update_list.strip(',') - sql = "UPDATE "+table_name+" SET " + update_list + " where " + criteria + sql = 'UPDATE ' + table_name + ' SET ' + update_list + ' where ' + criteria self.cursor.execute(sql, tuple(args)) self.connection.commit() else: - self.logger.warning("Could not update table cursor is None! Table Name : " + str(table_name)) + self.logger.warning('Could not update table cursor is None! Table Name : ' + str(table_name)) except Exception as e: - self.logger.error("Updating table error ! Table Name : " + str(table_name) + " " + str(e)) + self.logger.error('Updating table error ! Table Name : ' + str(table_name) + ' ' + str(e)) - def delete(self,table_name,criteria): + def delete(self, table_name, criteria): if self.cursor: - sql = "DELETE FROM " + table_name + sql = 'DELETE FROM ' + table_name if criteria: - sql+=' where '+str(criteria) + sql += ' where ' + str(criteria) self.cursor.execute(sql) self.connection.commit() @@ -77,17 +78,16 @@ class AhenkDbService(object): # Not implemented yet pass - def select(self,table_name, cols="*", criteria="", orderby=""): - print("seleeeeeeect") + def select(self, table_name, cols='*', criteria='', orderby=''): if self.cursor: try: - if not cols == "*": + if not cols == '*': cols = ', '.join([str(x) for x in cols]) - sql = "SELECT "+cols+" FROM " + table_name - if criteria != "": + sql = 'SELECT ' + cols + ' FROM ' + table_name + if criteria != '': sql += ' where ' sql += criteria - if orderby != "": + if orderby != '': sql += ' order by ' sql += orderby @@ -98,7 +98,7 @@ class AhenkDbService(object): except Exception as e: raise else: - self.logger.warning("Could not select table cursor is None! Table Name : " + str(table_name)) + self.logger.warning('Could not select table cursor is None! Table Name : ' + str(table_name)) def select_one_result(self, table_name, col, criteria=''): if self.cursor: @@ -121,4 +121,4 @@ class AhenkDbService(object): self.cursor.close() self.connection.close() except Exception as e: - self.logger.error("Closing database connection error " + str(e)) + self.logger.error('Closing database connection error:' + str(e)) diff --git a/opt/ahenk/base/deamon/BaseDeamon.py b/opt/ahenk/base/deamon/BaseDeamon.py index a2252ef..8d46139 100644 --- a/opt/ahenk/base/deamon/BaseDeamon.py +++ b/opt/ahenk/base/deamon/BaseDeamon.py @@ -1,137 +1,138 @@ #!/usr/bin/env python3 -import sys, os, time, atexit +import atexit +import os +import sys +import time from signal import SIGTERM + class BaseDaemon(object): - """ - A generic daemon class. + """ + A generic daemon class. - Usage: subclass the Daemon class and override the run() method - """ + Usage: subclass the Daemon class and override the run() method + """ - startmsg = "started with pid %s" + startmsg = 'started with pid %s' - def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): - self.stdin = stdin - self.stdout = stdout - self.stderr = stderr - self.pidfile = pidfile + def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): + self.stdin = stdin + self.stdout = stdout + self.stderr = stderr + self.pidfile = pidfile - def daemonize(self): - """ - do the UNIX double-fork magic, see Stevens' "Advanced - Programming in the UNIX Environment" for details (ISBN 0201563177) - http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 - """ - try: - pid = os.fork() - if pid > 0: - # exit first parent - sys.exit(0) - except OSError as e: - sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) - sys.exit(1) + def daemonize(self): + """ + do the UNIX double-fork magic, see Stevens' "Advanced + Programming in the UNIX Environment" for details (ISBN 0201563177) + http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 + """ + try: + pid = os.fork() + if pid > 0: + # exit first parent + sys.exit(0) + except OSError as e: + sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) + sys.exit(1) - # decouple from parent environment - os.chdir(".") - os.setsid() - os.umask(0) + # decouple from parent environment + os.chdir('.') + os.setsid() + os.umask(0) - # do second fork - try: - pid = os.fork() - if pid > 0: - # exit from second parent - sys.exit(0) - except OSError as e: - sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) - sys.exit(1) + # do second fork + try: + pid = os.fork() + if pid > 0: + # exit from second parent + sys.exit(0) + except OSError as e: + sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) + sys.exit(1) - # redirect standard file descriptors - si = open(self.stdin, 'r') - so = open(self.stdout, 'a+') - se = open(self.stderr, 'a+') + # redirect standard file descriptors + si = open(self.stdin, 'r') + so = open(self.stdout, 'a+') + se = open(self.stderr, 'a+') - pid = str(os.getpid()) + pid = str(os.getpid()) - sys.stderr.write("\n%s\n" % self.startmsg % pid) - sys.stderr.flush() + sys.stderr.write("\n%s\n" % self.startmsg % pid) + sys.stderr.flush() - if self.pidfile: - open(self.pidfile,'w+').write("%s\n" % pid) + if self.pidfile: + open(self.pidfile, 'w+').write("%s\n" % pid) - atexit.register(self.delpid) - os.dup2(si.fileno(), sys.stdin.fileno()) - os.dup2(so.fileno(), sys.stdout.fileno()) - os.dup2(se.fileno(), sys.stderr.fileno()) + atexit.register(self.delpid) + os.dup2(si.fileno(), sys.stdin.fileno()) + os.dup2(so.fileno(), sys.stdout.fileno()) + os.dup2(se.fileno(), sys.stderr.fileno()) + def delpid(self): + os.remove(self.pidfile) + def start(self): + """ + Start the daemon + """ + # Check for a pidfile to see if the daemon already runs + try: + pf = open(self.pidfile, 'r') + pid = int(pf.read().strip()) + pf.close() + except IOError: + pid = None + if pid: + message = "pidfile %s already exist. Daemon already running?\n" + sys.stderr.write(message % self.pidfile) + sys.exit(1) - def delpid(self): - os.remove(self.pidfile) + # Start the daemon + self.daemonize() + self.run() - def start(self): - """ - Start the daemon - """ - # Check for a pidfile to see if the daemon already runs - try: - pf = open(self.pidfile,'r') - pid = int(pf.read().strip()) - pf.close() - except IOError: - pid = None + def stop(self): + """ + Stop the daemon + """ + # Get the pid from the pidfile + try: + pf = open(self.pidfile, 'r') + pid = int(pf.read().strip()) + pf.close() + except IOError: + pid = None - if pid: - message = "pidfile %s already exist. Daemon already running?\n" - sys.stderr.write(message % self.pidfile) - sys.exit(1) + if not pid: + message = "pidfile %s does not exist. Daemon not running?\n" + sys.stderr.write(message % self.pidfile) + return # not an error in a restart - # Start the daemon - self.daemonize() - self.run() + # Try killing the daemon process + try: + while 1: + os.kill(pid, SIGTERM) + time.sleep(0.1) + except OSError as err: + err = str(err) + if err.find('No such process') > 0: + if os.path.exists(self.pidfile): + os.remove(self.pidfile) + else: + print(str(err)) + sys.exit(1) - def stop(self): - """ - Stop the daemon - """ - # Get the pid from the pidfile - try: - pf = open(self.pidfile,'r') - pid = int(pf.read().strip()) - pf.close() - except IOError: - pid = None + def restart(self): + """ + Restart the daemon + """ + self.stop() + self.start() - if not pid: - message = "pidfile %s does not exist. Daemon not running?\n" - sys.stderr.write(message % self.pidfile) - return # not an error in a restart - - # Try killing the daemon process - try: - while 1: - os.kill(pid, SIGTERM) - time.sleep(0.1) - except OSError as err: - err = str(err) - if err.find("No such process") > 0: - if os.path.exists(self.pidfile): - os.remove(self.pidfile) - else: - print(str(err)) - sys.exit(1) - - def restart(self): - """ - Restart the daemon - """ - self.stop() - self.start() - - def run(self): - """ - You should override this method when you subclass Daemon. It will be called after the process has been - daemonized by start() or restart(). - """ + def run(self): + """ + You should override this method when you subclass Daemon. It will be called after the process has been + daemonized by start() or restart(). + """ diff --git a/opt/ahenk/base/event/EventBase.py b/opt/ahenk/base/event/EventBase.py index 86bd198..5d10792 100644 --- a/opt/ahenk/base/event/EventBase.py +++ b/opt/ahenk/base/event/EventBase.py @@ -4,11 +4,13 @@ class EventBase(): listeners = [] + def __init__(self): self.listeners.append(self) self.listener_events = [] + def register_event(self, event_name, callback_func): - self.listener_events.append({'event_name' : event_name, 'callback_func' : callback_func}) + self.listener_events.append({'event_name': event_name, 'callback_func': callback_func}) class Event(): diff --git a/opt/ahenk/base/event/EventManager.py b/opt/ahenk/base/event/EventManager.py index 7130240..594f011 100644 --- a/opt/ahenk/base/event/EventManager.py +++ b/opt/ahenk/base/event/EventManager.py @@ -2,13 +2,14 @@ # -*- coding: utf-8 -*- # @author: İsmail BAŞARAN -from base.event.EventBase import EventBase,Event +from base.event.EventBase import EventBase, Event + class EventManager(EventBase): """docstring for EventManager""" + def __init__(self): EventBase.__init__(self) - - def fireEvent(self,event_name,*args): - Event(event_name,*args) + def fireEvent(self, event_name, *args): + Event(event_name, *args) diff --git a/opt/ahenk/base/execution/ExecutionManager.py b/opt/ahenk/base/execution/ExecutionManager.py index 0e2175a..2ccac74 100644 --- a/opt/ahenk/base/execution/ExecutionManager.py +++ b/opt/ahenk/base/execution/ExecutionManager.py @@ -2,11 +2,17 @@ # -*- coding: utf-8 -*- # Author: Volkan Şahin +import hashlib +import json +import os +import shutil +import stat import subprocess + from base.Scope import Scope -from base.model.Task import Task from base.model.Policy import Policy -import hashlib,json,os,stat,shutil +from base.model.Task import Task + class ExecutionManager(object): """docstring for FileTransferManager""" @@ -19,106 +25,106 @@ class ExecutionManager(object): self.event_manager = scope.getEventManager() self.task_manager = scope.getTaskManager() self.messager = scope.getMessager() - self.logger=scope.getLogger() - self.db_service=scope.getDbService() + self.logger = scope.getLogger() + self.db_service = scope.getDbService() # TODO move this event names to enumeration - 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) - self.event_manager.register_event('EXECUTE_TASK',self.execute_task) - self.event_manager.register_event('POLICY',self.update_policies) + 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) + self.event_manager.register_event('EXECUTE_TASK', self.execute_task) + self.event_manager.register_event('POLICY', self.update_policies) - def update_policies(self,arg): - print("updating policies...") + def update_policies(self, arg): + self.logger.debug('[ExecutionManager] Updating policies...') policy = Policy(json.loads(arg)) - #TODO get username - username='volkan' + # TODO get username + username = 'volkan' ahenk_policy_ver = self.db_service.select_one_result('policy', 'version', 'type = \'A\'') - user_policy_version = self.db_service.select_one_result('policy', 'version', 'type = \'U\' and name = \''+username+'\'') + user_policy_version = self.db_service.select_one_result('policy', 'version', 'type = \'U\' and name = \'' + username + '\'') installed_plugins = self.get_installed_plugins() missing_plugins = [] if policy.ahenk_policy_version != ahenk_policy_ver: ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'') - self.db_service.delete('profile', 'id='+str(ahenk_policy_id)) + self.db_service.delete('profile', 'id=' + str(ahenk_policy_id)) self.db_service.update('policy', ['version'], [str(policy.ahenk_policy_version)], 'type=\'A\'') for profile in policy.ahenk_profiles: profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'plugin'] args = [str(ahenk_policy_id), str(profile.create_date), str(profile.modify_date), str(profile.label), - str(profile.description), str(profile.overridable), str(profile.active), str(profile.deleted), str(profile.profile_data), str(profile.plugin)] + str(profile.description), str(profile.overridable), str(profile.active), str(profile.deleted), str(profile.profile_data), str(profile.plugin)] self.db_service.update('profile', profile_columns, args) if profile.plugin.name not in installed_plugins and profile.plugin.name not in missing_plugins: missing_plugins.append(profile.plugin.name) else: - print("already there ahenk policy") + self.logger.debug('[ExecutionManager] Already there is ahenk policy') if policy.user_policy_version != user_policy_version: - user_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'U\' and name=\''+username+'\'') - self.db_service.delete('profile', 'id='+str(user_policy_id)) - self.db_service.update('policy', ['version'], [str(policy.user_policy_version)], 'type=\'U\' and name=\''+username+'\'') + user_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'U\' and name=\'' + username + '\'') + self.db_service.delete('profile', 'id=' + str(user_policy_id)) + self.db_service.update('policy', ['version'], [str(policy.user_policy_version)], 'type=\'U\' and name=\'' + username + '\'') for profile in policy.user_profiles: profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'plugin'] args = [str(user_policy_id), str(profile.create_date), str(profile.modify_date), str(profile.label), - str(profile.description), str(profile.overridable), str(profile.active), str(profile.deleted), str(profile.profile_data), str(profile.plugin)] - self.db_service.update('profile',profile_columns, args) + str(profile.description), str(profile.overridable), str(profile.active), str(profile.deleted), str(profile.profile_data), str(profile.plugin)] + self.db_service.update('profile', profile_columns, args) if profile.plugin.name not in installed_plugins and profile.plugin.name not in missing_plugins: missing_plugins.append(profile.plugin.name) else: - print("already there user policy") + self.logger.debug('[ExecutionManager] Already there is user policy') - print("updated policies") - print("but first need these plugins:"+str(missing_plugins)) + # TODO check plugins + print("but first need these plugins:" + str(missing_plugins)) def get_installed_plugins(self): - plugins=self.db_service.select('plugin',['name','version']) - p_list=[] + plugins = self.db_service.select('plugin', ['name', 'version']) + p_list = [] for p in plugins: - p_list.append(str(p[0])+'-'+str(p[1])) + p_list.append(str(p[0]) + '-' + str(p[1])) return p_list - def execute_task(self,arg): + def execute_task(self, arg): self.logger.debug('[ExecutionManager] Adding new task...') task = Task(json.loads(arg)) self.task_manager.addTask(task) self.logger.debug('[ExecutionManager] Task added') - def move_file(self,arg): - default_file_path=self.config_manager.get('CONNECTION', 'receiveFileParam') + 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] '+file_name+' will be moved to '+target_file_path) - shutil.move(default_file_path+file_name,target_file_path+file_name) + # msg_id =str(j['id']).lower() + target_file_path = str(j['filepath']).lower() + file_name = str(j['filename']).lower() + self.logger.debug('[ExecutionManager] ' + file_name + ' will be moved to ' + target_file_path) + shutil.move(default_file_path + file_name, target_file_path + file_name) - def execute_script(self,arg): + 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() + # 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) + subprocess.call("/bin/sh " + file_path, shell=True) - #need to move somewhere else - def request_file(self,arg): + # 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) + # 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) self.messager.send_file(file_path) - def get_md5_file(self,fname): + def get_md5_file(self, fname): self.logger.debug('[ExecutionManager] md5 hashing') hash_md5 = hashlib.md5() - with open(fname, "rb") as f: + with open(fname, 'rb') as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) return str(hash_md5.hexdigest()) diff --git a/opt/ahenk/base/logger/AhenkLogger.py b/opt/ahenk/base/logger/AhenkLogger.py index 3a0c814..64c7f99 100644 --- a/opt/ahenk/base/logger/AhenkLogger.py +++ b/opt/ahenk/base/logger/AhenkLogger.py @@ -1,32 +1,34 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN -import sys import logging import logging.config + from base.Scope import Scope + class Logger(object): - """docstring for Logger""" - def __init__(self): - super(Logger, self).__init__() - scope = Scope.getInstance() - configManager = scope.getConfigurationManager() + """docstring for Logger""" - logging.config.fileConfig(configManager.get('BASE','logConfigurationFilePath')) - self.logger = logging.getLogger() + def __init__(self): + super(Logger, self).__init__() + scope = Scope.getInstance() + configManager = scope.getConfigurationManager() - def getLogger(self): - return self.logger + logging.config.fileConfig(configManager.get('BASE', 'logConfigurationFilePath')) + self.logger = logging.getLogger() - def info(self,logstring): - self.logger.info(logstring) + def getLogger(self): + return self.logger - def warning(self,logstring): - self.logger.warning(logstring) + def info(self, logstring): + self.logger.info(logstring) - def error(self,logstring): - self.logger.error(logstring) + def warning(self, logstring): + self.logger.warning(logstring) - def debug(self,logstring): - self.logger.debug(logstring) + def error(self, logstring): + self.logger.error(logstring) + + def debug(self, logstring): + self.logger.debug(logstring) diff --git a/opt/ahenk/base/logging/AhenkLogger.py b/opt/ahenk/base/logging/AhenkLogger.py index aa850d4..08857c3 100644 --- a/opt/ahenk/base/logging/AhenkLogger.py +++ b/opt/ahenk/base/logging/AhenkLogger.py @@ -1,32 +1,34 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN -import sys import logging import logging.config + from base.Scope import Scope + class AhenkLogger(object): - """docstring for Logger""" - def __init__(self): - super(Logger, self).__init__() - scope = Scope.getInstance() - configManager = scope.getConfigurationManager() + """docstring for Logger""" - logging.config.fileConfig(configManager.get('BASE','logConfigurationFilePath')) - self.logger = logging.getLogger() + def __init__(self): + super(Logger, self).__init__() + scope = Scope.getInstance() + configManager = scope.getConfigurationManager() - def getLogger(self): - return self.logger + logging.config.fileConfig(configManager.get('BASE', 'logConfigurationFilePath')) + self.logger = logging.getLogger() - def info(self,logstring): - self.logger.info(logstring) + def getLogger(self): + return self.logger - def warning(self,logstring): - self.logger.warning(logstring) + def info(self, logstring): + self.logger.info(logstring) - def error(self,logstring): - self.logger.error(logstring) + def warning(self, logstring): + self.logger.warning(logstring) - def debug(self,logstring): - self.logger.debug(logstring) + def error(self, logstring): + self.logger.error(logstring) + + def debug(self, logstring): + self.logger.debug(logstring) diff --git a/opt/ahenk/base/messaging/AnonymousMessager.py b/opt/ahenk/base/messaging/AnonymousMessager.py index 662b54f..59763bb 100644 --- a/opt/ahenk/base/messaging/AnonymousMessager.py +++ b/opt/ahenk/base/messaging/AnonymousMessager.py @@ -2,36 +2,37 @@ # -*- coding: utf-8 -*- # Author: Volkan Şahin # Author: İsmail BAŞARAN -import slixmpp, asyncio, sys +import asyncio +import slixmpp +import sys + sys.path.append('../..') from slixmpp.exceptions import IqError, IqTimeout from base.Scope import Scope class AnonymousMessager(slixmpp.ClientXMPP): - - def __init__(self,message,file_path): + def __init__(self, message, file_path): # global scope of ahenk scope = Scope().getInstance() self.logger = scope.getLogger() self.configuration_manager = scope.getConfigurationManager() - self.registration=scope.getRegistration() + self.registration = scope.getRegistration() self.event_manager = scope.getEventManager() - self.my_jid=str(self.configuration_manager.get('CONNECTION', 'host')) + self.my_jid = str(self.configuration_manager.get('CONNECTION', 'host')) + slixmpp.ClientXMPP.__init__(self, self.my_jid, None) - slixmpp.ClientXMPP.__init__(self, self.my_jid,None) + self.message = None + self.file = None + self.receiver = self.configuration_manager.get('CONNECTION', 'receiverjid') + '@' + self.configuration_manager.get('CONNECTION', 'host') + '/Smack' - self.message=None - self.file=None - self.receiver=self.configuration_manager.get('CONNECTION', 'receiverjid')+'@'+self.configuration_manager.get('CONNECTION', 'host')+'/Smack' - - if file_path is not None and file_path!='': - self.file=open(file_path, 'rb') + if file_path is not None and file_path != '': + self.file = open(file_path, 'rb') if message is not None: - self.message=message + self.message = message self.logger.debug('[MessageSender] XMPP Receiver parameters were set') @@ -46,14 +47,13 @@ class AnonymousMessager(slixmpp.ClientXMPP): self.add_event_handler("socks5_closed", self.stream_closed) self.logger.debug('[MessageSender] Event handlers were added') - def recv_direct_message(self, msg): if msg['type'] in ('chat', 'normal'): 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'])) + self.event_manager.fireEvent('confirm_registration', str(msg['body'])) ##TODO type fire -- only anonymous account can fire confirm_registration @asyncio.coroutine @@ -66,7 +66,7 @@ class AnonymousMessager(slixmpp.ClientXMPP): self.send_direct_message(self.message) if self.file is not None: - self.logger.debug('[MessageSender] Sending file: '+self.file.name) + 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. @@ -88,8 +88,8 @@ class AnonymousMessager(slixmpp.ClientXMPP): self.file.close() def stream_opened(self, 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') + 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): @@ -102,11 +102,11 @@ class AnonymousMessager(slixmpp.ClientXMPP): self.logger.debug('[MessageSender] Disconnecting...') self.disconnect() - def send_direct_message(self,msg): - self.logger.debug('[MessageSender] Sending message: '+msg) - self.send_message(mto=self.receiver,mbody=msg,mtype='normal') + def send_direct_message(self, 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. + 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() @@ -114,16 +114,16 @@ class AnonymousMessager(slixmpp.ClientXMPP): self.logger.debug('[MessageSender] Connection were established successfully') return True except Exception as e: - self.logger.error('[MessageSender] Connection to server is failed! '+e) + self.logger.error('[MessageSender] Connection to server is failed! ' + e) return False def register_extensions(self): try: - self.register_plugin('xep_0030') # Service Discovery - self.register_plugin('xep_0045') # Multi-User Chat - self.register_plugin('xep_0199') # XMPP Ping - self.register_plugin('xep_0065', {'auto_accept': True}) # SOCKS5 Bytestreams - self.register_plugin('xep_0047', {'auto_accept': True}) # In-band Bytestreams + self.register_plugin('xep_0030') # Service Discovery + self.register_plugin('xep_0045') # Multi-User Chat + self.register_plugin('xep_0199') # XMPP Ping + self.register_plugin('xep_0065', {'auto_accept': True}) # SOCKS5 Bytestreams + self.register_plugin('xep_0047', {'auto_accept': True}) # In-band Bytestreams self.logger.debug('[MessageSender] Extension were registered: xep_0030,xep_0045,xep_0199,xep_0065,xep_0047') return True diff --git a/opt/ahenk/base/messaging/MessageResponseQueue.py b/opt/ahenk/base/messaging/MessageResponseQueue.py index c6a0673..91312f7 100644 --- a/opt/ahenk/base/messaging/MessageResponseQueue.py +++ b/opt/ahenk/base/messaging/MessageResponseQueue.py @@ -3,13 +3,16 @@ # Author: İsmail BAŞARAN import threading + from base.Scope import Scope + class MessageResponseQueue(threading.Thread): """ This class handles responses and sends it to lider server. """ - def __init__(self,outQueue): + + def __init__(self, outQueue): super(MessageResponseQueue, self).__init__() scope = Scope.getInstance() self.logger = scope.getLogger() @@ -18,13 +21,13 @@ class MessageResponseQueue(threading.Thread): def run(self): try: - while True : + while True: # This item will send response to lider. # item must be response message. Response message may be generic message type responseMessage = self.outQueue.get(block=True) print(responseMessage) # Call message manager for response self.messageManager.send_direct_message(responseMessage) - #self.outQueue.task_done() + # self.outQueue.task_done() except: pass diff --git a/opt/ahenk/base/messaging/Messager.py b/opt/ahenk/base/messaging/Messager.py index f73c379..e97a5ae 100644 --- a/opt/ahenk/base/messaging/Messager.py +++ b/opt/ahenk/base/messaging/Messager.py @@ -2,56 +2,59 @@ # -*- coding: utf-8 -*- # Author: Volkan Şahin # Author: İsmail BAŞARAN -import json, os, asyncio, slixmpp, sys +import asyncio +import json +import os +import sys + +import slixmpp + sys.path.append('../..') from slixmpp.exceptions import IqError, IqTimeout from base.Scope import Scope - class Messager(slixmpp.ClientXMPP): - def __init__(self): # global scope of ahenk scope = Scope().getInstance() self.logger = scope.getLogger() self.configuration_manager = scope.getConfigurationManager() - self.event_manger=scope.getEventManager() - self.execution_manager=scope.getExecutionManager() + self.event_manger = scope.getEventManager() + self.execution_manager = scope.getExecutionManager() - self.my_jid=str(self.configuration_manager.get('CONNECTION', 'uid')+'@'+self.configuration_manager.get('CONNECTION', 'host')+'/receiver') - self.my_pass=str(self.configuration_manager.get('CONNECTION', 'password')) + self.my_jid = str(self.configuration_manager.get('CONNECTION', 'uid') + '@' + self.configuration_manager.get('CONNECTION', 'host') + '/receiver') + self.my_pass = str(self.configuration_manager.get('CONNECTION', 'password')) - slixmpp.ClientXMPP.__init__(self, self.my_jid,self.my_pass) + slixmpp.ClientXMPP.__init__(self, self.my_jid, self.my_pass) self.file = None - self.receiver=self.configuration_manager.get('CONNECTION', 'receiverjid')+'@'+self.configuration_manager.get('CONNECTION', 'host')+'/Smack' + 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.receive_file_path = self.configuration_manager.get('CONNECTION', 'receiveFileParam') self.logger.debug('[Messager] XMPP Receiver parameters were set') self.register_extensions() self.add_listeners() - 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('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) + 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.add_event_handler("ibb_stream_start", self.stream_opened) - self.add_event_handler("ibb_stream_data", self.stream_data) - self.add_event_handler("ibb_stream_end", self.stream_closed) + self.add_event_handler('ibb_stream_start', self.stream_opened) + self.add_event_handler('ibb_stream_data', self.stream_data) + self.add_event_handler('ibb_stream_end', self.stream_closed) self.logger.debug('[Messager] Event handlers were added') def stream_opened(self, sid): - self.logger.debug('[Messager] Stream was opened. Stream id: '+str(self.stream_id)) - self.file = open(self.receive_file_path+self.stream_id, 'wb') + self.logger.debug('[Messager] 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): @@ -68,11 +71,11 @@ class Messager(slixmpp.ClientXMPP): self.get_roster() self.send_presence() - #TODO need check - def send_file(self,file_path): - self.file=open(file_path, 'rb') - self.logger.debug('[Messager] Sending file: '+self.file.name) - self.logger.debug('[MessageSender] Sending file: '+self.file.name) + # TODO need check + def send_file(self, file_path): + self.file = open(file_path, 'rb') + self.logger.debug('[Messager] Sending file: ' + self.file.name) + 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. @@ -93,18 +96,18 @@ class Messager(slixmpp.ClientXMPP): finally: self.file.close() - def send_direct_message(self,msg): - self.logger.debug('[Messager] Sending message: '+msg) - self.send_message(mto=self.receiver,mbody=msg,mtype='normal') + def send_direct_message(self, msg): + self.logger.debug('[Messager] Sending message: ' + msg) + self.send_message(mto=self.receiver, mbody=msg, mtype='normal') def recv_direct_message(self, msg): if msg['type'] in ('chat', 'normal'): j = json.loads(str(msg['body'])) - type =j['type'] - self.logger.debug('[Messager] Fired event is: '+type) - self.event_manger.fireEvent(type,str(msg['body']).lower()) + type = j['type'] + self.logger.debug('[Messager] 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. + def connect_to_server(self): # Connect to the XMPP server and start processing XMPP stanzas. try: self.logger.debug('[Messager] Connecting to server as thread') loop = asyncio.new_event_loop() @@ -114,21 +117,21 @@ class Messager(slixmpp.ClientXMPP): self.logger.debug('[Messager] Connection were established successfully') return True except Exception as e: - self.logger.error('[Messager] Connection to server is failed! '+e) + self.logger.error('[Messager] Connection to server is failed! ' + e) return False def set_file_name_md5(self): self.logger.debug('[Messager] 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) + 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 - self.register_plugin('xep_0045') # Multi-User Chat - self.register_plugin('xep_0199') # XMPP Ping - self.register_plugin('xep_0065', {'auto_accept': True}) # SOCKS5 Bytestreams - self.register_plugin('xep_0047', {'auto_accept': True}) # In-band Bytestreams + self.register_plugin('xep_0030') # Service Discovery + self.register_plugin('xep_0045') # Multi-User Chat + self.register_plugin('xep_0199') # XMPP Ping + self.register_plugin('xep_0065', {'auto_accept': True}) # SOCKS5 Bytestreams + self.register_plugin('xep_0047', {'auto_accept': True}) # In-band Bytestreams self.logger.debug('Extension were registered: xep_0030,xep_0045,xep_0199,xep_0065,xep_0047') return True @@ -136,19 +139,20 @@ class Messager(slixmpp.ClientXMPP): self.logger.error('Extension registration is failed!(%s)\n' % (e.errno, e.strerror)) return False - """ - def invite_auto_accept(self, inv): - self.room=inv['from'] - self.logger.debug('[Messager] (%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 invite_auto_accept(self, inv): + self.room = inv['from'] + self.logger.debug('[Messager] (%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): if msg['mucnick'] != self.nick: - self.logger.debug('[Messager] %s : %s' % (str(msg['from']),str(msg['body'])) ) - self.send_message(mto=msg['from'].bare,mbody="I got it, %s." % msg['mucnick'],mtype='groupchat') + self.logger.debug('[Messager] %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: - self.logger.debug('[Messager] %s : %s' % (str(msg['mucnick']),str(msg['body']))) - """ + self.logger.debug('[Messager] %s : %s' % (str(msg['mucnick']), str(msg['body']))) + + ''' diff --git a/opt/ahenk/base/messaging/Messaging.py b/opt/ahenk/base/messaging/Messaging.py index 5896848..b3c4a7f 100644 --- a/opt/ahenk/base/messaging/Messaging.py +++ b/opt/ahenk/base/messaging/Messaging.py @@ -1,51 +1,53 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author: Volkan Şahin -import sys, pwd, os, datetime, json +import datetime +import json +import os +import pwd +import sys sys.path.append('../..') from base.Scope import Scope -import configparser -#TODO Message Factory + +# TODO Message Factory class Messaging(object): def __init__(self): scope = Scope().getInstance() self.logger = scope.getLogger() self.conf_manager = scope.getConfigurationManager() - self.db_service=scope.getDbService() + self.db_service = scope.getDbService() self.event_manger = scope.getEventManager() # TODO can use sh commands or api for getting username and timestamp def policy_request_msg(self): - #TODO volkan + # TODO volkan self.logger.debug('[Messaging] Creating policy request message') - ahenk_version=self.db_service.select('policy',['version'],'type = \'A\'') - username='volkan' - user_version=self.db_service.select('policy',['version'],'type = \'U\' and name = \''+username+'\'') + ahenk_version = self.db_service.select('policy', ['version'], 'type = \'A\'') + username = 'volkan' + user_version = self.db_service.select('policy', ['version'], 'type = \'U\' and name = \'' + username + '\'') - if len(ahenk_version)==0: + if len(ahenk_version) == 0: ahenk_version.append(-1) - if len(user_version)==0: + if len(user_version) == 0: user_version.append(-1) data = {} data['type'] = 'POLICY_REQUEST' data['username'] = username data['ahenkPolicyVersion'] = str(''.join(ahenk_version[0])) - data['userPolicyVersion'] =str(''.join(user_version[0])) + data['userPolicyVersion'] = str(''.join(user_version[0])) json_data = json.dumps(data) self.logger.debug('[Messaging] Policy request message was created') print(json_data) return json_data - - - def login_msg(self,username): + def login_msg(self, username): data = {} data['type'] = 'LOGIN' data['username'] = username @@ -54,7 +56,7 @@ class Messaging(object): self.logger.debug('[Messaging] Login message was created') return json_data - def logout_msg(self,username): + def logout_msg(self, username): data = {} data['type'] = 'LOGOUT' data['username'] = str(username) diff --git a/opt/ahenk/base/model/MessageFactory.py b/opt/ahenk/base/model/MessageFactory.py index 41283fc..fd33391 100644 --- a/opt/ahenk/base/model/MessageFactory.py +++ b/opt/ahenk/base/model/MessageFactory.py @@ -3,9 +3,9 @@ # Author: İsmail BAŞARAN from base.model.MessageType import MessageType -class MessageFactory(object): - def createMessage(self,type,message): +class MessageFactory(object): + def createMessage(self, type, message): if type == MessageType.TASK_RECEIVED: return "Message receivden response" @@ -13,4 +13,5 @@ class MessageFactory(object): return "Message processing response" else: return None + createMessage = staticmethod(createMessage) diff --git a/opt/ahenk/base/model/MessageType.py b/opt/ahenk/base/model/MessageType.py index 5713e04..49e223e 100644 --- a/opt/ahenk/base/model/MessageType.py +++ b/opt/ahenk/base/model/MessageType.py @@ -3,11 +3,12 @@ # Author: İsmail BAŞARAN from enum import Enum + class MessageType(Enum): - TASK_RECEIVED="TASK_RECEIVED" - TASK_PROCESSING="TASK_PROCESSING" - TASK_PROCESSED="TASK_PROCESSED" - TASK_ERROR="TASK_ERROR" - TASK_WARNING="TASK_WARNING" - POLICY_RECEIVED="POLICY_RECEIVED" - POLICY_PROCESSED="POLICY_PROCESSED" + TASK_RECEIVED = 'TASK_RECEIVED' + TASK_PROCESSING = 'TASK_PROCESSING' + TASK_PROCESSED = 'TASK_PROCESSED' + TASK_ERROR = 'TASK_ERROR' + TASK_WARNING = 'TASK_WARNING' + POLICY_RECEIVED = 'POLICY_RECEIVED' + POLICY_PROCESSED = 'POLICY_PROCESSED' diff --git a/opt/ahenk/base/model/Plugin.py b/opt/ahenk/base/model/Plugin.py index eaba0b5..1af27f4 100644 --- a/opt/ahenk/base/model/Plugin.py +++ b/opt/ahenk/base/model/Plugin.py @@ -3,9 +3,11 @@ # Author: Volkan Şahin import json + class Plugin(object): """docstring for Plugin""" - def __init__(self,message): + + def __init__(self, message): self.plugin = message @property diff --git a/opt/ahenk/base/model/Policy.py b/opt/ahenk/base/model/Policy.py index 07ba396..e3c18ef 100644 --- a/opt/ahenk/base/model/Policy.py +++ b/opt/ahenk/base/model/Policy.py @@ -2,12 +2,15 @@ # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN # Author: Volkan Şahin -from base.model.Profile import Profile import json +from base.model.Profile import Profile + + class Policy(object): """docstring for Policy""" - def __init__(self,message): + + def __init__(self, message): self.policy = message @property @@ -16,7 +19,7 @@ class Policy(object): @property def ahenk_profiles(self): - profiles=[] + profiles = [] for p in self.policy['ahenkprofiles']: profiles.append(Profile(p)) return profiles @@ -31,7 +34,7 @@ class Policy(object): @property def user_profiles(self): - profiles=[] + profiles = [] for p in self.policy['userprofiles']: profiles.append(Profile(p)) return profiles diff --git a/opt/ahenk/base/model/Profile.py b/opt/ahenk/base/model/Profile.py index c0f9d3d..9812aaf 100644 --- a/opt/ahenk/base/model/Profile.py +++ b/opt/ahenk/base/model/Profile.py @@ -1,12 +1,15 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author: Volkan Şahin -from base.model.Plugin import Plugin import json +from base.model.Plugin import Plugin + + class Profile(object): """docstring for Profile""" - def __init__(self,message): + + def __init__(self, message): self.profile = message @property @@ -54,4 +57,3 @@ class Profile(object): def to_json(self): return json.load(self.profile) - diff --git a/opt/ahenk/base/model/Task.py b/opt/ahenk/base/model/Task.py index c88c122..58d4fdb 100644 --- a/opt/ahenk/base/model/Task.py +++ b/opt/ahenk/base/model/Task.py @@ -2,12 +2,15 @@ # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN # Author: Volkan Şahin -from base.model.Plugin import Plugin import json +from base.model.Plugin import Plugin + + class Task(object): """docstring for Task""" - def __init__(self,message): + + def __init__(self, message): self.task = message['task'] @property @@ -43,7 +46,3 @@ class Task(object): def to_json(self): return json.load(self.task) - - - - diff --git a/opt/ahenk/base/plugin/AbstractCommand.py b/opt/ahenk/base/plugin/AbstractCommand.py index f0fdd98..289fcbb 100644 --- a/opt/ahenk/base/plugin/AbstractCommand.py +++ b/opt/ahenk/base/plugin/AbstractCommand.py @@ -3,8 +3,10 @@ # Author: İsmail BAŞARAN from base.Scope import Scope + class AbstractCommand(object): """docstring for AbstractCommand""" + def __init__(self): super(AbstractCommand, self).__init__() self.scope = Scope.getInstance() diff --git a/opt/ahenk/base/plugin/AbstractPlugin.py b/opt/ahenk/base/plugin/AbstractPlugin.py index 7ea2872..b38721c 100644 --- a/opt/ahenk/base/plugin/AbstractPlugin.py +++ b/opt/ahenk/base/plugin/AbstractPlugin.py @@ -3,8 +3,9 @@ # Author: İsmail BAŞARAN - class AbstractPlugin(object): - """This is base class for plugins""" - def __init__(self, arg): - super(AbstrackPlugin, self).__init__() - self.arg = arg +class AbstractPlugin(object): + """This is base class for plugins""" + + def __init__(self, arg): + super(AbstrackPlugin, self).__init__() + self.arg = arg diff --git a/opt/ahenk/base/plugin/Plugin.py b/opt/ahenk/base/plugin/Plugin.py index 84de106..808c29e 100644 --- a/opt/ahenk/base/plugin/Plugin.py +++ b/opt/ahenk/base/plugin/Plugin.py @@ -2,11 +2,14 @@ # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN import threading + from base.Scope import Scope + class Plugin(threading.Thread): """docstring for Plugin""" - def __init__(self, name,InQueue): + + def __init__(self, name, InQueue): threading.Thread.__init__(self) self.name = name self.InQueue = InQueue @@ -15,15 +18,15 @@ class Plugin(threading.Thread): self.logger = scope.getLogger() def run(self): - while True : + while True: try: - task=self.InQueue.get(block=True) - command = Scope.getInstance().getPluginManager().findCommand(self.getName(),task.command_cls_id) + task = self.InQueue.get(block=True) + command = Scope.getInstance().getPluginManager().findCommand(self.getName(), task.command_cls_id) command.handle_task(task) # TODO add result to response queue except Exception as e: - #TODO error log here + # TODO error log here self.logger.error("Plugin running exception " + str(e)) def getName(self): diff --git a/opt/ahenk/base/plugin/PluginManager.py b/opt/ahenk/base/plugin/PluginManager.py index 7061539..6bdec16 100644 --- a/opt/ahenk/base/plugin/PluginManager.py +++ b/opt/ahenk/base/plugin/PluginManager.py @@ -1,14 +1,18 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN +import imp +import os + +from base.Scope import Scope from base.plugin.Plugin import Plugin from base.plugin.PluginQueue import PluginQueue -from base.Scope import Scope -import imp,os + class PluginManager(object): """docstring for PluginManager""" - #implement logger + + # implement logger def __init__(self): super(PluginManager, self).__init__() self.scope = Scope.getInstance() @@ -31,45 +35,44 @@ class PluginManager(object): # TODO error log pass - def loadSinglePlugin(self,pluginName): + def loadSinglePlugin(self, pluginName): # TODO check already loaded plugin - self.pluginQueueDict[pluginName]=PluginQueue() - plugin = Plugin(pluginName,self.pluginQueueDict[pluginName]) + self.pluginQueueDict[pluginName] = PluginQueue() + plugin = Plugin(pluginName, self.pluginQueueDict[pluginName]) plugin.setDaemon(True) plugin.start() self.plugins.append(plugin) - def findCommand(self,pluginName,commandId): + def findCommand(self, pluginName, commandId): location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), pluginName) if os.path.isdir(location) and commandId + ".py" in os.listdir(location): info = imp.find_module(commandId, [location]) return imp.load_module(commandId, *info) else: - self.logger.warning('Command id -' + commandId +' - not found') + self.logger.warning('Command id -' + commandId + ' - not found') return None - def processTask(self,task): + def processTask(self, task): try: - if task.plugin.name.lower() in self.pluginQueueDict : - self.pluginQueueDict[task.plugin.name.lower()].put(task,1) + if task.plugin.name.lower() in self.pluginQueueDict: + self.pluginQueueDict[task.plugin.name.lower()].put(task, 1) except Exception as e: # TODO update task - status to not found command self.logger.error("[PluginManager] Exception occurred when processing task " + str(e)) - def reloadPlugins(self): # Not implemented yet pass - def checkPluginExists(self,pluginName): + def checkPluginExists(self, pluginName): # Not implemented yet pass - def reloadSinglePlugin(self,pluginName): + def reloadSinglePlugin(self, pluginName): # Not implemented yet pass - def checkCommandExist(self,pluginName,commandId): + def checkCommandExist(self, pluginName, commandId): # Not implemented yet pass diff --git a/opt/ahenk/base/plugin/PluginQueue.py b/opt/ahenk/base/plugin/PluginQueue.py index 3d97edc..d7a42e9 100644 --- a/opt/ahenk/base/plugin/PluginQueue.py +++ b/opt/ahenk/base/plugin/PluginQueue.py @@ -4,6 +4,7 @@ from queue import Queue + class PluginQueue(Queue): def __contains__(self, item): with self.mutex: diff --git a/opt/ahenk/base/registration/Registration.py b/opt/ahenk/base/registration/Registration.py index 07d30ea..45dcc68 100644 --- a/opt/ahenk/base/registration/Registration.py +++ b/opt/ahenk/base/registration/Registration.py @@ -2,14 +2,15 @@ # -*- coding: utf-8 -*- # Author: Volkan Şahin +import datetime +import json +import netifaces +import socket +import uuid +from uuid import getnode as get_mac + from base.Scope import Scope from base.messaging.AnonymousMessager import AnonymousMessager -from uuid import getnode as get_mac -import netifaces -import datetime -import socket -import json -import uuid class Registration(): diff --git a/opt/ahenk/base/task/TaskInQueue.py b/opt/ahenk/base/task/TaskInQueue.py index c9dab2d..08e2143 100644 --- a/opt/ahenk/base/task/TaskInQueue.py +++ b/opt/ahenk/base/task/TaskInQueue.py @@ -3,11 +3,14 @@ # Author: İsmail BAŞARAN import threading + from base.task.TaskJob import TaskJob + class TaskInQueue(threading.Thread): """docstring for TaskInQueue""" - def __init__(self,inQueue): + + def __init__(self, inQueue): super(TaskInQueue, self).__init__() self.inQueue = inQueue diff --git a/opt/ahenk/base/task/TaskJob.py b/opt/ahenk/base/task/TaskJob.py index 75d431f..4312f0e 100644 --- a/opt/ahenk/base/task/TaskJob.py +++ b/opt/ahenk/base/task/TaskJob.py @@ -2,10 +2,13 @@ # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN import threading + from base.Scope import Scope + class TaskJob(threading.Thread): """docstring for TaskJob""" + def __init__(self, task): super(TaskJob, self).__init__() scope = Scope.getInstance() diff --git a/opt/ahenk/base/task/TaskManager.py b/opt/ahenk/base/task/TaskManager.py index 01a982e..fbbf8d6 100644 --- a/opt/ahenk/base/task/TaskManager.py +++ b/opt/ahenk/base/task/TaskManager.py @@ -10,7 +10,7 @@ class TaskManager(object): """docstring for TaskManager""" def __init__(self): - #super(TaskManager, self).__init__() + # super(TaskManager, self).__init__() scope = Scope.getInstance() self.pluginManager = scope.getPluginManager() self.logger = scope.getLogger() @@ -19,7 +19,7 @@ class TaskManager(object): def addTask(self, task): try: self.logger.debug('Adding task ... ') - #self.saveTask(task) + # self.saveTask(task) self.logger.info('Task saved ') # TODO send task received message self.pluginManager.processTask(task)