From 42cb8dfb95d899c1dd0312e499e382ba4beb5040 Mon Sep 17 00:00:00 2001 From: Michael Catanzaro Date: Wed, 24 May 2023 17:36:03 -0500 Subject: [PATCH 1/3] guri: the default port for SOCKS URLs is 1080 This is true for socks://, socks4://, socks4a://, and socks5://. I could list them individually and risk breaking in the future if socks6:// ever exists, or test for "socks" and risk breaking if a future URL scheme begins with "socks" but doesn't use port 1080. I picked the latter. --- glib/guri.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/glib/guri.c b/glib/guri.c index 950becf12..f470ddcb1 100644 --- a/glib/guri.c +++ b/glib/guri.c @@ -820,6 +820,9 @@ default_scheme_port (const char *scheme) if (strcmp (scheme, "ftp") == 0) return 21; + if (strstr (scheme, "socks") == scheme) + return 1080; + return -1; } From cc5bb80b9a5f3c9a6dc9c0709e44786ce8ed373f Mon Sep 17 00:00:00 2001 From: Michael Catanzaro Date: Wed, 24 May 2023 17:37:57 -0500 Subject: [PATCH 2/3] Fix failure to respect default port of proxy URLs Currently we require explicitly specifying the port when configuring a proxy server, which is seriously weird. I take the fact that nobody reported a bug until 2022 to indicate that almost nobody is using proxies. Whatever. Let's assume that if no port is provided, the default port for the protocol should be used instead. For example, you can now specify in GNOME settings that your proxy server is https://example.com and it will work. Previously, you had to write https://example.com:443. Yuck! This was originally reported as GProxyResolver bug, but nothing is actually wrong there. It's actually GProxyAddressEnumerator that gets tripped up by URLs returned by GProxyResolver without a default port. This breaks GSocketClient. Fixing this requires exposing GUri's _default_scheme_port() function to GIO. I considered copy/pasting it since it's not very much code, but I figure the private call mechanism is probably not too expensive, and I don't like code duplication. Fixes #2832 --- gio/gproxyaddressenumerator.c | 7 ++++++- glib/glib-private.c | 2 ++ glib/glib-private.h | 5 +++++ glib/guri.c | 9 +++++---- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/gio/gproxyaddressenumerator.c b/gio/gproxyaddressenumerator.c index 4e6d58a2d..98987f227 100644 --- a/gio/gproxyaddressenumerator.c +++ b/gio/gproxyaddressenumerator.c @@ -29,6 +29,7 @@ #include "ginetaddress.h" #include "gioerror.h" #include "glibintl.h" +#include "glib-private.h" #include "gnetworkaddress.h" #include "gnetworkingprivate.h" #include "gproxy.h" @@ -158,9 +159,13 @@ next_enumerator (GProxyAddressEnumeratorPrivate *priv) else { GError *error = NULL; + int default_port; - connectable = g_network_address_parse_uri (priv->proxy_uri, 0, &error); + default_port = GLIB_PRIVATE_CALL (g_uri_get_default_scheme_port) (priv->proxy_type); + if (default_port == -1) + default_port = 0; + connectable = g_network_address_parse_uri (priv->proxy_uri, default_port, &error); if (error) { g_warning ("Invalid proxy URI '%s': %s", diff --git a/glib/glib-private.c b/glib/glib-private.c index 6b2205f86..9f4887aa2 100644 --- a/glib/glib-private.c +++ b/glib/glib-private.c @@ -69,6 +69,8 @@ glib__private__ (void) g_win32_pop_invalid_parameter_handler, g_find_program_for_path, + + g_uri_get_default_scheme_port, }; return &table; diff --git a/glib/glib-private.h b/glib/glib-private.h index 85bea5ffe..f49e38f7b 100644 --- a/glib/glib-private.h +++ b/glib/glib-private.h @@ -158,6 +158,8 @@ char *g_find_program_for_path (const char *program, const char *path, const char *working_dir); +int g_uri_get_default_scheme_port (const char *scheme); + #define GLIB_PRIVATE_CALL(symbol) (glib__private__()->symbol) @@ -222,6 +224,9 @@ typedef struct { const char *path, const char *working_dir); + /* See guri.c */ + int (* g_uri_get_default_scheme_port) (const char *scheme); + /* Add other private functions here, initialize them in glib-private.c */ } GLibPrivateVTable; diff --git a/glib/guri.c b/glib/guri.c index f470ddcb1..58725503d 100644 --- a/glib/guri.c +++ b/glib/guri.c @@ -25,6 +25,7 @@ #include "glib.h" #include "glibintl.h" +#include "glib-private.h" #include "guriprivate.h" /** @@ -808,8 +809,8 @@ normalize_port (const char *scheme, return port; } -static int -default_scheme_port (const char *scheme) +int +g_uri_get_default_scheme_port (const char *scheme) { if (strcmp (scheme, "http") == 0 || strcmp (scheme, "ws") == 0) return 80; @@ -1022,7 +1023,7 @@ g_uri_split_internal (const gchar *uri_string, } if (port && *port == -1) - *port = default_scheme_port (scheme_str); + *port = g_uri_get_default_scheme_port (scheme_str); } g_free (normalized_scheme); @@ -2510,7 +2511,7 @@ g_uri_get_port (GUri *uri) g_return_val_if_fail (uri != NULL, -1); if (uri->port == -1 && uri->flags & G_URI_FLAGS_SCHEME_NORMALIZE) - return default_scheme_port (uri->scheme); + return g_uri_get_default_scheme_port (uri->scheme); return uri->port; } From d624c850b2aae0235d0f2f2a7410e40672ed6588 Mon Sep 17 00:00:00 2001 From: Michael Catanzaro Date: Tue, 30 May 2023 09:52:33 -0500 Subject: [PATCH 3/3] Add some tests for socks:// URI default ports This also tests socks5h:// which we don't actually support yet, just to make sure it works, which it does. --- glib/tests/uri.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/glib/tests/uri.c b/glib/tests/uri.c index cf209efd0..0f32888d2 100644 --- a/glib/tests/uri.c +++ b/glib/tests/uri.c @@ -1912,6 +1912,16 @@ static const struct "ftp", "", 21 }, { "scheme://foo", G_URI_FLAGS_SCHEME_NORMALIZE, "scheme", "", -1 }, + { "socks://foo", G_URI_FLAGS_SCHEME_NORMALIZE, + "socks", "", 1080 }, + { "socks4://foo", G_URI_FLAGS_SCHEME_NORMALIZE, + "socks4", "", 1080 }, + { "socks4a://foo", G_URI_FLAGS_SCHEME_NORMALIZE, + "socks4a", "", 1080 }, + { "socks5://foo", G_URI_FLAGS_SCHEME_NORMALIZE, + "socks5", "", 1080 }, + { "socks5h://foo", G_URI_FLAGS_SCHEME_NORMALIZE, + "socks5h", "", 1080 }, }; static const struct