|
|
@ -3,10 +3,8 @@ |
|
|
|
#include "NetDb.h" |
|
|
|
#include "NetDb.h" |
|
|
|
#include "Destination.h" |
|
|
|
#include "Destination.h" |
|
|
|
#include "ClientContext.h" |
|
|
|
#include "ClientContext.h" |
|
|
|
|
|
|
|
#include "I2PEndian.h" |
|
|
|
#include <cstring> |
|
|
|
#include <cstring> |
|
|
|
#include <stdexcept> |
|
|
|
|
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp> |
|
|
|
|
|
|
|
#include <boost/bind.hpp> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace i2p |
|
|
|
namespace i2p |
|
|
|
{ |
|
|
|
{ |
|
|
@ -21,46 +19,29 @@ namespace proxy |
|
|
|
if(m_sock) { |
|
|
|
if(m_sock) { |
|
|
|
if (m_state == INITIAL) { |
|
|
|
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), |
|
|
|
boost::bind(&SOCKS4AHandler::HandleSockRecv, this, |
|
|
|
std::bind(&SOCKS4AHandler::HandleSockRecv, this, |
|
|
|
boost::asio::placeholders::error, |
|
|
|
std::placeholders::_1, std::placeholders::_2)); |
|
|
|
boost::asio::placeholders::bytes_transferred)); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
m_sock->async_receive(boost::asio::buffer(m_sock_buff, socks_buffer_size), |
|
|
|
LogPrint("--- socks4a state?? ", m_state); |
|
|
|
boost::bind(&SOCKS4AHandler::HandleSockForward, this, |
|
|
|
|
|
|
|
boost::asio::placeholders::error, |
|
|
|
|
|
|
|
boost::asio::placeholders::bytes_transferred)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
LogPrint("--- socks4a no socket for read"); |
|
|
|
LogPrint("--- socks4a no socket for read"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::AsyncStreamRead() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogPrint("--- socks4a async stream read"); |
|
|
|
|
|
|
|
if (m_stream) { |
|
|
|
|
|
|
|
m_stream->AsyncReceive( |
|
|
|
|
|
|
|
boost::asio::buffer(m_stream_buff, socks_buffer_size), |
|
|
|
|
|
|
|
boost::bind(&SOCKS4AHandler::HandleStreamRecv, this, |
|
|
|
|
|
|
|
boost::asio::placeholders::error, |
|
|
|
|
|
|
|
boost::asio::placeholders::bytes_transferred), socks_timeout); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
LogPrint("--- socks4a no stream for read"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::Terminate() { |
|
|
|
void SOCKS4AHandler::Terminate() { |
|
|
|
CloseStream(); |
|
|
|
CloseStream(); |
|
|
|
CloseSock(); |
|
|
|
CloseSock(); |
|
|
|
delete this; // ew
|
|
|
|
delete this; // HACK: ew
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::SocksFailed() |
|
|
|
void SOCKS4AHandler::SocksFailed() |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint("--- socks4a failed"); |
|
|
|
LogPrint("--- socks4a failed"); |
|
|
|
m_sock->send(boost::asio::buffer("\x00\x5b 12345")); |
|
|
|
//TODO: send the right response
|
|
|
|
Terminate(); |
|
|
|
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5b 12345"), |
|
|
|
|
|
|
|
std::bind(&SOCKS4AHandler::SentSocksFailed, this, |
|
|
|
|
|
|
|
std::placeholders::_1)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::CloseSock() |
|
|
|
void SOCKS4AHandler::CloseSock() |
|
|
@ -85,199 +66,160 @@ 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::HandleSockForward(const boost::system::error_code & ecode, std::size_t len) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if(ecode) { |
|
|
|
|
|
|
|
LogPrint("--- socks4a forward got error: ", ecode); |
|
|
|
|
|
|
|
Terminate(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogPrint("--- socks4a sock forward: ", len); |
|
|
|
|
|
|
|
m_stream->Send(m_sock_buff, len); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len) |
|
|
|
void SOCKS4AHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len) |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint("--- socks4a sock recv: ", len); |
|
|
|
LogPrint("--- socks4a sock recv: ", len); |
|
|
|
|
|
|
|
//TODO: we may not have received all the data :(
|
|
|
|
if(ecode) { |
|
|
|
if(ecode) { |
|
|
|
LogPrint(" --- sock recv got error: ", ecode); |
|
|
|
LogPrint(" --- sock recv got error: ", ecode); |
|
|
|
Terminate(); |
|
|
|
Terminate(); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (m_state == INITIAL) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char hostbuff[socks_hostname_size]; |
|
|
|
char hostbuff[socks_hostname_size]; |
|
|
|
char identbuff[socks_ident_size]; |
|
|
|
char identbuff[socks_ident_size]; |
|
|
|
std::memset(hostbuff, 0, sizeof(hostbuff)); |
|
|
|
std::memset(hostbuff, 0, sizeof(hostbuff)); |
|
|
|
std::memset(identbuff, 0, sizeof(hostbuff)); |
|
|
|
std::memset(identbuff, 0, sizeof(hostbuff)); |
|
|
|
std::string dest; |
|
|
|
std::string dest; |
|
|
|
// get port
|
|
|
|
|
|
|
|
uint16_t port = 0; |
|
|
|
uint16_t port = 0; |
|
|
|
|
|
|
|
uint32_t address = 0; |
|
|
|
uint16_t idx1 = 0; |
|
|
|
uint16_t idx1 = 0; |
|
|
|
uint16_t idx2 = 0; |
|
|
|
uint16_t idx2 = 0; |
|
|
|
|
|
|
|
|
|
|
|
LogPrint("--- socks4a state initial ", len); |
|
|
|
LogPrint(eLogDebug,"--- socks4a state initial ", len); |
|
|
|
|
|
|
|
|
|
|
|
// check valid request
|
|
|
|
// check valid request
|
|
|
|
if( m_sock_buff[0] != 4 || m_sock_buff[1] != 1 || m_sock_buff[len-1] ) { |
|
|
|
if( m_sock_buff[0] != 4 || m_sock_buff[1] != 1 || m_sock_buff[len-1] ) { |
|
|
|
LogPrint("--- socks4a rejected invalid"); |
|
|
|
LogPrint(eLogError,"--- socks4a rejected invalid"); |
|
|
|
SocksFailed(); |
|
|
|
SocksFailed(); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// get port
|
|
|
|
// get port
|
|
|
|
port = m_sock_buff[3] | m_sock_buff[2] << 8; |
|
|
|
port = bufbe16toh(m_sock_buff + 2); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get address
|
|
|
|
|
|
|
|
address = bufbe32toh(m_sock_buff + 4); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check valid request
|
|
|
|
|
|
|
|
if( address == 0 || address > 255 ) { |
|
|
|
|
|
|
|
LogPrint(eLogError,"--- socks4a rejected because it's actually socks4"); |
|
|
|
|
|
|
|
SocksFailed(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// read ident
|
|
|
|
// read ident
|
|
|
|
do { |
|
|
|
do { |
|
|
|
LogPrint("--- socks4a ", (int) m_sock_buff[9+idx1]); |
|
|
|
LogPrint(eLogDebug,"--- socks4a ", (int) m_sock_buff[9+idx1]); |
|
|
|
identbuff[idx1] = m_sock_buff[8+idx1]; |
|
|
|
identbuff[idx1] = m_sock_buff[8+idx1]; |
|
|
|
} while( identbuff[idx1++] && idx1 < socks_ident_size ); |
|
|
|
} while( identbuff[idx1++] && idx1 < socks_ident_size ); |
|
|
|
|
|
|
|
|
|
|
|
LogPrint("--- socks4a ident ", identbuff); |
|
|
|
LogPrint(eLogInfo,"--- socks4a ident ", identbuff); |
|
|
|
// read hostname
|
|
|
|
// read hostname
|
|
|
|
do { |
|
|
|
do { |
|
|
|
hostbuff[idx2] = m_sock_buff[8+idx1+idx2]; |
|
|
|
hostbuff[idx2] = m_sock_buff[8+idx1+idx2]; |
|
|
|
} while( hostbuff[idx2++] && idx2 < socks_hostname_size ); |
|
|
|
} while( hostbuff[idx2++] && idx2 < socks_hostname_size ); |
|
|
|
|
|
|
|
|
|
|
|
LogPrint("--- socks4a requested ", hostbuff, ":" , port); |
|
|
|
LogPrint(eLogInfo,"--- socks4a requested ", hostbuff, ":" , port); |
|
|
|
|
|
|
|
|
|
|
|
dest = std::string(hostbuff); |
|
|
|
dest = std::string(hostbuff); |
|
|
|
if(dest.find(".b32.i2p") == std::string::npos) { |
|
|
|
if(dest.find(".i2p") == std::string::npos) { |
|
|
|
LogPrint("--- socks4a invalid hostname: ", dest); |
|
|
|
LogPrint("--- socks4a invalid hostname: ", dest); |
|
|
|
SocksFailed(); |
|
|
|
SocksFailed(); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( i2p::data::Base32ToByteStream(hostbuff, destb32_len, (uint8_t *) m_dest, 32) != 32 ) { |
|
|
|
m_parent->GetLocalDestination ()->CreateStream ( |
|
|
|
LogPrint("--- sock4a invalid b32: ", dest); |
|
|
|
std::bind (&SOCKS4AHandler::HandleStreamRequestComplete, |
|
|
|
|
|
|
|
this, std::placeholders::_1), dest); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
LogPrint("--- sock4a find lease set"); |
|
|
|
void SOCKS4AHandler::ConnectionSuccess() |
|
|
|
m_ls = i2p::data::netdb.FindLeaseSet(m_dest); |
|
|
|
{ |
|
|
|
if (!m_ls || m_ls->HasNonExpiredLeases()) { |
|
|
|
LogPrint(eLogInfo,"--- socks4a connection success"); |
|
|
|
i2p::client::context.GetSharedLocalDestination ()->RequestDestination (m_dest); |
|
|
|
//TODO: send the right response
|
|
|
|
m_ls_timer.expires_from_now(boost::posix_time::seconds(socks_leaseset_timeout)); |
|
|
|
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5a 12345"), |
|
|
|
m_ls_timer.async_wait(boost::bind(&SOCKS4AHandler::LeaseSetTimeout, this, boost::asio::placeholders::error)); |
|
|
|
std::bind(&SOCKS4AHandler::SentConnectionSuccess, this, |
|
|
|
} else { |
|
|
|
std::placeholders::_1)); |
|
|
|
ConnectionSuccess(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
LogPrint("--- socks4a state?? ", m_state); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::HandleStreamRecv(const boost::system::error_code & ecode, std::size_t len) |
|
|
|
void SOCKS4AHandler::SentSocksFailed(const boost::system::error_code & ecode) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if(ecode) { LogPrint("--- socks4a stream recv error: ", ecode); m_state = END; } |
|
|
|
if (!ecode) { |
|
|
|
switch(m_state) { |
|
|
|
Terminate(); |
|
|
|
case INITIAL: |
|
|
|
} |
|
|
|
case END: |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint (eLogError,"--- socks4a Closing socket after sending failure because: ", ecode.message ()); |
|
|
|
Terminate(); |
|
|
|
Terminate(); |
|
|
|
return; |
|
|
|
|
|
|
|
case OKAY: |
|
|
|
|
|
|
|
LogPrint("--- socks4a stream recv ", len); |
|
|
|
|
|
|
|
boost::asio::async_write(*m_sock, boost::asio::buffer(m_stream_buff, len), |
|
|
|
|
|
|
|
boost::bind(&SOCKS4AHandler::StreamWrote, this, |
|
|
|
|
|
|
|
boost::asio::placeholders::error)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::SockWrote(const boost::system::error_code & ecode) |
|
|
|
void SOCKS4AHandler::SentConnectionSuccess(const boost::system::error_code & ecode) |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint("--- socks4a sock wrote"); |
|
|
|
if (!ecode) { |
|
|
|
if(ecode) { LogPrint("--- socks4a SockWrote error: ",ecode); } |
|
|
|
LogPrint (eLogInfo,"--- socks4a New I2PTunnel connection"); |
|
|
|
else { AsyncSockRead(); } |
|
|
|
auto connection = std::make_shared<i2p::client::I2PTunnelConnection>((i2p::client::I2PTunnel *)m_parent, m_sock, m_stream); |
|
|
|
|
|
|
|
m_parent->AddConnection (connection); |
|
|
|
|
|
|
|
connection->I2PConnect (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
void SOCKS4AHandler::StreamWrote(const boost::system::error_code & ecode) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint (eLogError,"--- socks4a Closing socket after sending success because: ", ecode.message ()); |
|
|
|
LogPrint("--- socks4a stream wrote"); |
|
|
|
Terminate(); |
|
|
|
if(ecode) { LogPrint("--- socks4a StreamWrote error: ",ecode); } |
|
|
|
} |
|
|
|
else { AsyncStreamRead(); } |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::LeaseSetTimeout(const boost::system::error_code & ecode) |
|
|
|
void SOCKS4AHandler::HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_ls = i2p::data::netdb.FindLeaseSet(m_dest); |
|
|
|
if (stream) |
|
|
|
if(m_ls) { |
|
|
|
{ |
|
|
|
|
|
|
|
m_stream = stream; |
|
|
|
ConnectionSuccess(); |
|
|
|
ConnectionSuccess(); |
|
|
|
} else { |
|
|
|
} |
|
|
|
LogPrint("--- socks4a ls timeout"); |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint (eLogError,"--- socks4a Issue when creating the stream, check the previous warnings for more info."); |
|
|
|
SocksFailed(); |
|
|
|
SocksFailed(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::ConnectionSuccess() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint("--- socks4a connection success"); |
|
|
|
|
|
|
|
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5a 12345"), |
|
|
|
|
|
|
|
boost::bind(&SOCKS4AHandler::SentConnectionSuccess, this, |
|
|
|
|
|
|
|
boost::asio::placeholders::error)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AHandler::SentConnectionSuccess(const boost::system::error_code & ecode) |
|
|
|
void SOCKS4AServer::Start () |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint("--- socks4a making connection"); |
|
|
|
m_Acceptor.listen (); |
|
|
|
m_stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream(*m_ls); |
|
|
|
Accept (); |
|
|
|
m_state = OKAY; |
|
|
|
|
|
|
|
LogPrint("--- socks4a state is ", m_state); |
|
|
|
|
|
|
|
AsyncSockRead(); |
|
|
|
|
|
|
|
AsyncStreamRead(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AServer::Run() |
|
|
|
void SOCKS4AServer::Stop () |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint("--- socks4a run"); |
|
|
|
m_Acceptor.close(); |
|
|
|
m_run = true; |
|
|
|
m_Timer.cancel (); |
|
|
|
while(m_run) { |
|
|
|
ClearConnections (); |
|
|
|
try { |
|
|
|
|
|
|
|
m_ios.run(); |
|
|
|
|
|
|
|
} catch (std::runtime_error & exc) { |
|
|
|
|
|
|
|
LogPrint("--- socks4a exception: ", exc.what()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AServer::Accept() |
|
|
|
void SOCKS4AServer::Accept () |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_new_sock = new boost::asio::ip::tcp::socket(m_ios); |
|
|
|
auto newSocket = new boost::asio::ip::tcp::socket (GetService ()); |
|
|
|
m_acceptor.async_accept(*m_new_sock, |
|
|
|
m_Acceptor.async_accept (*newSocket, std::bind (&SOCKS4AServer::HandleAccept, this, |
|
|
|
boost::bind( |
|
|
|
std::placeholders::_1, newSocket)); |
|
|
|
&SOCKS4AServer::HandleAccept, this, boost::asio::placeholders::error)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AServer::Start() |
|
|
|
void SOCKS4AServer::HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!ecode) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_run = true; |
|
|
|
LogPrint(eLogDebug,"--- socks4a accepted"); |
|
|
|
m_thread = new std::thread(std::bind(&SOCKS4AServer::Run, this)); |
|
|
|
new SOCKS4AHandler(this, socket); |
|
|
|
m_acceptor.listen(); |
|
|
|
|
|
|
|
Accept(); |
|
|
|
Accept(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
void SOCKS4AServer::Stop() |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
m_acceptor.close(); |
|
|
|
LogPrint (eLogError,"--- socks4a Closing socket on accept because: ", ecode.message ()); |
|
|
|
m_run = false; |
|
|
|
delete socket; |
|
|
|
m_ios.stop(); |
|
|
|
|
|
|
|
if (m_thread) { |
|
|
|
|
|
|
|
m_thread->join(); |
|
|
|
|
|
|
|
delete m_thread; |
|
|
|
|
|
|
|
m_thread = nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SOCKS4AServer::HandleAccept(const boost::system::error_code & ecode) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!ecode) { |
|
|
|
|
|
|
|
LogPrint("--- socks4a accepted"); |
|
|
|
|
|
|
|
new SOCKS4AHandler(&m_ios, m_new_sock); |
|
|
|
|
|
|
|
Accept(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|