📄 mdesproxy.c
字号:
/** 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 VERSION#define CONTLEN "Content-Length: "int alivetime;int mintime;int request_socket;int connection_status[MAX_CONNECTIONS];char *remote_port;void process_connection_request(struct pb *,int );char hostname[256][50];int first;struct pb pb;void sig_child(int );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; 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; }}int readcfg(char *file,const char *key,char *value){ FILE *fp; fp=fopen(file,"r"); char buf[1024]; char *pe; while(fgets(buf,sizeof(buf),fp)!=NULL){ pe=strchr(buf,'='); if(pe==NULL) continue; *pe++='\0'; if(pe[strlen(pe)-1]=='\n') pe[strlen(pe)-1]='\0'; if(strcmp(key,buf)==0){ strcpy(value,pe); return 0; } } return -1;}intmain (int argc, char **argv){ int connection, nfound; struct timeval timeout; char value[20]; if(readcfg("desproxy.conf","ALIVETIME",value)==0){ alivetime=atoi(value); printf("ALIVETIME=%d\n",alivetime); }else alivetime=8; if(readcfg("desproxy.conf","MINTIME",value)==0){ mintime=atoi(value); }else mintime=3; signal (SIGPIPE, SIG_IGN); signal(SIGCHLD,sig_child); 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) { 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)) { int pid=fork(); if(pid<0){ printf("fork err\n"); exit(-1); }else if(pid==0){ process_connection_request (&pb,1); /*child*/ printf("----------------------------------\n"); printf("fork a child\n"); close(request_socket); child_server(&pb); }else{ 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; struct timeval tv; tv.tv_sec=alivetime; tv.tv_usec=0; int n; char *p,*pe; int alllen=0; char buflen[10]; while(1){ rset=allset; n=select(maxfd,&rset,NULL,NULL,&tv); if(n<=0){ printf("select time out!client exit %d\n",getpid()); exit(-1); } if(FD_ISSET(prob->sockcli,&rset)){ tv.tv_sec=alivetime; len=read(prob->sockcli,buf,sizeof(buf)); if(len<0){ printf("read client err\n"); printf("client exit %d\n",getpid()); exit(-1); }else if(len==0){ printf("client close the socket\n"); printf("client exit %d\n",getpid()); 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"); printf("client exit %d\n",getpid()); exit(-1); } } if(FD_ISSET(prob->sockproxy,&rset)){ tv.tv_sec=mintime; len=read(prob->sockproxy,buf,sizeof(buf)); if(len<=0){ printf("read proxy err\n"); printf("client exit %d\n",getpid()); exit(-1); } /* p=strstr(buf,CONTLEN); if(p!=NULL){ p+=strlen(CONTLEN); pe=buflen; while(*p>'1'&&*p<'0') *pe++=*p++; *pe='\0'; alllen=atoi(buflen); }*/ if(write(prob->sockcli,buf,len)<=0){ printf("write cli err\n"); printf("client exit %d\n",getpid()); exit(-1); } /*alllen-=len; if(alllen<=0){ printf("Alllen is zero!\nexit\n"); alivetime=3; }*/ } }}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'; } }}void sig_child(int sig){ int st; printf("child exit\n"); wait(&st);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -