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