19 #include "connectionsocks5proxy.h"
31 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
32 # include <netinet/in.h>
35 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
37 #elif defined( _WIN32_WCE )
38 # include <winsock2.h>
48 const std::string& server,
51 m_logInstance( logInstance ), m_s5state( S5StateDisconnected ), m_ip( ip )
64 const std::string& server,
67 m_logInstance( logInstance ), m_s5state( S5StateDisconnected ), m_ip( ip )
94 m_connection = connection;
103 m_s5state = S5StateConnected;
110 m_s5state = S5StateConnecting;
111 return m_connection->
connect();
127 return m_connection->
recv( timeout );
135 return m_connection->
receive();
152 return m_connection->
send( data );
160 m_s5state = S5StateDisconnected;
178 const std::string& data )
196 case S5StateConnecting:
197 if( data.length() != 2 || data[0] != 0x05 )
200 if( data[1] == 0x00 )
204 else if( data[1] == 0x02 && !m_proxyUser.empty() && !m_proxyPwd.empty() )
207 "authenticating to socks5 proxy as user " + m_proxyUser );
208 m_s5state = S5StateAuthenticating;
209 char* d =
new char[3 + m_proxyUser.length() + m_proxyPwd.length()];
212 d[pos++] = (char)m_proxyUser.length();
213 strncpy( d + pos, m_proxyUser.c_str(), m_proxyUser.length() );
214 pos += m_proxyUser.length();
215 d[pos++] = (char)m_proxyPwd.length();
216 strncpy( d + pos, m_proxyPwd.c_str(), m_proxyPwd.length() );
217 pos += m_proxyPwd.length();
219 if( !
send( std::string( d, pos ) ) )
228 if( data[1] == (
char)(
unsigned char)0xFF && !m_proxyUser.empty() && !m_proxyPwd.empty() )
234 case S5StateNegotiating:
235 if( data.length() >= 6 && data[0] == 0x05 )
237 if( data[1] == 0x00 )
240 m_s5state = S5StateConnected;
249 case S5StateAuthenticating:
250 if( data.length() == 2 && data[0] == 0x01 && data[1] == 0x00 )
255 case S5StateConnected:
270 void ConnectionSOCKS5Proxy::negotiate()
272 m_s5state = S5StateNegotiating;
273 char* d =
new char[m_ip ? 10 : 6 +
m_server.length() + 1];
284 const size_t j = server.length();
286 for(
size_t k = 0; k < j && l < 4; ++k )
288 if( server[k] !=
'.' )
291 if( server[k] ==
'.' || k == j-1 )
293 d[pos++] =
static_cast<char>( atoi( s.c_str() ) & 0xFF );
306 const std::pair< std::string, int >& host = *servers.begin();
316 int nport = htons( port );
317 d[pos++] =
static_cast<char>( nport );
318 d[pos++] =
static_cast<char>( nport >> 8 );
320 std::string message =
"Requesting socks5 proxy connection to " + server +
":"
321 + util::int2string( port );
324 if( !
send( std::string( d, pos ) ) )
341 if( !servers.empty() )
343 const std::pair< std::string, int >& host = *servers.begin();
349 "Attempting to negotiate socks5 proxy connection" );
351 const bool auth = !m_proxyUser.empty() && !m_proxyPwd.empty();
354 static_cast<char>( auth ? 0x02
360 if( !
send( std::string( d, auth ? 4 : 3 ) ) )