ducky.tools.vm module

class ducky.tools.vm.DuckyProtocol(logger, options, config)[source]

Bases: autobahn.twisted.websocket.WebSocketServerProtocol

Protocol handling communication between VM and remote terminal emulator.

Parameters:
  • logger (logging.Logger) – Logger instanceto use for logging.
  • options – command-line options, as returned by option parser.
  • config (ducky.config.MachineConfig) – VM configuration.
_machines = []
onClose(wasClean, code, reason)[source]

Called when connection ends. Tell VM to halt, and wait for its thread to finish. Then, print some VM stats.

onMessage(payload, isBinary)[source]

Called when new data arrived from client. Feeds the data to VM’s input stream.

See autobahn.twisted.websocket.WebSocketServerProtocol.onMessage().

onOpen(*args, **kwargs)[source]

Called when new client connects to the server.

This callback will setup and start new VM, connecting it to remote terminal by wrapping this protocol instance in two streams (input/output), and spawn a fresh new thread for it.

run_machine()[source]

Wraps VM’s run() method by try/except clause, and call protocols sendClose() method when VM finishes.

This is the target of VM`s thread.

class ducky.tools.vm.DuckySocketServerFactory(logger, options, config, *args, **kwargs)[source]

Bases: autobahn.twisted.websocket.WebSocketServerFactory

buildProtocol(*args, **kwargs)[source]
class ducky.tools.vm.WSInputStream(protocol, *args, **kwargs)[source]

Bases: ducky.streams.InputStream

Websocket input stream, receiving bytes from opened websocket, and pushing them to keyboard frontend device.

Stream has an internal LIFO buffer that is being fed by protocol, and consumed by VM frontend device.

Parameters:protocol (DuckyProtocol) – protocol instance with opened websocket.
_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 39
_abc_registry = <_weakrefset.WeakSet object>
enqueue(buff)[source]

Called by protocol instance, to add newly received data to stream’s buffer.

Parameters:buff (bytearray) – recerived bytes.
has_poll_support()[source]

See ducky.streams.Stream.has_poll_support().

read(size=None)[source]

See ducky.streams.Stream.read().

register_with_reactor(reactor, on_read=None, on_error=None)[source]

See ducky.streams.Stream.register_with_reactor().

unregister_with_reactor(reactor)[source]

See ducky.streams.Stream.unregister_with_reactor().

class ducky.tools.vm.WSOutputStream(protocol, *args, **kwargs)[source]

Bases: ducky.streams.OutputStream

Websocket output stream, receiving bytes from TTY frontend, and pushing them to protocol’s socket.

Parameters:protocol (DuckyProtocol) – protocol instance with opened websocket.
_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 39
_abc_registry = <_weakrefset.WeakSet object>
write(buff)[source]

Write buffer into the socket.

Called by device from machine thread, therefore this method hands buffer over to the reactor thread.

Parameters:buff (bytearray) – bytes to send to client.
ducky.tools.vm.main()[source]
ducky.tools.vm.print_machine_stats(logger, M)[source]
ducky.tools.vm.process_config_options(logger, options, config_file=None, set_options=None, add_options=None, enable_devices=None, disable_devices=None)[source]

Load VM config file, and apply all necessary changes, as requested by command-line options.

Parameters:
  • logger (logging.Logger) – Logger instance to use for logging.
  • options – command-line options, as returned by option parser.
  • config_file (string) – path to configuration file.
Return type:

ducky.config.MachineConfig

Returns:

VM configuration.