#include "Socks5Server.h" using namespace std; // simulate socks5-server // usage: // 1. "g++ -c Socks5Server.cpp" // 2. "g++ -c main.cpp" // 3. "g++ -o sermax main.o Socks5Server.o -lpthread" // 4. "./sermax" // 5. test: // "curl -v --socks5 127.0.0.1:9011 https://www.baidu.com" => ok // make firefox browser's port 9011 => ok // local port listenner thread void* thread_fun1(void* val); // remote server listenner thread void* thread_fun2(void* val); // listen to the local port void Socks5Server::listenLocal(){ this->sockfd=socket(AF_INET, SOCK_STREAM, 0); /* bind the local address, so that the cliend can send to server */ bzero((char *)&(this->serv_addr), sizeof(this->serv_addr)); (this->serv_addr).sin_family = AF_INET; (this->serv_addr).sin_addr.s_addr = htonl(INADDR_ANY); (this->serv_addr).sin_port = htons(this->port); // bind while( bind(this->sockfd, (struct sockaddr *) &(this->serv_addr), sizeof(this->serv_addr)) < 0){ sleep(1) ; cout << "bind fail" << endl ; }; listen(this->sockfd,10); }; // waiting for the local port's connection int Socks5Server::acceptLocal(){ int newsockfd; struct sockaddr_in cli_addr; int cli_len=sizeof(cli_addr); cout<<"Waitting connection from localhost..."<socks_vision) { cout<<"socks version is wrong"<socks_vision; msg2[1] = this->ui0; if (send(newsockfd, msg2, 2, 0) == -1) { cout<<"cannot send socks-methond-reply"< Socks5Server::connShake(int val){ // right flag, the loop is going on when th flag==0 int flag=0; // newsockfd int newsockfd = val; //Get the first four bytes of information // such as the address type of URL request // from the client char buf4[4]; if (recv(newsockfd, buf4, 4, 0) == -1) { cout<<"cannot get version-cmd-_-addressType"< pairval){ int newsockfd = pairval.first; int remote_sockfd = pairval.second; printf("Data transmission is beginning.\n"); struct mypara { int para1;//参数1 int para2;//参数2 }; struct mypara pstru={newsockfd,remote_sockfd}; // listen to the local port pthread_t tid1 ; int ret1 = pthread_create(&tid1,NULL,thread_fun1,&(pstru)); cout<<"thread return1: "<acceptLocal(); if(this->negotShake(newsockfd)>0){ cout<<"negotShake error"< pairval=connShake(newsockfd); if(pairval.second>0){ cout<<"connShake error"<transData(make_pair(newsockfd,remote_sockfd)); } }; // server start void Socks5Server::start(){ this->listenLocal(); this->forever(); close(this->sockfd); }; // transfer data form local port to remote server void * thread_fun1(void* val){ struct mypara { int para1;//参数1 int para2;//参数2 }; mypara *pstru; pstru = (struct mypara *) val; // pair pairval=*((pair*)val); int local_sockfd = pstru->para1; int remote_sockfd= pstru->para2; char data[MAX_SIZE]; int datalen; int send_len = 0 ; while(true){ datalen=recv(local_sockfd, data, sizeof(data), 0); cout << "recv from local_sock datalen = " << datalen << endl ; if (datalen<= 0) { break ; }else{ // send to remote server send_len = send(remote_sockfd, data, datalen, 0) ; cout << "send to remote_sockfd len = " << send_len << endl ; } } close(local_sockfd) ; close(remote_sockfd) ; return NULL; } // transfer data form remote server to local port void * thread_fun2(void* val){ struct mypara { int para1;//参数1 int para2;//参数2 }; mypara *pstru; pstru = (struct mypara *) val; // pair pairval=*((pair*)val); int local_sockfd = pstru->para1; int remote_sockfd= pstru->para2; char data[MAX_SIZE]; int datalen; int send_len = 0 ; while(true){ datalen=recv(remote_sockfd, data, sizeof(data), 0); cout << "recv from remote_sock datalen = " << datalen << endl ; if (datalen <= 0) { //nothing break; }else{ // send to localhost send_len = send(local_sockfd, data, datalen, 0) ; cout << "send to local_sockfd len = " << send_len << endl ; } } close(local_sockfd) ; close(remote_sockfd) ; return NULL; }