1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2010, 2013 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see
19 * <http://www.gnu.org/licenses/>.
27 #include "gsimpleproxyresolver.h"
28 #include "ginetaddress.h"
29 #include "ginetaddressmask.h"
30 #include "gnetworkingprivate.h"
36 * SECTION:gsimpleproxyresolver
37 * @short_description: Simple proxy resolver implementation
39 * @see_also: g_socket_client_set_proxy_resolver()
41 * #GSimpleProxyResolver is a simple #GProxyResolver implementation
42 * that handles a single default proxy, multiple URI-scheme-specific
43 * proxies, and a list of hosts that proxies should not be used for.
45 * #GSimpleProxyResolver is never the default proxy resolver, but it
46 * can be used as the base class for another proxy resolver
47 * implementation, or it can be created and used manually, such as
48 * with g_socket_client_set_proxy_resolver().
57 } GSimpleProxyResolverDomain;
59 struct _GSimpleProxyResolverPrivate {
60 gchar *default_proxy, **ignore_hosts;
61 GHashTable *uri_proxies;
63 GPtrArray *ignore_ips;
64 GSimpleProxyResolverDomain *ignore_domains;
67 static void g_simple_proxy_resolver_iface_init (GProxyResolverInterface *iface);
69 G_DEFINE_TYPE_WITH_CODE (GSimpleProxyResolver, g_simple_proxy_resolver, G_TYPE_OBJECT,
70 G_ADD_PRIVATE (GSimpleProxyResolver)
71 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,
72 g_simple_proxy_resolver_iface_init))
81 static void reparse_ignore_hosts (GSimpleProxyResolver *resolver);
84 g_simple_proxy_resolver_finalize (GObject *object)
86 GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (object);
87 GSimpleProxyResolverPrivate *priv = resolver->priv;
89 g_free (priv->default_proxy);
90 g_hash_table_destroy (priv->uri_proxies);
92 g_clear_pointer (&priv->ignore_hosts, g_strfreev);
93 /* This will free ignore_ips and ignore_domains */
94 reparse_ignore_hosts (resolver);
96 G_OBJECT_CLASS (g_simple_proxy_resolver_parent_class)->finalize (object);
100 g_simple_proxy_resolver_init (GSimpleProxyResolver *resolver)
102 resolver->priv = g_simple_proxy_resolver_get_instance_private (resolver);
103 resolver->priv->uri_proxies = g_hash_table_new_full (g_str_hash, g_str_equal,
108 g_simple_proxy_resolver_set_property (GObject *object,
113 GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (object);
117 case PROP_DEFAULT_PROXY:
118 g_simple_proxy_resolver_set_default_proxy (resolver, g_value_get_string (value));
121 case PROP_IGNORE_HOSTS:
122 g_simple_proxy_resolver_set_ignore_hosts (resolver, g_value_get_boxed (value));
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131 g_simple_proxy_resolver_get_property (GObject *object,
136 GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (object);
140 case PROP_DEFAULT_PROXY:
141 g_value_set_string (value, resolver->priv->default_proxy);
144 case PROP_IGNORE_HOSTS:
145 g_value_set_boxed (value, resolver->priv->ignore_hosts);
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154 reparse_ignore_hosts (GSimpleProxyResolver *resolver)
156 GSimpleProxyResolverPrivate *priv = resolver->priv;
157 GPtrArray *ignore_ips;
158 GArray *ignore_domains;
159 gchar *host, *tmp, *colon, *bracket;
161 GInetAddressMask *mask;
162 GSimpleProxyResolverDomain domain;
166 if (priv->ignore_ips)
167 g_ptr_array_free (priv->ignore_ips, TRUE);
168 if (priv->ignore_domains)
170 for (i = 0; priv->ignore_domains[i].name; i++)
171 g_free (priv->ignore_domains[i].name);
172 g_free (priv->ignore_domains);
174 priv->ignore_ips = NULL;
175 priv->ignore_domains = NULL;
177 if (!priv->ignore_hosts || !priv->ignore_hosts[0])
180 ignore_ips = g_ptr_array_new_with_free_func (g_object_unref);
181 ignore_domains = g_array_new (TRUE, FALSE, sizeof (GSimpleProxyResolverDomain));
183 for (i = 0; priv->ignore_hosts[i]; i++)
185 host = g_strchomp (priv->ignore_hosts[i]);
187 /* See if it's an IP address or IP/length mask */
188 mask = g_inet_address_mask_new_from_string (host, NULL);
191 g_ptr_array_add (ignore_ips, mask);
201 bracket = strchr (host, ']');
202 if (!bracket || !bracket[1] || bracket[1] != ':')
205 port = strtoul (bracket + 2, &tmp, 10);
213 colon = strchr (host, ':');
214 if (colon && !strchr (colon + 1, ':'))
216 /* hostname:port or IPv4:port */
217 port = strtoul (colon + 1, &tmp, 10);
224 iaddr = g_inet_address_new_from_string (host);
226 g_object_unref (iaddr);
229 if (g_str_has_prefix (host, "*."))
231 else if (*host == '.')
235 memset (&domain, 0, sizeof (domain));
236 domain.name = g_strdup (host);
237 domain.length = strlen (domain.name);
239 g_array_append_val (ignore_domains, domain);
243 g_warning ("Ignoring invalid ignore_hosts value '%s'", host);
247 priv->ignore_ips = ignore_ips;
249 g_ptr_array_free (ignore_ips, TRUE);
251 if (ignore_domains->len)
252 priv->ignore_domains = (GSimpleProxyResolverDomain *)ignore_domains->data;
253 g_array_free (ignore_domains, ignore_domains->len == 0);
257 ignore_host (GSimpleProxyResolver *resolver,
261 GSimpleProxyResolverPrivate *priv = resolver->priv;
262 gchar *ascii_host = NULL;
263 gboolean ignore = FALSE;
264 gsize offset, length;
267 if (priv->ignore_ips)
271 iaddr = g_inet_address_new_from_string (host);
274 for (i = 0; i < priv->ignore_ips->len; i++)
276 GInetAddressMask *mask = priv->ignore_ips->pdata[i];
278 if (g_inet_address_mask_matches (mask, iaddr))
285 g_object_unref (iaddr);
291 if (priv->ignore_domains)
294 if (g_hostname_is_non_ascii (host))
295 host = ascii_host = g_hostname_to_ascii (host);
297 length = strlen (host);
299 for (i = 0; length > 0 && priv->ignore_domains[i].length; i++)
301 GSimpleProxyResolverDomain *domain = &priv->ignore_domains[i];
303 if (domain->length > length)
306 offset = length - domain->length;
307 if ((domain->port == 0 || domain->port == port) &&
308 (offset == 0 || (offset > 0 && host[offset - 1] == '.')) &&
309 (g_ascii_strcasecmp (domain->name, host + offset) == 0))
323 g_simple_proxy_resolver_lookup (GProxyResolver *proxy_resolver,
325 GCancellable *cancellable,
328 GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (proxy_resolver);
329 GSimpleProxyResolverPrivate *priv = resolver->priv;
330 const gchar *proxy = NULL;
333 if (priv->ignore_ips || priv->ignore_domains)
338 if (g_uri_split_network (uri, G_URI_FLAGS_NONE, NULL,
339 &host, &port, NULL) &&
340 ignore_host (resolver, host, port > 0 ? port : 0))
346 if (!proxy && g_hash_table_size (priv->uri_proxies))
348 gchar *scheme = g_ascii_strdown (uri, strcspn (uri, ":"));
350 proxy = g_hash_table_lookup (priv->uri_proxies, scheme);
355 proxy = priv->default_proxy;
359 if (!strncmp (proxy, "socks://", 8))
361 proxies = g_new0 (gchar *, 4);
362 proxies[0] = g_strdup_printf ("socks5://%s", proxy + 8);
363 proxies[1] = g_strdup_printf ("socks4a://%s", proxy + 8);
364 proxies[2] = g_strdup_printf ("socks4://%s", proxy + 8);
369 proxies = g_new0 (gchar *, 2);
370 proxies[0] = g_strdup (proxy);
377 g_simple_proxy_resolver_lookup_async (GProxyResolver *proxy_resolver,
379 GCancellable *cancellable,
380 GAsyncReadyCallback callback,
383 GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (proxy_resolver);
385 GError *error = NULL;
388 task = g_task_new (resolver, cancellable, callback, user_data);
389 g_task_set_source_tag (task, g_simple_proxy_resolver_lookup_async);
391 proxies = g_simple_proxy_resolver_lookup (proxy_resolver, uri,
392 cancellable, &error);
394 g_task_return_pointer (task, proxies, (GDestroyNotify)g_strfreev);
396 g_task_return_error (task, error);
397 g_object_unref (task);
401 g_simple_proxy_resolver_lookup_finish (GProxyResolver *resolver,
402 GAsyncResult *result,
405 g_return_val_if_fail (g_task_is_valid (result, resolver), NULL);
407 return g_task_propagate_pointer (G_TASK (result), error);
411 g_simple_proxy_resolver_class_init (GSimpleProxyResolverClass *resolver_class)
413 GObjectClass *object_class = G_OBJECT_CLASS (resolver_class);
415 object_class->get_property = g_simple_proxy_resolver_get_property;
416 object_class->set_property = g_simple_proxy_resolver_set_property;
417 object_class->finalize = g_simple_proxy_resolver_finalize;
420 * GSimpleProxyResolver:default-proxy: (nullable)
422 * The default proxy URI that will be used for any URI that doesn't
423 * match #GSimpleProxyResolver:ignore-hosts, and doesn't match any
424 * of the schemes set with g_simple_proxy_resolver_set_uri_proxy().
426 * Note that as a special case, if this URI starts with
427 * "socks://", #GSimpleProxyResolver will treat it as referring
428 * to all three of the socks5, socks4a, and socks4 proxy types.
430 g_object_class_install_property (object_class, PROP_DEFAULT_PROXY,
431 g_param_spec_string ("default-proxy",
433 P_("The default proxy URI"),
436 G_PARAM_STATIC_STRINGS));
439 * GSimpleProxyResolver:ignore-hosts:
441 * A list of hostnames and IP addresses that the resolver should
442 * allow direct connections to.
444 * Entries can be in one of 4 formats:
446 * - A hostname, such as "example.com", ".example.com", or
447 * "*.example.com", any of which match "example.com" or
448 * any subdomain of it.
450 * - An IPv4 or IPv6 address, such as "192.168.1.1",
451 * which matches only that address.
453 * - A hostname or IP address followed by a port, such as
454 * "example.com:80", which matches whatever the hostname or IP
455 * address would match, but only for URLs with the (explicitly)
456 * indicated port. In the case of an IPv6 address, the address
457 * part must appear in brackets: "[::1]:443"
459 * - An IP address range, given by a base address and prefix length,
460 * such as "fe80::/10", which matches any address in that range.
462 * Note that when dealing with Unicode hostnames, the matching is
463 * done against the ASCII form of the name.
465 * Also note that hostname exclusions apply only to connections made
466 * to hosts identified by name, and IP address exclusions apply only
467 * to connections made to hosts identified by address. That is, if
468 * example.com has an address of 192.168.1.1, and the :ignore-hosts list
469 * contains only "192.168.1.1", then a connection to "example.com"
470 * (eg, via a #GNetworkAddress) will use the proxy, and a connection to
471 * "192.168.1.1" (eg, via a #GInetSocketAddress) will not.
473 * These rules match the "ignore-hosts"/"noproxy" rules most
474 * commonly used by other applications.
476 g_object_class_install_property (object_class, PROP_IGNORE_HOSTS,
477 g_param_spec_boxed ("ignore-hosts",
479 P_("Hosts that will not use the proxy"),
482 G_PARAM_STATIC_STRINGS));
487 g_simple_proxy_resolver_iface_init (GProxyResolverInterface *iface)
489 iface->lookup = g_simple_proxy_resolver_lookup;
490 iface->lookup_async = g_simple_proxy_resolver_lookup_async;
491 iface->lookup_finish = g_simple_proxy_resolver_lookup_finish;
495 * g_simple_proxy_resolver_new:
496 * @default_proxy: (nullable): the default proxy to use, eg
497 * "socks://192.168.1.1"
498 * @ignore_hosts: (array zero-terminated=1) (nullable): an optional list of hosts/IP addresses
499 * to not use a proxy for.
501 * Creates a new #GSimpleProxyResolver. See
502 * #GSimpleProxyResolver:default-proxy and
503 * #GSimpleProxyResolver:ignore-hosts for more details on how the
504 * arguments are interpreted.
506 * Returns: (transfer full) a new #GSimpleProxyResolver
511 g_simple_proxy_resolver_new (const gchar *default_proxy,
512 gchar **ignore_hosts)
514 return g_object_new (G_TYPE_SIMPLE_PROXY_RESOLVER,
515 "default-proxy", default_proxy,
516 "ignore-hosts", ignore_hosts,
521 * g_simple_proxy_resolver_set_default_proxy:
522 * @resolver: a #GSimpleProxyResolver
523 * @default_proxy: (nullable): the default proxy to use
525 * Sets the default proxy on @resolver, to be used for any URIs that
526 * don't match #GSimpleProxyResolver:ignore-hosts or a proxy set
527 * via g_simple_proxy_resolver_set_uri_proxy().
529 * If @default_proxy starts with "socks://",
530 * #GSimpleProxyResolver will treat it as referring to all three of
531 * the socks5, socks4a, and socks4 proxy types.
536 g_simple_proxy_resolver_set_default_proxy (GSimpleProxyResolver *resolver,
537 const gchar *default_proxy)
539 g_return_if_fail (G_IS_SIMPLE_PROXY_RESOLVER (resolver));
540 g_return_if_fail (default_proxy == NULL || g_uri_is_valid (default_proxy, G_URI_FLAGS_NONE, NULL));
542 g_free (resolver->priv->default_proxy);
543 resolver->priv->default_proxy = g_strdup (default_proxy);
544 g_object_notify (G_OBJECT (resolver), "default-proxy");
548 * g_simple_proxy_resolver_set_ignore_hosts:
549 * @resolver: a #GSimpleProxyResolver
550 * @ignore_hosts: (array zero-terminated=1): %NULL-terminated list of hosts/IP addresses
551 * to not use a proxy for
553 * Sets the list of ignored hosts.
555 * See #GSimpleProxyResolver:ignore-hosts for more details on how the
556 * @ignore_hosts argument is interpreted.
561 g_simple_proxy_resolver_set_ignore_hosts (GSimpleProxyResolver *resolver,
562 gchar **ignore_hosts)
564 g_return_if_fail (G_IS_SIMPLE_PROXY_RESOLVER (resolver));
566 g_strfreev (resolver->priv->ignore_hosts);
567 resolver->priv->ignore_hosts = g_strdupv (ignore_hosts);
568 reparse_ignore_hosts (resolver);
569 g_object_notify (G_OBJECT (resolver), "ignore-hosts");
573 * g_simple_proxy_resolver_set_uri_proxy:
574 * @resolver: a #GSimpleProxyResolver
575 * @uri_scheme: the URI scheme to add a proxy for
576 * @proxy: the proxy to use for @uri_scheme
578 * Adds a URI-scheme-specific proxy to @resolver; URIs whose scheme
579 * matches @uri_scheme (and which don't match
580 * #GSimpleProxyResolver:ignore-hosts) will be proxied via @proxy.
582 * As with #GSimpleProxyResolver:default-proxy, if @proxy starts with
583 * "socks://", #GSimpleProxyResolver will treat it
584 * as referring to all three of the socks5, socks4a, and socks4 proxy
590 g_simple_proxy_resolver_set_uri_proxy (GSimpleProxyResolver *resolver,
591 const gchar *uri_scheme,
594 g_return_if_fail (G_IS_SIMPLE_PROXY_RESOLVER (resolver));
596 g_hash_table_replace (resolver->priv->uri_proxies,
597 g_ascii_strdown (uri_scheme, -1),