forked from macton/microproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.c
More file actions
575 lines (472 loc) · 14.5 KB
/
proxy.c
File metadata and controls
575 lines (472 loc) · 14.5 KB
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
//
// proxy.c : a minimal http proxy server
//
// CopyLeft (c) May 2001, by David McNab - david@rebirthing.co.nz, http://freeweb.sf.org
// Released subject to GNU General Public License.
//
// This module implements a minimal transparent http proxy server, with two key features:
// 1) Proxy's listening port, as well as hostname and port of downstream proxy server, can
// be easily changed in real time
// 2) It's easy to plug in a 'callback' function, which allows the caller to intervene in all http
// requests. This allows the caller to block certain websites, or redirect http requests to
// other sources of data, such as Freenet
//
// You shouldn't need to modify this code - it should offer you enough flexibility to meet your needs.
// But if you come up with any useful mods, I'd be warmly grateful for an email from you
// You are free to use, modify and redistrbute this source code, and any binaries, obj modules or
// libraries which use this code, subject to the following conditions:
//
// USAGE CONDITIONS
//
// 1) You must include in your source code the name, email address and website of the author (see above)
// 2) If you modify the code, you must note in this header that it is modified by you, and briefly state your modifications
// 3) You must not distribute any programs which use this code, unless you supply convenient access to
// full, current, working and buildable source code for such programs
//
// Failure to observe these modest conditions will render you a complete twat, and cause you to set up
// universal forces which will culminate in you finding yourself in numerous humiliating predicaments which
// others will find extremely amusing at your expense.
//
#include "stdio.h"
#include "string.h"
#ifdef WINDOWS
// Windows-specific wrappers for socket functions
#include "winsock2.h"
#define SockSend(sd, buf, len) send(sd, buf, len, 0)
#define SockReceive(sd, buf, len) recv(sd, buf, len, 0)
#define SockClose(sd) closesocket(sd)
#define SockAddrType struct sockaddr
#define SockError(msg) MessageBox(0,msg,"Proxy server problem", MB_SYSTEMMODAL)
#define SockLastError() WSAGetLastError()
#define SockErrWouldBlock WSAEWOULDBLOCK
#define SockSleep() Sleep(100L)
#define Strnicmp(s1, s2, n) strnicmp(s1, s2, n)
#else
// Linux/Cygwin-specific wrappers for socket functions
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
typedef int SOCKET;
typedef unsigned int UINT;
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR (-1)
#define SockSend(sd, buf, len) write(sd, buf, len)
#define SockReceive(sd, buf, len) read(sd, buf, len)
#define SockClose(sd) close(sd)
#define SockAddrType struct sockaddr_in
#define SockError(msg) fprintf(stderr, msg)
#define SockLastError() (errno)
#define SockErrWouldBlock EWOULDBLOCK
#define SockSleep() usleep(200000)
#define Strnicmp(s1, s2, n) strncasecmp(s1, s2, n)
#endif // platform-specific wrappers for socket functions
//
// EXPORTED DECLARATIONS
//
void proxy_start(int listenPortparm, char *extproxyaddr, int extproxyport, int (*callback_fn)(), int singlethread);
void proxy_newport(int newport);
//
// PRIVATE DECLARATIONS
//
static int proxy_server();
static UINT proxy_handlereq(SOCKET socket);
static int proxy_sockgets(SOCKET sock, char *buf, int len);
static int proxy_listenport;
static int proxy_singlethread;
static char *proxy_extproxyaddr = NULL;
static int proxy_extproxyport = 80;
static int (*proxy_callback_fn)(char *host, char *headers, int client_sock);
static void LaunchThread(int (*func)(), void *parg);
static int proxy_getaddr(char* HostName, int Port, SockAddrType *Result);
/////////////////////////////////////////////////////////////////////////
//
// EXPORTED FUNCTIONS
//
/////////////////////////////////////////////////////////////////////////
//
// Function: proxy_start()
//
// Arguments: listenPortParm port on which this proxy listens for http requests
// extproxyaddr string - hostname of downstream proxy, or NULL if none
// extproxyport port number of downstream proxy, disregarded if extproxyhost is NULL
// callback_fn pointer to function to call to intercept requests, or NULL if none
// comeback 1 to return immediately to caller and run proxy as a separate thread
// 0 to never return to the caller
// singlethread 1 to prevent proxy from launching separate threads for each request.
// useful for debugging, but makes the proxy very slow
//
// Returns: nothing
//
void proxy_start(int listenport, char *extproxyaddr, int extproxyport, int (*callback_fn)(), int comeback, int singlethread)
{
//printf("proxy_start: listenport = %d\n", listenport);
proxy_listenport = listenport;
proxy_extproxyaddr = extproxyaddr != NULL ? strdup(extproxyaddr) : NULL;
proxy_extproxyport = extproxyport;
proxy_callback_fn = callback_fn;
//proxy_singlethread = 1;
proxy_singlethread = singlethread;
if (comeback)
LaunchThread(proxy_server, NULL);
else
proxy_server(NULL);
}
// function which allows client to set a new listening port
void proxy_newport(int newport)
{
proxy_listenport = newport;
}
void proxy_newextproxy(char *addr, int port)
{
proxy_extproxyaddr = strdup(addr);
proxy_extproxyport = port;
}
//
// main thread which listens for incoming http connections, and launches a thread pair
// when a connection comes in
//
static int proxy_server(void *dummy)
{
int Status;
SOCKET sock_listen, sock_client;
int oldListenPort;
char *oldextproxyaddr;
int lastError;
unsigned long nbParm = 1000;
int one = 1;
SockAddrType sockAddr;
// loop around waiting for client connection or change of listen port
while (1)
{
// create the socket
oldListenPort = proxy_listenport;
oldextproxyaddr = proxy_extproxyaddr;
if (proxy_getaddr("127.0.0.1", proxy_listenport, &sockAddr) == 0)
{
SockError("proxy_server(): failed to get socket address");
return 1;
}
// bind the socket
sock_listen = socket(AF_INET, SOCK_STREAM, 0);
//Status = setsockopt (sock_listen, IPPROTO_TCP, TCP_NODELAY, (char * ) &one, sizeof (int));
Status = bind(sock_listen, &sockAddr, sizeof(sockAddr));
if(Status < 0)
{
SockError("proxy_server(): Failed to connect socket for receiving connections");
return 1;
}
// set socket to non-blocking mode
if ((Status = SockBlock(sock_listen, 1)) < 0)
{
SockError("proxy_server(): Failed to set non-block mode on socket");
return 1;
}
// set socket to listen
if (listen(sock_listen, SOMAXCONN) != 0)
{
SockError("proxy_server(): Failed to listening mode on socket");
return 1;
}
//printf("about to start awaiting connections\n");
// loop accepting connections with timeout
while (1)
{
// if external proxy addr has changed, ditch the strdup()'ed string
if (oldextproxyaddr != proxy_extproxyaddr)
{
if (oldextproxyaddr != NULL)
free(oldextproxyaddr);
oldextproxyaddr = proxy_extproxyaddr;
}
// has someone changed the listening port?
if (oldListenPort != proxy_listenport)
{
SockClose(sock_listen);
break; // fall out to top of outer loop, set up another socket on new port
}
// check for incoming connections
if ((sock_client = accept(sock_listen, NULL, NULL)) == INVALID_SOCKET)
{
if (SockLastError() != SockErrWouldBlock)
{
SockError("proxy_server(): accept() call failed");
return 1;
}
else
{
// no connections
SockSleep();
continue;
}
}
// there's a new connection
//printf("got connection\n");
if (proxy_singlethread)
proxy_handlereq(sock_client); // single-thread mode
else
{
// spawn a thread to handle request
// p_thrdsecattr initstacksize threadfn threadarg creationflags p_retthreadid
LaunchThread(proxy_handlereq, sock_client);
}
//MessageBox(0, "Got incoming connection", "FreeWeb Proxy", MB_SYSTEMMODAL);
}
// don't add any statements after this inner loop!
} // 'while (1)'
return 0; // how on earth did we get here???
} // 'proxyServer()'
//
// thread which handles a single http request
//
static int proxy_handlereq(SOCKET sock_client)
{
SOCKET sock_server;
char buf[65536];
char header_line[1024];
char host[256];
int serverPort = 80;
struct sockaddr serverSockAddr;
int gotHeader = 0;
int count;
int Status;
unsigned long nbParm = 0L;
char newline = '\n';
fd_set rfds;
int n, maxfd;
int one = 1;
buf[0] = '\0';
host[0] = '\0';
// set client socket non-blocking
if ((Status = SockBlock(sock_client, 0)) < 0)
{
SockError("proxy_handlereq(): Failed to clear non-block mode on socket");
return 1;
}
// now get headers from client
while ((count = proxy_sockgets(sock_client, header_line, 1024)) > 0)
{
gotHeader = 1;
// look for 'Host:' header
if (!Strnicmp(header_line, "host: ", 6))
{
char *pPort;
strcpy(host, header_line + 6);
if ((pPort = strchr(host, ':')) != NULL)
{
*pPort++ = '\0';
serverPort = atoi(pPort);
}
}
//MessageBox(0, header_line, "Req header", MB_SYSTEMMODAL);
strcat(buf, header_line);
strcat(buf, "\n");
}
// add second line terminator to mark end of header
strcat(buf, "\n");
// bail if nothing came through
if (!gotHeader)
{
SockClose(sock_client);
return 1;
}
// did we get a host address?
if (host[0] == '\0')
{
char err400[] = "HTTP/1.0 400 Invalid header received from browser\n\n";
SockSend(sock_client, err400, strlen(err400));
SockClose(sock_client);
SockError("proxy_handlereq(): host missing from http header");
return 1;
}
// allow callback function to intercept
if (proxy_callback_fn != NULL)
if ((*proxy_callback_fn)(host, buf, sock_client) != 0)
{
// callback took over
SockClose(sock_client);
return 0;
}
//MessageBox(0, buf, "Header", MB_SYSTEMMODAL);
// change host and portnum if using downstream proxy
if (proxy_extproxyaddr != NULL)
{
strcpy(host, proxy_extproxyaddr);
serverPort = proxy_extproxyport;
}
// try to find server
if (proxy_getaddr(host, serverPort, &serverSockAddr) == 0)
{
SockSend(sock_client, "404 Host Not Found\n\n", 20);
SockClose(sock_client);
return 1;
}
sock_server = socket(AF_INET, SOCK_STREAM, 0);
#ifdef WINDOWS
setsockopt(sock_server, IPPROTO_TCP, TCP_NODELAY, (char * ) &one, sizeof (int));
#endif
// try to connect to server
if ((Status = connect(sock_server, &serverSockAddr, sizeof(serverSockAddr))) < 0)
{
SockSend(sock_client, "404 Host Not Found\n\n", 20);
SockClose(sock_client);
return 1;
}
// send client's req to server
SockSend(sock_server, buf, strlen(buf));
//
// now loop around relaying stuff between server and client
//
maxfd = (sock_client > sock_server ) ? sock_client : sock_server;
for(;;)
{
FD_ZERO(&rfds);
FD_SET(sock_client, &rfds);
FD_SET(sock_server, &rfds);
if ((n = select(maxfd+1, &rfds, NULL, NULL, NULL)) < 0)
{
SockError("proxy_handlereq(): select() failed while handling http req");
//fprintf(logfp, "%s: select() failed!: ", prog);
//fperror(logfp, "");
return 1;
}
// got data from client - relay to server
if(FD_ISSET(sock_client, &rfds))
{
if ((n = SockReceive(sock_client, buf, sizeof(buf))) <= 0)
break; // end of request
if (SockSend(sock_server, buf, n) != n)
{
//fprintf(logfp, "%s: write to: %s failed: ", prog, http->host);
//fperror(logfp, "");
return 1;
}
continue;
}
// got data from server - relay to client
if (FD_ISSET(sock_server, &rfds))
{
if ((n = SockReceive(sock_server, buf, sizeof(buf))) < 0)
{
//fprintf(logfp, "%s: read from: %s failed: ", prog, http->host);
//fperror(logfp, "");
// eno = safe_strerror(errno);
// sprintf(buf, CFAIL, http->hostport, eno);
// freez(eno);
//write_socket(m_SockClient, buf, strlen(buf), 0);
return 1;
}
if (n == 0)
break; // got all from server
/* just write */
if (SockSend(sock_client, buf, n) != n)
{
//fprintf(logfp, "%s: write to client failed: ", prog);
//fperror(logfp, "");
return 1;
}
continue;
} // 'if (got something from server)'
} // 'for (;;)' - relaying data between client and server
// all done - close sockets and terminate this thread
SockClose(sock_client);
SockClose(sock_server);
// see you later
return 0;
} // 'proxy_handlereq()'
static int proxy_sockgets(SOCKET sock, char *buf, int len)
{
char *ptr = buf;
char *ptr_end = ptr + len - 1;
int error;
while (ptr < ptr_end)
{
switch (SockReceive(sock, ptr, 1))
{
case 1:
// got a char
if (*ptr == '\r')
continue;
else if (*ptr == '\n')
{
*ptr = '\0';
return ptr - buf;
}
else
{
ptr++;
continue;
}
case 0:
// connection closed at other end
*ptr = '\0';
return ptr - buf;
default:
// failure
#ifdef WINDOWS
error = WSAGetLastError();
#endif
return -1;
} // 'switch (recv)'
} // 'while ()'
// buffer full - return what we've got
return len;
} // 'SockGets()'
static int proxy_getaddr(char *HostName, int Port, SockAddrType *Result)
{
struct hostent* Host;
#ifdef WINDOWS
SOCKADDR_IN Address;
memset(Result, 0, sizeof(*Result));
memset(&Address, 0, sizeof(Address));
if ((Host = gethostbyname(HostName)) != NULL)
{
Address.sin_family = AF_INET;
Address.sin_port = htons((short)Port);
memcpy(&Address.sin_addr, Host->h_addr_list[0], Host->h_length);
memcpy(Result, &Address, sizeof(Address));
}
#else
Result->sin_family=AF_INET;
Result->sin_port=htons((unsigned short)Port);
Host=gethostbyname(HostName);
if(!Host)
{
unsigned long int addr=inet_addr(HostName);
if(addr!=-1)
Host=gethostbyaddr((char*)addr,sizeof(addr),AF_INET);
if(!Host)
{
if(errno!=ETIMEDOUT)
errno=-1; /* use h_errno */
printf("Unknown host '%s' for server [%!s].", HostName);
return(0);
}
}
memcpy((char*)&Result->sin_addr,(char*)Host->h_addr,sizeof(Result->sin_addr));
#endif
return Host != NULL;
}
static void LaunchThread(int (*func)(), void *parg)
{
#ifdef WINDOWS
LPDWORD tid;
CreateThread(NULL, 0L, (void *)func, parg, 0L, &tid);
#else
pthread_t pth;
pthread_create(&pth, NULL, func, parg);
#endif
} // 'LaunchThread()'
static int SockBlock(int sock, int onoff)
{
#ifdef WINDOWS
unsigned long nbParm = onoff;
return ioctlsocket(sock, FIONBIO, &nbParm);
#else
return fcntl(sock, F_SETFL, (onoff ? O_NONBLOCK : 0));
#endif
}