|
|
|
@ -5,6 +5,7 @@
@@ -5,6 +5,7 @@
|
|
|
|
|
#include "ClientContext.h" |
|
|
|
|
#include "I2PEndian.h" |
|
|
|
|
#include <cstring> |
|
|
|
|
#include <cassert> |
|
|
|
|
|
|
|
|
|
namespace i2p |
|
|
|
|
{ |
|
|
|
@ -15,17 +16,13 @@ namespace proxy
@@ -15,17 +16,13 @@ namespace proxy
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::AsyncSockRead() |
|
|
|
|
{ |
|
|
|
|
LogPrint("--- socks4a async sock read"); |
|
|
|
|
LogPrint(eLogDebug,"--- SOCKS async sock read"); |
|
|
|
|
if(m_sock) { |
|
|
|
|
if (m_state == INITIAL) { |
|
|
|
|
m_sock->async_receive(boost::asio::buffer(m_sock_buff, socks_buffer_size), |
|
|
|
|
std::bind(&SOCKS4AHandler::HandleSockRecv, this, |
|
|
|
|
std::placeholders::_1, std::placeholders::_2)); |
|
|
|
|
} else { |
|
|
|
|
LogPrint("--- socks4a state?? ", m_state); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
LogPrint("--- socks4a no socket for read"); |
|
|
|
|
LogPrint(eLogError,"--- SOCKS no socket for read"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -37,7 +34,7 @@ namespace proxy
@@ -37,7 +34,7 @@ namespace proxy
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::SocksFailed() |
|
|
|
|
{ |
|
|
|
|
LogPrint("--- socks4a failed"); |
|
|
|
|
LogPrint(eLogWarning,"--- SOCKS failed"); |
|
|
|
|
//TODO: send the right response
|
|
|
|
|
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5b 12345"), |
|
|
|
|
std::bind(&SOCKS4AHandler::SentSocksFailed, this, |
|
|
|
@ -47,7 +44,7 @@ namespace proxy
@@ -47,7 +44,7 @@ namespace proxy
|
|
|
|
|
void SOCKS4AHandler::CloseSock() |
|
|
|
|
{ |
|
|
|
|
if (m_sock) { |
|
|
|
|
LogPrint("--- socks4a close sock"); |
|
|
|
|
LogPrint(eLogDebug,"--- SOCKS close sock"); |
|
|
|
|
m_sock->close(); |
|
|
|
|
delete m_sock; |
|
|
|
|
m_sock = nullptr; |
|
|
|
@ -57,7 +54,7 @@ namespace proxy
@@ -57,7 +54,7 @@ namespace proxy
|
|
|
|
|
void SOCKS4AHandler::CloseStream() |
|
|
|
|
{ |
|
|
|
|
if (m_stream) { |
|
|
|
|
LogPrint("--- socks4a close stream"); |
|
|
|
|
LogPrint(eLogDebug,"--- SOCKS close stream"); |
|
|
|
|
m_stream.reset (); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -66,78 +63,147 @@ namespace proxy
@@ -66,78 +63,147 @@ namespace proxy
|
|
|
|
|
const size_t socks_ident_size = 1024; |
|
|
|
|
const size_t destb32_len = 52; |
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len) |
|
|
|
|
std::size_t SOCKS4AHandler::HandleData(uint8_t *sock_buff, std::size_t len) |
|
|
|
|
{ |
|
|
|
|
LogPrint("--- socks4a sock recv: ", len); |
|
|
|
|
//TODO: we may not have received all the data :(
|
|
|
|
|
if(ecode) { |
|
|
|
|
LogPrint(" --- sock recv got error: ", ecode); |
|
|
|
|
assert(len); // This should always be called with a least a byte left to parse
|
|
|
|
|
switch (m_state) { |
|
|
|
|
case GET_VERSION: |
|
|
|
|
return HandleVersion(sock_buff); |
|
|
|
|
case SOCKS4A: |
|
|
|
|
return HandleSOCKS4A(sock_buff,len); |
|
|
|
|
default: |
|
|
|
|
LogPrint(eLogError,"--- SOCKS state?? ", m_state); |
|
|
|
|
Terminate(); |
|
|
|
|
return; |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
char hostbuff[socks_hostname_size]; |
|
|
|
|
char identbuff[socks_ident_size]; |
|
|
|
|
std::memset(hostbuff, 0, sizeof(hostbuff)); |
|
|
|
|
std::memset(identbuff, 0, sizeof(hostbuff)); |
|
|
|
|
std::string dest; |
|
|
|
|
|
|
|
|
|
uint16_t port = 0; |
|
|
|
|
uint32_t address = 0; |
|
|
|
|
uint16_t idx1 = 0; |
|
|
|
|
uint16_t idx2 = 0; |
|
|
|
|
|
|
|
|
|
LogPrint(eLogDebug,"--- socks4a state initial ", len); |
|
|
|
|
std::size_t SOCKS4AHandler::HandleVersion(uint8_t *sock_buff) |
|
|
|
|
{ |
|
|
|
|
switch (*sock_buff) { |
|
|
|
|
case 4: |
|
|
|
|
m_state = SOCKS4A; // Switch to the 4a handler
|
|
|
|
|
m_pstate = GET4A_COMMAND; //Initialize the parser at the right position
|
|
|
|
|
return 1; |
|
|
|
|
default: |
|
|
|
|
LogPrint(eLogError,"--- SOCKS rejected invalid version", ((int)*sock_buff)); |
|
|
|
|
Terminate(); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// check valid request
|
|
|
|
|
if( m_sock_buff[0] != 4 || m_sock_buff[1] != 1 || m_sock_buff[len-1] ) { |
|
|
|
|
LogPrint(eLogError,"--- socks4a rejected invalid"); |
|
|
|
|
std::size_t SOCKS4AHandler::HandleSOCKS4A(uint8_t *sock_buff, std::size_t len) |
|
|
|
|
{ |
|
|
|
|
std::size_t rv = 0; |
|
|
|
|
while (len > 0) { |
|
|
|
|
rv++; |
|
|
|
|
switch (m_pstate) |
|
|
|
|
{ |
|
|
|
|
case GET4A_COMMAND: |
|
|
|
|
if ( *sock_buff != 1 ) { |
|
|
|
|
LogPrint(eLogError,"--- SOCKS4a unsupported command", ((int)*sock_buff)); |
|
|
|
|
SocksFailed(); |
|
|
|
|
return; |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
m_pstate = GET4A_PORT1; |
|
|
|
|
break; |
|
|
|
|
case GET4A_PORT1: |
|
|
|
|
m_port = ((uint16_t)*sock_buff) << 8; |
|
|
|
|
m_pstate = GET4A_PORT2; |
|
|
|
|
break; |
|
|
|
|
case GET4A_PORT2: |
|
|
|
|
m_port |= ((uint16_t)*sock_buff); |
|
|
|
|
m_pstate = GET4A_IP1; |
|
|
|
|
break; |
|
|
|
|
case GET4A_IP1: |
|
|
|
|
m_ip = ((uint32_t)*sock_buff) << 24; |
|
|
|
|
m_pstate = GET4A_IP2; |
|
|
|
|
break; |
|
|
|
|
case GET4A_IP2: |
|
|
|
|
m_ip |= ((uint32_t)*sock_buff) << 16; |
|
|
|
|
m_pstate = GET4A_IP3; |
|
|
|
|
break; |
|
|
|
|
case GET4A_IP3: |
|
|
|
|
m_ip |= ((uint32_t)*sock_buff) << 8; |
|
|
|
|
m_pstate = GET4A_IP4; |
|
|
|
|
break; |
|
|
|
|
case GET4A_IP4: |
|
|
|
|
m_ip |= ((uint32_t)*sock_buff); |
|
|
|
|
m_pstate = GET4A_IDENT; |
|
|
|
|
if( m_ip == 0 || m_ip > 255 ) { |
|
|
|
|
LogPrint(eLogError,"--- SOCKS4a rejected because it's actually SOCKS4"); |
|
|
|
|
SocksFailed(); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case GET4A_IDENT: |
|
|
|
|
if (!*sock_buff) |
|
|
|
|
m_pstate = GET4A_HOST; |
|
|
|
|
break; |
|
|
|
|
case GET4A_HOST: |
|
|
|
|
if (!*sock_buff) { |
|
|
|
|
m_pstate = DONE; |
|
|
|
|
m_state = READY; |
|
|
|
|
return rv; |
|
|
|
|
} |
|
|
|
|
if (m_destination.size() > HOST_NAME_MAX) { |
|
|
|
|
LogPrint(eLogError,"--- SOCKS4a destination is too large "); |
|
|
|
|
SocksFailed(); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
m_destination.push_back(*sock_buff); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
LogPrint(eLogError,"--- SOCKS4a parse state?? ", m_pstate); |
|
|
|
|
Terminate(); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
sock_buff++; |
|
|
|
|
len--; |
|
|
|
|
} |
|
|
|
|
return rv; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// get port
|
|
|
|
|
port = bufbe16toh(m_sock_buff + 2); |
|
|
|
|
void SOCKS4AHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len) |
|
|
|
|
{ |
|
|
|
|
LogPrint(eLogDebug,"--- SOCKS sock recv: ", len); |
|
|
|
|
if(ecode) { |
|
|
|
|
LogPrint(eLogWarning," --- SOCKS sock recv got error: ", ecode); |
|
|
|
|
Terminate(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// get address
|
|
|
|
|
address = bufbe32toh(m_sock_buff + 4); |
|
|
|
|
std::size_t pos = 0; |
|
|
|
|
while (pos != len && m_state != READY) { |
|
|
|
|
assert(pos < len); //We are overflowing the buffer otherwise
|
|
|
|
|
std::size_t rv = HandleData(m_sock_buff + pos, len - pos); |
|
|
|
|
if (!rv) return; //Something went wrong die misserably
|
|
|
|
|
pos += rv; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// check valid request
|
|
|
|
|
if( address == 0 || address > 255 ) { |
|
|
|
|
LogPrint(eLogError,"--- socks4a rejected because it's actually socks4"); |
|
|
|
|
if (m_state == READY) { |
|
|
|
|
LogPrint(eLogInfo,"--- SOCKS requested ", m_destination, ":" , m_port); |
|
|
|
|
if (pos != len) { |
|
|
|
|
LogPrint(eLogError,"--- SOCKS rejected because be can't handle extra data"); |
|
|
|
|
SocksFailed(); |
|
|
|
|
return ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// read ident
|
|
|
|
|
do { |
|
|
|
|
LogPrint(eLogDebug,"--- socks4a ", (int) m_sock_buff[9+idx1]); |
|
|
|
|
identbuff[idx1] = m_sock_buff[8+idx1]; |
|
|
|
|
} while( identbuff[idx1++] && idx1 < socks_ident_size ); |
|
|
|
|
|
|
|
|
|
LogPrint(eLogInfo,"--- socks4a ident ", identbuff); |
|
|
|
|
// read hostname
|
|
|
|
|
do { |
|
|
|
|
hostbuff[idx2] = m_sock_buff[8+idx1+idx2]; |
|
|
|
|
} while( hostbuff[idx2++] && idx2 < socks_hostname_size ); |
|
|
|
|
|
|
|
|
|
LogPrint(eLogInfo,"--- socks4a requested ", hostbuff, ":" , port); |
|
|
|
|
|
|
|
|
|
dest = std::string(hostbuff); |
|
|
|
|
if(dest.find(".i2p") == std::string::npos) { |
|
|
|
|
LogPrint("--- socks4a invalid hostname: ", dest); |
|
|
|
|
if(m_destination.find(".i2p") == std::string::npos) { |
|
|
|
|
LogPrint(eLogError,"--- SOCKS invalid hostname: ", m_destination); |
|
|
|
|
SocksFailed(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: Pass port see m_port
|
|
|
|
|
m_parent->GetLocalDestination ()->CreateStream ( |
|
|
|
|
std::bind (&SOCKS4AHandler::HandleStreamRequestComplete, |
|
|
|
|
this, std::placeholders::_1), dest); |
|
|
|
|
this, std::placeholders::_1), m_destination); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::ConnectionSuccess() |
|
|
|
|
{ |
|
|
|
|
LogPrint(eLogInfo,"--- socks4a connection success"); |
|
|
|
|
LogPrint(eLogInfo,"--- SOCKS connection success"); |
|
|
|
|
//TODO: send the right response
|
|
|
|
|
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5a 12345"), |
|
|
|
|
std::bind(&SOCKS4AHandler::SentConnectionSuccess, this, |
|
|
|
@ -151,7 +217,7 @@ namespace proxy
@@ -151,7 +217,7 @@ namespace proxy
|
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
LogPrint (eLogError,"--- socks4a Closing socket after sending failure because: ", ecode.message ()); |
|
|
|
|
LogPrint (eLogError,"--- SOCKS Closing socket after sending failure because: ", ecode.message ()); |
|
|
|
|
Terminate(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -159,14 +225,14 @@ namespace proxy
@@ -159,14 +225,14 @@ namespace proxy
|
|
|
|
|
void SOCKS4AHandler::SentConnectionSuccess(const boost::system::error_code & ecode) |
|
|
|
|
{ |
|
|
|
|
if (!ecode) { |
|
|
|
|
LogPrint (eLogInfo,"--- socks4a New I2PTunnel connection"); |
|
|
|
|
LogPrint (eLogInfo,"--- SOCKS New I2PTunnel connection"); |
|
|
|
|
auto connection = std::make_shared<i2p::client::I2PTunnelConnection>((i2p::client::I2PTunnel *)m_parent, m_sock, m_stream); |
|
|
|
|
m_parent->AddConnection (connection); |
|
|
|
|
connection->I2PConnect (); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
LogPrint (eLogError,"--- socks4a Closing socket after sending success because: ", ecode.message ()); |
|
|
|
|
LogPrint (eLogError,"--- SOCKS Closing socket after sending success because: ", ecode.message ()); |
|
|
|
|
Terminate(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -180,7 +246,7 @@ namespace proxy
@@ -180,7 +246,7 @@ namespace proxy
|
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
LogPrint (eLogError,"--- socks4a Issue when creating the stream, check the previous warnings for more info."); |
|
|
|
|
LogPrint (eLogError,"--- SOCKS Issue when creating the stream, check the previous warnings for more info."); |
|
|
|
|
SocksFailed(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -210,13 +276,13 @@ namespace proxy
@@ -210,13 +276,13 @@ namespace proxy
|
|
|
|
|
{ |
|
|
|
|
if (!ecode) |
|
|
|
|
{ |
|
|
|
|
LogPrint(eLogDebug,"--- socks4a accepted"); |
|
|
|
|
LogPrint(eLogDebug,"--- SOCKS accepted"); |
|
|
|
|
new SOCKS4AHandler(this, socket); |
|
|
|
|
Accept(); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
LogPrint (eLogError,"--- socks4a Closing socket on accept because: ", ecode.message ()); |
|
|
|
|
LogPrint (eLogError,"--- SOCKS Closing socket on accept because: ", ecode.message ()); |
|
|
|
|
delete socket; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|