def run(self, host="config", port="config", key_file="config"): """ | Starts the plugin by connecting to the server specified. | If arguments (-a, -p) are given when starting plugin, host/port will be ignored. | Function will return only when plugin exits. :param host: NTS IP address if plugin started without -a option :param port: NTS port if plugin started without -p option :type host: str :type port: int """ if (host == "config"): self.__host = config.fetch("host") else: self.__host = host if (port == "config"): self.__port = config.fetch("port") else: self.__port = port if (key_file == "config"): self.__key_file = config.fetch("key_file") else: self.__key_file = key_file self.__parse_args() Logs.debug("Start plugin") if self.__has_autoreload: self.__autoreload() else: self.__run()
def __exit(self): Logs.debug('Exiting') for session in _Plugin.instance._sessions.values(): session.signal_and_close_pipes() session.plugin_process.join() if self._post_run != None: self._post_run() sys.exit(0)
def __on_client_connection(self, session_id, version_table): main_conn_net, process_conn_net = Pipe() main_conn_proc, process_conn_proc = Pipe() session = Network._Session(session_id, self._network, self._process_manager, main_conn_net, main_conn_proc) process = Process(target=_Plugin._launch_plugin, args=(self._plugin_class, session_id, process_conn_net, process_conn_proc, _Plugin.__serializer, _Plugin._plugin_id, version_table, _TypeSerializer.get_version_table(), Logs._is_verbose(), _Plugin._custom_data)) process.start() session.plugin_process = process self._sessions[session_id] = session Logs.debug("Registered new session:", session_id)
def _launch_plugin(cls, plugin_class, session_id, pipe_net, pipe_proc, serializer, plugin_id, version_table, original_version_table, verbose, custom_data): plugin = plugin_class() _PluginInstance.__init__(plugin, session_id, pipe_net, pipe_proc, serializer, plugin_id, version_table, original_version_table, verbose, custom_data) Logs.debug("Starting plugin") plugin._run()
def _on_packet_received(self, packet): if packet.packet_type == Network._Packet.packet_type_message_to_plugin: session_id = packet.session_id if session_id in self._sessions: # packet.decompress() self._sessions[session_id]._on_packet_received(packet.payload) return # If we don't know this session_id, try to register it first if _Plugin.__serializer.try_register_session( packet.payload) == True: received_version_table, _, _ = _Plugin.__serializer.deserialize_command( packet.payload, None) version_table = _TypeSerializer.get_best_version_table( received_version_table) self.__on_client_connection(session_id, version_table) # Doesn't register? It's an error else: Logs.warning("Received a command from an unregistered session", session_id) elif packet.packet_type == Network._Packet.packet_type_plugin_connection: _Plugin._plugin_id = packet.plugin_id Logs.message("Registered with plugin ID", _Plugin._plugin_id, "\n=======================================\n") elif packet.packet_type == Network._Packet.packet_type_plugin_disconnection: if _Plugin._plugin_id == -1: if self._description['auth'] == None: Logs.error( "Connection refused by NTS. Are you missing a security key file?" ) else: Logs.error( "Connection refused by NTS. Your security key file might be invalid" ) sys.exit(1) else: Logs.debug("Connection ended by NTS") sys.exit(0) elif packet.packet_type == Network._Packet.packet_type_client_disconnection: try: id = packet.session_id self._sessions[id].signal_and_close_pipes() del self._sessions[id] Logs.debug("Session", id, "disconnected") except: pass elif packet.packet_type == Network._Packet.packet_type_keep_alive: pass else: Logs.warning("Received a packet of unknown type", packet.packet_type, ". Ignoring")