14 #include "socks5bytestreamserver.h"
15 #include "connectiontcpserver.h"
16 #include "mutexguard.h"
23 const std::string& ip )
24 : m_tcpServer( 0 ), m_logInstance( logInstance ), m_ip( ip ), m_port( port )
34 ConnectionMap::const_iterator it = m_connections.begin();
35 for( ; it != m_connections.end(); ++it )
56 ConnectionMap::const_iterator it = m_connections.begin();
57 ConnectionMap::const_iterator it2;
58 while( it != m_connections.end() )
61 (*it2).first->recv( timeout );
93 ConnectionBase* SOCKS5BytestreamServer::getConnection(
const std::string& hash )
97 ConnectionMap::iterator it = m_connections.begin();
98 for( ; it != m_connections.end(); ++it )
100 if( (*it).second.hash == hash )
104 m_connections.erase( it );
112 void SOCKS5BytestreamServer::registerHash(
const std::string& hash )
114 util::MutexGuard mg( m_mutex );
115 m_hashes.push_back( hash );
118 void SOCKS5BytestreamServer::removeHash(
const std::string& hash )
120 util::MutexGuard mg( m_mutex );
121 m_hashes.remove( hash );
128 ci.state = StateUnnegotiated;
129 m_connections[connection] = ci;
133 const std::string& data )
135 ConnectionMap::iterator it = m_connections.find( const_cast<ConnectionBase*>( connection ) );
136 if( it == m_connections.end() )
139 switch( (*it).second.state )
141 case StateDisconnected:
142 (*it).first->disconnect();
144 case StateUnnegotiated:
148 c[1] = (char)(
unsigned char)0xFF;
149 (*it).second.state = StateDisconnected;
151 if( data.length() >= 3 && data[0] == 0x05 )
153 unsigned int sz = ( data.length() - 2 <
static_cast<unsigned int>( data[1] ) )
154 ?
static_cast<unsigned int>( data.length() - 2 )
155 : static_cast<unsigned int>( data[1] );
156 for(
unsigned int i = 2; i < sz + 2; ++i )
158 if( data[i] == 0x00 )
161 (*it).second.state = StateAuthAccepted;
166 (*it).first->send( std::string( c, 2 ) );
169 case StateAuthmethodAccepted:
172 case StateAuthAccepted:
174 std::string reply = data;
175 if( reply.length() < 2 )
180 (*it).second.state = StateDisconnected;
182 if( data.length() == 47 && data[0] == 0x05 && data[1] == 0x01 && data[2] == 0x00
183 && data[3] == 0x03 && data[4] == 0x28 && data[45] == 0x00 && data[46] == 0x00 )
185 const std::string hash = data.substr( 5, 40 );
187 HashMap::const_iterator ith = m_hashes.begin();
188 for( ; ith != m_hashes.end() && (*ith) != hash; ++ith )
191 if( ith != m_hashes.end() )
194 (*it).second.hash = hash;
195 (*it).second.state = StateDestinationAccepted;
198 (*it).first->send( reply );
201 case StateDestinationAccepted:
216 m_connections.erase( const_cast<ConnectionBase*>( connection ) );
217 m_oldConnections.push_back( connection );