Beispiel #1
0
 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}"
         }
Beispiel #2
0
def cleanup() -> None:
    emulator.stop()
    bridge.stop()
Beispiel #3
0
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)
            }))
Beispiel #4
0
def cleanup():
    emulator.stop()
    bridge.stop()
Beispiel #5
0
 def run_emulator_command(self) -> dict:
     if self.command == "emulator-start":
         version = self.request_dict.get("version",
                                         binaries.FIRMWARES["TT"][0])
         wipe = self.request_dict.get("wipe", False)
         output_to_logfile = self.request_dict.get("output_to_logfile",
                                                   True)
         save_screenshots = self.request_dict.get("save_screenshots", False)
         emulator.start(version, wipe, output_to_logfile, save_screenshots)
         response_text = f"Emulator version {version} started"
         if wipe:
             response_text += " and wiped to be empty"
         return {"response": response_text}
     elif self.command == "emulator-start-from-url":
         url = self.request_dict["url"]
         model = self.request_dict["model"]
         wipe = self.request_dict.get("wipe", False)
         output_to_logfile = self.request_dict.get("output_to_logfile",
                                                   True)
         save_screenshots = self.request_dict.get("save_screenshots", False)
         emulator.start_from_url(url, model, wipe, output_to_logfile,
                                 save_screenshots)
         response_text = f"Emulator downloaded from {url} and started"
         if wipe:
             response_text += " and wiped to be empty"
         return {"response": response_text}
     elif self.command == "emulator-stop":
         emulator.stop()
         return {"response": "Emulator stopped"}
     elif self.command == "emulator-setup":
         emulator.setup_device(
             self.request_dict["mnemonic"],
             self.request_dict["pin"],
             self.request_dict["passphrase_protection"],
             self.request_dict["label"],
             self.request_dict.get("needs_backup", False),
         )
         return {"response": f"Emulator set up - {self.request_dict}"}
     elif self.command == "emulator-press-yes":
         emulator.press_yes()
         return {"response": "Pressed YES"}
     elif self.command == "emulator-press-no":
         emulator.press_no()
         return {"response": "Pressed NO"}
     elif self.command == "emulator-input":
         value = self.request_dict["value"]
         emulator.input(value)
         return {"response": f"Input into emulator: {value}"}
     elif self.command == "emulator-click":
         x = self.request_dict["x"]
         y = self.request_dict["y"]
         emulator.click(x=x, y=y)
         return {"response": f"Clicked in emulator: x: {x}, y: {y}"}
     elif self.command == "emulator-read-and-confirm-mnemonic":
         emulator.read_and_confirm_mnemonic()
         return {"response": "Read and confirm mnemonic"}
     elif self.command == "emulator-read-and-confirm-shamir-mnemonic":
         shares = self.request_dict.get("shares", 1)
         threshold = self.request_dict.get("threshold", 1)
         emulator.read_and_confirm_shamir_mnemonic(shares=shares,
                                                   threshold=threshold)
         return {
             "response":
             f"Read and confirm Shamir mnemonic for {shares} shares and threshold {threshold}."
         }
     elif self.command == "emulator-allow-unsafe-paths":
         emulator.allow_unsafe()
         return {"response": "Allowed unsafe path"}
     elif self.command == "select-num-of-words":
         num = self.request_dict["num"]
         emulator.select_num_of_words(num)
         return {"response": f"Selected {num} words"}
     elif self.command == "emulator-swipe":
         direction = self.request_dict["direction"]
         emulator.swipe(direction)
         return {"response": f"Swiped {direction}"}
     elif self.command == "emulator-wipe":
         emulator.wipe_device()
         return {"response": "Device wiped"}
     elif self.command == "emulator-apply-settings":
         # Relaying all the relevant fields from the request, to make sure
         #   the client is notified when it sends an unknown field
         #   and the function will throw an Exception
         settings_fields = deepcopy(self.request_dict)
         settings_fields.pop("type", None)
         settings_fields.pop("id", None)
         emulator.apply_settings(**settings_fields)
         return {
             "response": f"Applied settings on emulator {settings_fields}"
         }
     elif self.command == "emulator-reset-device":
         emulator.reset_device()
         return {"response": "Device reset"}
     elif self.command == "emulator-get-screenshot":
         screen_base_64 = emulator.get_current_screen()
         return {"response": screen_base_64}
     else:
         return {
             "success": False,
             "error": f"Unknown emulator command - {self.command}",
         }