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 ) ) )
An abstract base class for a connection.
static HostMap resolve(const std::string &service, const std::string &proto, const std::string &domain, const LogSink &logInstance)
std::map< std::string, int > HostMap
virtual ConnectionBase * newInstance() const
virtual void handleReceivedData(const ConnectionBase *connection, const std::string &data)=0
void setConnectionImpl(ConnectionBase *connection)
virtual void handleReceivedData(const ConnectionBase *connection, const std::string &data)
virtual void disconnect()=0
virtual ConnectionError connect()=0
This is an abstract base class to receive events from a ConnectionBase-derived object.
virtual ConnectionError receive()
virtual ConnectionBase * newInstance() const =0
virtual void handleDisconnect(const ConnectionBase *connection, ConnectionError reason)=0
The namespace for the gloox library.
const std::string & server() const
virtual ~ConnectionSOCKS5Proxy()
virtual bool send(const std::string &data)=0
virtual void handleConnect(const ConnectionBase *connection)=0
virtual void getStatistics(long int &totalIn, long int &totalOut)
virtual void handleConnect(const ConnectionBase *connection)
virtual ConnectionError receive()=0
void registerConnectionDataHandler(ConnectionDataHandler *cdh)
virtual void disconnect()
virtual ConnectionError recv(int timeout=-1)=0
bool idna(const std::string &domain, std::string &out)
virtual void getStatistics(long int &totalIn, long int &totalOut)=0
ConnectionSOCKS5Proxy(ConnectionBase *connection, const LogSink &logInstance, const std::string &server, int port=-1, bool ip=false)
void dbg(LogArea area, const std::string &message) const
ConnectionDataHandler * m_handler
virtual void handleDisconnect(const ConnectionBase *connection, ConnectionError reason)
ConnectionState state() const
virtual ConnectionError recv(int timeout=-1)
An implementation of log sink and source.
const std::string EmptyString
virtual bool send(const std::string &data)
virtual ConnectionError connect()