From 0b7ef71a157bea746ec467d7078480cfb30d62fc Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Thu, 16 Jun 2022 10:29:34 +0100 Subject: Use GProxyResolver to auto-detect the correct proxy --- client/as-util.c | 12 ++++++++++++ libappstream-glib/as-app-validate.c | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/client/as-util.c b/client/as-util.c index 4c3fdaa..ce973ed 100644 --- a/client/as-util.c +++ b/client/as-util.c @@ -39,6 +39,7 @@ typedef struct { GCancellable *cancellable; AsProfile *profile; CURL *curl; + GProxyResolver *proxy_resolver; } AsUtilPrivate; typedef gboolean (*AsUtilPrivateCb) (AsUtilPrivate *util, @@ -3425,6 +3426,9 @@ as_util_mirror_screenshots_app_url (AsUtilPrivate *priv, } else if (priv->nonet) { as_util_app_log (app, "Missing %s:%s", url, cache_filename); } else { + g_auto(GStrv) proxies = NULL; + g_autoptr(GError) error_proxy = NULL; + if (g_str_has_prefix (url, "file:")) { g_set_error (error, AS_ERROR, @@ -3434,6 +3438,13 @@ as_util_mirror_screenshots_app_url (AsUtilPrivate *priv, } as_util_app_log (app, "Downloading %s", url); + /* set proxy if required */ + proxies = g_proxy_resolver_lookup(priv->proxy_resolver, url, NULL, &error_proxy); + if (proxies == NULL) { + g_warning("failed to lookup proxy for %s: %s", url, error_proxy->message); + } else if (g_strcmp0(proxies[0], "direct://") != 0) { + (void)curl_easy_setopt(priv->curl, CURLOPT_PROXY, proxies[0]); + } (void)curl_easy_setopt(priv->curl, CURLOPT_URL, url); (void)curl_easy_setopt(priv->curl, CURLOPT_ERRORBUFFER, errbuf); (void)curl_easy_setopt(priv->curl, @@ -4446,6 +4457,7 @@ main (int argc, char *argv[]) priv->profile = as_profile_new (); /* networking */ + priv->proxy_resolver = g_proxy_resolver_get_default(); priv->curl = curl_easy_init(); (void)curl_easy_setopt(priv->curl, CURLOPT_USERAGENT, "appstream-util"); (void)curl_easy_setopt(priv->curl, CURLOPT_CONNECTTIMEOUT, 10L); diff --git a/libappstream-glib/as-app-validate.c b/libappstream-glib/as-app-validate.c index 0f9130b..f18d245 100644 --- a/libappstream-glib/as-app-validate.c +++ b/libappstream-glib/as-app-validate.c @@ -22,6 +22,7 @@ typedef struct { GPtrArray *screenshot_urls; GPtrArray *probs; CURL *curl; + GProxyResolver *proxy_resolver; gboolean previous_para_was_short; gchar *previous_para_was_short_str; guint para_chars_before_list; @@ -437,8 +438,10 @@ ai_app_validate_image_check (AsImage *im, AsAppValidateHelper *helper) guint ss_size_height_min = 351; guint ss_size_width_max = 1600; guint ss_size_width_min = 624; + g_auto(GStrv) proxies = NULL; g_autoptr(GdkPixbuf) pixbuf = NULL; g_autoptr(GByteArray) buf = g_byte_array_new(); + g_autoptr(GError) error_proxy = NULL; g_autoptr(GInputStream) stream = NULL; /* make the requirements more strict */ @@ -466,6 +469,15 @@ ai_app_validate_image_check (AsImage *im, AsAppValidateHelper *helper) (void)curl_easy_setopt(helper->curl, CURLOPT_WRITEFUNCTION, as_app_validate_download_write_callback_cb); + + /* set proxy if required */ + proxies = g_proxy_resolver_lookup(helper->proxy_resolver, url, NULL, &error_proxy); + if (proxies == NULL) { + g_warning("failed to lookup proxy for %s: %s", url, error_proxy->message); + } else if (g_strcmp0(proxies[0], "direct://") != 0) { + (void)curl_easy_setopt(helper->curl, CURLOPT_PROXY, proxies[0]); + } + (void)curl_easy_setopt(helper->curl, CURLOPT_WRITEDATA, buf); res = curl_easy_perform(helper->curl); if (res != CURLE_OK) { -- cgit v1.2.1