2024-08-05 22:12:43 +03:00
|
|
|
import os
|
2024-08-06 22:38:33 +03:00
|
|
|
import time
|
2024-08-05 22:12:43 +03:00
|
|
|
import sys
|
|
|
|
import socket
|
|
|
|
import json
|
|
|
|
import threading
|
2024-08-06 22:38:33 +03:00
|
|
|
import logging
|
|
|
|
import traceback
|
2024-08-11 22:09:40 +03:00
|
|
|
import hashlib
|
|
|
|
|
|
|
|
from hook_manager import hook_manager
|
|
|
|
from module_manager import ModuleManager
|
2024-08-05 22:12:43 +03:00
|
|
|
|
2024-08-06 22:38:33 +03:00
|
|
|
# Configure logging
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
2024-08-05 22:12:43 +03:00
|
|
|
|
|
|
|
class CoreDaemon:
|
2024-08-06 21:33:04 +03:00
|
|
|
def __init__(self, host='localhost', port=9999, module_dirs=None):
|
2024-08-05 22:12:43 +03:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2024-08-06 22:38:33 +03:00
|
|
|
self.module_dirs = module_dirs or ['modules']
|
2024-08-06 21:33:04 +03:00
|
|
|
self.module_manager = ModuleManager(self.module_dirs)
|
2024-08-11 22:09:40 +03:00
|
|
|
self.script_path = sys.argv[0]
|
|
|
|
self.initial_hash = self.calculate_file_hash(self.script_path)
|
|
|
|
self.hook_manager = hook_manager
|
|
|
|
|
|
|
|
def calculate_file_hash(self, file_path):
|
|
|
|
sha256_hash = hashlib.sha256()
|
|
|
|
with open(file_path, "rb") as f:
|
|
|
|
for byte_block in iter(lambda: f.read(4096), b""):
|
|
|
|
sha256_hash.update(byte_block)
|
|
|
|
return sha256_hash.hexdigest()
|
2024-08-05 22:12:43 +03:00
|
|
|
|
|
|
|
def start(self):
|
2024-08-06 22:38:33 +03:00
|
|
|
try:
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
s.bind((self.host, self.port))
|
|
|
|
s.listen()
|
|
|
|
logging.info(f"Core daemon listening on {self.host}:{self.port}")
|
|
|
|
while True:
|
|
|
|
conn, addr = s.accept()
|
|
|
|
threading.Thread(target=self.handle_client, args=(conn,)).start()
|
|
|
|
except Exception as e:
|
|
|
|
logging.error(f"Error starting core daemon: {str(e)}")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
logging.info("Shutting down core daemon...")
|
|
|
|
return {'success': True, 'message': 'Core daemon is shutting down'}
|
|
|
|
|
|
|
|
def _delayed_shutdown(self):
|
|
|
|
time.sleep(1) # Give time for the response to be sent
|
|
|
|
os._exit(0)
|
2024-08-05 22:12:43 +03:00
|
|
|
|
|
|
|
def handle_client(self, conn):
|
|
|
|
with conn:
|
|
|
|
while True:
|
2024-08-06 22:38:33 +03:00
|
|
|
try:
|
|
|
|
data = conn.recv(1024)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
command = json.loads(data.decode())
|
|
|
|
response = self.process_command(command)
|
|
|
|
conn.sendall(json.dumps(response).encode())
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
logging.error("Received invalid JSON data")
|
|
|
|
conn.sendall(json.dumps({'success': False, 'message': 'Invalid command format'}).encode())
|
|
|
|
except Exception as e:
|
|
|
|
logging.error(f"Error handling client request: {str(e)}")
|
|
|
|
conn.sendall(json.dumps({'success': False, 'message': 'Internal server error'}).encode())
|
2024-08-05 22:12:43 +03:00
|
|
|
|
2024-08-11 22:09:40 +03:00
|
|
|
def check_for_updates(self):
|
|
|
|
logging.info("Checking for updates...")
|
|
|
|
script_modified = False
|
|
|
|
current_script_hash = self.calculate_file_hash(self.script_path)
|
|
|
|
if current_script_hash != self.initial_hash:
|
|
|
|
script_modified = True
|
|
|
|
|
|
|
|
modified_modules = self.module_manager.check_modules_modified()
|
|
|
|
|
|
|
|
if script_modified or modified_modules:
|
|
|
|
message = "Updates detected:\n"
|
|
|
|
if script_modified:
|
|
|
|
message += "- Core script has been modified\n"
|
|
|
|
if modified_modules:
|
|
|
|
message += f"- Modified modules: {', '.join(modified_modules)}\n"
|
|
|
|
return True, message
|
|
|
|
else:
|
|
|
|
return False, "No updates detected."
|
|
|
|
|
2024-08-05 22:12:43 +03:00
|
|
|
def upgrade(self):
|
2024-08-11 22:09:40 +03:00
|
|
|
update_available, message = self.check_for_updates()
|
|
|
|
if update_available:
|
|
|
|
logging.info(message)
|
|
|
|
# Here you would typically perform any necessary upgrade steps
|
2024-08-06 22:38:33 +03:00
|
|
|
# For this example, we'll just restart the script
|
|
|
|
os.execv(sys.executable, ['python'] + sys.argv)
|
2024-08-11 22:09:40 +03:00
|
|
|
return True, "Upgrade initiated. Restarting with new version."
|
|
|
|
else:
|
|
|
|
logging.info("No updates detected. Skipping upgrade.")
|
|
|
|
return False, "No upgrade needed. Current version is up to date."
|
2024-08-05 22:12:43 +03:00
|
|
|
|
|
|
|
def process_command(self, command):
|
|
|
|
action = command.get('action')
|
2024-08-11 22:09:40 +03:00
|
|
|
|
|
|
|
# Execute pre-command hooks
|
|
|
|
pre_command_results = self.hook_manager.execute_hook('pre_command', command)
|
|
|
|
for result in pre_command_results:
|
|
|
|
if result is False:
|
|
|
|
return {'success': False, 'message': "Command rejected by pre-command hook"}
|
|
|
|
|
2024-08-06 22:38:33 +03:00
|
|
|
try:
|
|
|
|
if action == 'load':
|
2024-08-11 22:09:40 +03:00
|
|
|
success, message = self.module_manager.load_module(command.get('module'))
|
2024-08-06 22:38:33 +03:00
|
|
|
elif action == 'unload':
|
2024-08-11 22:09:40 +03:00
|
|
|
success, message = self.module_manager.unload_module(command.get('module'))
|
2024-08-06 22:38:33 +03:00
|
|
|
elif action == 'list':
|
2024-08-11 22:09:40 +03:00
|
|
|
success, modules = True, self.module_manager.list_modules()
|
|
|
|
message = modules
|
2024-08-06 22:38:33 +03:00
|
|
|
elif action == 'execute':
|
2024-08-11 22:09:40 +03:00
|
|
|
success, message = self.module_manager.execute_command(command.get('command'), command.get('args'))
|
2024-08-06 22:38:33 +03:00
|
|
|
elif action == 'upgrade':
|
2024-08-11 22:09:40 +03:00
|
|
|
success, message = self.upgrade()
|
2024-08-06 22:38:33 +03:00
|
|
|
elif action == 'list_commands':
|
2024-08-11 22:09:40 +03:00
|
|
|
success, commands = True, self.module_manager.list_commands()
|
|
|
|
message = commands
|
2024-08-06 22:38:33 +03:00
|
|
|
elif action == 'add_module_dir':
|
2024-08-11 22:09:40 +03:00
|
|
|
success, message = self.module_manager.add_module_dir(command.get('dir'))
|
2024-08-06 22:38:33 +03:00
|
|
|
elif action == 'shutdown':
|
|
|
|
response = self.shutdown()
|
|
|
|
# After sending the response, exit the program
|
|
|
|
threading.Thread(target=self._delayed_shutdown).start()
|
|
|
|
return response
|
|
|
|
else:
|
2024-08-11 22:09:40 +03:00
|
|
|
success, message = False, "Unknown command"
|
2024-08-06 22:38:33 +03:00
|
|
|
except Exception as e:
|
|
|
|
logging.error(f"Error processing command {action}: {str(e)}")
|
2024-08-11 22:09:40 +03:00
|
|
|
success, message = False, f"Error processing command: {str(e)}"
|
|
|
|
|
|
|
|
response = {'success': success, 'message': message}
|
|
|
|
|
|
|
|
# Execute post-command hooks
|
|
|
|
self.hook_manager.execute_hook('post_command', command, response)
|
|
|
|
|
|
|
|
return response
|
2024-08-05 22:12:43 +03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-08-06 21:33:04 +03:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Core Daemon with dynamic module loading")
|
|
|
|
parser.add_argument('--host', default='localhost', help='Host to bind the daemon to')
|
|
|
|
parser.add_argument('--port', type=int, default=9999, help='Port to bind the daemon to')
|
|
|
|
parser.add_argument('--module-dirs', nargs='*', default=['modules'], help='Directories to load modules from')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-08-06 22:38:33 +03:00
|
|
|
try:
|
|
|
|
daemon = CoreDaemon(host=args.host, port=args.port, module_dirs=args.module_dirs)
|
|
|
|
daemon.start()
|
|
|
|
except Exception as e:
|
|
|
|
logging.critical(f"Critical error in core daemon: {str(e)}")
|
|
|
|
logging.debug(traceback.format_exc())
|
|
|
|
sys.exit(1)
|