MCPcopy Create free account
hub / github.com/blacklanternsecurity/bbot / worker

Method worker

bbot/core/engine.py:485–547  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

483 return True
484
485 async def worker(self):
486 self.engine_debug(f"{self.name}: starting worker")
487 try:
488 while 1:
489 client_id, binary = await self.socket.recv_multipart()
490 message = self.unpickle(binary)
491 self.engine_debug(f"{self.name} got message: {message}")
492 if self.check_error(message):
493 continue
494
495 cmd = message.get("c", None)
496 if not isinstance(cmd, int):
497 self.log.warning(f"{self.name}: no command sent in message: {message}")
498 continue
499
500 # -1 == cancel task
501 if cmd == -1:
502 self.engine_debug(f"{self.name} got cancel signal")
503 await self.send_socket_multipart(client_id, {"m": "CANCEL_OK"})
504 await self.cancel_task(client_id)
505 continue
506
507 # -99 == shutdown task
508 if cmd == -99:
509 self.log.verbose(f"{self.name} got shutdown signal")
510 await self.send_socket_multipart(client_id, {"m": "SHUTDOWN_OK"})
511 await self._shutdown()
512 return
513
514 args = message.get("a", ())
515 if not isinstance(args, tuple):
516 self.log.warning(f"{self.name}: received invalid args of type {type(args)}, should be tuple")
517 continue
518 kwargs = message.get("k", {})
519 if not isinstance(kwargs, dict):
520 self.log.warning(f"{self.name}: received invalid kwargs of type {type(kwargs)}, should be dict")
521 continue
522
523 command_name = self.CMDS[cmd]
524 command_fn = getattr(self, command_name, None)
525
526 if command_fn is None:
527 self.log.warning(f'{self.name} has no function named "{command_fn}"')
528 continue
529
530 if inspect.isasyncgenfunction(command_fn):
531 self.engine_debug(f"{self.name}: creating run-and-yield coroutine for {command_name}()")
532 coroutine = self.run_and_yield(client_id, command_fn, *args, **kwargs)
533 else:
534 self.engine_debug(f"{self.name}: creating run-and-return coroutine for {command_name}()")
535 coroutine = self.run_and_return(client_id, command_fn, *args, **kwargs)
536
537 self.engine_debug(f"{self.name}: creating task for {command_name}() coroutine")
538 task = asyncio.create_task(coroutine)
539 self.tasks[client_id] = task, command_fn, args, kwargs
540 self.engine_debug(f"{self.name}: finished creating task for {command_name}() coroutine")
541 except BaseException as e:
542 await self._shutdown()

Callers 1

server_processMethod · 0.80

Calls 14

check_errorMethod · 0.95
send_socket_multipartMethod · 0.95
cancel_taskMethod · 0.95
_shutdownMethod · 0.95
run_and_yieldMethod · 0.95
run_and_returnMethod · 0.95
in_exception_chainFunction · 0.90
engine_debugMethod · 0.80
unpickleMethod · 0.80
getMethod · 0.45
warningMethod · 0.45
verboseMethod · 0.45

Tested by

no test coverage detected