From a1a89d10e5da77e4ac71fcf1cc645c1822fedd97 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Wed, 24 Nov 2021 14:37:01 +0100 Subject: restore: Use new reverse proxy implementation from latest libimobiledevice with fallback to the legacy code with a compile time warning. The legacy code will be removed in the future once a newer libimobiledevice has been officially released. --- configure.ac | 16 ++++++++++++++-- src/restore.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index e748924..ed81dde 100644 --- a/configure.ac +++ b/configure.ac @@ -113,11 +113,23 @@ AC_CACHE_CHECK(for enum idevice_connection_type, ac_cv_enum_idevice_connection_t ]], [[ enum idevice_connection_type conn_type = CONNECTION_USBMUXD; ]])],[ac_cv_enum_idevice_connection_type=yes],[ac_cv_enum_idevice_connection_type=no])) - -if (test "$ac_cv_enum_idevice_connection_type" = "yes"); then +if test "$ac_cv_enum_idevice_connection_type" = "yes"; then AC_DEFINE(HAVE_ENUM_IDEVICE_CONNECTION_TYPE, 1, [Define if enum idevice_connection_type is available]) fi +# check if libimobiledevice has reverse proxy support +AC_CACHE_CHECK(for reverse proxy support in libimobiledevice, ac_cv_reverse_proxy, + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ + #include + #include + ]], [[ + reverse_proxy_client_t rp = NULL; + reverse_proxy_client_create_with_port(NULL, &rp, REVERSE_PROXY_DEFAULT_PORT); + ]])],[ac_cv_reverse_proxy=yes],[ac_cv_reverse_proxy=no])) +if test "$ac_cv_reverse_proxy" = "yes"; then + AC_DEFINE(HAVE_REVERSE_PROXY, 1, [Define if libimobiledevice has a reverse proxy implementation]) +fi + CFLAGS="$CACHED_CFLAGS" AC_ARG_WITH([openssl], diff --git a/src/restore.c b/src/restore.c index 13c3625..0593f2d 100644 --- a/src/restore.c +++ b/src/restore.c @@ -30,6 +30,11 @@ #include #include #include +#ifdef HAVE_REVERSE_PROXY +#include +#else +#warning Linking against libimobiledevice without reverse proxy support. Please update to a newer version of libimobiledevice, the legacy code used will be removed in a future version of idevicerestore. +#endif #include #include @@ -3604,6 +3609,18 @@ plist_t restore_supported_message_types() return dict; } +#ifdef HAVE_REVERSE_PROXY +static void rp_log_cb(reverse_proxy_client_t client, const char* log_msg, void* user_data) +{ + info("ReverseProxy[%s]: %s\n", (reverse_proxy_get_type(client) == RP_TYPE_CTRL) ? "Ctrl" : "Conn", log_msg); +} + +static void rp_status_cb(reverse_proxy_client_t client, reverse_proxy_status_t status, const char* status_msg, void* user_data) +{ + info("ReverseProxy[%s]: (status=%d) %s\n", (reverse_proxy_get_type(client) == RP_TYPE_CTRL) ? "Ctrl" : "Conn", status, status_msg); +} +#endif + int restore_device(struct idevicerestore_client_t* client, plist_t build_identity, const char* filesystem) { int err = 0; @@ -3614,7 +3631,9 @@ int restore_device(struct idevicerestore_client_t* client, plist_t build_identit idevice_t device = NULL; restored_client_t restore = NULL; restored_error_t restore_error = RESTORE_E_SUCCESS; +#ifndef HAVE_REVERSE_PROXY THREAD_T fdr_thread = THREAD_T_NULL; +#endif restore_finished = 0; @@ -3695,6 +3714,34 @@ int restore_device(struct idevicerestore_client_t* client, plist_t build_identit client->restore->bbtss = plist_copy(client->tss); } +#ifdef HAVE_REVERSE_PROXY + info("Starting Reverse Proxy\n"); + reverse_proxy_client_t rproxy = NULL; + if (reverse_proxy_client_create_with_port(device, &rproxy, REVERSE_PROXY_DEFAULT_PORT) != REVERSE_PROXY_E_SUCCESS) { + error("Could not create Reverse Proxy\n"); + } else { + if (client->flags & FLAG_DEBUG) { + reverse_proxy_client_set_log_callback(rproxy, rp_log_cb, NULL); + } + reverse_proxy_client_set_status_callback(rproxy, rp_status_cb, NULL); + if (reverse_proxy_client_start_proxy(rproxy, 2) != REVERSE_PROXY_E_SUCCESS) { + error("Device didn't accept new reverse proxy protocol, trying to use old one\n"); + reverse_proxy_client_free(rproxy); + rproxy = NULL; + if (reverse_proxy_client_create_with_port(device, &rproxy, REVERSE_PROXY_DEFAULT_PORT) != REVERSE_PROXY_E_SUCCESS) { + error("Could not create Reverse Proxy\n"); + } else { + if (client->flags & FLAG_DEBUG) { + reverse_proxy_client_set_log_callback(rproxy, rp_log_cb, NULL); + } + reverse_proxy_client_set_status_callback(rproxy, rp_status_cb, NULL); + if (reverse_proxy_client_start_proxy(rproxy, 1) != REVERSE_PROXY_E_SUCCESS) { + error("ReverseProxy: Device didn't accept old protocol, giving up\n"); + } + } + } + } +#else fdr_client_t fdr_control_channel = NULL; info("Starting FDR listener thread\n"); if (!fdr_connect(device, FDR_CTRL, &fdr_control_channel)) { @@ -3706,6 +3753,7 @@ int restore_device(struct idevicerestore_client_t* client, plist_t build_identit error("ERROR: Failed to start FDR Ctrl channel\n"); // FIXME: We might want to return failure here as it will likely fail } +#endif plist_t opts = plist_new_dict(); // FIXME: required? @@ -3977,6 +4025,9 @@ int restore_device(struct idevicerestore_client_t* client, plist_t build_identit message = NULL; } +#ifdef HAVE_REVERSE_PROXY + reverse_proxy_client_free(rproxy); +#else if (thread_alive(fdr_thread)) { if (fdr_control_channel) { fdr_disconnect(fdr_control_channel); @@ -3984,6 +4035,7 @@ int restore_device(struct idevicerestore_client_t* client, plist_t build_identit fdr_control_channel = NULL; } } +#endif restore_client_free(client); return err; -- cgit v1.1-32-gdbae