本文主要讲代理服务器源码,是一位叫Carl Harris大神写的,非常简单易懂,把代理服务器(Proxy Server)本质完全体现出来。相信读懂了这段代码,以后想搞定http代理等其他类型的代理服务器也能行。在附录中会贴出proxy全部源码,仅供学习使用。
一、代理服务器的定义
代理服务器(Proxy Server)是一种重要的服务器安全功能,它的工作主要在开放系统互联(OSI)模型的会话层,从而起到防火墙的作用。代理(英语:Proxy),也称网络代理,是一种特殊的网络服务,允许一个网络终端(一般为客户端)通过这个服务与另一个网络终端(一般为服务器)进行非直接的连接。一些网关、路由器等网络设备具备网络代理功能。一般认为代理服务有利于保障网络终端的隐私或安全,防止攻击。
以上截取了网上Proxy Server的定义。看起来晦涩难懂。简单来说,代理服务器就是起到一个转发功能。比如,你在本机A想要访问国外的服务器C,你本机没权限访问C,需要通过向服务器B发送数据,B再把你的数据发送给C,C返回数据也是先把数据交给了B,然后B再转交给你。这里B服务器别名为代理服务器(Proxy Server)。等会分析到proxy源码,就更加清楚了。
二、proxy源码分析
以下是proxy的主程序。
int main(int argc, char **argv)
{
int clilen;
int childpid;
int sockfd, newsockfd;
struct sockaddr_in servaddr, cliaddr;
parse_args(argc, argv);//prepare an address struct to listen for connect
bzero((char*)&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = proxy_port;
//get asocket ..
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fputs("failed to crate server socket\r\n", stderr);
exit(1);
}
// and bind our address and port to it
if(bind(sockfd, (struct sockaddr_in*)&servaddr, sizeof(servaddr)) < 0)
{
fputs("failed to bind server socket to specified/r/n", stderr);
exit(1);
}
// get ready to accept with at most 5 clients waitting to connect
listen(sockfd, 5);
// turn ourselves into daemon
daemonize(sockfd);
//fall into a loop to accept new connections and spawn children
while(1)
{
//accept the next connection
clilen = sizeof(cliaddr);
newsockfd = accept(sockfd, (struct sockaddr*)&cliaddr, &clilen);
if(newsockfd <0 && errno == EINTR)
continue;
//a signal might interrupt our accept call
else if(newsockfd < 0)
errorout("failed to accept connection");//sth quiet amiss--kill server
//fork a child to handle this connection
if((childpid = fork()) == 0)
{
close(sockfd);// inherit
do_proxy(newsockfd);
exit(0);
}
// if fork falied the connection is silently dropped --oops
close(newsockfd);
}
return 0;
}
从上面的程序看出,先是用函数parse_args(argc, argv) 解析获得用户输入的信息,包括想要绑定的代理服务器端口号、远程主机的host name 和 远程主机的服务号。获取之后设置到全局变量。接着用socket函数创建tcp 套接字,用bind函数绑定刚才获取到的端口号,用listen监听有没有客户端连接到代理服务器。然后,程序调用daemonize(sockfd)函数,这个函数的作用是创建一个守护进程。程序往下是在while循环里面调用accept函数阻塞,如果发现有客户端连接,就返回一个套接字,并fork出子进程处理这个套接字发过来的请求。do_proxy函数就是专门来干这事的。
do_proxy函数代码如下:
void do_proxy (int usersockfd)
{
int isosockfd;
fd_set rdfdset;
int connstat;
int iolen;
char buf[2048];
/* open a socket to connect to the isolated host */
if ((isosockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
errorout("failed to create socket to host");
/* attempt a connection */
connstat = connect(isosockfd,(struct sockaddr *) &hostaddr, sizeof(hostaddr));
switch (connstat)
{