aboutsummaryrefslogtreecommitdiffstats
path: root/tools/manual-proxy-test.c
blob: 2ec47cce27c26d245c0dc09e892470a3324d35a8 (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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/*
 *
 *  PACrunner - Proxy configuration daemon
 *
 *  Copyright (C) 2011  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <glib.h>

#define DBG(fmt, arg...) do { \
	printf("%s() " fmt "\n", __func__ , ## arg); \
} while (0)

enum pacrunner_manual_exclude_appliance {
	PACRUNNER_MANUAL_EXCLUDE_POST = 0,
	PACRUNNER_MANUAL_EXCLUDE_PRE  = 1,
	PACRUNNER_MANUAL_EXCLUDE_ANY  = 2,
};

enum pacrunner_manual_protocol {
	PACRUNNER_PROTOCOL_ALL            = 0,
	PACRUNNER_PROTOCOL_HTTP           = 1,
	PACRUNNER_PROTOCOL_HTTPS          = 2,
	PACRUNNER_PROTOCOL_FTP            = 3,
	PACRUNNER_PROTOCOL_SOCKS          = 4,
	PACRUNNER_PROTOCOL_SOCKS4         = 5,
	PACRUNNER_PROTOCOL_SOCKS5         = 6,
	PACRUNNER_PROTOCOL_MAXIMUM_NUMBER = 7,
	PACRUNNER_PROTOCOL_UNKNOWN        = 8,
};

struct pacrunner_manual_exclude {
	enum pacrunner_manual_exclude_appliance appliance;
	int host_length;
	char *host;
};

static enum pacrunner_manual_protocol get_protocol_from_string(const char *protocol)
{
	if (protocol == NULL)
		return PACRUNNER_PROTOCOL_ALL;

	if (g_strcmp0(protocol, "http") == 0)
		return PACRUNNER_PROTOCOL_HTTP;
	if (g_strcmp0(protocol, "https") == 0)
		return PACRUNNER_PROTOCOL_HTTPS;
	if (g_strcmp0(protocol, "ftp") == 0)
		return PACRUNNER_PROTOCOL_FTP;
	if (g_strcmp0(protocol, "socks") == 0)
		return PACRUNNER_PROTOCOL_SOCKS;
	if (g_strcmp0(protocol, "socks4") == 0)
		return PACRUNNER_PROTOCOL_SOCKS4;
	if (g_strcmp0(protocol, "socks5") == 0)
		return PACRUNNER_PROTOCOL_SOCKS5;

	return PACRUNNER_PROTOCOL_UNKNOWN;
}

static const char *get_protocol_to_string(enum pacrunner_manual_protocol protocol)
{
	switch (protocol) {
	case PACRUNNER_PROTOCOL_ALL:
		return "ALL";
	case PACRUNNER_PROTOCOL_HTTP:
		return "HTTP";
	case PACRUNNER_PROTOCOL_HTTPS:
		return "HTTPS";
	case PACRUNNER_PROTOCOL_FTP:
		return "FTP";
	case PACRUNNER_PROTOCOL_SOCKS:
		return "SOCKS";
	case PACRUNNER_PROTOCOL_SOCKS4:
		return "SOCKS4";
	case PACRUNNER_PROTOCOL_SOCKS5:
		return "SOCKS5";
	case PACRUNNER_PROTOCOL_MAXIMUM_NUMBER:
	case PACRUNNER_PROTOCOL_UNKNOWN:
	default:
		break;
	}

	return "UNKNOWN";
}

static const char *get_appliance_to_string(enum pacrunner_manual_exclude_appliance appliance)
{
	switch (appliance) {
	case PACRUNNER_MANUAL_EXCLUDE_POST:
		return "POST";
	case PACRUNNER_MANUAL_EXCLUDE_PRE:
		return "PRE";
	case PACRUNNER_MANUAL_EXCLUDE_ANY:
		return "ANY";
	default:
		break;
	}

	return "UNKNOWN";
}

static int parse_uri(char *uri,
			char **host,
			char **protocol,
			gboolean no_path,
			gboolean exclusion)
{
	int ret = PACRUNNER_MANUAL_EXCLUDE_POST;
	gboolean proto, post_confirmed, ipv6;
	char *scheme, *sep, *cur;
	int length;
	long int port;

	proto = post_confirmed = ipv6 = FALSE;
	port = -1;

	/**
	 * Make sure host and protocol, if given, are properly set.
	 */
	if (host != NULL)
		*host = NULL;

	if (protocol != NULL)
		*protocol = NULL;

	/**
	 * The parsing will actually process on a copy of given uri
	 */
	scheme = g_strdup(uri);
	if (scheme == NULL)
		goto error;

	cur = scheme;

	/**
	 * 1 - parsing protocol first
	 * Note: protocol scheme is here totally ignored
	 */
	sep = strstr(cur, "://");
	if (sep != NULL) {
		if (sep == cur)
			goto error;

		if (protocol != NULL) {
			*sep = '\0';

			*protocol = g_strdup(cur);
			if (*protocol == NULL)
				goto error;
		}

		cur = sep + 3;
		proto = TRUE;
	}

	/**
	 * 2 - detecting end of uri
	 * Note: in case of server/exclusion configuration,
	 * no path should be present
	 */
	sep = strchr(cur, '/');
	if (sep != NULL) {
		if (exclusion == TRUE || (*(sep + 1) != '\0' &&
							no_path == TRUE))
			goto error;

		*sep = '\0';
	}

	/**
	 * 3 - We skip <login:password> if present
	 * Note: exclusion rule cannot contain such authentication information
	 */
	sep = strchr(cur, '@');
	if (sep != NULL) {
		if (exclusion == TRUE)
			goto error;

		*sep = '\0';
		cur = sep + 1;
	}

	/**
	 * 4 - Are we in front of a possible IPv6 address?
	 * Note: ipv6 format is not checked!
	 */
	sep = strchr(cur, '[');
	if (sep != NULL) {
		char *bracket;

		bracket = strchr(cur, ']');
		if (bracket == NULL)
			goto error;

		cur = sep;
		sep = strchr(bracket, ':');

		ipv6 = TRUE;
	} else
		sep = strchr(cur, ':');

	/**
	 * 5 - Checking port validity if present
	 * Note: exclusion rule cannot embed port
	 */
	if (sep != NULL) {
		char *err = NULL;

		if (exclusion == TRUE)
			goto error;

		errno = 0;
		port = strtol(sep+1, &err, 10);
		if (*err != '\0' || port <= 0 || port > USHRT_MAX ||
					errno == ERANGE || errno == EINVAL)
			goto error;

		*sep = '\0';
	}

	/**
	 * 6 - We detect/trim '.'/'*' from start
	 * Note: This is valid only for exclusion URI since it defines
	 * its rule's appliance */
	for (sep = cur; *sep != '\0' && (*sep == '*' || *sep == '.'); sep++)
		*sep = '\0';

	if (sep != cur) {
		if (exclusion == FALSE)
			goto error;

		cur = sep;
		post_confirmed = TRUE;
	}

	/**
	 * 7 - Parsing host if present
	 */
	length = strlen(cur);
	if (length > 0) {
		const char *forbidden_chars;
		char **forbidden = NULL;

		/**
		 * We first detect/trim '.'/'*' from end
		 * Note: valid only for exclusion
		 */
		for (sep = cur + length - 1;
			*sep != '\0' && (*sep == '*' || *sep == '.'); sep--)
			*sep = '\0';

		if (sep - cur + 1 != length) {
			if (exclusion == FALSE)
				goto error;

			length = sep - cur + 1;

			ret = PACRUNNER_MANUAL_EXCLUDE_PRE;
			if (post_confirmed == TRUE)
				ret = PACRUNNER_MANUAL_EXCLUDE_ANY;
		}

		if ((length > 255) || (*cur == '-' || *sep == '-') ||
				((*cur == '\0') && (exclusion == FALSE ||
				(exclusion == TRUE && proto == FALSE))))
			goto error;

		/**
		 * We do not allow some characters. However we do not run
		 * a strict check if it's an IP address which is given
		 */
		if (ipv6 == TRUE)
			forbidden_chars = "%?!,;@\\'*|<>{}()+=$&~# \"";
		else
			forbidden_chars = "%?!,;@\\'*|<>{}[]()+=$&~# \"";

		forbidden = g_strsplit_set(cur, forbidden_chars, -1);
		if (forbidden != NULL) {
			length = g_strv_length(forbidden);
			g_strfreev(forbidden);

			if (length > 1)
				goto error;
		}

		if (host != NULL && *cur != '\0') {
			if (port > 0) {
				/**
				 * Instead of transcoding the port back
				 * to string we just get the host:port line
				 * from the original uri.
				 * */
				cur = uri + (cur - scheme);

				sep = strchr(cur, '/');
				if (sep != NULL)
					length = sep - cur;
				else
					length = strlen(cur);

				*host = g_strndup(cur, length);
			} else
				*host = g_strdup(cur);

			if (*host == NULL)
				goto error;
		}
	} else {
		if (exclusion == FALSE ||
				(exclusion == TRUE && proto == FALSE))
			goto error;
		else
			ret = PACRUNNER_MANUAL_EXCLUDE_ANY;
	}

	g_free(scheme);

	return ret;

error:
	if (protocol != NULL) {
		g_free(*protocol);
		*protocol = NULL;
	}

	g_free(scheme);

	return -EINVAL;
}

static void free_exclude(gpointer data)
{
	struct pacrunner_manual_exclude *exclude;

	exclude = (struct pacrunner_manual_exclude *) data;
	if (exclude == NULL)
		return;

	g_free(exclude->host);
	g_free(exclude);
}

static void __pacrunner_manual_destroy_excludes(GList **excludes)
{
	int i;

	if (excludes == NULL)
		return;

	for (i = 0; i < PACRUNNER_PROTOCOL_MAXIMUM_NUMBER; i++)
		g_list_free_full(excludes[i], free_exclude);

	g_free(excludes);
}

static GList **__pacrunner_manual_create_excludes(const char **excludes)
{
	struct pacrunner_manual_exclude *exclude;
	char *host, *protocol;
	GList **result = NULL;
	int ret, proto;
	char **uri;

	if (excludes == NULL)
		return NULL;

	result = g_try_malloc0(PACRUNNER_PROTOCOL_MAXIMUM_NUMBER *
							sizeof(GList *));
	if (result == NULL)
		return NULL;

	for (uri = (char **)excludes; *uri != NULL; uri++) {
		ret = parse_uri(*uri, &host, &protocol, TRUE, TRUE);

		if (ret < 0)
			continue;

		proto = get_protocol_from_string(protocol);
		if (proto == PACRUNNER_PROTOCOL_UNKNOWN)
			goto error;

		exclude = g_try_malloc0(sizeof(
					struct pacrunner_manual_exclude));
		if (exclude == NULL)
			goto error;

		exclude->appliance = ret;
		exclude->host = host;

		if (host != NULL)
			exclude->host_length = strlen(host);

		result[proto] = g_list_append(result[proto], exclude);

		g_free(protocol);
		protocol = NULL;
		host = NULL;
	}

	return result;

error:
	g_free(host);
	g_free(protocol);

	__pacrunner_manual_destroy_excludes(result);

	return NULL;
}

static void __pacrunner_manual_destroy_servers(GList **servers)
{
	int i;

	if (servers == NULL)
		return;

	for (i = 0; i < PACRUNNER_PROTOCOL_MAXIMUM_NUMBER; i++)
		g_list_free_full(servers[i], g_free);

	g_free(servers);
}

static const char *protocol_to_prefix_string(enum pacrunner_manual_protocol proto)
{
	switch (proto) {
	case PACRUNNER_PROTOCOL_ALL:
	case PACRUNNER_PROTOCOL_HTTP:
	case PACRUNNER_PROTOCOL_HTTPS:
	case PACRUNNER_PROTOCOL_FTP:
		return "PROXY";
	case PACRUNNER_PROTOCOL_SOCKS:
		return "SOCKS";
	case PACRUNNER_PROTOCOL_SOCKS4:
		return "SOCKS4";
	case PACRUNNER_PROTOCOL_SOCKS5:
		return "SOCKS5";
	case PACRUNNER_PROTOCOL_MAXIMUM_NUMBER:
	case PACRUNNER_PROTOCOL_UNKNOWN:
		break;
	};

	return "";
}

static GList *append_proxy(GList *list,
			enum pacrunner_manual_protocol proto, char *host)
{
	char *proxy;

	proxy = g_strdup_printf("%s %s",
				protocol_to_prefix_string(proto), host);
	if (proxy == NULL)
		return list;

	return g_list_append(list, proxy);
}

static GList **__pacrunner_manual_create_servers(const char **servers)
{
	char *host, *protocol;
	GList **result;
	char **uri;
	int proto;
	int ret;

	if (servers == NULL)
		return NULL;

	result = g_try_malloc0(PACRUNNER_PROTOCOL_MAXIMUM_NUMBER *
							sizeof(GList *));
	if (result == NULL)
		return NULL;

	for (uri = (char **)servers; *uri != NULL; uri++) {
		ret = parse_uri(*uri, &host, &protocol, TRUE, FALSE);

		if (ret < 0)
			continue;

		proto = get_protocol_from_string(protocol);
		if (proto == PACRUNNER_PROTOCOL_UNKNOWN)
			goto error;

		result[proto] = append_proxy(result[proto], proto, host);

		if (proto == PACRUNNER_PROTOCOL_SOCKS) {
			result[PACRUNNER_PROTOCOL_SOCKS4] = append_proxy(
					result[PACRUNNER_PROTOCOL_SOCKS4],
					PACRUNNER_PROTOCOL_SOCKS4, host);
			result[PACRUNNER_PROTOCOL_SOCKS5] = append_proxy(
					result[PACRUNNER_PROTOCOL_SOCKS5],
					PACRUNNER_PROTOCOL_SOCKS5, host);
		}

		g_free(protocol);
		g_free(host);
	}

	return result;

error:
	g_free(host);
	g_free(protocol);

	__pacrunner_manual_destroy_servers(result);

	return NULL;
}

static gboolean is_exclusion_matching(GList *excludes_list,
					const char *host)
{
	struct pacrunner_manual_exclude *exclusion;
	GList *excludes = NULL;
	char *cursor;

	for (excludes = excludes_list; excludes != NULL;
					excludes = excludes->next) {
		exclusion = (struct pacrunner_manual_exclude *) excludes->data;
		if (exclusion == NULL)
			continue;

		cursor = NULL;

		if (exclusion->host != NULL)
			cursor = strstr(host, exclusion->host);

		switch (exclusion->appliance) {
		case PACRUNNER_MANUAL_EXCLUDE_POST:
			if (cursor == NULL)
				break;

			if ((int)strlen(cursor) < exclusion->host_length)
				break;

			if (*(cursor + exclusion->host_length) == '\0')
				return TRUE;

			break;
		case PACRUNNER_MANUAL_EXCLUDE_PRE:
			if (cursor == host)
				return TRUE;

			break;
		case PACRUNNER_MANUAL_EXCLUDE_ANY:
			if (exclusion->host != NULL) {
				if (cursor != NULL)
					return TRUE;
				else
					break;
			}

			return TRUE;
		default:
			break;
		}
	}

	return FALSE;
}

static gboolean is_url_excluded(GList **excludes,
				const char *host,
				enum pacrunner_manual_protocol proto)
{
	if (excludes == NULL)
		return FALSE;

	if (excludes[PACRUNNER_PROTOCOL_ALL] != NULL)
		if (is_exclusion_matching(excludes[PACRUNNER_PROTOCOL_ALL],
								host) == TRUE)
			return TRUE;

	if (proto == PACRUNNER_PROTOCOL_UNKNOWN)
		return FALSE;

	if (excludes[proto] != NULL)
		if (is_exclusion_matching(excludes[proto], host) == TRUE)
			return TRUE;

	return FALSE;
}

static inline char *append_server(char *prev_result, const char *proxy)
{
	char *result;

	if (prev_result == NULL)
		return g_strdup(proxy);

	result = g_strjoin("; ", prev_result, proxy, NULL);
	if (result == NULL)
		return prev_result;

	g_free(prev_result);

	return result;
}

static inline char *append_servers_to_proxy_string(char *prev_result,
							GList *proxies)
{
	char *result = prev_result;
	GList *list, *prev;

	prev = NULL;
	for (list = proxies; list != NULL && list != prev;
						prev = list, list = list->next)
		result = append_server(result, (const char *) list->data);

	return result;
}

static char *generate_proxy_string(GList **servers,
				enum pacrunner_manual_protocol proto)
{
	enum pacrunner_manual_protocol i;
	char *result = NULL;

	/* if the protocol is known, we will prefer to set same
	 * protocol-based proxies first, if any... */
	if (proto >= PACRUNNER_PROTOCOL_HTTP &&
				proto < PACRUNNER_PROTOCOL_MAXIMUM_NUMBER) {
		if (servers[proto] != NULL)
			result = append_servers_to_proxy_string(result,
							servers[proto]);

		if (proto == PACRUNNER_PROTOCOL_SOCKS) {
			if (servers[PACRUNNER_PROTOCOL_SOCKS4] != NULL)
				result = append_servers_to_proxy_string(result,
					servers[PACRUNNER_PROTOCOL_SOCKS4]);
			if (servers[PACRUNNER_PROTOCOL_SOCKS5] != NULL)
				result = append_servers_to_proxy_string(result,
					servers[PACRUNNER_PROTOCOL_SOCKS5]);
		}
	}

	/* And/or we add the rest in the list */
	for (i = 0; i < PACRUNNER_PROTOCOL_MAXIMUM_NUMBER; i++) {
		if (i == proto || (proto == PACRUNNER_PROTOCOL_SOCKS &&
					(i == PACRUNNER_PROTOCOL_SOCKS4 ||
					 i == PACRUNNER_PROTOCOL_SOCKS5)))
			continue;

		if (servers[i] != NULL)
			result = append_servers_to_proxy_string(result,
								servers[i]);
	}

	return result;
}

static char *__pacrunner_manual_execute(const char *url,
					GList **servers,
					GList **excludes)
{
	char *protocol = NULL;
	char *result = NULL;
	char *host = NULL;
	int proto;

	if (servers == NULL)
		return NULL;

	if (parse_uri((char *)url, &host, &protocol, FALSE, FALSE) < 0)
		goto direct;

	proto = get_protocol_from_string(protocol);

	if (is_url_excluded(excludes, host, proto) == TRUE)
		goto direct;

	result = generate_proxy_string(servers, proto);

direct:
	g_free(protocol);
	g_free(host);

	return result;
}

static void dump_proxy_exclusion(gpointer data, gpointer user_data)
{
	struct pacrunner_manual_exclude *exclusion;

	exclusion = (struct pacrunner_manual_exclude *) data;
	if (exclusion == NULL)
		return;

	printf("\tappliance: %s\n",
			get_appliance_to_string(exclusion->appliance));
	printf("\thost: %s (%d)\n", exclusion->host, exclusion->host_length);
}

static void dump_excludes(GList **excludes)
{
	int i;

	printf("EXCLUDES:\n");

	if (excludes == NULL) {
		printf("\tNO EXCLUDES\n");
		return;
	}

	for (i = 0; i < PACRUNNER_PROTOCOL_MAXIMUM_NUMBER; i++) {
		printf("Protocol: %s\n", get_protocol_to_string(i));

		if (excludes[i] == NULL) {
			printf("\tNONE\n");
			continue;
		}

		g_list_foreach(excludes[i], dump_proxy_exclusion, NULL);
		printf("\n");
	}
}

static void dump_proxy_server(gpointer data, gpointer user_data)
{
	printf("\t%s\n", (char *)data);
}

static void dump_servers(GList **servers)
{
	int i;

	printf("SERVERS:\n");

	if (servers == NULL) {
		printf("\tNO SERVERS\n");
		return;
	}

	for (i = 0; i < PACRUNNER_PROTOCOL_MAXIMUM_NUMBER; i++) {
		printf("Protocol: %s\n", get_protocol_to_string(i));

		if (servers[i] == NULL) {
			printf("\tNONE\n");
			continue;
		}

		g_list_foreach(servers[i], dump_proxy_server, NULL);
		printf("\n");
	}
}

static void print_help(void)
{
	printf("Manual Proxy configuration test tool\n"
		"Usage:\n"
		"-e,--excludes <list>: provides an exclusion list (optional)\n"
		"-h,--help           : prints this help message\n"
		"-s,--servers  <list>: provides the proxy server(s) (mandatory)\n"
		"-t,--test     <list>: provides the URLs to test (mandatory)\n"
		"list should be made like \"elt1,elt2\" and so on\n"
		"\n"
		"Servers example:\n"
		"http://stuff.com   -> http proxy\n"
		"stuff.com          -> general proxy\n"
		"ftp://foo.org:4578 -> ftp proxy, with specific port\n"
		"\n"
		"Exclusion example:\n"
		"If a uri matches an exclusion rule, it will not be proxied\n"
		"port number is not allowed in exclusion rules\n"
		"'*' is not allowed for protocols\n"
		"stuff*foo is not allowed\n"
		"http://      -> all http\n"
		"http://*foo* -> all http containing foo\n"
		"*com         -> all host ending with com\n"
		"ftp://stuff* -> all ftp starting with stuff\n");
}

static struct option long_options[] = {
	{"excludes", 1, 0, 'e'},
	{"help",     0, 0, 'h'},
	{"servers",  1, 0, 's'},
	{"test",     1, 0, 't'},
	{NULL,       0, 0,  0 }
};

static const char *short_options = "e:hs:t:";

int main(int argc, char *argv[])
{
	char **excludes_list, **servers_list, **tests_list;
	GList **excludes = NULL;
	GList **servers = NULL;
	int opt, option_index;
	char **url;

	excludes_list = NULL;
	servers_list = NULL;
	tests_list = NULL;
	option_index = 0;

	while ((opt = getopt_long(argc, argv, short_options,
				long_options, &option_index)) != -1) {
		switch (opt) {
		case 'e':
			excludes_list = g_strsplit(optarg, ",", 0);
			if (excludes_list == NULL)
				exit(EXIT_FAILURE);

			break;
		case 'h':
			print_help();
			exit(EXIT_SUCCESS);

			break;
		case 's':
			servers_list = g_strsplit(optarg, ",", 0);
			if (servers_list == NULL)
				exit(EXIT_FAILURE);

			break;
		case 't':
			tests_list = g_strsplit(optarg, ",", 0);
			if (tests_list == NULL)
				exit(EXIT_FAILURE);

			break;
		case '?':
			break;
		default:
			printf("Unknown option\n"
				"please see --help for more informations\n");
			exit(EXIT_FAILURE);

			break;
		}
	}

	if (servers_list == NULL || tests_list == NULL) {
		printf("You must provide server(s) and test(s) options\n");
		exit(EXIT_FAILURE);
	}

	servers = __pacrunner_manual_create_servers((const char **)
								servers_list);
	if (servers == NULL)
		exit(EXIT_FAILURE);

	excludes = __pacrunner_manual_create_excludes((const char **)
								excludes_list);

	dump_excludes(excludes);
	dump_servers(servers);

	g_strfreev(excludes_list);
	g_strfreev(servers_list);

	for (url = tests_list; *url != NULL; url++) {
		char *proxy;

		printf("Url: %s -> ", *url);

		proxy = __pacrunner_manual_execute(*url, servers, excludes);
		if (proxy == NULL)
			printf("DIRECT\n");
		else
			printf("%s\n", proxy);
	}

	g_strfreev(tests_list);

	__pacrunner_manual_destroy_excludes(excludes);
	__pacrunner_manual_destroy_servers(servers);

	return 0;
}