📄 mdesproxy.c.bak
字号:
/** desproxy.c: Main program file for desproxy** Copyright (C) 2003 Miguelanxo Otero Salgueiro** This program is free software; you can redistribute it and/or modify* it under the tems of the GNU General Public License version 2 as* published by the Free Software Foundation.**/#include "mdesproxy.h"#define PROGRAM_NAME "desproxy"#define PROGRAM_VERSION VERSIONint request_socket;int connection_status[MAX_CONNECTIONS];char *remote_port;void process_connection_request(struct pb *,int );char hostname[256][50];int first;#define PBSIZE 30struct pb pb[PBSIZE];int pbindex;int read_request(struct pb *prob){ prob->len=read(prob->sockcli,prob->buffer,sizeof(prob->buffer)); if(prob->len<=0){ printf("readd from client err\n"); return -1; } return 0;}int client_to_proxy (struct pb *prob){ int count; if ((count = write (prob->sockproxy,prob->buffer, prob->len)) == -1) { perror ("write"); return -1; } return 0;}int proxy_to_client (struct pb *prob){ int count; if ((count = read (prob->sockproxy,prob->buffer, sizeof (prob->buffer))) == -1) { perror ("read"); return -1; } else { if ((count = write (prob->sockcli, prob->buffer, count)) == -1) { perror ("write"); return -1; } } return 0;}voidparse_command_line (int argc, char **argv){ debug_printf (">parse_command_line(COMMAND_LINE)\n"); if (argc != 5) { printf (gettext ("Usage: desproxy remote_port proxy_host" " proxy_port local_port\n\n")); exit (1); } remote_port = argv[1]; proxy_host = argv[2]; proxy_port = argv[3]; local_port = argv[4]; debug_printf ("parse_command_line>\n");}voidprocess_connection_request(struct pb *prob,int flag){ prob->sockcli=accept (request_socket, (struct sockaddr *) &client, &client_length); if (prob->sockcli < 0) { perror ("accept"); return; } printf("%s %d\n",inet_ntoa (client.sin_addr), ntohs (client.sin_port)); /*******************************/ /*add by dp*/ if(flag){ read_request(prob); char myhost[1024]; char *buf=prob->buffer; printf("read from client=%s\n",prob->buffer); char *myp=strstr(buf,"GET http://"); char *mypend; char mb[1024]; if(myp!=NULL){ myp+=strlen("GET http://"); mypend=strchr(myp,'/'); if(mypend!=NULL){ strncpy(mb,myp,mypend-myp); mb[mypend-myp]='\0'; printf("mb=%s\n",mb); strcpy(prob->host,mb); } } } /**************/ if (connect_host_to_proxy (prob, remote_port) != 0) { printf ("connect_host_to_proxy: ERROR\n"); return; }}intmain (int argc, char **argv){ int connection, nfound; struct timeval timeout; signal (SIGPIPE, SIG_IGN); initialize_gettext (); print_program_version (PROGRAM_NAME, PROGRAM_VERSION); parse_command_line (argc, argv); request_socket = listen_in_TCP_port (atoi (local_port)); printf (gettext ("Press <Control+C> to Quit\n\n")); mark_all_client_sockets_as_free (); FD_ZERO (&mask); FD_SET (request_socket, &mask); maxfd = request_socket; while (1) { memset(pb,0,sizeof(struct pb)*PBSIZE); rmask = mask; timeout.tv_sec = 5; timeout.tv_usec = 0; nfound = select (maxfd + 1, &rmask, NULL, NULL, &timeout); debug_printf ("%d", nfound); if (nfound < 0) { if (errno == EINTR) { printf (gettext ("Interrupted by system call\n")); continue; } perror ("select"); exit (1); } if (FD_ISSET (request_socket, &rmask)) { process_connection_request (&pb[pbindex],1); int pid=fork(); if(pid<0){ printf("fork err\n"); continue; }else if(pid==0){ /*child*/ printf("----------------------------------\n"); printf("fork a child\n"); close(request_socket); child_server(&pb[pbindex]); pbindex++; if(pbindex>=PBSIZE) pbindex=0; }else{ close(pb[pbindex].sockcli); close(pb[pbindex].sockproxy); memset(&pb[pbindex],0,sizeof(struct pb)); pbindex++; if(pbindex>=PBSIZE) pbindex=0; continue; } } fflush (stdout); }}int child_server(struct pb *prob){ if(first==0){ write(prob->sockproxy,prob->buffer,prob->len); } first++; fd_set rset,allset; FD_ZERO(&allset); FD_SET(prob->sockcli,&allset); FD_SET(prob->sockproxy,&allset); char buf[4096]; char host[200]; int len; int maxfd=prob->sockcli>prob->sockproxy?prob->sockcli+1:prob->sockproxy+1; while(1){ rset=allset; select(maxfd,&rset,NULL,NULL,NULL); if(FD_ISSET(prob->sockcli,&rset)){ len=read(prob->sockcli,buf,sizeof(buf)-1); if(len<0){ printf("read client err\n"); close(prob->sockcli); close(prob->sockproxy); exit(-1); }else if(len==0){ printf("client close the socket\n"); exit(-1); } /* get_host(buf,host); if(host[0]!='\0'&&strcmp(host,prob->host)!=0){ strcpy(prob->host,host); close(prob->sockcli); close(prob->sockproxy); process_connection_request(prob,0); }*/ if(write(prob->sockproxy,buf,len)<0){ printf("write proxy err\n"); exit(-1); } } if(FD_ISSET(prob->sockproxy,&rset)){ len=read(prob->sockproxy,buf,sizeof(buf)-1); if(len<=0){ printf("read proxy err\n"); exit(-1); } if(write(prob->sockcli,buf,len)<0){ printf("write cli err\n"); exit(-1); } } }}get_host(const char *buf,char *host){ char *myp,*mypend; myp=strstr(buffer,"GET http://"); if(myp!=NULL){ myp+strlen("GET http://"); mypend=strchr(myp,'/'); if(mypend!=NULL){ strncpy(host,myp,mypend-myp); host[mypend-myp]='\0'; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -