def run_command_and_get_its_response(self) -> dict: """Performs wanted action and returns details of what happened.""" if self.command == "ping": return {"response": "pong"} elif self.command == "log": text = self.request_dict["text"] log(text) return {"response": "Text logged"} elif self.command == "background-check": bridge_status = bridge.get_status() emulator_status = emulator.get_status() return { "response": "Background check done", "bridge_status": bridge_status, "emulator_status": emulator_status, "background_check": True, } elif self.command == "exit": emulator.stop() bridge.stop() log("Exiting", "red") exit(1) elif self.command.startswith("bridge"): return self.run_bridge_command() elif (self.command.startswith("emulator") or self.command == "select-num-of-words"): return self.run_emulator_command() elif self.command.startswith("regtest"): return self.run_regtest_command() else: return { "success": False, "error": f"Unknown command - {self.command}" }
def run_bridge_command(self) -> dict: if self.command == "bridge-start": version = self.request_dict.get("version", binaries.BRIDGES[0]) output_to_logfile = self.request_dict.get("output_to_logfile", True) bridge.start(version, proxy=BRIDGE_PROXY, output_to_logfile=output_to_logfile) response_text = f"Bridge version {version} started" if BRIDGE_PROXY: response_text += " with bridge proxy" return {"response": response_text} elif self.command == "bridge-stop": bridge.stop(proxy=BRIDGE_PROXY) response_text = "Stopping bridge" if BRIDGE_PROXY: response_text += " + stopping bridge proxy" return {"response": response_text} else: return { "success": False, "error": f"Unknown bridge command - {self.command}", }
def cleanup() -> None: emulator.stop() bridge.stop()
def message_received(client, server, message): print("Client(%d) request: %s" % (client['id'], message)) try: cmd = json.loads(message) cmdId = cmd["id"] cmdType = cmd["type"] except: server.send_message( client, json.dumps({ "success": False, "error": "Invalid json message" })) return response = None try: if cmdType == "ping": server.send_message(client, "pong") elif cmdType == "emulator-start": version = cmd.get("version") or DEFAULT_TREZOR_VERSION wipe = cmd.get("wipe") or False emulator.start(version, wipe) response = {"success": True} elif cmdType == "emulator-stop": emulator.stop() response = {"success": True} elif cmdType == "emulator-setup": emulator.setup_device(cmd["mnemonic"], cmd["pin"], cmd["passphrase_protection"], cmd["label"], cmd["needs_backup"]) response = {"success": True} elif cmdType == "emulator-press-yes": emulator.press_yes() response = {"success": True} elif cmdType == "emulator-press-no": emulator.press_no() response = {"success": True} elif cmdType == "emulator-input": emulator.input(cmd['value']) response = {"success": True} elif cmdType == "emulator-read-and-confirm-mnemonic": emulator.read_and_confirm_mnemonic() response = {"success": True} elif cmdType == "select-num-of-words": emulator.select_num_of_words(cmd['num']) response = {"success": True} elif cmdType == "emulator-swipe": emulator.swipe(cmd["direction"]) response = {"success": True} elif cmdType == "emulator-wipe": emulator.wipe_device() response = {"success": True} elif cmdType == "emulator-apply-settings": emulator.apply_settings(cmd['passphrase_always_on_device'], ) response = {"success": True} elif cmdType == "emulator-reset-device": resp = emulator.reset_device() print(resp) response = {"success": True} elif cmdType == "bridge-start": version = cmd.get("version") or DEFAULT_TREZORD_VERSION bridge.start(version) response = {"success": True} elif cmdType == "bridge-stop": bridge.stop() response = {"success": True} elif cmdType == "exit": emulator.stop() bridge.stop() os._exit(1) else: server.send_message( client, json.dumps({ "success": False, "error": "unknown command" })) return print("Client(%d) response: %s" % (client['id'], str(response))) if response is not None: server.send_message( client, json.dumps(dict(response, id=cmdId, success=True))) except Exception as e: print("Client(%d) response: %s" % (client['id'], str(e))) server.send_message( client, json.dumps({ "id": cmdId, "success": False, "error": str(e) }))
def cleanup(): emulator.stop() bridge.stop()