proxy_web.cpp 16.1 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 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
/***************************************************************************
 *   Copyright (C) 2005 by Jeff Ferr                                       *
 *   root@sat                                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
/*
 * Simple WEB Proxy for Linux and perhaps other systems
 *
 *             This program is used to redirect HTTP requests it receives to the right HTTP server, or to
 *             another instance of itself, on an other host. It acts like a proxy and all the Web browsers
 *             that will have to use it must be setup to use it as the HTTP Proxy. It then allows several
 *             hosts on a network to access the Web via one only server, which is particularly interesting
 *             in case of a server connected to an Internet provider via a modem with PPP.
 *
 *             One interesting aspect is that it doesn't require superuser privileges to run  :-)
 *
 * Authors:    based on stuff by
 *             Willy Tarreau <tarreau@aemiaif.ibp.fr>
 *             Pavel Krauz <kra@fsid.cvut.cz>
 * 
 *             Multithreaded code, POST http method, SIGPIPE, fixes, (rework)
 * 
 * Todo:       - make a list of hosts and network which can be accessed directly, and those which need another proxy
 *             - add an option to supply an access log with hostnames and requests
 */

#include "jserversocket.h"
#include "jsocket.h"
#include "jsocketexception.h"
#include "jthreadlib.h"
#include "jthread.h"
#include "jsemaphoretimeoutexception.h"
#include "jautolock.h"

#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

#define METHOD_GET			1
#define METHOD_POST			2
#define METHOD_HEAD			3

#define ERR_SOCKET			-5
#define ERR_GETHOSTBYNAME	-6
#define ERR_CONNECT			-7
#define ERR_CONNECT_TIMEOUT	-8

// default listen port
#define LISTENPORT			8080

// default timeout for any read or write, in seconds 
#define TIMEOUT_OUT			6

// exit timeout for idle thread
#define TIMEOUT_THREAD_EXIT	15

// length of data buffer in chars 
#define LDATA				1024

// length of remote server address 
#define LADR				128

// default port to connect to if unspecified 
#define DEFAULTPORT			80

// max proxy threads for requests
#if 1
#define MAX_PROXY_THREADS	64
#else
#define MAX_PROXY_THREADS	4
#endif

int ConnectToProxy = 0;		// 1 here means this program will connect to another instance of it 0 means we'll connect directly to the Internet 
char NextProxyAdr[128];		// the name of the host where the next instance of the program runs 
int NextProxyPort;			// and its port 
int NoCache = 0;			// if not 0, prevents web browsers from retrieving pages in their own cache when the users does a "Reload" action 
int timeout_out = TIMEOUT_OUT;
int max_proxy_threads = MAX_PROXY_THREADS;

struct th_proxy_struct {
	struct th_proxy_struct *next_free;
	jthread::Mutex mu;
	jthread::Condition cond;
	jsocket::Socket *sock_in;
};

// global variables
jthread::Mutex free_q_mu;
jthread::Condition free_q_cond;
struct th_proxy_struct *free_q;
int thread_count = 0;		// protected with free_q_mu 

// functions prototype
void help();
struct th_proxy_struct *new_proxy_th(void);
struct th_proxy_struct *alloc_proxy_th(void);
void server(int port);
void sayerror(char *msg, jsocket::Socket *sockIn, jsocket::Socket *sockOut);
int process_request(jsocket::Socket *sockIn);

// global constants
char *BADREQ = (char *)
	"HTTP/1.0 400 ERROR\r\n"
	"Server: thproxyd\r\n"
	"Content-type: text/html\r\n"
	"\r\n"
	"<HEAD><TITLE>400 Bad Request</TITLE></HEAD>\n"
	"<BODY><H1>400 Bad Request</H1>\n"
	"Your client sent a query that this server could not\n"
	"understand.<P>\n"
	"Reason: Invalid or unsupported method.<P>\n"
	"</BODY>\n";

char *SERVERR = (char *)
	"HTTP/1.0 500 ERROR\r\n"
	"Server: thproxyd\r\n"
	"Content-type: text/html\r\n"
	"\r\n"
	"<HEAD><TITLE>500 Server Error</TITLE></HEAD>\n"
	"<BODY><H1>500 Server Error</H1>\n"
	"Internal proxy error while processing your query.<P>\n"
	"Reason: Internal proxy error.<P>\n"
	"</BODY>\n";

char *SERVCOERR = (char *)
	"HTTP/1.0 500 ERROR\r\n"
	"Server: thproxyd\r\n"
	"Content-type: text/html\r\n"
	"\r\n"
	"<HEAD><TITLE>500 Server Error</TITLE></HEAD>\n"
	"<BODY><H1>500 Server Error</H1>\n"
	"Internal proxy error while processing your query.<P>\n"
	"Reason: Invalid connection.<P>\n"
	"</BODY>\n";

char *SERVDNSERR = (char *)
	"HTTP/1.0 500 ERROR\r\n"
	"Server: thproxyd\r\n"
	"Content-type: text/html\r\n"
	"\r\n"
	"<HEAD><TITLE>500 Server Error</TITLE></HEAD>\n"
	"<BODY><H1>500 Server Error</H1>\n"
	"Internal proxy error while processing your query.<P>\n"
	"Reason: Bad address - DNS cann't resolve address.<P>\n"
	"</BODY>\n";

char *SERVTIMEOUT = (char *)
	"HTTP/1.0 500 ERROR\r\n"
	"Server: thproxyd\r\n"
	"Content-type: text/html\r\n"
	"\r\n"
	"<HEAD><TITLE>500 Server Error</TITLE></HEAD>\n"
	"<BODY><H1>500 Server Error</H1>\n"
	"Internal proxy error while processing your query.<P>\n"
	"Reason: Server time out while connection establishment or data transfer.<P>\n"
	"</BODY>\n";

char *POSTERR = (char *)
	"HTTP/1.0 500 ERROR\r\n"
	"Server: thproxyd\r\n"
	"Content-type: text/html\r\n"
	"\r\n"
	"<HEAD><TITLE>500 Proxy Server Error</TITLE></HEAD>\n"
	"<BODY><H1>500 Proxy Server Error</H1>\n"
	"Failed to POST.<P>\n"
	"Reason: post method error ???.<P>\n"
	"</BODY>\n";

/* displays the right syntax to call Webroute */
void help()
{
	std::cout << "Syntax:" << std::endl;
	std::cout << "webroute [ -p port ] [ -x h:p ] [ -t timeout ] [ -m max_threads ] [ -n ]" << std::endl;
	std::cout << "Available options are:" << std::endl;
	std::cout << "  -p allows you to run webroute on the port <port>." << std::endl;
	std::cout << "     If you don't have superuser privileges, you must use a port > 5000." << std::endl;
	std::cout << "     The default port is " << LISTENPORT << std::endl;
	std::cout << "  -x enables multi-proxy feature. This means that this instance of Webroute" << std::endl;
	std::cout << "     doesn't have itself access to the internet, but the one which is running" << std::endl;
	std::cout << "     on port <p> of host <h> can provide an access. It's possible to chain" << std::endl;
	std::cout << "     as many instances of Webroute as you want. That depends of your network topology" << std::endl;
	std::cout << "  -t <timeout> specifies how many seconds the connection will stay connected (or trying to connect) when" << std::endl;
	std::cout << "     no data arrives. After this time, the connection will be canceled." << std::endl;
	std::cout << "  -n prevents browsers from retrieving web pages from their own cache when the" << std::endl;
	std::cout << "     user asks for a \"Reload\". The page will then always be reloaded." << std::endl;
	std::cout << "  -m max count of proxy threads allocated to serve the requests" << std::endl;
}

class Client : public jthread::Thread{

	private:
		struct th_proxy_struct *th_proxy;

	public:
		Client(struct th_proxy_struct *th)
		{
			th_proxy = th;
		}

		virtual ~Client()
		{
		}

		virtual void Run()
		{
			struct timespec ts;
			struct timeval tv;
			struct th_proxy_struct **th;

			signal(SIGPIPE, SIG_IGN);
			for (;;) {
				th_proxy->mu.Lock();

				while (th_proxy->sock_in == NULL) {
					gettimeofday(&tv, NULL);
					ts.tv_sec = tv.tv_sec + TIMEOUT_THREAD_EXIT;
					ts.tv_nsec = 0;
					
					try {
						th_proxy->cond.Wait((long long)ts.tv_sec*1000000LL + (long long)ts.tv_nsec/1000LL, &th_proxy->mu);
					} catch (jthread::SemaphoreTimeoutException &e) {
						free_q_mu.Lock();
						th = &free_q;
						while (*th && *th != th_proxy)
							th = &((*th)->next_free);
						if (*th == th_proxy) {
							// remove yourself from queue and exit
							*th = th_proxy->next_free;
							thread_count--;
							free_q_mu.Unlock();
							free(th_proxy);
							goto run_exit;
						}
						free_q_mu.Unlock();
					}
				}

				th_proxy->mu.Unlock();

				process_request(th_proxy->sock_in);

				th_proxy->mu.Lock();
				th_proxy->sock_in = NULL;
				th_proxy->mu.Unlock();

				free_q_mu.Lock();
				th_proxy->next_free = free_q;
				free_q = th_proxy;
				free_q_mu.Unlock();
				free_q_cond.Notify();
			}

			run_exit:
				return;
		}
};

struct th_proxy_struct *new_proxy_th(void)
{
	struct th_proxy_struct *th_proxy = NULL;

	try {
		th_proxy = new struct th_proxy_struct;

		memset(th_proxy, 0, sizeof(struct th_proxy_struct));

		th_proxy->next_free = NULL;
		th_proxy->sock_in = NULL;

		Client *client = new Client(th_proxy);

		client->Start();
	} catch (std::bad_alloc &e) {
		if ((void *)th_proxy != NULL) {
			delete th_proxy;
		}

		return NULL;
	}

	return th_proxy;
}

struct th_proxy_struct *alloc_proxy_th(void)
{
	struct th_proxy_struct *th_proxy;

	jthread::AutoLock l(&free_q_mu);

	do {
		th_proxy = free_q;

		if (free_q) {
			free_q = free_q->next_free;
		} else {
			if (thread_count < max_proxy_threads) {
				if ((th_proxy = new_proxy_th())) {
					thread_count++;
				}
			}
			if (!th_proxy) {
				free_q_cond.Wait(&free_q_mu);
			}
		}
	} while (!th_proxy);

	return th_proxy;
}

void server(int port)
{
	struct th_proxy_struct *th_proxy;

	jsocket::ServerSocket server(port);
	jsocket::Socket *s;

	for (;;) {
		try {
			s = server.Accept();
		
			th_proxy = alloc_proxy_th();

			th_proxy->mu.Lock();
			th_proxy->sock_in = s;
			th_proxy->mu.Unlock();

			th_proxy->cond.Notify();
		} catch (jsocket::SocketException &e) {
			perror("Broken connection");
		}
	}
}

/* this function should be called only by the child */
void sayerror(char *msg, jsocket::Socket *sockIn, jsocket::Socket *sockOut)
{
	sockIn->Send(msg, strlen(msg));
	
	jsocket::SocketOptions *o = sockIn->GetSocketOptions();

	o->SetLinger(1, 4);

	delete o;

	o = sockOut->GetSocketOptions();

	o->SetLinger(1, 1);

	delete o;

	sockOut->Close();
	sockIn->Close();
}

int process_request(jsocket::Socket *sockIn)
{
	char data[LDATA];
	char adr[LADR], *p;
	int ldata, lreq, port, req_len, req_method;
	jsocket::Socket *sockOut = NULL;
	FILE *fsin;
	
	jsocket::SocketOptions *o = sockIn->GetSocketOptions();

	o->SetReuseAddress(true);
	o->SetKeepAlive(true);

	delete o;

	if ((fsin = fdopen(sockIn->GetHandler(), "rw")) == NULL) {
		goto serverr;
	}

	// here, we'll analyze the request and get rid of "http://adr:port". The address and port willbe duplicated and used to open the connection
	if (fgets(data, LDATA, fsin) == NULL) {
		goto badreq;
	}

	// it's easy to log all requests here  fprintf(stderr,"requete recue: %s",data); 
	ldata = strlen(data);
	
	if (strncmp(data, "GET ", 4) == 0) {
		req_len = 4;
		req_method = METHOD_GET;
	} else if (strncmp(data, "POST ", 5) == 0) {
		req_len = 5;
		req_method = METHOD_POST;
	} else if (strncmp(data, "HEAD ", 5) == 0) {
		req_len = 5;
		req_method = METHOD_HEAD;
	} else
		goto badreq;

	if (!ConnectToProxy) {	/* if proxy-to-proxy connection, we don't modify the request */
		char *str;

		str = data + req_len;
		while (*str == ' ')
			str++;
		if (!strncmp(str, "http://", 7))
			str += 7;
		if ((p = strchr(str, '/')) != NULL) {
			strncpy(adr, str, (p - str));	/* copies addresse in adr */
			adr[p - str] = 0;
			str = p;	/* points to the rest of the request (without address) */
			lreq = ldata - (str - data);
		} else
			goto badreq;	/* if no /, error */
		/* at this stage, adr contains addr[:port], and str points to the local URL with the first  '/' */
		if (adr[0] == 0)
			goto badreq;
		p = strchr(adr, ':');
		if (p == NULL)	/* unspecified port. The default one will be used */
			port = DEFAULTPORT;
		else {		/* port is available. let's read it */
			*(p++) = 0;	/* ends hostname */
			port = atoi(p);
		}
		/* end of request analysis. The hostname is in "adr", and the port in "port" */
		try {
			sockOut = new jsocket::Socket(adr, port);
		} catch (jsocket::SocketException &e) {
			switch (errno) {
				case ERR_GETHOSTBYNAME:
					goto servdnserr;
			}
			goto servcoerr;
		}
		/* As it becomes a local URL, we only say "GET" and the end of the request. */
		switch (req_method) {
		case METHOD_GET:
			sockOut->Send("GET ", 4);
			break;
		case METHOD_POST:
			sockOut->Send("POST ", 5);
			break;
		case METHOD_HEAD:
			sockOut->Send("HEAD ", 5);
			break;
		}
		sockOut->Send(str, lreq);
	} else {		/* proxy-to-proxy connection ! */
		try {
			sockOut = new jsocket::Socket(NextProxyAdr, NextProxyPort);
		} catch (jsocket::SocketException &e) {
			switch (errno) {
				case ERR_GETHOSTBYNAME:
					goto servdnserr;
			}
			goto servcoerr;
		}
	}
	/* now, let's copy all what we don't have copied yet */
	if (req_method == METHOD_POST) {
		int c_len = 0;
		char *p;

		do {
			fgets(data, LDATA, fsin);
			ldata = strlen(data);
			if (strncasecmp(data, "Content-Length", 14) == 0) {
				p = data + 14;
				while (*p != ':') {
					p++;
				}
				c_len = atoi(++p);
			}
			sockOut->Send(data, ldata);
		} while (ldata && data[0] != '\n' && data[0] != '\r');
		if (c_len == 0) {
			goto posterr;
		}
		while (c_len) {
			ldata = fread(data, 1, (LDATA > c_len ? c_len : LDATA), fsin);
			sockOut->Send(data, ldata);
			c_len -= ldata;
		}
	} else { /*
		  * METHOD_GET, METHOD_HEAD
		  */
		do {
			fgets(data, LDATA, fsin);
			ldata = strlen(data);
			if (!NoCache || (strncmp(data, "If-Mod", 6))) {
				sockOut->Send(data, ldata);
			}
		} while (ldata && data[0] != '\n' && data[0] != '\r');
	}
	/* retrieve data from server */
	do {
		int err;
		do {
			ldata = sockOut->Receive(data, LDATA);
		} while (ldata == -1 && errno == EINTR);	/* retry on interrupt */
		if (ldata < 0)
			goto serverr;
		if (ldata) {	/* if ldata > 0, it's not the end yet */
			do {
				err = sockIn->Send(data, ldata);
			} while (err == -1 && errno == EINTR);
			if (errno == EPIPE) {	/* other end (client) closed the conection */
				goto end;
			}
			if (err == -1)
				goto serverr;
		}
	} while (ldata > 0);	/* loops while more data available */

	end:
	sockIn->Close();
	sockOut->Close();
	return 0;	
    
	badreq:
	sayerror(BADREQ, sockIn, sockOut);
	return -1;
    
	serverr:
	sayerror(SERVERR, sockIn, sockOut);
	return -2;
    
	servcoerr:
	sayerror(SERVCOERR, sockIn, sockOut);
	return -4;
    
	servdnserr:
	sayerror(SERVDNSERR, sockIn, sockOut);
	return -5;
    
	posterr:
	sayerror(POSTERR, sockIn, sockOut);
	return -6;
}

int main(int argc, char **argv)
{
	int listenport = LISTENPORT;
	int opt;

	while ((opt = getopt(argc, argv, "p:x:t:nm:")) != -1) {
		switch (opt) {
		case ':':	/* missing parameter */
		case '?':	/* unknown option */
			help();
			exit(1);
		case 'p':	/* port */
			listenport = atoi(optarg);
			break;
		case 'x':	/* external proxy */
			char *p;
			p = strchr(optarg, ':');
			if (p == NULL) {	/* unspecified port number. let's quit */
				std::cout << "missing port for next proxy" << std::endl;
				help();
				exit(1);
			}
			*(p++) = 0;	/* ends hostname */
			NextProxyPort = atoi(p);
			strcpy(NextProxyAdr, optarg);
			ConnectToProxy = 1;
			break;
		case 't':	/* disconnect time-out */
			timeout_out = atoi(optarg);
			break;
		case 'n':	/* no cache */
			NoCache = 1;
			break;
		case 'm':
			max_proxy_threads = atoi(optarg);
			break;
		}
	}

	free_q = NULL;

	server(listenport);

	return 0;
}