|
|
@ -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 { |
|
|
|
} else { |
|
|
|
LogPrint("--- socks4a state?? ", m_state); |
|
|
|
LogPrint(eLogError,"--- SOCKS no socket for read"); |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
LogPrint("--- socks4a 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); |
|
|
|
|
|
|
|
case SOCKS4A: |
|
|
|
|
|
|
|
return HandleSOCKS4A(sock_buff,len); |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
LogPrint(eLogError,"--- SOCKS state?? ", m_state); |
|
|
|
Terminate(); |
|
|
|
Terminate(); |
|
|
|
return; |
|
|
|
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
|
|
|
|
std::size_t SOCKS4AHandler::HandleSOCKS4A(uint8_t *sock_buff, std::size_t len) |
|
|
|
if( m_sock_buff[0] != 4 || m_sock_buff[1] != 1 || m_sock_buff[len-1] ) { |
|
|
|
{ |
|
|
|
LogPrint(eLogError,"--- socks4a rejected invalid"); |
|
|
|
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(); |
|
|
|
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
|
|
|
|
void SOCKS4AHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len) |
|
|
|
port = bufbe16toh(m_sock_buff + 2); |
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint(eLogDebug,"--- SOCKS sock recv: ", len); |
|
|
|
|
|
|
|
if(ecode) { |
|
|
|
|
|
|
|
LogPrint(eLogWarning," --- SOCKS sock recv got error: ", ecode); |
|
|
|
|
|
|
|
Terminate(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// get address
|
|
|
|
std::size_t pos = 0; |
|
|
|
address = bufbe32toh(m_sock_buff + 4); |
|
|
|
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 (m_state == READY) { |
|
|
|
if( address == 0 || address > 255 ) { |
|
|
|
LogPrint(eLogInfo,"--- SOCKS requested ", m_destination, ":" , m_port); |
|
|
|
LogPrint(eLogError,"--- socks4a rejected because it's actually socks4"); |
|
|
|
if (pos != len) { |
|
|
|
|
|
|
|
LogPrint(eLogError,"--- SOCKS rejected because be can't handle extra data"); |
|
|
|
SocksFailed(); |
|
|
|
SocksFailed(); |
|
|
|
return ; |
|
|
|
return ; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if(m_destination.find(".i2p") == std::string::npos) { |
|
|
|
// read ident
|
|
|
|
LogPrint(eLogError,"--- SOCKS invalid hostname: ", m_destination); |
|
|
|
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); |
|
|
|
|
|
|
|
SocksFailed(); |
|
|
|
SocksFailed(); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Pass port see m_port
|
|
|
|
m_parent->GetLocalDestination ()->CreateStream ( |
|
|
|
m_parent->GetLocalDestination ()->CreateStream ( |
|
|
|
std::bind (&SOCKS4AHandler::HandleStreamRequestComplete, |
|
|
|
std::bind (&SOCKS4AHandler::HandleStreamRequestComplete, |
|
|
|
this, std::placeholders::_1), dest); |
|
|
|
this, std::placeholders::_1), m_destination); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|