Debian Codegraph @

src/backend/px-manager.c

raw
1 /* px-manager.c
2 *
3 * Copyright 2022-2023 The Libproxy Team
4 *
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.1 of the License, or (at your option) any later version.
9 *
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.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "px-backend-config.h"
23
24 #include <glib.h>
25 #include <glib-object.h>
26 #include <gio/gio.h>
27
28 #include "px-manager.h"
29 #include "px-plugin-config.h"
30 #include "px-plugin-pacrunner.h"
31
32 #ifdef HAVE_CONFIG_ENV
33 #include <plugins/config-env/config-env.h>
34 #endif
35
36 #ifdef HAVE_CONFIG_GNOME
37 #include <plugins/config-gnome/config-gnome.h>
38 #endif
39
40 #ifdef HAVE_CONFIG_KDE
41 #include <plugins/config-kde/config-kde.h>
42 #endif
43
44 #ifdef HAVE_CONFIG_OSX
45 #include <plugins/config-osx/config-osx.h>
46 #endif
47
48 #ifdef HAVE_CONFIG_SYSCONFIG
49 #include <plugins/config-sysconfig/config-sysconfig.h>
50 #endif
51
52 #ifdef HAVE_CONFIG_WINDOWS
53 #include <plugins/config-windows/config-windows.h>
54 #endif
55
56 #ifdef HAVE_CONFIG_XDP
57 #include <plugins/config-xdp/config-xdp.h>
58 #endif
59
60 #ifdef HAVE_PACRUNNER_DUKTAPE
61 #include <plugins/pacrunner-duktape/pacrunner-duktape.h>
62 #endif
63
64 #ifdef HAVE_CURL
65 #include <curl/curl.h>
66 #endif
67
68 enum {
69 PROP_0,
70 PROP_CONFIG_PLUGIN,
71 PROP_CONFIG_OPTION,
72 PROP_FORCE_ONLINE,
73 LAST_PROP
74 };
75
76 static GParamSpec *obj_properties[LAST_PROP];
77
78 /**
79 * PxManager:
80 *
81 * Manage libproxy modules
82 */
83
84 struct _PxManager {
85 GObject parent_instance;
86 GList *config_plugins;
87 GList *pacrunner_plugins;
88 GNetworkMonitor *network_monitor;
89 #ifdef HAVE_CURL
90 CURL *curl;
91 #endif
92
93 char *config_plugin;
94 char *config_option;
95
96 gboolean force_online;
97 gboolean online;
98 gboolean wpad;
99 GBytes *pac_data;
100 char *pac_url;
101
102 GMutex mutex;
103 };
104
105 G_DEFINE_TYPE (PxManager, px_manager, G_TYPE_OBJECT)
106
107 static void
108 px_manager_on_network_changed (GNetworkMonitor *monitor,
109 gboolean network_available,
110 gpointer user_data)
111 {
112 PxManager *self = PX_MANAGER (user_data);
113
114 g_debug ("%s: Network connection changed, clearing pac data", __FUNCTION__);
115
116 self->wpad = FALSE;
117 self->online = network_available;
118 g_clear_pointer (&self->pac_url, g_free);
119 g_clear_pointer (&self->pac_data, g_bytes_unref);
120 }
121
122 static gint
123 config_order_compare (gconstpointer a,
124 gconstpointer b)
125 {
126 PxConfig *config_a = (PxConfig *)a;
127 PxConfig *config_b = (PxConfig *)b;
128 PxConfigInterface *ifc_a = PX_CONFIG_GET_IFACE (config_a);
129 PxConfigInterface *ifc_b = PX_CONFIG_GET_IFACE (config_b);
130
131 if (ifc_a->priority < ifc_b->priority)
132 return -1;
133
134 if (ifc_a->priority == ifc_b->priority)
135 return 0;
136
137 return 1;
138 }
139
140 static void
141 px_manager_add_config_plugin (PxManager *self,
142 GType type)
143 {
144 g_autoptr (PxConfig) config = g_object_new (type, "config-option", self->config_option, NULL);
145 PxConfigInterface *ifc = PX_CONFIG_GET_IFACE (config);
146 const char *env = g_getenv ("PX_FORCE_CONFIG");
147 const char *force_config = self->config_plugin ? self->config_plugin : env;
148
149 if (!force_config || g_strcmp0 (ifc->name, force_config) == 0)
150 self->config_plugins = g_list_insert_sorted (self->config_plugins, g_steal_pointer (&config), config_order_compare);
151 }
152
153 static void
154 px_manager_add_pacrunner_plugin (PxManager *self,
155 GType type)
156 {
157 PxPacRunner *pacrunner = g_object_new (type, NULL);
158
159 self->pacrunner_plugins = g_list_append (self->pacrunner_plugins, pacrunner);
160 }
161
162 static void
163 px_manager_constructed (GObject *object)
164 {
165 PxManager *self = PX_MANAGER (object);
166
167 if (g_getenv ("PX_DEBUG")) {
168 const gchar *g_messages_debug;
169
170 g_messages_debug = g_getenv ("G_MESSAGES_DEBUG");
171
172 if (!g_messages_debug) {
173 g_setenv ("G_MESSAGES_DEBUG", G_LOG_DOMAIN, TRUE);
174 } else {
175 g_autofree char *new_g_messages_debug = NULL;
176
177 new_g_messages_debug = g_strconcat (g_messages_debug, " ", G_LOG_DOMAIN, NULL);
178 if (new_g_messages_debug)
179 g_setenv ("G_MESSAGES_DEBUG", new_g_messages_debug, TRUE);
180 }
181 }
182
183 #ifdef HAVE_CONFIG_ENV
184 px_manager_add_config_plugin (self, PX_CONFIG_TYPE_ENV);
185 #endif
186 #ifdef HAVE_CONFIG_GNOME
187 px_manager_add_config_plugin (self, PX_CONFIG_TYPE_GNOME);
188 #endif
189 #ifdef HAVE_CONFIG_KDE
190 px_manager_add_config_plugin (self, PX_CONFIG_TYPE_KDE);
191 #endif
192 #ifdef HAVE_CONFIG_OSX
193 px_manager_add_config_plugin (self, PX_CONFIG_TYPE_OSX);
194 #endif
195 #ifdef HAVE_CONFIG_SYSCONFIG
196 px_manager_add_config_plugin (self, PX_CONFIG_TYPE_SYSCONFIG);
197 #endif
198 #ifdef HAVE_CONFIG_WINDOWS
199 px_manager_add_config_plugin (self, PX_CONFIG_TYPE_WINDOWS);
200 #endif
201 #ifdef HAVE_CONFIG_XDP
202 px_manager_add_config_plugin (self, PX_CONFIG_TYPE_XDP);
203 #endif
204
205 g_debug ("Active config plugins:");
206 for (GList *list = self->config_plugins; list && list->data; list = list->next) {
207 PxConfig *config = list->data;
208 PxConfigInterface *ifc = PX_CONFIG_GET_IFACE (config);
209
210 g_debug (" - %s", ifc->name);
211 }
212
213 #ifdef HAVE_PACRUNNER_DUKTAPE
214 px_manager_add_pacrunner_plugin (self, PX_PACRUNNER_TYPE_DUKTAPE);
215 #endif
216
217 self->pac_data = NULL;
218
219 if (!self->force_online) {
220 self->network_monitor = g_network_monitor_get_default ();
221 g_signal_connect_object (G_OBJECT (self->network_monitor), "network-changed", G_CALLBACK (px_manager_on_network_changed), self, 0);
222
223 /* Expect to be online until network-changed is emitted */
224 self->wpad = FALSE;
225 self->online = TRUE;
226 } else {
227 px_manager_on_network_changed (NULL, TRUE, self);
228 }
229
230 g_debug ("%s: Up and running", __FUNCTION__);
231 }
232
233 static void
234 px_manager_dispose (GObject *object)
235 {
236 PxManager *self = PX_MANAGER (object);
237
238 g_clear_list (&self->config_plugins, g_object_unref);
239 g_clear_list (&self->pacrunner_plugins, g_object_unref);
240
241 g_clear_pointer (&self->config_plugin, g_free);
242 #ifdef HAVE_CURL
243 g_clear_pointer (&self->curl, curl_easy_cleanup);
244 #endif
245
246 G_OBJECT_CLASS (px_manager_parent_class)->dispose (object);
247 }
248
249 static void
250 px_manager_set_property (GObject *object,
251 guint prop_id,
252 const GValue *value,
253 GParamSpec *pspec)
254 {
255 PxManager *self = PX_MANAGER (object);
256
257 switch (prop_id) {
258 case PROP_CONFIG_PLUGIN:
259 self->config_plugin = g_strdup (g_value_get_string (value));
260 break;
261 case PROP_CONFIG_OPTION:
262 self->config_option = g_strdup (g_value_get_string (value));
263 break;
264 case PROP_FORCE_ONLINE:
265 self->force_online = g_value_get_boolean (value);
266 break;
267 default:
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 }
270 }
271
272 static void
273 px_manager_get_property (GObject *object,
274 guint prop_id,
275 GValue *value,
276 GParamSpec *pspec)
277 {
278 switch (prop_id) {
279 case PROP_CONFIG_PLUGIN:
280 break;
281 default:
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283 break;
284 }
285 }
286
287 static void
288 px_manager_class_init (PxManagerClass *klass)
289 {
290 GObjectClass *object_class = G_OBJECT_CLASS (klass);
291
292 object_class->constructed = px_manager_constructed;
293 object_class->dispose = px_manager_dispose;
294 object_class->set_property = px_manager_set_property;
295 object_class->get_property = px_manager_get_property;
296
297 obj_properties[PROP_CONFIG_PLUGIN] = g_param_spec_string ("config-plugin",
298 NULL,
299 NULL,
300 NULL,
301 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
302
303 obj_properties[PROP_CONFIG_OPTION] = g_param_spec_string ("config-option",
304 NULL,
305 NULL,
306 NULL,
307 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
308
309 obj_properties[PROP_FORCE_ONLINE] = g_param_spec_boolean ("force-online",
310 NULL,
311 NULL,
312 FALSE,
313 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
314
315 g_object_class_install_properties (object_class, LAST_PROP, obj_properties);
316 }
317
318 static void
319 px_manager_init (PxManager *self)
320 {
321 }
322
323 /**
324 * px_manager_new_with_options:
325 * @optname1: name of first property to set
326 * @...: value of @optname1, followed by additional property/value pairs
327 *
328 * Create a new `PxManager` with the specified options.
329 *
330 * Returns: the newly created `PxManager`
331 */
332 PxManager *
333 px_manager_new_with_options (const char *optname1,
334 ...)
335 {
336 PxManager *self;
337 va_list ap;
338
339 va_start (ap, optname1);
340 self = (PxManager *)g_object_new_valist (PX_TYPE_MANAGER, optname1, ap);
341 va_end (ap);
342
343 return self;
344 }
345
346 /**
347 * px_manager_new:
348 *
349 * Create a new `PxManager`.
350 *
351 * Returns: the newly created `PxManager`
352 */
353 PxManager *
354 px_manager_new (void)
355 {
356 return px_manager_new_with_options (NULL);
357 }
358
359 #ifdef HAVE_CURL
360 static size_t
361 store_data (void *contents,
362 size_t size,
363 size_t nmemb,
364 void *user_pointer)
365 {
366 GByteArray *byte_array = user_pointer;
367 size_t real_size = size * nmemb;
368
369 g_byte_array_append (byte_array, contents, real_size);
370
371 return real_size;
372 }
373 #endif
374
375 /**
376 * px_manager_pac_download:
377 * @self: a px manager
378 * @uri: PAC uri
379 *
380 * Downloads a PAC file from provided @url.
381 *
382 * Returns: (nullable): a newly created `GBytes` containing PAC data, or %NULL on error.
383 */
384 GBytes *
385 px_manager_pac_download (PxManager *self,
386 const char *uri)
387 {
388 #ifdef HAVE_CURL
389 GByteArray *byte_array = g_byte_array_new ();
390 CURLcode res;
391 const char *url = uri;
392
393 if (!self->curl)
394 self->curl = curl_easy_init ();
395
396 if (!self->curl)
397 return NULL;
398
399 if (g_str_has_prefix (url, "pac+"))
400 url += 4;
401
402 if (curl_easy_setopt (self->curl, CURLOPT_NOSIGNAL, 1) != CURLE_OK)
403 g_debug ("Could not set NOSIGNAL, continue");
404
405 if (curl_easy_setopt (self->curl, CURLOPT_FOLLOWLOCATION, 1) != CURLE_OK)
406 g_debug ("Could not set FOLLOWLOCATION, continue");
407
408 if (curl_easy_setopt (self->curl, CURLOPT_NOPROXY, "*") != CURLE_OK) {
409 g_warning ("Could not set NOPROXY, ABORT!");
410 return NULL;
411 }
412
413 if (curl_easy_setopt (self->curl, CURLOPT_CONNECTTIMEOUT, 30) != CURLE_OK)
414 g_debug ("Could not set CONENCTIONTIMEOUT, continue");
415
416 if (curl_easy_setopt (self->curl, CURLOPT_USERAGENT, "libproxy") != CURLE_OK)
417 g_debug ("Could not set USERAGENT, continue");
418
419 if (curl_easy_setopt (self->curl, CURLOPT_URL, url) != CURLE_OK) {
420 g_warning ("Could not set URL, ABORT!");
421 return NULL;
422 }
423
424 if (curl_easy_setopt (self->curl, CURLOPT_WRITEFUNCTION, store_data) != CURLE_OK) {
425 g_warning ("Could not set WRITEFUNCTION, ABORT!");
426 return NULL;
427 }
428
429 if (curl_easy_setopt (self->curl, CURLOPT_WRITEDATA, byte_array) != CURLE_OK) {
430 g_warning ("Could not set WRITEDATA, ABORT!");
431 return NULL;
432 }
433
434 res = curl_easy_perform (self->curl);
435 if (res != CURLE_OK) {
436 g_debug ("%s: Could not download data: %s", __FUNCTION__, curl_easy_strerror (res));
437 return NULL;
438 }
439
440 return g_byte_array_free_to_bytes (byte_array);
441 #else
442 return NULL;
443 #endif
444 }
445
446 /**
447 * px_manager_get_configuration:
448 * @self: a px manager
449 * @uri: PAC uri
450 *
451 * Get raw proxy configuration for gien @uri.
452 *
453 * Returns: (transfer full) (nullable): a newly created `GStrv` containing configuration data for @uri.
454 */
455 char **
456 px_manager_get_configuration (PxManager *self,
457 GUri *uri)
458 {
459 g_autoptr (GStrvBuilder) builder = g_strv_builder_new ();
460
461 for (GList *list = self->config_plugins; list && list->data; list = list->next) {
462 PxConfig *config = PX_CONFIG (list->data);
463 PxConfigInterface *ifc = PX_CONFIG_GET_IFACE (config);
464
465 ifc->get_config (config, uri, builder);
466 }
467
468 return g_strv_builder_end (builder);
469 }
470
471 static void
472 px_manager_run_pac (PxPacRunner *pacrunner,
473 GBytes *pac,
474 GUri *uri,
475 GStrvBuilder *builder)
476 {
477 PxPacRunnerInterface *ifc = PX_PAC_RUNNER_GET_IFACE (pacrunner);
478 g_auto (GStrv) proxies_split = NULL;
479 char *pac_response;
480
481 pac_response = ifc->run (PX_PAC_RUNNER (pacrunner), uri);
482
483 /* Split line to handle multiple proxies */
484 proxies_split = g_strsplit (pac_response, ";", -1);
485
486 for (int idx = 0; idx < g_strv_length (proxies_split); idx++) {
487 char *line = g_strstrip (proxies_split[idx]);
488 g_auto (GStrv) word_split = g_strsplit (line, " ", -1);
489
490 /* Check for syntax "METHOD SERVER" */
491 if (g_strv_length (word_split) == 2) {
492 g_autoptr (GUri) proxy_uri = NULL;
493 g_autofree char *uri_string = NULL;
494 g_autofree char *proxy_string = NULL;
495 g_autoptr (GUri) test_uri = NULL;
496 char *method;
497 char *server;
498
499 method = word_split[0];
500 server = word_split[1];
501
502 uri_string = g_strconcat ("http://", server, NULL);
503 proxy_uri = g_uri_parse (uri_string, G_URI_FLAGS_NONE, NULL);
504 if (!proxy_uri)
505 continue;
506
507 if (g_ascii_strncasecmp (method, "proxy", 5) == 0) {
508 proxy_string = g_uri_to_string (proxy_uri);
509 } else if (g_ascii_strncasecmp (method, "socks4a", 7) == 0) {
510 proxy_string = g_strconcat ("socks4a://", server, NULL);
511 } else if (g_ascii_strncasecmp (method, "socks4", 6) == 0) {
512 proxy_string = g_strconcat ("socks4://", server, NULL);
513 } else if (g_ascii_strncasecmp (method, "socks5", 6) == 0) {
514 proxy_string = g_strconcat ("socks5://", server, NULL);
515 } else if (g_ascii_strncasecmp (method, "socks", 5) == 0) {
516 proxy_string = g_strconcat ("socks://", server, NULL);
517 }
518
519 if (proxy_string) {
520 test_uri = g_uri_parse (proxy_string, G_URI_FLAGS_NONE, NULL);
521
522 if (test_uri)
523 px_strv_builder_add_proxy (builder, proxy_string);
524 }
525 } else if (g_strv_length (word_split) == 1 && g_ascii_strncasecmp (word_split[0], "direct", 6) == 0) {
526 px_strv_builder_add_proxy (builder, "direct://");
527 }
528 }
529 }
530
531 static gboolean
532 px_manager_set_pac (PxManager *self)
533 {
534 GList *list;
535
536 for (list = self->pacrunner_plugins; list && list->data; list = list->next) {
537 PxPacRunner *pacrunner = PX_PAC_RUNNER (list->data);
538 PxPacRunnerInterface *ifc = PX_PAC_RUNNER_GET_IFACE (pacrunner);
539
540 if (!ifc->set_pac (PX_PAC_RUNNER (pacrunner), self->pac_data))
541 return FALSE;
542 }
543
544 return TRUE;
545 }
546
547 static gboolean
548 px_manager_expand_wpad (PxManager *self,
549 GUri *uri)
550 {
551 const char *scheme = g_uri_get_scheme (uri);
552 gboolean ret = FALSE;
553
554 if (g_strcmp0 (scheme, "wpad") == 0) {
555 ret = TRUE;
556
557 if (!self->wpad) {
558 g_clear_pointer (&self->pac_data, g_bytes_unref);
559 g_clear_pointer (&self->pac_url, g_free);
560 self->wpad = TRUE;
561 }
562
563 if (!self->pac_data) {
564 GUri *wpad_url = g_uri_parse ("http://wpad/wpad.dat", G_URI_FLAGS_NONE, NULL);
565
566 g_debug ("%s: Trying to find the PAC using WPAD...", __FUNCTION__);
567 self->pac_url = g_uri_to_string (wpad_url);
568 self->pac_data = px_manager_pac_download (self, self->pac_url);
569 if (!self->pac_data) {
570 g_clear_pointer (&self->pac_url, g_free);
571 ret = FALSE;
572 } else {
573 g_debug ("%s: PAC recevied!", __FUNCTION__);
574 if (!px_manager_set_pac (self)) {
575 g_debug ("%s: Unable to set PAC from %s while online = %d!", __FUNCTION__, self->pac_url, self->online);
576 g_clear_pointer (&self->pac_url, g_free);
577 g_clear_pointer (&self->pac_data, g_bytes_unref);
578 ret = FALSE;
579 }
580 }
581 }
582 }
583
584 return ret;
585 }
586
587 static gboolean
588 px_manager_expand_pac (PxManager *self,
589 GUri *uri)
590 {
591 gboolean ret = FALSE;
592 const char *scheme = g_uri_get_scheme (uri);
593
594 if (g_str_has_prefix (scheme, "pac+")) {
595 ret = TRUE;
596
597 if (self->wpad)
598 self->wpad = FALSE;
599
600 if (self->pac_data) {
601 g_autofree char *uri_str = g_uri_to_string (uri);
602
603 if (g_strcmp0 (self->pac_url, uri_str) != 0) {
604 g_clear_pointer (&self->pac_url, g_free);
605 g_clear_pointer (&self->pac_data, g_bytes_unref);
606 }
607 }
608
609 if (!self->pac_data) {
610 self->pac_url = g_uri_to_string (uri);
611 self->pac_data = px_manager_pac_download (self, self->pac_url);
612
613 if (!self->pac_data) {
614 g_warning ("%s: Unable to download PAC from %s while online = %d!", __FUNCTION__, self->pac_url, self->online);
615 g_clear_pointer (&self->pac_url, g_free);
616 ret = FALSE;
617 } else {
618 g_debug ("%s: PAC recevied!", __FUNCTION__);
619 if (!px_manager_set_pac (self)) {
620 g_warning ("%s: Unable to set PAC from %s while online = %d!", __FUNCTION__, self->pac_url, self->online);
621 g_clear_pointer (&self->pac_url, g_free);
622 g_clear_pointer (&self->pac_data, g_bytes_unref);
623 ret = FALSE;
624 }
625 }
626 }
627 }
628
629 return ret;
630 }
631
632 /**
633 * px_manager_get_proxies_sync:
634 * @self: a px manager
635 * @url: a url
636 *
637 * Get proxies for giben @url.
638 *
639 * Returns: (transfer full) (nullable): a newly created `GStrv` containing proxy related information.
640 */
641 char **
642 px_manager_get_proxies_sync (PxManager *self,
643 const char *url)
644 {
645 g_autoptr (GStrvBuilder) builder = NULL;
646 g_autoptr (GUri) uri = NULL;
647 g_auto (GStrv) config = NULL;
648 g_autoptr (GError) error = NULL;
649
650 g_mutex_lock (&self->mutex);
651
652 builder = g_strv_builder_new ();
653 uri = g_uri_parse (url, G_URI_FLAGS_NONE, &error);
654
655 g_debug ("%s: url=%s online=%d", __FUNCTION__, url ? url : "?", self->online);
656 if (!uri || !self->online) {
657 px_strv_builder_add_proxy (builder, "direct://");
658 g_mutex_unlock (&self->mutex);
659 return g_strv_builder_end (builder);
660 }
661
662 config = px_manager_get_configuration (self, uri);
663
664 for (int idx = 0; idx < g_strv_length (config); idx++) {
665 GUri *conf_url = g_uri_parse (config[idx], G_URI_FLAGS_NONE, NULL);
666
667 g_debug ("%s: Config[%d] = %s", __FUNCTION__, idx, config[idx]);
668
669 /* Ignore invalid proxy URL, so we won't call GUri API on NULL. */
670 if (!conf_url)
671 continue;
672
673 if (px_manager_expand_wpad (self, conf_url) || px_manager_expand_pac (self, conf_url)) {
674 GList *list;
675
676 for (list = self->pacrunner_plugins; list && list->data; list = list->next) {
677 PxPacRunner *pacrunner = PX_PAC_RUNNER (list->data);
678
679 px_manager_run_pac (pacrunner, self->pac_data, uri, builder);
680 }
681 } else if (!g_str_has_prefix (g_uri_get_scheme (conf_url), "wpad") && !g_str_has_prefix (g_uri_get_scheme (conf_url), "pac+")) {
682 px_strv_builder_add_proxy (builder, g_uri_to_string (conf_url));
683 }
684 }
685
686 /* In case no proxy could be found, assume direct connection */
687 if (((GPtrArray *)builder)->len == 0)
688 px_strv_builder_add_proxy (builder, "direct://");
689
690 for (int idx = 0; idx < ((GPtrArray *)builder)->len; idx++)
691 g_debug ("%s: Proxy[%d] = %s", __FUNCTION__, idx, (char *)((GPtrArray *)builder)->pdata[idx]);
692
693 g_mutex_unlock (&self->mutex);
694 return g_strv_builder_end (builder);
695 }
696
697 void
698 px_strv_builder_add_proxy (GStrvBuilder *builder,
699 const char *value)
700 {
701 for (int idx = 0; idx < ((GPtrArray *)builder)->len; idx++) {
702 if (g_strcmp0 ((char *)((GPtrArray *)builder)->pdata[idx], value) == 0)
703 return;
704 }
705
706 g_strv_builder_add (builder, value);
707 }
708
709 static gboolean
710 ignore_domain (GUri *uri,
711 char *ignore)
712 {
713 g_auto (GStrv) ignore_split = NULL;
714 const char *host = g_uri_get_host (uri);
715 char *ignore_host;
716 int ignore_port = -1;
717 int port;
718
719 if (g_strcmp0 (ignore, "*") == 0)
720 return TRUE;
721
722 if (!host || !ignore || strlen (ignore) == 0)
723 return FALSE;
724
725 ignore_split = g_strsplit (ignore, ":", -1);
726 port = g_uri_get_port (uri);
727
728 /* Get our ignore pattern's hostname and port */
729 ignore_host = ignore_split[0];
730 if (g_strv_length (ignore_split) == 2)
731 ignore_port = atoi (ignore_split[1]);
732
733 /* Hostname match (domain.com or domain.com:80) */
734 if (g_strcmp0 (host, ignore_host) == 0)
735 return (ignore_port == -1 || port == ignore_port);
736
737 /**
738 * Treat the following three options as a wildcard for a domain:
739 * - .domain.com or .domain.com:80
740 * - *.domain.com or *.domain.com:80
741 * - domain.com or domain.com:80
742 */
743 if (strlen (ignore_host) > 2) {
744 if (ignore_host[0] == '.' && ((g_ascii_strncasecmp (host, ignore_host + 1, strlen (host)) == 0) || g_str_has_suffix (host, ignore_host)))
745 return (ignore_port == -1 || port == ignore_port);
746
747 if (ignore_host[0] == '*' && ignore_host[1] == '.' && ((g_ascii_strncasecmp (host, ignore_host + 2, strlen (host)) == 0) || g_str_has_suffix (host, ignore_host + 1)))
748 return (ignore_port == -1 || port == ignore_port);
749
750 if (strlen (host) > strlen (ignore_host) && host[strlen (host) - strlen (ignore_host) - 1] == '.' && g_str_has_suffix (host, ignore_host))
751 return (ignore_port == -1 || port == ignore_port);
752 }
753
754 /* No match was found */
755 return FALSE;
756 }
757
758 static gboolean
759 ignore_hostname (GUri *uri,
760 char *ignore)
761 {
762 const char *host = g_uri_get_host (uri);
763
764 if (!host)
765 return FALSE;
766
767 if (g_strcmp0 (ignore, "<local>") == 0 && strchr (host, ':') == NULL && strchr (host, '.') == NULL)
768 return TRUE;
769
770 return FALSE;
771 }
772
773 static gboolean
774 ignore_ip (GUri *uri,
775 char *ignore)
776 {
777 g_autoptr (GInetAddress) uri_address = NULL;
778 g_autoptr (GInetAddress) ignore_address = NULL;
779 g_auto (GStrv) ignore_split = NULL;
780 g_autoptr (GError) error = NULL;
781 const char *uri_host = g_uri_get_host (uri);
782 int port = g_uri_get_port (uri);
783 int ignore_port = 0;
784 gboolean result;
785
786 if (!uri_host)
787 return FALSE;
788
789 uri_address = g_inet_address_new_from_string (uri_host);
790
791 /*
792 * IPv4/CIDR
793 * IPv4/IPv4
794 * IPv6/CIDR
795 * IPv6/IPv6
796 *
797 * uri must be in ip string format, no host name resolution is done
798 */
799 if (uri_address && strchr (ignore, '/')) {
800 GInetAddressMask *address_mask = g_inet_address_mask_new_from_string (ignore, &error);
801
802 if (!address_mask) {
803 g_warning ("Could not parse ignore mask: %s", error->message);
804 return FALSE;
805 }
806
807 if (g_inet_address_mask_matches (address_mask, uri_address))
808 return TRUE;
809 }
810
811 /*
812 * IPv4
813 * IPv6
814 */
815 if (!g_hostname_is_ip_address (uri_host) || !g_hostname_is_ip_address (ignore))
816 return FALSE;
817
818 /*
819 * IPv4:port
820 * [IPv6]:port
821 */
822 ignore_split = g_strsplit (ignore, ":", -1);
823 if (g_strv_length (ignore_split) == 2)
824 ignore_port = atoi (ignore_split[1]);
825
826 ignore_address = g_inet_address_new_from_string (ignore);
827 result = g_inet_address_equal (uri_address, ignore_address);
828
829 return ignore_port != 0 ? ((port == ignore_port) && result) : result;
830 }
831
832 gboolean
833 px_manager_is_ignore (GUri *uri,
834 GStrv ignores)
835 {
836 if (!ignores)
837 return FALSE;
838
839 for (int idx = 0; idx < g_strv_length (ignores); idx++) {
840 if (ignore_hostname (uri, ignores[idx]))
841 return TRUE;
842
843 if (ignore_domain (uri, ignores[idx]))
844 return TRUE;
845
846 if (ignore_ip (uri, ignores[idx]))
847 return TRUE;
848 }
849
850 return FALSE;
851 }