def message_routing(self): """ This method handles all the queues, it routes the incoming massages to appropriate queue's according to the message type :return: """ while self.status: if self.inbox_queue.not_empty: new_block = self.inbox_queue.get() # print("DEBUG " + str(new_block)) self.logger.debug("[message_routing] Routing message " + str(new_block)) if new_block.type == MessageType.event: self.UI_queue.put(new_block) elif new_block.type == MessageType.utility: # self.handle_utility(new_block) self.message_handler.handle_message(new_block) elif new_block.type == MessageType.communication: self.handle_comms(new_block) else: ColorPrint.print_message("Warning", str(new_block.sender), str(new_block.payload)) self.logger.warning( "[Message Router] invalid message type " + str(new_block.sender) + " " + str(new_block.payload)) print("Message routing shutting down")
def remove_client(self, client, reason=None): """ This method removes the client from the server in a higher level, it creates the appropriate messages, logs the time stamp and such. In future mysql will also log sesion time here. :param client: the client object that is being removed from the server :return: nothing, literally nothing """ total_session_time = datetime.datetime.now() - client.connection_time ColorPrint.print_message("Warning", "remove_client", "kicking the client " + str(client.username) + " Reason " + str(reason)) self.logger.warning("Kicking client " + str(client.username) + " Total Session Time " + str(total_session_time) + " Reason " + str(reason)) # cleaning up the sockets and such client_conn = client.socket_connection try: self.all_clients.pop(client.username) self.all_connections.remove(client_conn) self.comm_layer.close_connection(client_conn) except Exception as e: # Probably client is already removed self.logger.error("[remove client] remove_client error " + str(e)) self.logger.error("[remove client] " + str(traceback.format_exc())) client_disconnected_message = Message("server", "server", "event", "Client Disconnected " + str(client) + " Reason " + str(reason)) self.inbox_queue.put(client_disconnected_message)
def ui(self): while self.client_controller.status: try: print("Please input command [info, tools]") user_input = input() if user_input == "info": self.ui_info() elif user_input == "tools": self.ui_tools() except EOFError as e: ColorPrint.print_message("Error", "UI", "Exception occurred " + str(e)) print("UI Terminating")
def ui_read_messages(self): print("Listing messages") print(self.server_controller.UI_queue.not_empty) # TODO fix blocking here while not self.server_controller.UI_queue.empty(): # UI_queue.get(block=True) #Blocks till a message appears! new_block = self.server_controller.UI_queue.get() # username, type_of_event, message = new_block if new_block.type is "event": ColorPrint.print_message( "Event", str(new_block.sender), new_block.payload + " -Message Created " + str(new_block.date)) elif new_block.type is "message": ColorPrint.print_message("Message", str(new_block.sender), new_block.payload)
def check_for_messages(self): """ This method checks for messages using select, if the readable port is the own server port, it means that we have an incomming connection request, so we call accept connection for that. :return: """ while self.status: # print("will do select! ") readable, writable, exceptional = select.select(self.all_connections, [], []) for connection in readable: # print("reading conn " + str(connection)) if connection is self.comm_layer.socket: try: self.accept_connections() except Exception as e: ColorPrint.print_message("ERROR", "accept_connection, check_for_messages", 'Error accepting connections: %s' % str(e)) self.logger.error("[check_for_messages] Error accepting connection " + str(e)) self.logger.error("[check_for_messages] " + str(traceback.format_exc())) # continue the loop continue else: try: received_message = self.comm_layer.read_message_from_connection(connection) # print("Received " + str(received_message)) username = self.get_username_from_connection(connection) client = self.get_client_from_username(username) if received_message is not None and received_message != b'': json_string = received_message.decode("utf-8") self.logger.info("Received message " + str(json_string) + " from " + str(username)) try: new_message = Message.json_string_to_message(json_string) self.inbox_queue.put(new_message) # current_seconds = int(round(time.time())) # client.last_ping = current_seconds except Exception as e: print("Received unexpected message " + str(e) + " " + received_message) self.logger.error("[check_for_messages] received unexpected message " + str(received_message) + " " + str(e)) self.logger.error("[check_for_messages] " + str(traceback.format_exc())) elif not self.is_client_alive(client): # if the client is spamming us with b'' we should check whether it is dead or not. self.remove_client(client, reason="B-storm") except Exception as e: print("Exception occurred in check_for_messages " + str(e)) self.logger.error("[check_for_messages] Exception occured " + str(e)) self.logger.error("[check_for_messages] " + str(traceback.format_exc())) username = self.get_username_from_connection(connection) client = self.get_client_from_username(username) if not self.is_client_alive(client) and client is not None: self.remove_client(client) print("Read Messages shutting down")
def ui_ssh(self): print("[SSH MENU] Select Option (put in the number) ") print("1)Start SSH") print("2)Close SSH Connection") print("3)Main Menu") user_input = input() if user_input == "1": print("[SSH MENU 2] Select Client") available_clients = self.server_controller.list_available_client_usernames( ) i = 0 for client in available_clients: print(str(i) + " " + client) i += 1 print(str(len(available_clients)) + " Cancel") # Never trust the user try: user_input = input() if int(user_input) < len(available_clients): parameters = { "local_port": 22, "remote_port": 7005, "name": "shell connection" } payload = { "action_type": "SSH", "parameters": json.dumps(parameters), "command": "SSH-Start" } new_message = Message( "server", list(available_clients)[int(user_input)], "action", json.dumps(payload)) self.server_controller.outbox_queue.put(new_message) else: pass except Exception as e: print("Invalid input " + str(e)) elif user_input == "2": print("[SSH MENU] Select Client to Close Connection") available_clients = self.server_controller.list_available_client_usernames( ) i = 0 for client in available_clients: print(str(i) + " " + client) i += 1 print(str(len(available_clients)) + " Cancel") try: user_input = input() if int(user_input) < len(available_clients): parameters = {"name": "shell connection"} payload = { "action_type": "SSH", "parameters": json.dumps(parameters), "command": "SSH-Stop" } close_ssh_message = Message( "server", list(available_clients)[int(user_input)], "action", json.dumps(payload)) self.server_controller.outbox_queue.put(close_ssh_message) else: pass except Exception as e: ColorPrint.print_message("Error", "UI", "Invalid input " + str(e))