// simulate socks5-server. coding by wefree in 2019.12 #include "Socks5Server.h" using namespace std; // listen to the local port void Socks5Server::ndn_listen_local(string prefix){ mndn_socket.listen(prefix.data()) ; cout << "listen on : " << prefix << endl ; } // waiting for the local port's connection string Socks5Server::ndn_accept_local(string new_prefix) { char buff[1000] ; memset(buff,0,1000) ; cout<<"Waitting connection [socks5-server] ..."<socks_vision) { cout<<"socks version is wrong"<socks_vision; msg2[1] = this->ui0; if (ndn_socket.write(msg2,2) < 0) { cout<<"cannot send socks-methond-reply"< Socks5Server::ndn_connShake(Ndn_socket &ndn_socket){ // right flag, the loop is going on when th flag==0 int flag=0; char buf4[4]; if (ndn_socket.read(buf4,4) < 0 ) { cout<<"cannot get version-cmd-_-addressType"<ndn_negotShake(ndn_socket)>0){ cout<<"negotShake error"< pairval= ndn_connShake(ndn_socket) ; if(pairval.second>0){ cout<<"connShake error"<0){ close(pairval.first); } ndn_socket.close() ; exit(0); } int remote_sockfd = pairval.first; printf("Data transmission is beginning.\n"); void *sockfds[2] = {&ndn_socket,&remote_sockfd} ; pthread_t ptid1, ptid2 ; pthread_create(&ptid1 , NULL , ndn_thread1 , sockfds) ; pthread_create(&ptid2 , NULL , ndn_thread2 , sockfds) ; pthread_join(ptid1,NULL) ; pthread_join(ptid2,NULL) ; cout<<"child process exit()"<sockfd); // exit the son process exit(0); } }; // forever listenner function void Socks5Server::ndn_forever(){ while(true){ string new_prefix = this->listen_prefix+ to_string(p_port_seq++) ; string client_prefix = this->ndn_accept_local(new_prefix) ; ndn_transData(new_prefix, client_prefix ) ; } }; // server start void Socks5Server::start(){ this->ndn_listen_local(base_listen_prefix) ; this->ndn_forever() ; mndn_socket.close() ; }; // transfer data from local server host to remote-server void *Socks5Server::ndn_thread1(void *val){ Ndn_socket *ndn_socketp = *((Ndn_socket**)val) ; int remote_sockfd = **((int**)val + 1) ; char *data=new char[MAX_SIZE]; int datalen; int send_len = 0 ; while(true){ datalen = ndn_socketp->read(data , MAX_SIZE) ; cout << "**************recv from ndn_socket 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 ; } } cout << ">>>>>>>>>>>>>>>>>>>>thread1 end" << endl ; delete data; ndn_socketp->close() ; close(remote_sockfd) ; return NULL; } // transfer data form remote-server to local server host void *Socks5Server::ndn_thread2(void *val){ Ndn_socket *ndn_socketp = *((Ndn_socket**)val) ; int remote_sockfd = **((int**)val + 1) ; char *data=new char[MAX_SIZE]; int datalen; int send_len = 0 ; while(true){ datalen=recv(remote_sockfd, data, MAX_SIZE, 0); cout << "************recv from remote_sock datalen = " << datalen << endl ; if (datalen <= 0) { break; }else{ send_len = ndn_socketp->write(data, datalen) ; cout << "**************send to ndn_socket len = " << send_len << endl ; } } cout << ">>>>>>>>>>>>>>>>>>>>thread2 end" << endl ; delete data; ndn_socketp->close() ; close(remote_sockfd) ; return NULL; }