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>
46 const std::string& server,
49 m_logInstance( logInstance ), m_s5state( S5StateDisconnected ), m_ip( ip )
62 const std::string& server,
65 m_logInstance( logInstance ), m_s5state( S5StateDisconnected ), m_ip( ip )
92 m_connection = connection;
101 m_s5state = S5StateConnected;
108 m_s5state = S5StateConnecting;
109 return m_connection->
connect();
125 return m_connection->
recv( timeout );
133 return m_connection->
receive();
150 return m_connection->
send( data );
158 m_s5state = S5StateDisconnected;
176 const std::string& data )
194 case S5StateConnecting:
195 if( data.length() != 2 || data[0] != 0x05 )
198 if( data[1] == 0x00 )
202 else if( data[1] == 0x02 && !m_proxyUser.empty() && !m_proxyPwd.empty() )
205 "authenticating to socks5 proxy as user " + m_proxyUser );
206 m_s5state = S5StateAuthenticating;
207 char* d =
new char[3 + m_proxyUser.length() + m_proxyPwd.length()];
210 d[pos++] = (char)m_proxyUser.length();
211 strncpy( d + pos, m_proxyUser.c_str(), m_proxyUser.length() );
212 pos += m_proxyUser.length();
213 d[pos++] = (char)m_proxyPwd.length();
214 strncpy( d + pos, m_proxyPwd.c_str(), m_proxyPwd.length() );
215 pos += m_proxyPwd.length();
217 if( !
send( std::string( d, pos ) ) )
226 if( data[1] == (
char)(
unsigned char)0xFF && !m_proxyUser.empty() && !m_proxyPwd.empty() )
232 case S5StateNegotiating:
233 if( data.length() >= 6 && data[0] == 0x05 )
235 if( data[1] == 0x00 )
238 m_s5state = S5StateConnected;
247 case S5StateAuthenticating:
248 if( data.length() == 2 && data[0] == 0x01 && data[1] == 0x00 )
253 case S5StateConnected:
268 void ConnectionSOCKS5Proxy::negotiate()
270 m_s5state = S5StateNegotiating;
271 char* d =
new char[m_ip ? 10 : 6 +
m_server.length() + 1];
282 const size_t j = server.length();
284 for(
size_t k = 0; k < j && l < 4; ++k )
286 if( server[k] !=
'.' )
289 if( server[k] ==
'.' || k == j-1 )
291 d[pos++] =
static_cast<char>( atoi( s.c_str() ) & 0xFF );
304 const std::pair< std::string, int >& host = *servers.begin();
314 int nport = htons( port );
315 d[pos++] =
static_cast<char>( nport );
316 d[pos++] =
static_cast<char>( nport >> 8 );
318 std::string message =
"Requesting socks5 proxy connection to " + server +
":"
319 + util::int2string( port );
322 if( !
send( std::string( d, pos ) ) )
339 if( !servers.empty() )
341 const std::pair< std::string, int >& host = *servers.begin();
347 "Attempting to negotiate socks5 proxy connection" );
349 const bool auth = !m_proxyUser.empty() && !m_proxyPwd.empty();
352 static_cast<char>( auth ? 0x02
358 if( !
send( std::string( d, auth ? 4 : 3 ) ) )