Connecting to and using socks 4 proxy servers
Hi, im trying to find out information on how to connect to and use socks 4/5 servers. Ive got the RFC for socks 5, but its not clear how you actually use the socks server to do something useful once it has conneced. For example, how do you tell the socks server which server to connect to? and which port? and the type of conenction you want. Can anyone provide places where i can find this information?
thanks
-keyboard
its all in the docs..
example:
Connect to the socks server
...
if you want to support non passworded and passworded socks send this handshake:
//Send Handshake
char buf[255];
int bytesRead;
buf[0]=0x05; //version
buf[1]=0x02; //number of methods
buf[2]=0x00; //method (no auth required)
buf[3]=0x02; //method (username/password)
then read the reply (2 bytes) and check which method the socks requires..
hmm.. ill just give u the dam code from my class :sigh: u should be able to work out whats going on
after your connected you can use the socket as if directly connected
example:
Connect to the socks server
...
if you want to support non passworded and passworded socks send this handshake:
//Send Handshake
char buf[255];
int bytesRead;
buf[0]=0x05; //version
buf[1]=0x02; //number of methods
buf[2]=0x00; //method (no auth required)
buf[3]=0x02; //method (username/password)
then read the reply (2 bytes) and check which method the socks requires..
hmm.. ill just give u the dam code from my class :sigh: u should be able to work out whats going on
ConnectReturn Win_TCPSocket::ConnectSocks( const char *pszHost, int nPort, const char *pszSocksHost, int nSocksPort, const char *pszUsername, const char *pszPassword ){ unsigned long ulAddr = 0; //get ip address for SOCKS server hostent *pEnt = ::gethostbyname( pszSocksHost ); SOCKADDR_IN addrSocks; if( pEnt == 0 ) { ulAddr = ::inet_addr( pszSocksHost ); if( ulAddr == INADDR_NONE ) { return Connect_BadAddress; } else { addrSocks.sin_family = AF_INET; } } else { memcpy( &ulAddr, pEnt->h_addr_list[0], sizeof( long ) ); addrSocks.sin_family = pEnt->h_addrtype; } addrSocks.sin_addr.s_addr = ulAddr; addrSocks.sin_port = ::htons( nSocksPort ); memset( addrSocks.sin_zero, 0, sizeof( addrSocks.sin_zero ) );//SOCKS 5 Connect //Connect to socks server if( ::connect( m_hSocket, (const sockaddr *)&addrSocks, sizeof( SOCKADDR_IN ) ) == SOCKET_ERROR ) { return Connect_ErrorConnecting; } //Send Handshake char buf[255]; int bytesRead; buf[0]=0x05; //version buf[1]=0x02; //number of methods buf[2]=0x00; //method (no auth required) buf[3]=0x02; //method (username/password) if( this->Send( buf, 4 ) != 4) { this->Close(); return Connect_ErrorComSocks; } //check accepted method this->Recv( buf, 2, bytesRead, 0, true ); switch( buf[1] ) { case 0x00: //No Auth break; case 0x02: //Username/Password { if( pszUsername == NULL || pszPassword == NULL ) { this->Close(); return Connect_ReqUnPw; } //send username and password info buf[0]=0x01; //username/password version buf[1]=strlen(pszUsername); //UName Length memcpy( &buf[2], pszUsername, buf[1] ); //UName buf[2+buf[1]]=strlen(pszPassword); //PWord Length memcpy( &buf[3+buf[1]], pszPassword, buf[2+buf[1]] ); //PWord if( this->Send( buf, 2+buf[1]+1+buf[2+buf[1]] ) != 2+buf[1]+1+buf[2+buf[1]]) { this->Close(); return Connect_ErrorComSocks; } //check accepted un/pw this->Recv( buf, 2, bytesRead, 0, true ); switch( buf[1] ) { case 0x00: //OK break; default: //not accepted { this->Close(); return Connect_BadUnPw; } } break; } default: //non excepted { this->Close(); return Connect_SocksNotSupport; } } //Send connect request buf[0]=0x05; //version buf[1]=0x01; //Connect command buf[2]=0x00; //Reserved //try and get address if its a dotted ip address ulAddr = ::inet_addr( pszHost ); u_short port = ::htons( nPort ); if( ulAddr != INADDR_NONE ) { //send the ip buf[3]=0x01; //Address Type (IP4) memcpy( &buf[4], &ulAddr, 4 ); //IP Address memcpy( &buf[8], &port, 2 ); //Port if( this->Send( buf, 10 ) != 10) { this->Close(); return Connect_ErrorComSocks; } }else { //just send the hostname then buf[3]=0x03; //Address Type (HOSTNAME) buf[4]=strlen(pszHost); memcpy( &buf[5], pszHost, buf[4] ); //HOSTNAME memcpy( &buf[5+buf[4]], &port, 2 ); //Port if( this->Send( buf, 7+buf[4] ) != 7+buf[4]) { this->Close(); return Connect_ErrorComSocks; } } //check reply this->Recv( buf, 4, bytesRead, 0, true ); switch( buf[1] ) { case 0x00: //Success { if( buf[3] == 0x01 ) //IP Address { this->Recv( &buf[4], 6, bytesRead, 0, true ); }else if( buf[3] == 0x03 ) //Domain Name { //size this->Recv( &buf[4], 1, bytesRead, 0, true ); //string and port this->Recv( &buf[5], buf[4]+2, bytesRead, 0, true ); }else { this->Close(); return Connect_ErrorComSocks; } break; } case 0x01: this->Close(); return Connect_Socks_Fail; case 0x02: this->Close(); return Connect_Socks_Fail_Ruleset; case 0x03: this->Close(); return Connect_Socks_NetworkUnreach; case 0x04: this->Close(); return Connect_Socks_HostUnreach; case 0x05: this->Close(); return Connect_Socks_ConnRefused; case 0x06: this->Close(); return Connect_Socks_TTLExpired; case 0x07: this->Close(); return Connect_Socks_ComNotSupport; case 0x08: this->Close(); return Connect_Socks_AddrTypeNotSupport; default: //Failure { this->Close(); return Connect_ErrorConnectingViaSocks; } } m_connected = true; return Connect_OK;}
after your connected you can use the socket as if directly connected
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement