下载  >  开发技术  >  C  > C语言实现的socket5代理

C语言实现的socket5代理 评分:

C语言实现的socket5代理
// Socks5程序,只支持TCP代理 //版本0.1,作者云舒 //2007年1月9日凌晨1点15分,GF回家已经11天了 2008年1月25日修改,今年GF一直在我身边,哈哈 / //参考: //http://www.rfc-editor.org/rfc/rfc1928.txt //hllp://www.rlc-ediior.org/rfc/rfc1929.lxl //编译: / gcc -o socks5-02 Socks5 c -lpthread( RedHat AS5 i]ir) #include <stdio. h> #include <netinet/in. h> #include < netdb h1> #include < sys/ time. h> #include <sys/ types. h) #include unistd. h> #include stdlib. h> #include signal. h> #include pthread. h> #include errno. h> #include <string. h> #include <sys/socket. h> #include arpa/inet. h> #include socks5. h #define mAX user 10 #define BUFF size 1024 #define auth code oxo2 #define time out 600000o #define user name yunshu #define pass Word phantom / Select auth method, return o if success, -1 if failed int SelectMethod( int sock char recy buffer [BUFF SIZE]=(0) char reply buffer[2]=[0 j METHOD SELECT REQUEST method request METHOD SELECT RESPONSE **me Lhod response / recV METHOD SELECT REQUEST int ret= recv( sock, recy buffer, BUFF SIZE, 0) if( ret <=0) perror(recv error close( sock return -1 //printf( SelectMethod: recv %d bytes\n, ret / if client request a wrong version or a wrong number method method request =(METHOD SELECT REQUEST *recv buffer method response=(METHOD SELECT RESPONSE *)reply buffer me thod response->version= VERSION / if not socks5 if((int)method request->version ! VERSION method response->select me thod= Oxff send( sock, method response, sizeof (METHOD SELECT RESPONSE),0) lose( sock return -1 method response->select method- AUTH CODE if(-1= send( sock, me thod response, sizeof (METHOD SELECT RESPONSE) 0)) close( sock return -1 return o / test password, return 0 for success int AuthPassword( int sock char recy buffer LBUFF SIZe]=0j char reply buffer LBUFF SIZE]=[0 AUTH REQUEST auth request AUTH RESPONSE *auth response / auth username and password int ret= recy( sock, reel buffer, BUFF SIZE, 0) if( ret<=0) perror( recv username and password error") close( sock return -1 //printf(AuthPass: recy %d bytes\n", ret auth request=(AUTH REQUEST recy buffer memset( reply buffer, 0, BUFF SIZE auth response=(AUTH RESPONSE *reply buffer auth response->version=0x01 char recv name[256 0} char recv pass256」= {0} / auth request->name len is a char, max number is oxff char pwd str[2]=t 0 j strncpy( pwd str, auth request->name +auth request->name len, 1) int pwd len =(int)pwd strLOJ strncpy( recv name, auth request->name, auth request->name len strncpy( recy pass, auth request->name auth request->name len sizeof(auth request->pwd len), pwd len //printf( username: %s\password: %s\n", recv name, recv pass / check username and password if( (strncmp( recv name, USER NAME, strlen (USer NAMe))==0)&& (strncmp( recy pass, PASS WORD, strlen(PASS WORD))==0) auth response->result=0x00 if(-1== send( sock, auth response, sizeof (AUTH rESPONSE),0)) close( sock return -1 else return 0: else auth response->result=0xol send( sock, auth response, sizeof(AUTH RESPONSE),0 close( sock return -1 / parse command, and try to connect real server / return socket for success,-1 for failed int ParseCommand( int sock char recy buffer LBUFF SIZE]=i0 j char reply buffer LBUFF SIZE]=0 SOCKS5 REQUEST ksocks5 request SOCKS5 RESPONSE *socks5 response / recv command int ret recy( sock, recy buffer, BUFF SIZE, 0 f( ret < =0) perror( recv connect command error lose( sock return-I socks5 request =(SOCKS5 REQUEST *recy buff if((socks5 request->version ! -VERSION)(socks5 request->cmd CONNECT) (socks5 request->address type=- IPV6 //printf("connect command error. \n") close( sock return -1 / begain process connect request struct sockaddr in sin memset( (void *)&sin,0, sizeof (struct sockaddr in)) sinsin fammily= AF INET / get real server&#39; s ip address if( socks5 request->address type ==IPv4) memepy( &sin sin addr.s addr, &socks5 reques t->address type+ sizeof (socks5 request->address type), 4) memcpy(&sin sin port, &socks5 request->address type sizeof (socks5 request->address type)+4, 2 //printr( Real Server: %S %d\n, inet nloa( sin sin addr ntohs( sin sin port ) else if( socks5 request->address type= DOMAIn char domain length *k &socks5 request->address type sizeof (socks5 request->address type)) char target domain[ 256=(0 j strncpy( target domain, &socks5 request->address type 2,(unsigned int)domain length //printf(target: %s\n", target domain struct hostent *phost= gethostbyname( target domain f( host = NULL //printf(Resolve %s error!\n", target domain close( sock return -I memcpy(&sin sin addr, phost->h addr list[o, phost->h length memcpy(&sin sin port, &socks5 request->address type sizeof (socks5 request->address type)+ sizeor(domain length)+ domain length, 2) / try to connect to real server int real server sock socket( AF INET, SOCK STREAM, 0) if( real server sock<0) perror(Socket creation failed\n") close( sock return -1 memset( reply buffer, 0, sizeof(BUFF SIZE)) socks5 response=(SOCKS5 RESPONSE *reply buffer socks5 response->version= VERSION socks5 response->reserved=0x00 socks5 response->address type =0x01 memset( socks5 response +4, 0, 6) ret= connect( real server sock, (struct sockaddr *)&sin sizeof (struct sockaddr in) if( ret ==0) socks5 response->reply-0X00 if(-1 == send( sock, socks5 response, 10,0)) close( sock return -1 else perror( connect to real server error socks5 response->reply=0x01 send( sock, socks5 response, 10,0 close( Soc return -1 return real server sock int ForwardData( int sock, int real server sock char recy bufTer [BUFF SIZE=0) fd set fd read struct timeval line out Lime out. ty sec =0 time out. ty usec time out int ret=0 while(1 FD ZERo &fd read FD SET( Sock, &ld read FD SET( real server sock, &fd read ret= select((sock >real server sock sock: real server sock)+1, &fd read, NULL, nULL, &time out f( ret perror( select socket error break else if(0== ret //perror(" select time out") continue; //printf("[debug] testing readable! \n") if( FD ISSET(sock, &fd read)) //printf("client can read! \n") memset( recy buffer, 0, BUFF SIZE ret- recv( sock, recy buffer, BUFF SIZE, 0 if( ret >0) //printf( %s", recy buffer printf o "recv %d bytes from client.\n, et //1 ret= send( real server sock, recy buffer, ret, 0) if( ret perror( send data to real server error") break //printf(send %d bytes to client!\", ret else if( ret ==0) //printr( client close sockeL. \n") break e⊥Se perror( recv from client error break else if( FD ISSET (real server sock, &fd read)) //printf( real server can read! \n") memset( recy buffer, 0, BUFF SIZE ret= recv( real server sock, recy buffer, BUFF SIZE, 0) if( ret >0 //printf(%s", recv buffer //printf("recv %d bytes from real server. \n", ret ret= send( sock, recy buffer, ret, 0) if( ret ==-1) perror( send data to client error") break else if( ret--0) //printf("real server close socket. \ n") break else

...展开详情
2015-11-03 上传 大小:81KB
举报 收藏 (3)
分享

评论 下载该资源后可以进行评论 共1条

qq_34520141 这怎么用啊
2019-02-22
回复