This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathcore.c
1457 lines (1166 loc) · 47 KB
/
core.c
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _PYTHON_PLUGIN
/* Python2 API specificity : http://docs.python.org/2/c-api/intro.html#includes */
#include <Python.h>
#endif
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <fnmatch.h>
#include <time.h>
#include <sys/time.h>
#if defined( __DARWIN__ ) || defined( __FREESBD__ )
#include <signal.h>
#endif
#include "core.h"
#include "main.h"
#include "utils.h"
#include "socket.h"
#include "plugin.h"
#include "http.h"
#include "ssl.h"
#include "control-server.h"
#ifdef _PYTHON_PLUGIN
#include "plugin-python.h"
#endif
#ifdef _C_PLUGIN
#include "plugin-c.h"
#endif
#ifdef _RUBY_PLUGIN
#include "plugin-ruby.h"
#endif
#ifdef _PERL_PLUGIN
#include "plugin-perl.h"
#endif
#ifdef _LUA_PLUGIN
#include "plugin-lua.h"
#endif
#ifdef _TCL_PLUGIN
#include "plugin-tcl.h"
#endif
#ifdef _JAVA_PLUGIN
#include "plugin-java.h"
#endif
static pthread_mutex_t request_id_mutex;
/**
* Allocate safely a new and unique request id
*
* @return the new request id
*/
static unsigned long get_new_request_id()
{
unsigned long rid;
pthread_mutex_lock(&request_id_mutex);
rid = request_id;
request_id++;
pthread_mutex_unlock(&request_id_mutex);
#ifdef DEBUG
xlog(LOG_DEBUG, "Allocating ID #%ld\n", rid);
#endif
return rid;
}
/**
*
*/
void proxenet_initialize_plugins()
{
plugin_t *plugin = plugins_list;
plugin_t *prec_plugin = NULL;
plugin_t *next_plugin = NULL;
while(plugin) {
/* plugin already loaded, move on */
if(plugin->state == ACTIVE){
plugin = plugin->next;
continue;
}
switch (plugin->type) {
#ifdef _PYTHON_PLUGIN
case _PYTHON_:
if (!proxenet_python_initialize_vm(plugin) && !proxenet_python_load_file(plugin))
proxenet_plugin_set_state(plugin, ACTIVE);
break;
#endif
#ifdef _C_PLUGIN
case _C_:
if (!proxenet_c_initialize_vm(plugin) && !proxenet_c_load_file(plugin))
proxenet_plugin_set_state(plugin, ACTIVE);
break;
#endif
#ifdef _RUBY_PLUGIN
case _RUBY_:
if (!proxenet_ruby_initialize_vm(plugin) && !proxenet_ruby_load_file(plugin))
proxenet_plugin_set_state(plugin, ACTIVE);
break;
#endif
#ifdef _PERL_PLUGIN
case _PERL_:
if (!proxenet_perl_initialize_vm(plugin) && !proxenet_perl_load_file(plugin))
proxenet_plugin_set_state(plugin, ACTIVE);
break;
#endif
#ifdef _LUA_PLUGIN
case _LUA_:
if (!proxenet_lua_initialize_vm(plugin) && !proxenet_lua_load_file(plugin))
proxenet_plugin_set_state(plugin, ACTIVE);
break;
#endif
#ifdef _TCL_PLUGIN
case _TCL_:
if (!proxenet_tcl_initialize_vm(plugin) && !proxenet_tcl_load_file(plugin))
proxenet_plugin_set_state(plugin, ACTIVE);
break;
#endif
#ifdef _JAVA_PLUGIN
case _JAVA_:
if (!proxenet_java_initialize_vm(plugin) && !proxenet_java_load_file(plugin))
proxenet_plugin_set_state(plugin, ACTIVE);
break;
#endif
default:
break;
}
if (plugin->state == ACTIVE){
if (cfg->verbose > 1)
xlog(LOG_INFO, "Successfully initialized '%s'\n", plugin->filename);
prec_plugin = plugin;
plugin = plugin->next;
continue;
}
proxenet_plugin_set_state(plugin, INACTIVE);
if(prec_plugin) prec_plugin->next = plugin->next;
else plugins_list = plugin->next;
if (cfg->verbose > 1)
xlog(LOG_ERROR, "Removing '%s' from plugin list\n", plugin->filename);
next_plugin = plugin->next;
proxenet_free_plugin(plugin);
plugin = next_plugin;
}
}
/**
* Disable plugins and kill all the interpreters allocated in a clean (i.e. API-provided) way.
*
* Caller must make sure proxenet is in safe state (child threads are joined) before calling
* this function.
*/
void proxenet_destroy_plugins_vm()
{
plugin_t *p;
interpreter_t *vm;
for (p=plugins_list; p!=NULL; p=p->next) {
vm = NULL;
if (!p->interpreter->ready)
continue;
/*
* The function `proxenet_*_destroy_plugin()` must set the current plugin as
* INACTIVE, remove all the active references and clear allocated blocks.
*/
switch (p->type) {
#ifdef _PYTHON_PLUGIN
case _PYTHON_: proxenet_python_destroy_plugin(p); break;
#endif
#ifdef _C_PLUGIN
case _C_: proxenet_c_destroy_plugin(p); break;
#endif
#ifdef _RUBY_PLUGIN
case _RUBY_: proxenet_ruby_destroy_plugin(p); break;
#endif
#ifdef _PERL_PLUGIN
case _PERL_: proxenet_perl_destroy_plugin(p); break;
#endif
#ifdef _LUA_PLUGIN
case _LUA_: proxenet_lua_destroy_plugin(p); break;
#endif
#ifdef _TCL_PLUGIN
case _TCL_: proxenet_tcl_destroy_plugin(p); break;
#endif
#ifdef _JAVA_PLUGIN
case _JAVA_: proxenet_java_destroy_plugin(p); break;
#endif
default: break;
}
if(cfg->verbose)
xlog(LOG_INFO, "Plugin '%s' has been destroyed.\n", p->name);
if (count_initialized_plugins_by_type(p->type) > 0)
continue;
vm = p->interpreter;
/*
* The function `proxenet_*_destroy_vm()` calls API to delete the VM. The function
* is called when the last plugin[type] has been destroyed
*/
switch (p->type) {
#ifdef _PYTHON_PLUGIN
case _PYTHON_: proxenet_python_destroy_vm(vm); break;
#endif
#ifdef _C_PLUGIN
case _C_: proxenet_c_destroy_vm(vm); break;
#endif
#ifdef _RUBY_PLUGIN
case _RUBY_: proxenet_ruby_destroy_vm(vm); break;
#endif
#ifdef _PERL_PLUGIN
case _PERL_: proxenet_perl_destroy_vm(vm); break;
#endif
#ifdef _LUA_PLUGIN
case _LUA_: proxenet_lua_destroy_vm(vm); break;
#endif
#ifdef _TCL_PLUGIN
case _TCL_: proxenet_tcl_destroy_vm(vm); break;
#endif
#ifdef _JAVA_PLUGIN
case _JAVA_: proxenet_java_destroy_vm(vm); break;
#endif
default: break;
}
if(cfg->verbose)
xlog(LOG_INFO, "VM %s has been destroyed.\n", supported_plugins_str[p->type]);
}
return;
}
/**
* (De)Activate a plugin at runtime
*
* @return 0 -> new status inactive
* @return 1 -> new status active
* @return -1 on error
*/
int proxenet_toggle_plugin(int plugin_id)
{
plugin_t *p;
p = proxenet_get_plugin_by_id( plugin_id );
if (!p)
return -1;
switch (p->state){
case INACTIVE:
proxenet_plugin_set_state(p, ACTIVE);
return 1;
case ACTIVE:
proxenet_plugin_set_state(p, INACTIVE);
return 0;
default:
return -1;
}
return -1;
}
/**
*
*/
static int proxenet_apply_plugins(request_t *request)
{
plugin_t *p = NULL;
char *old_data = NULL;
char *res = NULL;
char* (*plugin_function)(plugin_t*, request_t*) = NULL;
for (p=plugins_list; p!=NULL; p=p->next) {
if (p->state == INACTIVE)
continue;
switch (p->type) {
#ifdef _PYTHON_PLUGIN
case _PYTHON_: plugin_function = proxenet_python_plugin; break;
#endif
#ifdef _C_PLUGIN
case _C_: plugin_function = proxenet_c_plugin; break;
#endif
#ifdef _RUBY_PLUGIN
case _RUBY_: plugin_function = proxenet_ruby_plugin; break;
#endif
#ifdef _PERL_PLUGIN
case _PERL_: plugin_function = proxenet_perl_plugin; break;
#endif
#ifdef _LUA_PLUGIN
case _LUA_: plugin_function = proxenet_lua_plugin; break;
#endif
#ifdef _TCL_PLUGIN
case _TCL_: plugin_function = proxenet_tcl_plugin; break;
#endif
#ifdef _JAVA_PLUGIN
case _JAVA_: plugin_function = proxenet_java_plugin; break;
#endif
default:
xlog(LOG_CRITICAL, "Type %d not supported (yet)\n", p->type);
return -1;
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Calling '%s:%s' with rid=%d (%s)\n",
p->name,
request->type==REQUEST?CFG_REQUEST_PLUGIN_FUNCTION:CFG_RESPONSE_PLUGIN_FUNCTION,
request->id,
supported_plugins_str[p->type]
);
#endif
old_data = request->data;
#ifdef DEBUG
struct timeval tstart, tend;
proxenet_xzero(&tstart, sizeof(struct timeval));
proxenet_xzero(&tend, sizeof(struct timeval));
gettimeofday(&tstart, NULL);
#endif
res = (*plugin_function)(p, request);
#ifdef DEBUG
gettimeofday(&tend, NULL);
unsigned long sec = tend.tv_sec-tstart.tv_sec;
unsigned long msec = (tend.tv_usec-tstart.tv_usec)/1000;
unsigned long usec = (tend.tv_usec-tstart.tv_usec)%1000;
xlog(LOG_DEBUG, "[%s] '%s:%s' executed in %dsec, %dms, %dus\n",
supported_plugins_str[p->type], p->name,
request->type==REQUEST?CFG_REQUEST_PLUGIN_FUNCTION:CFG_RESPONSE_PLUGIN_FUNCTION,
sec, msec, usec);
#endif
/*
* We consider a plugin execution has failed if one of those conditions is found:
* - the plugin_*_apply() function returned NULL
* - the new request size is 0
*/
if (res == NULL || request->size==0) {
/*
* If the plugin function returned NULL, it means that an error occured
* somewhere in the VM run. To be safe, we disable the plugin, and restore
* the previous context.
*/
request->data = old_data;
xlog(LOG_ERROR, "Plugin '%s' failed, disabling...\n", p->name);
proxenet_plugin_set_state(p, INACTIVE);
continue;
}
request->data = res;
if (request->data) {
/*
* If new_data is different, it means a new buffer was allocated by
* (*plugin_function)(). The old_data can then be free-ed.
*/
proxenet_xfree(old_data);
#ifdef DEBUG
xlog(LOG_DEBUG,
"New data from '%s:%s' on rid=%d\n",
request->type==REQUEST?CFG_REQUEST_PLUGIN_FUNCTION:CFG_RESPONSE_PLUGIN_FUNCTION,
p->name,
p->id);
#endif
} else {
/* Otherwise (data different or error), use the original data */
request->data = old_data;
#ifdef DEBUG
xlog(LOG_DEBUG,
"No new data from '%s:%s' on rid=%d\n",
request->type==REQUEST?CFG_REQUEST_PLUGIN_FUNCTION:CFG_RESPONSE_PLUGIN_FUNCTION,
p->name,
p->id);
#endif
}
}
return 0;
}
/**
* This function is called by all threads to treat to process the request and response.
* It will also apply the plugins.
*
* @param server_socket is the socket received by the main thread from the web browser (acting like server
* to web browser)
*/
void proxenet_process_http_request(sock_t server_socket)
{
unsigned char server_socket_ip[128];
int server_socket_port;
sock_t client_socket;
request_t req;
int retcode, n;
fd_set rfds;
struct timespec ts;
ssl_context_t ssl_context;
bool is_ssl;
sigset_t emptyset;
bool is_new_http_connection = false;
client_socket = retcode = n = -1;
proxenet_xzero(&req, sizeof(request_t));
proxenet_xzero(&ssl_context, sizeof(ssl_context_t));
proxenet_xzero(server_socket_ip, sizeof(server_socket_ip));
if (get_ip_address_from_fd(server_socket_ip, sizeof(server_socket_ip), server_socket) < 0)
return;
if ((server_socket_port = get_port_from_fd(server_socket)) < 0)
return;
if(cfg->verbose)
xlog(LOG_INFO, "Processing new HTTP request on sock=#%d for %s:%d\n", server_socket, server_socket_ip, server_socket_port);
/* wait for any event on sockets */
while(proxy_state == ACTIVE) {
if (server_socket < 0) {
xlog(LOG_ERROR, "sock browser->%s (#%d) died unexpectedly\n", PROGNAME, server_socket);
break;
}
ts.tv_sec = HTTP_TIMEOUT_SOCK;
ts.tv_nsec = 0;
FD_ZERO(&rfds);
FD_SET(server_socket, &rfds);
if (client_socket > 0)
FD_SET(client_socket, &rfds);
sigemptyset(&emptyset);
retcode = pselect(FD_SETSIZE, &rfds, NULL, NULL, &ts, &emptyset);
if (retcode < 0) {
if (errno == EINTR) {
continue;
} else {
xlog(LOG_CRITICAL, "[thread] pselect returned %d: %s\n", retcode, strerror(errno));
break;
}
}
if (retcode == 0) {
continue;
}
is_ssl = ssl_context.use_ssl;
/* is there data from web browser to proxy ? */
if( FD_ISSET(server_socket, &rfds ) ) {
if(is_ssl && req.do_intercept) {
n = proxenet_read_all(server_socket,
&req.data,
&(ssl_context.server.context));
} else {
n = proxenet_read_all(server_socket, &req.data, NULL);
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Got %dB from client (%s srv_sock=#%d intercept_flag=%s)\n",
n, is_ssl?"SSL":"PLAIN", server_socket,
req.do_intercept?"true":"false");
#endif
if (n < 0) {
break;
}
if (n == 0){
#ifdef DEBUG
xlog(LOG_DEBUG, "%s\n", "Socket EOF from client");
#endif
break;
}
/* from here, n can only be positive */
req.size = (size_t) n;
bytes_sent += n;
if (req.id > 0 && !req.do_intercept){
#ifdef DEBUG
xlog(LOG_DEBUG, "Intercept disabled for browser->'%s'\n", req.http_infos.hostname);
#endif
goto send_to_server;
}
/* proxy keep-alive */
if (req.id > 0){
request_t* old_req = (request_t*)proxenet_xmalloc(sizeof(request_t));
memcpy(old_req, &req, sizeof(request_t));
char* host = proxenet_xstrdup2( req.http_infos.hostname );
free_http_infos(&(req.http_infos));
if (parse_http_request(&req) < 0){
xlog(LOG_ERROR, "Failed to update HTTP information for request %d\n", req.id);
proxenet_xfree( host );
proxenet_xfree( old_req );
req.id = 0;
break;
}
if (strcmp( host, req.http_infos.hostname )){
/* reset the client connection parameters */
if (cfg->verbose)
xlog(LOG_INFO, "Reusing sock=%d (old request=%d, old sock=%d) %s/%s\n",
server_socket, req.id, client_socket, host, req.http_infos.hostname );
proxenet_close_socket(client_socket, &(ssl_context.client));
free_http_infos(&(req.http_infos));
client_socket = -1;
}
proxenet_xclean( old_req, sizeof(request_t) );
proxenet_xfree( host );
}
req.type = REQUEST;
req.id = get_new_request_id();
/* is connection to server not established ? -> new request */
if ( client_socket < 0) {
retcode = create_http_socket(&req, &server_socket, &client_socket, &ssl_context);
if (retcode < 0) {
xlog(LOG_ERROR, "Failed to create %s->server socket\n", PROGNAME);
proxenet_xfree(req.data);
req.id = 0;
break;
}
if (ssl_context.use_ssl) {
req.is_ssl = true;
if (req.do_intercept == false) {
#ifdef DEBUG
xlog(LOG_DEBUG, "SSL interception client <-> %s <-> server disabled\n", PROGNAME);
#endif
proxenet_xfree(req.data);
req.type = REQUEST;
req.id = get_new_request_id();
continue;
} else if (ssl_context.server.is_valid && ssl_context.client.is_valid) {
#ifdef DEBUG
xlog(LOG_DEBUG, "SSL interception client <-> %s <-> server established\n", PROGNAME);
#endif
proxenet_xfree(req.data);
is_new_http_connection = true;
continue;
}
xlog(LOG_ERROR, "%s\n", "Failed to establish interception");
proxenet_xfree(req.data);
break;
}
is_new_http_connection = true;
}
/* if proxenet does not relay to another proxy */
if (!cfg->proxy.host) {
if (is_new_http_connection) {
if (req.http_infos.proto_type==HTTPS) {
/*
* SSL request fields still have the values gathered in the CONNECT
* Those values must be updated to reflect the real request
*/
free_http_infos(&(req.http_infos));
retcode = parse_http_request(&req);
} else {
/*
* Format requests
* GET http://foo/bar.blah HTTP/1.1 ...
* into
* GET /bar.blah HTTP/1.1 ...
*/
retcode = format_http_request(&req);
}
} else {
/* if here, at least 1 request has been to server */
/* so simply forward */
/* e.g. using HTTP/1.1 100 Continue */
#ifdef DEBUG
xlog(LOG_DEBUG, "Resuming stream '%d'->'%d'\n", client_socket, server_socket);
#endif
free_http_infos(&(req.http_infos));
retcode = parse_http_request(&req);
}
if (retcode < 0){
xlog(LOG_ERROR, "Failed to update %s information in request %d\n",
is_ssl?"HTTPS":"HTTP", req.id);
proxenet_xfree(req.data);
break;
}
}
if(cfg->ie_compat){
if (is_ssl)
retcode = ie_compat_read_post_body(server_socket, &req, &(ssl_context.server.context));
else
retcode = ie_compat_read_post_body(server_socket, &req, NULL);
if (retcode < 0){
xlog(LOG_ERROR, "%s\n", "Extending IE POST: failed");
proxenet_xfree(req.data);
break;
}
}
/* apply plugins for requests (from browser to server) */
if (cfg->verbose) {
xlog(LOG_INFO, "%s request to '%s:%d'\n",
is_ssl?"SSL":"plain", req.http_infos.hostname, req.http_infos.port);
if (cfg->verbose > 1)
xlog(LOG_INFO, "%s %s %s\n",
req.http_infos.method, req.http_infos.uri, req.http_infos.version);
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Request %d pre-plugins: buflen:%lu\n",
req.id, req.size);
#endif
/* hook request with all plugins in plugins_list */
if ( proxenet_apply_plugins(&req) < 0) {
/* extremist action: any error on any plugin discard the whole request */
proxenet_xfree( req.data );
break;
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Request %d post-plugins: buflen:%lu\n",
req.id, req.size);
if(cfg->verbose > 2)
proxenet_hexdump(req.data, req.size);
#endif
send_to_server:
/* send modified data */
if (is_ssl && req.do_intercept) {
retcode = proxenet_ssl_write(&(ssl_context.client.context), req.data, req.size);
} else {
retcode = proxenet_write(client_socket, req.data, req.size);
}
/* reset data */
proxenet_xfree(req.data);
req.size = 0;
if (retcode < 0) {
xlog(LOG_ERROR, "[%d] %s\n", req.id, "Failed to write to server");
break;
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Written %d bytes to server (socket=%s socket #%d)\n",
retcode, is_ssl?"SSL":"PLAIN", client_socket);
#endif
} /* end FD_ISSET(data_from_browser) */
/* is there data from remote server to proxy ? */
if( client_socket > 0 && FD_ISSET(client_socket, &rfds ) ) {
if(req.is_ssl && req.do_intercept) {
n = proxenet_read_all(client_socket, &req.data, &ssl_context.client.context);
} else {
n = proxenet_read_all(client_socket, &req.data, NULL);
}
if (n==0 || n==-ENODATA){
#ifdef DEBUG
xlog(LOG_DEBUG, "Socket EOF from server (cli_sock=#%d)\n",
client_socket);
#endif
break;
}
if (n < 0){
xlog(LOG_ERROR, "read() %s on cli_sock=#%d failed: %d\n",
is_ssl?"SSL":"PLAIN",
client_socket, n);
break;
}
/* update request data structure */
req.type = RESPONSE;
/* from here, n can only be positive */
req.size = (size_t) n;
bytes_recv += n;
if (req.do_intercept==false){
#ifdef DEBUG
xlog(LOG_DEBUG, "Intercept disabled for '%s'->browser\n", req.http_infos.hostname);
#endif
goto send_to_client;
}
/* apply plugins for response (from server to browser) */
#ifdef DEBUG
xlog(LOG_DEBUG, "Response %d pre-plugins: buflen:%lu\n",
req.id, req.size);
#endif
/* execute response hooks */
if ( proxenet_apply_plugins(&req) < 0) {
/* extremist action: any error on any plugin discard the whole request */
proxenet_xfree(req.data);
break;
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Response %d post-plugins: buflen:%lu\n",
req.id, req.size);
if(cfg->verbose > 2)
proxenet_hexdump(req.data, req.size);
#endif
send_to_client:
/* send modified data to client */
if (req.is_ssl && req.do_intercept)
retcode = proxenet_ssl_write(&(ssl_context.server.context), req.data, req.size);
else
retcode = proxenet_write(server_socket, req.data, req.size);
if (retcode < 0) {
xlog(LOG_ERROR, "[%d] %s\n", req.id, "proxy->client: write failed");
proxenet_xfree(req.data);
break;
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Written %d bytes to browser (socket=%s socket #%d)\n",
retcode, is_ssl?"SSL":"PLAIN", client_socket);
#endif
proxenet_xfree(req.data);
} /* end FD_ISSET(data_from_server) */
} /* end for(;;) { select() } */
if (req.id) {
if (cfg->verbose)
xlog(LOG_INFO, "End of request %d, cleaning context\n", req.id);
free_http_infos(&(req.http_infos));
}
/* close client socket */
if (client_socket > 0) {
if (cfg->verbose >= 2)
xlog(LOG_INFO, "Closing %s->server (fd=#%d)\n", PROGNAME, client_socket);
proxenet_close_socket(client_socket, &(ssl_context.client));
}
/* close local socket */
if (server_socket > 0) {
if (cfg->verbose >= 2)
xlog(LOG_INFO, "Closing browser->%s (fd=#%d)\n", PROGNAME, server_socket);
proxenet_close_socket(server_socket, &(ssl_context.server));
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Request %d: Structures closed, leaving\n", req.id);
#endif
/* and that's all folks */
return;
}
/**
*
*/
static void mark_thread_as_active(int i)
{
active_threads_bitmask |= (1 << i);
return;
}
/**
*
*/
static void mark_thread_as_inactive(int i)
{
active_threads_bitmask &= (unsigned long long)~(1 << i);
return;
}
/**
* Checks active threads bitmask to determine if a thread is alive.
*
* @param idx is the index of the thread to look up for
* @return true if this thread still alive
*/
bool is_thread_active(int idx)
{
return active_threads_bitmask & (1<<idx);
}
/**
*
*/
static void* process_thread_job(void* arg)
{
tinfo_t* tinfo = (tinfo_t*) arg;
pthread_t parent_tid;
#ifdef DEBUG
unsigned int tnum = tinfo->thread_num;
xlog(LOG_DEBUG, "Starting thread %d\n", tnum);
#endif
parent_tid = tinfo->main_tid;
/* treat request */
proxenet_process_http_request(tinfo->sock);
/* purge thread */
proxenet_xfree(arg);
/* signal main thread (parent) to clean up */
if (pthread_kill(parent_tid, SIGCHLD) < 0){
xlog(LOG_ERROR, "Sending SIGCHLD failed: %s\n", strerror(errno));
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Ending thread %d\n", tnum);
#endif
pthread_exit(0);
}
/**
* Returns the number of threads alive.
*/
unsigned int get_active_threads_size()
{
int i,n;
for (i=0, n=0; i<cfg->nb_threads; i++)
if (is_thread_active(i)) n++;
return n;
}
/**
*
*/
static int proxenet_start_new_thread(sock_t conn, int tnum, pthread_t* thread, pthread_attr_t* tattr)
{
tinfo_t* tinfo;
void* tfunc;
tinfo = (tinfo_t*)proxenet_xmalloc(sizeof(tinfo_t));
tfunc = &process_thread_job;
tinfo->thread_num = tnum;
tinfo->sock = conn;
tinfo->main_tid = pthread_self();
mark_thread_as_active( tnum );
return pthread_create(thread, tattr, tfunc, (void*)tinfo);
}
/**
*
*/
static void proxenet_join_thread(int i)
{
int ret;
ret = pthread_join(threads[i], NULL);
switch(ret){
case EDEADLK:
case EINVAL:
xlog(LOG_ERROR, "proxenet_join_thread() joining thread-%d produced error %d\n", i, ret);
break;
case ESRCH:
xlog(LOG_WARNING, "proxenet_join_thread() could not find thread-%d\n", i);
default:
#ifdef DEBUG
xlog(LOG_DEBUG, "Thread-%d has finished\n", i);
#endif
mark_thread_as_inactive( i );
}
return;
}
/**
* This function is a simple heartbeat for proxenet threads (childs only). It is
* based on pthread_kill(), probing for ESRCH return value to check if a thread has
* ended and clean the bitmask accordingly.
*/
static void purge_zombies()
{
int i;
for (i=0; i < cfg->nb_threads; i++) {
if (!is_thread_active(i)) continue;
if (pthread_kill(threads[i], 0) == ESRCH) {
#ifdef DEBUG
xlog(LOG_DEBUG, "Joining thread-%d\n", i);
#endif
proxenet_join_thread(i);
}
}
return;
}
/**
* Kill all the zombies !! Join all zombie threads and consider them as inactive.
*/
static void kill_zombies()
{
int i;