/*
* Simple WEB Proxy for Linux and perhaps other systems
* by Willy Tarreau
*
* 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>
*
* Multithreaded code, POST http method, SIGPIPE, fixes, ...
* (rework)
* Pavel Krauz <kra@fsid.cvut.cz>
*
*
* 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.
*
* Copyright (C) 1996 <Willy Tarreau>
* E-mail: tarreau@aemiaif.ibp.fr
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <setjmp.h>
#include <pthread.h>
/* default listen port */
#define LISTENPORT 8080
/*
* default timeout for any read or write, in seconds
*/
#define TIMEOUT_OUT 60
/*
* 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 {
pthread_t th; /* only for server */
struct th_proxy_struct *next_free;
pthread_mutex_t mu;
pthread_cond_t cond;
int sock_in;
};
pthread_mutex_t free_q_mu;
pthread_cond_t free_q_cond;
struct th_proxy_struct *free_q;
int thread_count = 0; /* protected with free_q_mu */
pthread_key_t key_alarm;
pthread_mutex_t gethostbyname_mu; /* used for protect gethostbyname
* for gethostbyname_r it isn't needed
*/
void request_alarm(int n);
void server(int sockListen, struct sockaddr_in *inputSocket);
void *client(struct th_proxy_struct *th_proxy);
void displaysyntax(void);
char *BADREQ =
"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 =
"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 =
"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 =
"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 =
"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 =
"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";
void main(int argc, char **argv)
{
int listenport = LISTENPORT;
struct sockaddr_in ListenSocket;
int sockListen;
struct sigaction sa;
int opt, val;
while ((opt = getopt(argc, argv, "p:x:t:nm:")) != -1) {
switch (opt) {
case ':': /* missing parameter */
case '?': /* unknown option */
displaysyntax();
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 */
fprintf(stderr, "missing port for next proxy\n");
displaysyntax();
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;
} /* end of switch() */
} /* end of while() */
/* initialization of listen socket */
pthread_mutex_init(&free_q_mu, NULL);
pthread_mutex_init(&gethostbyname_mu, NULL);
pthread_cond_init(&free_q_cond, NULL);
free_q = NULL;
pthread_key_create(&key_alarm, NULL);
sa.sa_handler = request_alarm;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, NULL);
if ((sockListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
perror("Webroute(socket)");
exit(1);
}
val = 1;
if ((setsockopt(sockListen, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))) == -1) {
perror("Webroute(setsockopt)");
exit(1);
}
bzero((char *) &ListenSocket, sizeof(ListenSocket));
ListenSocket.sin_family = AF_INET;
ListenSocket.sin_addr.s_addr = htonl(INADDR_ANY);
ListenSocket.sin_port = htons(listenport);
if (bind(sockListen, (struct sockaddr *) &ListenSocket, sizeof(ListenSocket)) == -1) {
perror("Webroute(bind)");
exit(1);
}
if (listen(sockListen, 5) == -1) {
perror("Webroute(listen)");
exit(1);
}
/* the socket is ready. Let's wait for requests ... */
/* let's close stdin, stdout, stderr to prevent messages from appearing on the console */
#ifndef DEBUG
close(0)
没有合适的资源?快使用搜索试试~ 我知道了~
glibc-linuxthreads-2.0.1.tar.gz
需积分: 1 0 下载量 119 浏览量
2024-03-04
13:07:56
上传
评论
收藏 69KB GZ 举报
温馨提示
共81个文件
c:21个
h:20个
man:20个
gnu安装包
资源推荐
资源详情
资源评论
收起资源包目录
glibc-linuxthreads-2.0.1.tar.gz (81个子文件)
linuxthreads
condvar.c 6KB
Changes 2KB
cancel.c 4KB
configure 171B
ChangeLog 859B
Examples
ex5.c 2KB
ex1.c 951B
ex3.c 4KB
Makefile 198B
ex2.c 3KB
proxy.c 20KB
ex4.c 3KB
README 8KB
queue.h 2KB
join.c 4KB
man
pthread_key_create.man 3KB
pthread_detach.man 1KB
sem_init.man 4KB
Makefile 795B
troffprepro 1KB
pthread_once.man 912B
pthread_attr_init.man 7KB
pthread_exit.man 937B
pthread_cancel.man 5KB
pthread_self.man 427B
pthread_mutex_init.man 6KB
pthread_create.man 1KB
pthread_join.man 2KB
pthread_setschedparam.man 2KB
pthread_cond_init.man 7KB
pthread_cleanup_push.man 5KB
pthread_condattr_init.man 1KB
pthread_kill_other_threads_np.man 1KB
pthread_atfork.man 2KB
pthread_sigmask.man 3KB
pthread_equal.man 492B
pthread_mutexattr_init.man 3KB
pthread.c 13KB
Makefile 2KB
LICENSE 25KB
errno.c 1KB
weaks.c 4KB
attr.c 3KB
sysdeps
mips
pt-machine.h 3KB
Implies 16B
sparc
pt-machine.h 2KB
Implies 19B
m68k
pt-machine.h 2KB
Implies 16B
alpha
semaphorebits.h 1KB
pt-machine.h 3KB
unix
sysv
linux
configure 94B
posix_opt.h 3KB
local_lim.h 1KB
Implies 8B
pthread
pthread.h 17KB
stdio-lock.h 1KB
cmpxchg
semaphorebits.h 1KB
no-cmpxchg
semaphorebits.h 1KB
libc-lock.h 5KB
stub
pt-machine.h 155B
i386
i586
Implies 66B
pt-machine.h 1KB
i686
Implies 67B
i486
pt-machine.h 1KB
Implies 19B
ptfork.c 3KB
Banner 33B
semaphore.c 5KB
shlib-versions 79B
signals.c 3KB
README.Xfree3.2 9KB
lockfile.c 2KB
specific.c 3KB
manager.c 12KB
semaphore.h 1KB
wrapsyscall.c 4KB
restart.h 2KB
mutex.c 5KB
spinlock.h 1KB
internals.h 7KB
共 81 条
- 1
资源评论
程序员Chino的日记
- 粉丝: 2962
- 资源: 4万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功