1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2010, 2013 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see
17 * <http://www.gnu.org/licenses/>.
25 #include "gsimpleproxyresolver.h"
26 #include "ginetaddress.h"
27 #include "ginetaddressmask.h"
28 #include "gnetworkingprivate.h"
34 * SECTION:gsimpleproxyresolver
35 * @short_description: Simple proxy resolver implementation
37 * @see_also: g_socket_client_set_proxy_resolver()
39 * #GSimpleProxyResolver is a simple #GProxyResolver implementation
40 * that handles a single default proxy, multiple URI-scheme-specific
41 * proxies, and a list of hosts that proxies should not be used for.
43 * #GSimpleProxyResolver is never the default proxy resolver, but it
44 * can be used as the base class for another proxy resolver
45 * implementation, or it can be created and used manually, such as
46 * with g_socket_client_set_proxy_resolver().
55 } GSimpleProxyResolverDomain;
57 struct _GSimpleProxyResolverPrivate {
58 gchar *default_proxy, **ignore_hosts;
59 GHashTable *uri_proxies;
61 GPtrArray *ignore_ips;
62 GSimpleProxyResolverDomain *ignore_domains;
65 static void g_simple_proxy_resolver_iface_init (GProxyResolverInterface *iface);
67 G_DEFINE_TYPE_WITH_CODE (GSimpleProxyResolver, g_simple_proxy_resolver, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,
69 g_simple_proxy_resolver_iface_init))
78 static void reparse_ignore_hosts (GSimpleProxyResolver *resolver);
81 g_simple_proxy_resolver_finalize (GObject *object)
83 GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (object);
84 GSimpleProxyResolverPrivate *priv = resolver->priv;
86 g_free (priv->default_proxy);
87 g_hash_table_destroy (priv->uri_proxies);
89 g_clear_pointer (&priv->ignore_hosts, g_strfreev);
90 /* This will free ignore_ips and ignore_domains */
91 reparse_ignore_hosts (resolver);
93 G_OBJECT_CLASS (g_simple_proxy_resolver_parent_class)->finalize (object);
97 g_simple_proxy_resolver_init (GSimpleProxyResolver *resolver)
99 resolver->priv = G_TYPE_INSTANCE_GET_PRIVATE (resolver,
100 G_TYPE_SIMPLE_PROXY_RESOLVER,
101 GSimpleProxyResolverPrivate);
102 resolver->priv->uri_proxies = g_hash_table_new_full (g_str_hash, g_str_equal,
107 g_simple_proxy_resolver_set_property (GObject *object,
112 GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (object);
116 case PROP_DEFAULT_PROXY:
117 g_simple_proxy_resolver_set_default_proxy (resolver, g_value_get_string (value));
120 case PROP_IGNORE_HOSTS:
121 g_simple_proxy_resolver_set_ignore_hosts (resolver, g_value_get_boxed (value));
125 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130 g_simple_proxy_resolver_get_property (GObject *object,
135 GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (object);
139 case PROP_DEFAULT_PROXY:
140 g_value_set_string (value, resolver->priv->default_proxy);
143 case PROP_IGNORE_HOSTS:
144 g_value_set_boxed (value, resolver->priv->ignore_hosts);
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153 reparse_ignore_hosts (GSimpleProxyResolver *resolver)
155 GSimpleProxyResolverPrivate *priv = resolver->priv;
156 GPtrArray *ignore_ips;
157 GArray *ignore_domains;
158 gchar *host, *tmp, *colon, *bracket;
160 GInetAddressMask *mask;
161 GSimpleProxyResolverDomain domain;
165 if (priv->ignore_ips)
166 g_ptr_array_free (priv->ignore_ips, TRUE);
167 if (priv->ignore_domains)
169 for (i = 0; priv->ignore_domains[i].name; i++)
170 g_free (priv->ignore_domains[i].name);
171 g_free (priv->ignore_domains);
173 priv->ignore_ips = NULL;
174 priv->ignore_domains = NULL;
176 if (!priv->ignore_hosts || !priv->ignore_hosts[0])
179 ignore_ips = g_ptr_array_new_with_free_func (g_object_unref);
180 ignore_domains = g_array_new (TRUE, FALSE, sizeof (GSimpleProxyResolverDomain));
182 for (i = 0; priv->ignore_hosts[i]; i++)
184 host = g_strchomp (priv->ignore_hosts[i]);
186 /* See if it's an IP address or IP/length mask */
187 mask = g_inet_address_mask_new_from_string (host, NULL);
190 g_ptr_array_add (ignore_ips, mask);
200 bracket = strchr (host, ']');
201 if (!bracket || !bracket[1] || bracket[1] != ':')
204 port = strtoul (bracket + 2, &tmp, 10);
212 colon = strchr (host, ':');
213 if (colon && !strchr (colon + 1, ':'))
215 /* hostname:port or IPv4:port */
216 port = strtoul (colon + 1, &tmp, 10);
223 iaddr = g_inet_address_new_from_string (host);
225 g_object_unref (iaddr);
228 if (g_str_has_prefix (host, "*."))
230 else if (*host == '.')
234 memset (&domain, 0, sizeof (domain));
235 domain.name = g_strdup (host);
236 domain.length = strlen (domain.name);
238 g_array_append_val (ignore_domains, domain);
242 g_warning ("Ignoring invalid ignore_hosts value '%s'", host);
246 priv->ignore_ips = ignore_ips;
248 g_ptr_array_free (ignore_ips, TRUE);
250 if (ignore_domains->len)
251 priv->ignore_domains = (GSimpleProxyResolverDomain *)ignore_domains->data;
252 g_array_free (ignore_domains, ignore_domains->len == 0);
256 ignore_host (GSimpleProxyResolver *resolver,
260 GSimpleProxyResolverPrivate *priv = resolver->priv;
261 gchar *ascii_host = NULL;
262 gboolean ignore = FALSE;
263 gint i, length, offset;
265 if (priv->ignore_ips)
269 iaddr = g_inet_address_new_from_string (host);
272 for (i = 0; i < priv->ignore_ips->len; i++)
274 GInetAddressMask *mask = priv->ignore_ips->pdata[i];
276 if (g_inet_address_mask_matches (mask, iaddr))
283 g_object_unref (iaddr);
289 if (priv->ignore_domains)
291 if (g_hostname_is_non_ascii (host))
292 host = ascii_host = g_hostname_to_ascii (host);
293 length = strlen (host);
295 for (i = 0; priv->ignore_domains[i].length; i++)
297 GSimpleProxyResolverDomain *domain = &priv->ignore_domains[i];
299 offset = length - domain->length;
300 if ((domain->port == 0 || domain->port == port) &&
301 (offset == 0 || (offset > 0 && host[offset - 1] == '.')) &&
302 (g_ascii_strcasecmp (domain->name, host + offset) == 0))
316 g_simple_proxy_resolver_lookup (GProxyResolver *proxy_resolver,
318 GCancellable *cancellable,
321 GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (proxy_resolver);
322 GSimpleProxyResolverPrivate *priv = resolver->priv;
323 const gchar *proxy = NULL;
326 if (priv->ignore_ips || priv->ignore_domains)
331 if (_g_uri_parse_authority (uri, &host, &port, NULL) &&
332 ignore_host (resolver, host, port))
338 if (!proxy && g_hash_table_size (priv->uri_proxies))
340 gchar *scheme = g_ascii_strdown (uri, strcspn (uri, ":"));
342 proxy = g_hash_table_lookup (priv->uri_proxies, scheme);
347 proxy = priv->default_proxy;
351 if (!strncmp (proxy, "socks://", 8))
353 proxies = g_new0 (gchar *, 4);
354 proxies[0] = g_strdup_printf ("socks5://%s", proxy + 8);
355 proxies[1] = g_strdup_printf ("socks4a://%s", proxy + 8);
356 proxies[2] = g_strdup_printf ("socks4://%s", proxy + 8);
361 proxies = g_new0 (gchar *, 2);
362 proxies[0] = g_strdup (proxy);
369 g_simple_proxy_resolver_lookup_async (GProxyResolver *proxy_resolver,
371 GCancellable *cancellable,
372 GAsyncReadyCallback callback,
375 GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (proxy_resolver);
377 GError *error = NULL;
380 task = g_task_new (resolver, cancellable, callback, user_data);
382 proxies = g_simple_proxy_resolver_lookup (proxy_resolver, uri,
383 cancellable, &error);
385 g_task_return_pointer (task, proxies, (GDestroyNotify)g_strfreev);
387 g_task_return_error (task, error);
388 g_object_unref (task);
392 g_simple_proxy_resolver_lookup_finish (GProxyResolver *resolver,
393 GAsyncResult *result,
396 g_return_val_if_fail (g_task_is_valid (result, resolver), NULL);
398 return g_task_propagate_pointer (G_TASK (result), error);
402 g_simple_proxy_resolver_class_init (GSimpleProxyResolverClass *resolver_class)
404 GObjectClass *object_class = G_OBJECT_CLASS (resolver_class);
406 g_type_class_add_private (resolver_class, sizeof (GSimpleProxyResolverPrivate));
408 object_class->get_property = g_simple_proxy_resolver_get_property;
409 object_class->set_property = g_simple_proxy_resolver_set_property;
410 object_class->finalize = g_simple_proxy_resolver_finalize;
413 * GSimpleProxyResolver:default-proxy:
415 * The default proxy URI that will be used for any URI that doesn't
416 * match #GSimpleProxyResolver:ignore-hosts, and doesn't match any
417 * of the schemes set with g_simple_proxy_resolver_set_uri_proxy().
419 * Note that as a special case, if this URI starts with
420 * "<literal>socks://</literal>", #GSimpleProxyResolver will treat
421 * it as referring to all three of the <literal>socks5</literal>,
422 * <literal>socks4a</literal>, and <literal>socks4</literal> proxy
425 g_object_class_install_property (object_class, PROP_DEFAULT_PROXY,
426 g_param_spec_string ("default-proxy",
428 P_("The default proxy URI"),
431 G_PARAM_STATIC_STRINGS));
434 * GSimpleProxyResolver:ignore-hosts:
436 * A list of hostnames and IP addresses that the resolver should
437 * allow direct connections to.
439 * Entries can be in one of 4 formats:
443 * A hostname, such as "<literal>example.com</literal>",
444 * "<literal>.example.com</literal>", or
445 * "<literal>*.example.com</literal>", any of which match
446 * "<literal>example.com</literal>" or any subdomain of it.
449 * An IPv4 or IPv6 address, such as
450 * "<literal>192.168.1.1</literal>", which matches only
454 * A hostname or IP address followed by a port, such as
455 * "<literal>example.com:80</literal>", which matches whatever
456 * the hostname or IP address would match, but only for URLs
457 * with the (explicitly) indicated port. In the case of an IPv6
458 * address, the address part must appear in brackets:
459 * "<literal>[::1]:443</literal>"
462 * An IP address range, given by a base address and prefix length,
463 * such as "<literal>fe80::/10</literal>", which matches any
464 * address in that range.
468 * Note that when dealing with Unicode hostnames, the matching is
469 * done against the ASCII form of the name.
471 * Also note that hostname exclusions apply only to connections made
472 * to hosts identified by name, and IP address exclusions apply only
473 * to connections made to hosts identified by address. That is, if
474 * <literal>example.com</literal> has an address of
475 * <literal>192.168.1.1</literal>, and the :ignore-hosts list
476 * contains only "<literal>192.168.1.1</literal>", then a connection
477 * to "<literal>example.com</literal>" (eg, via a #GNetworkAddress)
478 * will use the proxy, and a connection to
479 * "<literal>192.168.1.1</literal>" (eg, via a #GInetSocketAddress)
482 * These rules match the "ignore-hosts"/"noproxy" rules most
483 * commonly used by other applications.
485 g_object_class_install_property (object_class, PROP_IGNORE_HOSTS,
486 g_param_spec_boxed ("ignore-hosts",
488 P_("Hosts that will not use the proxy"),
491 G_PARAM_STATIC_STRINGS));
496 g_simple_proxy_resolver_iface_init (GProxyResolverInterface *iface)
498 iface->lookup = g_simple_proxy_resolver_lookup;
499 iface->lookup_async = g_simple_proxy_resolver_lookup_async;
500 iface->lookup_finish = g_simple_proxy_resolver_lookup_finish;
504 * g_simple_proxy_resolver_new:
505 * @default_proxy: (allow-none): the default proxy to use, eg
506 * "socks://192.168.1.1"
507 * @ignore_hosts: (allow-none): an optional list of hosts/IP addresses
508 * to not use a proxy for.
510 * Creates a new #GSimpleProxyResolver. See
511 * #GSimpleProxyResolver:default-proxy and
512 * #GSimpleProxyResolver:ignore-hosts for more details on how the
513 * arguments are interpreted.
515 * Returns: a new #GSimpleProxyResolver
520 g_simple_proxy_resolver_new (const gchar *default_proxy,
521 gchar **ignore_hosts)
523 return g_object_new (G_TYPE_SIMPLE_PROXY_RESOLVER,
524 "default-proxy", default_proxy,
525 "ignore-hosts", ignore_hosts,
530 * g_simple_proxy_resolver_set_default_proxy:
531 * @resolver: a #GSimpleProxyResolver
532 * @default_proxy: the default proxy to use
534 * Sets the default proxy on @resolver, to be used for any URIs that
535 * don't match #GSimpleProxyResolver:ignore-hosts or a proxy set
536 * via g_simple_proxy_resolver_set_uri_proxy().
538 * If @default_proxy starts with "<literal>socks://</literal>",
539 * #GSimpleProxyResolver will treat it as referring to all three of
540 * the <literal>socks5</literal>, <literal>socks4a</literal>, and
541 * <literal>socks4</literal> proxy types.
546 g_simple_proxy_resolver_set_default_proxy (GSimpleProxyResolver *resolver,
547 const gchar *default_proxy)
549 g_return_if_fail (G_IS_SIMPLE_PROXY_RESOLVER (resolver));
551 g_free (resolver->priv->default_proxy);
552 resolver->priv->default_proxy = g_strdup (default_proxy);
553 g_object_notify (G_OBJECT (resolver), "default-proxy");
557 g_simple_proxy_resolver_set_ignore_hosts (GSimpleProxyResolver *resolver,
558 gchar **ignore_hosts)
560 g_return_if_fail (G_IS_SIMPLE_PROXY_RESOLVER (resolver));
562 g_strfreev (resolver->priv->ignore_hosts);
563 resolver->priv->ignore_hosts = g_strdupv (ignore_hosts);
564 reparse_ignore_hosts (resolver);
565 g_object_notify (G_OBJECT (resolver), "ignore-hosts");
569 * g_simple_proxy_resolver_set_uri_proxy:
570 * @resolver: a #GSimpleProxyResolver
571 * @uri_scheme: the URI scheme to add a proxy for
572 * @proxy: the proxy to use for @uri_scheme
574 * Adds a URI-scheme-specific proxy to @resolver; URIs whose scheme
575 * matches @uri_scheme (and which don't match
576 * #GSimpleProxyResolver:ignore-hosts) will be proxied via @proxy.
578 * As with #GSimpleProxyResolver:default-proxy, if @proxy starts with
579 * "<literal>socks://</literal>", #GSimpleProxyResolver will treat it
580 * as referring to all three of the <literal>socks5</literal>,
581 * <literal>socks4a</literal>, and <literal>socks4</literal> proxy
587 g_simple_proxy_resolver_set_uri_proxy (GSimpleProxyResolver *resolver,
588 const gchar *uri_scheme,
591 g_return_if_fail (G_IS_SIMPLE_PROXY_RESOLVER (resolver));
593 g_hash_table_replace (resolver->priv->uri_proxies,
594 g_ascii_strdown (uri_scheme, -1),