def poll_new_commands(): """ The view where a client reaches when he polls for new commands """ client_id = request.args.get('id') client = Client.objects(cid=client_id).first() if not client: return jsonify({'status': CLIENT_NOT_FOUND}) client.update_last_seen() command = client.get_first_not_served_command() if not command: return jsonify({'status': NO_COMMANDS_FOR_YOU}) command.set_served() return jsonify({ 'status': COMMAND_IS_SERVED, 'command': { 'id': command.cid, 'text': command.text } })
def complete_select(self, text: str, line: str, begin_index: int, end_index: int) -> List[str]: """ Handles the auto completion for this command """ choices = dict(id=Client.unique_client_ids()) completer = argparse_completer.AutoCompleter(SelectPlugin.select_parser, arg_choices=choices) tokens, _ = self.tokens_for_completion(line, begin_index, end_index) return completer.complete_command(tokens, text, line, begin_index, end_index)
def _client_plugin_handle_kill(self, client_id: str) -> None: """ Removes a specific client from the database """ if client_id == '*': Client.delete_all_clients() self.print_ok('Killed all clients!') return client: Client = Client.objects(cid=client_id).first() if not client: self.print_error('client does not exist!') else: if client == self.selected_client: self.print_error('you cant kill the selected client!') else: client.delete_from_db() self.print_ok('client deleted successfully!')
def _select_plugin_handle_select(self, client_id: str) -> None: """ Selected a client as the selected client by ID """ client = Client.objects(cid=client_id).first() if not client: self.print_error('client does not exist!') return self.select_client(client)
def _refresh_client_data_post_parse_hook( self, params: PostparsingData) -> PostparsingData: """ Refreshes the selected client data from the database. We do that because of `mongoengine`s behaviour, where if we set the current client, we do not track for modifications. This way, before every command is parsed we re-select the client """ if self.selected_client: cid = self.selected_client.cid self.select_client(Client.objects(cid=cid).first()) return params
def _client_plugin_handle_single_id(self, client_id: str) -> None: """ Shows information for a specific client ID """ client = Client.objects(cid=client_id).first() if not client: self.print_error('client does not exist!') else: self.print_pairs('Client Information', { 'ID ': client.cid, 'Registered On ': client.registered_on, 'Last Seen ': client.humanized_last_seen, 'Num of Commands ': len(client.commands) })
def register(): """ The view where a new clients reaches when he desires to register """ c = Client(user_agent=request.form.get('user_agent', '-')) c.save() for script in read_pre_flight(): c.run_command(script) return jsonify({'status': ALL_GOOD, 'id': c.cid})
def _alert_function(self) -> None: """ When the client list is larger than the one we know of alert the user that a new client has registered """ while not self._stop_thread: if self.terminal_lock.acquire(blocking=False): current_clients = set(Client.unique_client_ids()) delta = current_clients - self._seen_clients if len(delta) > 0: self.async_alert( self.t.bold_blue(' << new client registered >>'), self.prompt) self._seen_clients = current_clients self.terminal_lock.release() sleep(0.5)
def __init__(self): Cmd.__init__(self, startup_script=read_config().get('STARTUP_SCRIPT', ''), use_ipython=True) self.aliases.update({'exit': 'quit', 'help': 'help -v'}) self.hidden_commands.extend([ 'load', 'pyscript', 'set', 'shortcuts', 'alias', 'unalias', 'shell', 'macro' ]) self.t = Terminal() self.selected_client = None self.prompt = self.get_prompt() self.allow_cli_args = False # Alerts Thread self._stop_thread = False self._seen_clients = set(Client.unique_client_ids()) self._alert_thread = Thread() # Register the hook functions self.register_preloop_hook(self._alert_thread_preloop_hook) self.register_postloop_hook(self._alert_thread_postloop_hook) # Set the window title self.set_window_title('<< JSShell 2.0 >>') categorize([ BasePlugin.do_help, BasePlugin.do_quit, BasePlugin.do_py, BasePlugin.do_ipy, BasePlugin.do_history, BasePlugin.do_edit ], BasePlugin.CATEGORY_GENERAL) self.register_postparsing_hook( self._refresh_client_data_post_parse_hook)