14 #include "socks5bytestreamserver.h"
15 #include "connectiontcpserver.h"
16 #include "mutexguard.h"
22 const std::string& ip )
23 : m_tcpServer( 0 ), m_logInstance( logInstance ), m_ip( ip ), m_port( port )
33 ConnectionMap::const_iterator it = m_connections.begin();
34 for( ; it != m_connections.end(); ++it )
55 ConnectionMap::const_iterator it = m_connections.begin();
56 ConnectionMap::const_iterator it2;
57 while( it != m_connections.end() )
60 (*it2).first->recv( timeout );
63 ConnectionList::iterator it3 = m_oldConnections.begin();
64 ConnectionList::iterator it4;
65 while( it3 != m_oldConnections.end() )
69 m_oldConnections.erase( it4 );
84 ConnectionBase* SOCKS5BytestreamServer::getConnection(
const std::string& hash )
88 ConnectionMap::iterator it = m_connections.begin();
89 for( ; it != m_connections.end(); ++it )
91 if( (*it).second.hash == hash )
95 m_connections.erase( it );
103 void SOCKS5BytestreamServer::registerHash(
const std::string& hash )
105 MutexGuard mg( m_mutex );
106 m_hashes.push_back( hash );
109 void SOCKS5BytestreamServer::removeHash(
const std::string& hash )
111 MutexGuard mg( m_mutex );
112 m_hashes.remove( hash );
119 ci.state = StateUnnegotiated;
120 m_connections[connection] = ci;
124 const std::string& data )
126 ConnectionMap::iterator it = m_connections.find( const_cast<ConnectionBase*>( connection ) );
127 if( it == m_connections.end() )
130 switch( (*it).second.state )
132 case StateDisconnected:
133 (*it).first->disconnect();
135 case StateUnnegotiated:
140 (*it).second.state = StateDisconnected;
142 if( data.length() >= 3 && data[0] == 0x05 )
144 unsigned int sz = ( data.length() - 2 < (
unsigned int)data[1] )
145 ? ( data.length() - 2 )
146 : ( (
unsigned int)data[1] );
147 for(
unsigned int i = 2; i < sz + 2; ++i )
149 if( data[i] == 0x00 )
152 (*it).second.state = StateAuthAccepted;
157 (*it).first->send( std::string( c, 2 ) );
160 case StateAuthmethodAccepted:
163 case StateAuthAccepted:
165 std::string reply = data;
166 if( reply.length() < 2 )
171 (*it).second.state = StateDisconnected;
173 if( data.length() == 47 && data[0] == 0x05 && data[1] == 0x01 && data[2] == 0x00
174 && data[3] == 0x03 && data[4] == 0x28 && data[45] == 0x00 && data[46] == 0x00 )
176 const std::string hash = data.substr( 5, 40 );
178 HashMap::const_iterator ith = m_hashes.begin();
179 for( ; ith != m_hashes.end() && (*ith) != hash; ++ith )
182 if( ith != m_hashes.end() )
185 (*it).second.hash = hash;
186 (*it).second.state = StateDestinationAccepted;
189 (*it).first->send( reply );
192 case StateDestinationAccepted:
207 m_connections.erase( const_cast<ConnectionBase*>( connection ) );
208 m_oldConnections.push_back( connection );