summaryrefslogtreecommitdiff
path: root/src/lib/proxy_factory.c
blob: 64a5808f767d76bbdfcc7f220c5b0c1487e22b59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/*******************************************************************************
 * libproxy - A library for proxy configuration
 * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 ******************************************************************************/

#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <dlfcn.h>
#include <math.h>
#include <sys/socket.h>
#include <arpa/inet.h>


#include "misc.h"
#include "proxy_factory.h"
#include "wpad.h"
#include "config_file.h"

#define DEFAULT_CONFIG_ORDER "USER,SESSION,SYSTEM"

struct _pxProxyFactoryConfig {
	pxConfigCategory           category;
	char                      *name;
	pxProxyFactoryPtrCallback  callback;
};
typedef struct _pxProxyFactoryConfig pxProxyFactoryConfig;

struct _pxKeyVal {
	char *key;
	void *value;
};
typedef struct _pxKeyVal pxKeyVal;

struct _pxProxyFactory {
	void                      **plugins;
	pxProxyFactoryConfig      **configs;
	pxKeyVal                  **misc;
	pxProxyFactoryVoidCallback *on_get_proxy;
	pxPACRunnerCallback         pac_runner;
	pxPAC                      *pac;
	pxWPAD                     *wpad;
	pxConfigFile               *cf;
};

// Convert the PAC formatted response into our proxy URL array response
static char **
_format_pac_response(char *response)
{
	char **chain, *tmp;
	
	if (!response) return px_strsplit("direct://", ";");
	chain = px_strsplit(response, ";");
	px_free(response);
	
	for (int i=0 ; chain[i] ; i++)
	{
		tmp = px_strstrip(chain[i]);
		px_free(chain[i]);
		
		if (!strncmp(tmp, "PROXY", 5) || !strncmp(tmp, "SOCKS", 5))
		{
			char *hostport = px_strstrip(tmp + 5);
			if (!strncmp(tmp, "PROXY", 5))
				chain[i] = px_strcat("http://", hostport, NULL);
			else
				chain[i] = px_strcat("socks://", hostport, NULL);
			px_free(hostport);
		}
		else
			chain[i] = px_strdup("direct://");
		
		px_free(tmp);
	}
	
	return chain;
}

static inline bool
_endswith(char *string, char *suffix)
{
	int st_len = strlen(string);
	int su_len = strlen(suffix);
	
	return (st_len >= su_len && !strcmp(string + (st_len-su_len), suffix));
}

static bool
_sockaddr_equals(const struct sockaddr *ip_a, const struct sockaddr *ip_b, const struct sockaddr *nm)
{
	if (!ip_a || !ip_b) return false;
	if (ip_a->sa_family != ip_b->sa_family) return false;
	if (nm && ip_a->sa_family != nm->sa_family) return false;
	
	// Setup the arrays
	uint8_t bytes = 0, *a_data = NULL, *b_data = NULL, *nm_data = NULL;
	if (ip_a->sa_family == AF_INET)
	{
		bytes   = 32 / 8;
		a_data  = (uint8_t *) &((struct sockaddr_in *) ip_a)->sin_addr;
		b_data  = (uint8_t *) &((struct sockaddr_in *) ip_b)->sin_addr;
		nm_data = nm ? (uint8_t *) &((struct sockaddr_in *) nm)->sin_addr : NULL;
	}
	else if (ip_a->sa_family == AF_INET6)
	{
		bytes   = 128 / 8;
		a_data  = (uint8_t *) &((struct sockaddr_in6 *) ip_a)->sin6_addr;
		b_data  = (uint8_t *) &((struct sockaddr_in6 *) ip_b)->sin6_addr;
		nm_data = nm ? (uint8_t *) &((struct sockaddr_in6 *) nm)->sin6_addr : NULL;
	}
	else
		return false;
	
	for (int i=0 ; i < bytes ; i++)
	{
		if (nm && (a_data[i] & nm_data[i]) != (b_data[i] & nm_data[i]))
			return false;
		else if (a_data[i] != b_data[i])
			return false;
	}
	return true;
}

static struct sockaddr *
_sockaddr_from_string(const char *ip, int len)
{
	if (!ip) return NULL;
	struct sockaddr *result = NULL;
	
	// Copy the string
	if (len >= 0)
		ip = px_strndup(ip, len);
	else
		ip = px_strdup(ip);
	
	// Try to parse IPv4
	result = px_malloc0(sizeof(struct sockaddr_in));
	result->sa_family = AF_INET;
	if (inet_pton(AF_INET, ip, &((struct sockaddr_in *) result)->sin_addr) > 0)
		goto out;
	
	// Try to parse IPv6
	px_free(result);
	result = px_malloc0(sizeof(struct sockaddr_in6));
	result->sa_family = AF_INET6;
	if (inet_pton(AF_INET6, ip, &((struct sockaddr_in6 *) result)->sin6_addr) > 0)
		goto out;
	
	// No address found
	px_free(result);
	result = NULL;
	out:
		px_free((char *) ip);
		return result;
}

static struct sockaddr *
_sockaddr_from_cidr(int af, int cidr)
{
	// TODO: Support CIDR notation
	return NULL;
}

static bool
_ip_ignore(pxURL *url, char *ignore)
{
	if (!url || !ignore) return false;
	
	bool result   = false;
	uint32_t port = 0;
	const struct sockaddr *dst_ip = px_url_get_ip_no_dns(url);
	      struct sockaddr *ign_ip = NULL, *net_ip = NULL;
	
	// IPv4
	// IPv6
	if ((ign_ip = _sockaddr_from_string(ignore, -1)))
		goto out;
	
	// IPv4/CIDR
	// IPv4/IPv4
	// IPv6/CIDR
	// IPv6/IPv6
	if (strchr(ignore, '/'))
	{
		ign_ip = _sockaddr_from_string(ignore, strchr(ignore, '/') - ignore);
		net_ip = _sockaddr_from_string(strchr(ignore, '/') + 1, -1);
		
		// If CIDR notation was used, get the netmask
		if (ign_ip && !net_ip)
		{
			uint32_t cidr = 0;
			if (sscanf(strchr(ignore, '/') + 1, "%d", &cidr) == 1)
				net_ip = _sockaddr_from_cidr(ign_ip->sa_family, cidr);
		}
		
		if (ign_ip && net_ip && ign_ip->sa_family == net_ip->sa_family)
			goto out;

		px_free(ign_ip);
		px_free(net_ip);
		ign_ip = NULL;
		net_ip = NULL;
	}
	
	// IPv4:port
	// [IPv6]:port
	if (strrchr(ignore, ':') && sscanf(strrchr(ignore, ':'), ":%u", &port) == 1 && port > 0)
	{
		ign_ip = _sockaddr_from_string(ignore, strrchr(ignore, ':') - ignore);
		
		// Make sure this really is just a port and not just an IPv6 address
		if (ign_ip && (ign_ip->sa_family != AF_INET6 || ignore[0] == '['))
			goto out;
		
		px_free(ign_ip);
		ign_ip = NULL;
		port   = 0;
	}
	
	out:
		result = _sockaddr_equals(dst_ip, ign_ip, net_ip);
		px_free(ign_ip);
		px_free(net_ip);
		return port != 0 ? (port == px_url_get_port(url) && result): result;
}

static inline bool
_domain_ignore(pxURL *url, char *ignore)
{
	if (!url || !ignore)
		return false;
	
	// Get our URL's hostname and port
	char *host = px_strdup(px_url_get_host(url));
	int   port = px_url_get_port(url);
	
	// Get our ignore pattern's hostname and port
	char *ihost = px_strdup(ignore);
	int   iport = 0;
	if (strchr(ihost, ':'))
	{
		char *tmp = strchr(ihost, ':');
		if (sscanf(tmp+1, "%d", &iport) == 1)
			*tmp  = '\0';
		else
			iport = 0;
	}
	
	// Hostname match (domain.com or domain.com:80)
	if (!strcmp(host, ihost))
		if (!iport || port == iport)
			goto match;
	
	// Endswith (.domain.com or .domain.com:80)
	if (ihost[0] == '.' && _endswith(host, ihost))
		if (!iport || port == iport)
			goto match;
	
	// Glob (*.domain.com or *.domain.com:80)
	if (ihost[0] == '*' && _endswith(host, ihost+1))
		if (!iport || port == iport)
			goto match;
	
	// No match was found
	px_free(host);
	px_free(ihost);
	return false;
	
	// A match was found
	match:
		px_free(host);
		px_free(ihost);
		return true;
}

/**
 * Creates a new pxProxyFactory instance.  This instance
 * and all its methods are NOT thread safe, so please take
 * care in their use.
 * 
 * @return A new pxProxyFactory instance or NULL on error
 */
pxProxyFactory *
px_proxy_factory_new ()
{
	pxProxyFactory *self = px_malloc0(sizeof(pxProxyFactory));
	unsigned int i;
	
	// Open the plugin dir
	DIR *plugindir = opendir(PLUGINDIR);
	if (!plugindir) return self;
	
	// Count the number of plugins
	for (i=0 ; readdir(plugindir) ; i++);
	self->plugins = (void **) px_malloc0(sizeof(void *) * (i + 1));
	rewinddir(plugindir);
	
	// For each plugin...
	struct dirent *ent;
	for (i=0 ; (ent = readdir(plugindir)) ; i++)
	{
		// Load the plugin
		char *tmp = px_strcat(PLUGINDIR, "/", ent->d_name, NULL);
		self->plugins[i] = dlopen(tmp, RTLD_LOCAL);
		px_free(tmp);
		if (!(self->plugins[i]))
		{
			i--;
			continue;
		}
		
		// Call the instantiation hook
		pxProxyFactoryBoolCallback instantiate;
		instantiate = dlsym(self->plugins[i], "on_proxy_factory_instantiate");
		if (instantiate && !instantiate(self))
		{
			dlclose(self->plugins[i]);
			self->plugins[i--] = NULL;
			continue;
		}
	}
	closedir(plugindir);
	
	return self;
}

bool
px_proxy_factory_config_add(pxProxyFactory *self, const char *name, pxConfigCategory category, pxProxyFactoryPtrCallback callback)
{
	int count;
	pxProxyFactoryConfig **tmp;
	
	// Verify some basic stuff
	if (!self)                      return false;
	if (!callback)                  return false;
	if (!name || !strcmp(name, "")) return false;
	
	// Allocate an empty config array if there is none
	if (!self->configs) self->configs = px_malloc0(sizeof(pxProxyFactoryConfig *));
	
	// Make sure that 'name' is unique
	// Also, get a count of how many configs we have
	for (count=0 ; self->configs[count] ; count++)
		if (!strcmp(self->configs[count]->name, name))
			return false;
	
	// Allocate new array, copy old values into it and free old array
	tmp = px_malloc0(sizeof(pxProxyFactoryConfig *) * (count + 2));
	memcpy(tmp, self->configs, sizeof(pxProxyFactoryConfig *) * count);
	px_free(self->configs);
	self->configs = tmp;
	
	// Add the new callback to the end
	self->configs[count]           = px_malloc0(sizeof(pxProxyFactoryConfig));
	self->configs[count]->category = category;
	self->configs[count]->name     = px_strdup(name);
	self->configs[count]->callback = callback;
	
	return true;
}

bool
px_proxy_factory_config_del(pxProxyFactory *self, const char *name)
{
	int i,j;
	
	// Verify some basic stuff
	if (!self)                      return false;
	if (!name || !strcmp(name, "")) return false;
	if (!self->configs)             return false;
	
	// Remove and shift all configs down (if found)
	for (i=0,j=0 ; self->configs[j]; i++,j++)
	{
		if (i != j)
			self->configs[j] = self->configs[i];
		else if (!strcmp(self->configs[i]->name, name))
		{
			px_free(self->configs[i]->name);
			px_free(self->configs[j--]);
		}
	}
	
	// If we have an empty array, free it
	if (!self->configs[0])
	{
		px_free(self->configs);
		self->configs = NULL;
	}
	
	return i != j ? true : false;
}

bool
px_proxy_factory_misc_set(pxProxyFactory *self, const char *key, const void *value)
{
	int count;
	pxKeyVal **tmp;
	
	// Verify some basic stuff
	if (!self)                                return false;
	if (!key || !strcmp(key, ""))             return false;
	
	// Allocate an empty config array if there is none
	if (!self->misc) self->misc = px_malloc0(sizeof(pxKeyVal *));
	
	// Count the number of values
	for (count=0 ; self->misc[count] ; count++);
	
	// Unset value
	if (!value)
	{
		// Remove the keyval, shifting downward
		for (int i=0,j=0 ; self->misc[i] ; i++, j++)
		{
			// If the key is found, remove it
			if (!strcmp(key, self->misc[i]->key))
			{
				px_free(self->misc[i]->key);
				px_free(self->misc[i]);
				self->misc[i] = NULL;
				count--;
				j--;
			}
			
			// Shift down
			if (i > 0 && j > 0)
				self->misc[j] = self->misc[i];
		}
		
		// Resize array
		tmp = px_malloc0(sizeof(pxKeyVal *) * (count + 1));
		memcpy(tmp, self->misc, sizeof(pxKeyVal *) * count);
		px_free(self->misc);
		self->misc = tmp;
		return true;
	}
	
	// Attempt to update the value within the array
	for (int i=0 ; self->misc[i] ; i++)
	{
		if (!strcmp(key, self->misc[i]->key))
		{
			self->misc[i]->value = (void *) value;
			return true;
		}
	}
	
	// The key was not found in the array, so add it
	tmp = px_malloc0(sizeof(pxKeyVal *) * (count + 2));
	memcpy(tmp, self->misc, sizeof(pxKeyVal *) * count);
	tmp[count]        = px_malloc0(sizeof(pxKeyVal));
	tmp[count]->key   = px_strdup(key);
	tmp[count]->value = (void *) value;
	px_free(self->misc);
	self->misc = tmp;
	return true;
}

void *
px_proxy_factory_misc_get(pxProxyFactory *self, const char *key)
{
	// Verify some basic stuff
	if (!self)                    return NULL;
	if (!key || !strcmp(key, "")) return NULL;
	if (!self->misc)              return NULL;
	
	// Find the value listed
	for (int i=0 ; self->misc[i] ; i++)
		if (!strcmp(key, self->misc[i]->key))
			return self->misc[i]->value;
	
	return NULL;
}

/**
 * Get which proxies to use for the specified URL.
 * 
 * A NULL-terminated array of proxy strings is returned.
 * If the first proxy fails, the second should be tried, etc...
 * 
 * The format of the returned proxy strings are as follows:
 *   - http://proxy:port
 *   - socks://proxy:port
 *   - direct://
 * @url The URL we are trying to reach
 * @return A NULL-terminated array of proxy strings to use
 */
char **
px_proxy_factory_get_proxies (pxProxyFactory *self, char *url)
{
	pxURL    *realurl  = px_url_new(url);
	pxConfig *config   = NULL;
	char    **response = px_strsplit("direct://", ";");
	char     *tmp = NULL, *order = NULL, **orderv = NULL;
	
	// Verify some basic stuff
	if (!self)                    goto do_return;
	if (!url || !strcmp(url, "")) goto do_return;
	if (!realurl)                 goto do_return;
	
	// Call the events
	for (int i=0 ; self->on_get_proxy && self->on_get_proxy[i] ; i++)
		self->on_get_proxy[i](self);
	
	// If our config file is stale, close it
	if (self->cf && px_config_file_is_stale(self->cf))
	{
		px_config_file_free(self->cf);
		self->cf = NULL;
	}
	
	// Try to open our config file if we don't have one
	if (!self->cf)
		self->cf = px_config_file_new(SYSCONFDIR "/proxy.conf");

	// If we have a config file, load the order from it		
	if (self->cf)
		tmp = px_config_file_get_value(self->cf, PX_CONFIG_FILE_DEFAULT_SECTION, "config_order");
		
	// Attempt to get info from the environment
	order = getenv("PX_CONFIG_ORDER");
	
	// Create the config order
	order = px_strcat(tmp ? tmp : "", ",", order ? order : "", ",", DEFAULT_CONFIG_ORDER, NULL);
	px_free(tmp); tmp = NULL;
	
	// Create the config plugin order vector
	orderv = px_strsplit(order, ",");
	px_free(order);
	
	// Get the config by searching the config order
	for (int i=0 ; orderv[i] && !config ; i++)
	{
		// Get the category (if applicable)
		pxConfigCategory category;
		if (!strcmp(orderv[i], "USER"))
			category = PX_CONFIG_CATEGORY_USER;
		else if (!strcmp(orderv[i], "SESSION"))
			category = PX_CONFIG_CATEGORY_SESSION;
		else if (!strcmp(orderv[i], "SYSTEM"))
			category = PX_CONFIG_CATEGORY_SYSTEM;
		else
			category = PX_CONFIG_CATEGORY_NONE;
		
		for (int j=0 ; self->configs && self->configs[j] && !config ; j++)
		{
			if (category != PX_CONFIG_CATEGORY_NONE && self->configs[j]->category == category)
				config = self->configs[j]->callback(self);
			else if (category == PX_CONFIG_CATEGORY_NONE && !strcmp(self->configs[j]->name, orderv[i]))
				config = self->configs[j]->callback(self);
		}
	}
	px_strfreev(orderv);
	
	// No config was found via search order, call all plugins
	for (int i=0 ; self->configs && self->configs[i] && !config ; i++)
		config = self->configs[i]->callback(self);
	
	// No plugin returned a valid config, fall back to 'wpad://'
	if (!config)
	{
		fprintf(stderr, "*** Unable to locate valid config! Falling back to auto-detection...\n");
		config         = px_malloc0(sizeof(pxConfig));
		config->url    = px_strdup("wpad://");
		config->ignore = px_strdup("");
	}
	
	// If the config plugin returned an invalid config type or malformed URL, fall back to 'wpad://'
	if (!(!strncmp(config->url, "http://", 7) || 
		  !strncmp(config->url, "socks://", 8) ||
		  !strncmp(config->url, "pac+", 4) ||
		  !strcmp (config->url, "wpad://") ||
		  !strcmp (config->url, "direct://")))
	{
		fprintf(stderr, "*** Config plugin returned invalid URL type! Falling back to auto-detection...\n");
		px_free(config->url);
		config->url = px_strdup("wpad://");
	}
	else if (!strncmp(config->url, "pac+", 4) && !px_url_is_valid(config->url + 4))
	{
		fprintf(stderr, "*** Config plugin returned malformed URL! Falling back to auto-detection...\n");
		px_free(config->url);
		config->url = px_strdup("wpad://");
	}
	else if ((!strncmp(config->url, "http://", 7) || !strncmp(config->url, "socks://", 8)) && 
			  !px_url_is_valid(config->url))
	{
		fprintf(stderr, "*** Config plugin returned malformed URL! Falling back to auto-detection...\n");
		px_free(config->url);
		config->url = px_strdup("wpad://");
	}
	
	// Check our ignore patterns
	char **ignores = px_strsplit(config->ignore, ",");
	for (int i=0 ; ignores[i] ; i++)
	{
		if (_domain_ignore(realurl, ignores[i]) || _ip_ignore(realurl, ignores[i]))
		{
			px_strfreev(ignores);
			goto do_return;
		}
	}
	px_strfreev(ignores);
	
	// If we have a wpad config
	if (!strcmp(config->url, "wpad://"))
	{
		// Get the WPAD object if needed
		if (!self->wpad)
		{
			if (self->pac) px_pac_free(self->pac);
			self->pac  = NULL;
			self->wpad = px_wpad_new();
			if (!self->wpad)
			{
				fprintf(stderr, "*** Unable to create WPAD! Falling back to direct...\n");
				goto do_return;
			}
		}
		
		// If we have no PAC, get one
		// If getting the PAC fails, but the WPAD cycle worked, restart the cycle
		if (!self->pac && !(self->pac = px_wpad_next(self->wpad)) && px_wpad_pac_found(self->wpad))
		{
			px_wpad_rewind(self->wpad);
			self->pac = px_wpad_next(self->wpad);
		}
		
		// If the WPAD cycle failed, fall back to direct
		if (!self->pac)
		{
			fprintf(stderr, "*** Unable to locate PAC! Falling back to direct...\n");
			goto do_return;
		}
		
		// Run the PAC
		if (self->pac_runner)
		{
			px_strfreev(response);
			response = _format_pac_response(self->pac_runner(self, self->pac, realurl));
		}
		
		// No PAC runner found, fall back to direct
		else
			fprintf(stderr, "*** PAC found, but no active PAC runner! Falling back to direct...\n");
	}
	
	// If we have a PAC config
	else if (!strncmp(config->url, "pac+", 4))
	{
		// Clear WPAD to indicate that this is a non-WPAD PAC
		if (self->wpad)
		{
			px_wpad_free(self->wpad);
			self->wpad = NULL;
		}
		
		// If a PAC alread exists, but comes from a different URL than the one
		// specified, remove it
		if (self->pac)
		{
			pxURL *urltmp = px_url_new(config->url + 4);
			if (!urltmp)
			{
				fprintf(stderr, "*** Invalid PAC URL! Falling back to direct...\n");
				goto do_return;
			}
			if (!px_url_equals(urltmp, px_pac_get_url(self->pac)))
			{
				px_pac_free(self->pac);
				self->pac = NULL;
			}
			px_url_free(urltmp);
		}
		
		// Try to load the PAC if it is not already loaded
		if (!self->pac && !(self->pac = px_pac_new_from_string(config->url + 4)))
		{
			fprintf(stderr, "*** Invalid PAC URL! Falling back to direct...\n");
			goto do_return;
		}

		// Run the PAC
		if (self->pac_runner)
		{
			px_strfreev(response);
			response = _format_pac_response(self->pac_runner(self, self->pac, realurl));
		}
		else
			fprintf(stderr, "*** PAC found, but no active PAC runner! Falling back to direct...\n");
	}
	
	// If we have a manual config (http://..., socks://...)
	else if (!strncmp(config->url, "http://", 7) || !strncmp(config->url, "socks://", 8))
	{
		if (self->wpad) { px_wpad_free(self->wpad); self->wpad = NULL; }
		if (self->pac)  { px_pac_free(self->pac);   self->pac = NULL; }
		px_strfreev(response);
		response = px_strsplit(config->url, ";");
	}
	
	// Actually return, freeing misc stuff
	do_return:
		if (config)  { px_free(config->url); px_free(config->ignore); px_free(config); }
		if (realurl) px_url_free(realurl);
		return response;
}

bool
px_proxy_factory_on_get_proxy_add (pxProxyFactory *self, pxProxyFactoryVoidCallback callback)
{
	int count;
	pxProxyFactoryVoidCallback *tmp;
	
	// Verify some basic stuff
	if (!self)     return false;
	if (!callback) return false;
	
	// Allocate an empty config array if there is none
	if (!self->on_get_proxy) self->on_get_proxy = px_malloc0(sizeof(pxProxyFactoryVoidCallback));
	
	// Get a count of how many callbacks we have
	for (count=0 ; self->on_get_proxy[count] ; count++);
	
	// Allocate new array, copy old values into it and free old array
	tmp = px_malloc0(sizeof(pxProxyFactoryVoidCallback) * (count + 2));
	memcpy(tmp, self->on_get_proxy, sizeof(pxProxyFactoryVoidCallback) * count);
	px_free(self->on_get_proxy);
	self->on_get_proxy = tmp;
	
	// Add the new callback to the end
	self->on_get_proxy[count] = callback;
	
	return true;
}

bool
px_proxy_factory_on_get_proxy_del (pxProxyFactory *self, pxProxyFactoryVoidCallback callback)
{
	int i,j;
	
	// Verify some basic stuff
	if (!self)               return false;
	if (!callback)           return false;
	if (!self->on_get_proxy) return false;
	
	// Remove and shift all callbacks down (if found)
	for (i=0,j=0 ; self->on_get_proxy[j]; i++,j++)
	{
		if (i != j)
			self->on_get_proxy[j] = self->on_get_proxy[i];
		else if (self->on_get_proxy[i] == callback)
			self->on_get_proxy[j--] = NULL;
	}
	
	// If we have an empty array, free it
	if (!self->on_get_proxy[0])
	{
		px_free(self->on_get_proxy);
		self->on_get_proxy = NULL;
	}
	
	return i != j ? true : false;
}

bool
px_proxy_factory_pac_runner_set (pxProxyFactory *self, pxPACRunnerCallback callback)
{
	if (!self) return false;
	if (self->pac_runner && callback && self->pac_runner != callback) return false;
	self->pac_runner = callback;
	return true;
}

void
px_proxy_factory_network_changed(pxProxyFactory *self)
{
	if (self->wpad)
	{
		px_wpad_free(self->wpad);
		self->wpad = NULL;
	}
	
	if (self->pac)
	{
		px_pac_free(self->pac);
		self->pac = NULL;
	}
}

/**
 * Frees the pxProxyFactory instance when no longer used.
 */
void
px_proxy_factory_free (pxProxyFactory *self)
{
	unsigned int i;
	
	if (!self) return;
	
	// Free the plugins
	if (self->plugins)
	{
		for (i=0 ; self->plugins[i] ; i++)
		{
			// Call the destantiation hook
			pxProxyFactoryVoidCallback destantiate;
			destantiate = dlsym(self->plugins[i], "on_proxy_factory_destantiate");
			if (destantiate)
				destantiate(self);
			
			// Unload the plugin
			dlclose(self->plugins[i]);
			self->plugins[i] = NULL;
		}
		px_free(self->plugins);
	}
	
	// Free misc
	if (self->misc)
	{
		for (i=0 ; self->misc[i] ; i++)
		{
			px_free(self->misc[i]->key);
			px_free(self->misc[i]);
		}
		px_free(self->misc);
	}
	
	// Free everything else
	px_pac_free(self->pac);
	px_wpad_free(self->wpad);
	px_config_file_free(self->cf);
	px_free(self);
}