Browse Source

Merge pull request #131 from klondi/httpproxy

Get SOCKS proxy back in working state
pull/132/head
orignal 10 years ago
parent
commit
b58d58ef29
  1. 5
      I2PTunnel.cpp
  2. 1
      I2PTunnel.h
  3. 326
      SOCKS.cpp
  4. 64
      SOCKS.h

5
I2PTunnel.cpp

@ -73,7 +73,7 @@ namespace client
void I2PTunnelConnection::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred) void I2PTunnelConnection::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{ {
if (ecode) if (ecode)
{ {
LogPrint ("I2PTunnel read error: ", ecode.message ()); LogPrint ("I2PTunnel read error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate (); Terminate ();
@ -169,8 +169,7 @@ namespace client
I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination): I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination):
I2PTunnel (localDestination), I2PTunnel (localDestination),
m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
m_Timer (GetService ()), m_Destination (destination), m_DestinationIdentHash (nullptr), m_Timer (GetService ()), m_Destination (destination), m_DestinationIdentHash (nullptr)
m_RemoteLeaseSet (nullptr)
{ {
} }

1
I2PTunnel.h

@ -101,7 +101,6 @@ namespace client
boost::asio::deadline_timer m_Timer; boost::asio::deadline_timer m_Timer;
std::string m_Destination; std::string m_Destination;
const i2p::data::IdentHash * m_DestinationIdentHash; const i2p::data::IdentHash * m_DestinationIdentHash;
const i2p::data::LeaseSet * m_RemoteLeaseSet;
}; };
class I2PServerTunnel: public I2PTunnel class I2PServerTunnel: public I2PTunnel

326
SOCKS.cpp

@ -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
{ {
@ -14,55 +12,38 @@ namespace proxy
{ {
const uint8_t socks_leaseset_timeout = 10; const uint8_t socks_leaseset_timeout = 10;
const uint8_t socks_timeout = 60; const uint8_t socks_timeout = 60;
void SOCKS4AHandler::AsyncSockRead() void SOCKS4AHandler::AsyncSockRead()
{ {
LogPrint("--- socks4a async sock read"); LogPrint("--- socks4a async sock read");
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 { LogPrint("--- socks4a state?? ", m_state);
m_sock->async_receive(boost::asio::buffer(m_sock_buff, socks_buffer_size),
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() void SOCKS4AHandler::Terminate() {
{ CloseStream();
CloseSock();
LogPrint("--- socks4a async stream read"); delete this; // HACK: ew
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() {
CloseStream();
CloseSock();
delete this; // 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()
{ {
if (m_sock) { if (m_sock) {
@ -73,211 +54,172 @@ namespace proxy
} }
} }
void SOCKS4AHandler::CloseStream() void SOCKS4AHandler::CloseStream()
{ {
if (m_stream) { if (m_stream) {
LogPrint("--- socks4a close stream"); LogPrint("--- socks4a close stream");
m_stream.reset (); m_stream.reset ();
} }
} }
const size_t socks_hostname_size = 1024; const size_t socks_hostname_size = 1024;
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 identbuff[socks_ident_size];
std::memset(hostbuff, 0, sizeof(hostbuff));
std::memset(identbuff, 0, sizeof(hostbuff));
std::string dest;
char hostbuff[socks_hostname_size]; uint16_t port = 0;
char identbuff[socks_ident_size]; uint32_t address = 0;
std::memset(hostbuff, 0, sizeof(hostbuff)); uint16_t idx1 = 0;
std::memset(identbuff, 0, sizeof(hostbuff)); uint16_t idx2 = 0;
std::string dest;
// get port
uint16_t port = 0;
uint16_t idx1 = 0;
uint16_t idx2 = 0;
LogPrint("--- socks4a state initial ", len);
// check valid request LogPrint(eLogDebug,"--- socks4a state initial ", len);
if( m_sock_buff[0] != 4 || m_sock_buff[1] != 1 || m_sock_buff[len-1] ) {
LogPrint("--- socks4a rejected invalid");
SocksFailed();
return;
}
// get port // check valid request
port = m_sock_buff[3] | m_sock_buff[2] << 8; if( m_sock_buff[0] != 4 || m_sock_buff[1] != 1 || m_sock_buff[len-1] ) {
LogPrint(eLogError,"--- socks4a rejected invalid");
SocksFailed();
return;
}
// read ident // get port
do { port = bufbe16toh(m_sock_buff + 2);
LogPrint("--- socks4a ", (int) m_sock_buff[9+idx1]);
identbuff[idx1] = m_sock_buff[8+idx1]; // get address
} while( identbuff[idx1++] && idx1 < socks_ident_size ); address = bufbe32toh(m_sock_buff + 4);
LogPrint("--- socks4a ident ", identbuff); // check valid request
// read hostname if( address == 0 || address > 255 ) {
do { LogPrint(eLogError,"--- socks4a rejected because it's actually socks4");
hostbuff[idx2] = m_sock_buff[8+idx1+idx2]; SocksFailed();
} while( hostbuff[idx2++] && idx2 < socks_hostname_size ); return;
}
LogPrint("--- socks4a requested ", hostbuff, ":" , port); // 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 );
dest = std::string(hostbuff); LogPrint(eLogInfo,"--- socks4a ident ", identbuff);
if(dest.find(".b32.i2p") == std::string::npos) { // read hostname
LogPrint("--- socks4a invalid hostname: ", dest); do {
SocksFailed(); hostbuff[idx2] = m_sock_buff[8+idx1+idx2];
return; } while( hostbuff[idx2++] && idx2 < socks_hostname_size );
}
LogPrint(eLogInfo,"--- socks4a requested ", hostbuff, ":" , port);
if ( i2p::data::Base32ToByteStream(hostbuff, destb32_len, (uint8_t *) m_dest, 32) != 32 ) {
LogPrint("--- sock4a invalid b32: ", dest); dest = std::string(hostbuff);
} if(dest.find(".i2p") == std::string::npos) {
LogPrint("--- socks4a invalid hostname: ", dest);
LogPrint("--- sock4a find lease set"); SocksFailed();
m_ls = i2p::data::netdb.FindLeaseSet(m_dest); return;
if (!m_ls || m_ls->HasNonExpiredLeases()) {
i2p::client::context.GetSharedLocalDestination ()->RequestDestination (m_dest);
m_ls_timer.expires_from_now(boost::posix_time::seconds(socks_leaseset_timeout));
m_ls_timer.async_wait(boost::bind(&SOCKS4AHandler::LeaseSetTimeout, this, boost::asio::placeholders::error));
} else {
ConnectionSuccess();
}
} else {
LogPrint("--- socks4a state?? ", m_state);
} }
m_parent->GetLocalDestination ()->CreateStream (
std::bind (&SOCKS4AHandler::HandleStreamRequestComplete,
this, std::placeholders::_1), dest);
} }
void SOCKS4AHandler::HandleStreamRecv(const boost::system::error_code & ecode, std::size_t len) void SOCKS4AHandler::ConnectionSuccess()
{
LogPrint(eLogInfo,"--- socks4a connection success");
//TODO: send the right response
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5a 12345"),
std::bind(&SOCKS4AHandler::SentConnectionSuccess, this,
std::placeholders::_1));
}
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) { LogPrint("--- socks4a SockWrote error: ",ecode); }
else { AsyncSockRead(); }
}
void SOCKS4AHandler::StreamWrote(const boost::system::error_code & ecode)
{ {
if (!ecode) {
LogPrint("--- socks4a stream wrote"); LogPrint (eLogInfo,"--- socks4a New I2PTunnel connection");
if(ecode) { LogPrint("--- socks4a StreamWrote error: ",ecode); } auto connection = std::make_shared<i2p::client::I2PTunnelConnection>((i2p::client::I2PTunnel *)m_parent, m_sock, m_stream);
else { AsyncStreamRead(); } m_parent->AddConnection (connection);
connection->I2PConnect ();
}
else
{
LogPrint (eLogError,"--- socks4a Closing socket after sending success because: ", ecode.message ());
Terminate();
}
} }
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_run = true;
while(m_run) {
try {
m_ios.run();
} catch (std::runtime_error & exc) {
LogPrint("--- socks4a exception: ", exc.what());
}
}
}
void SOCKS4AServer::Accept()
{ {
m_new_sock = new boost::asio::ip::tcp::socket(m_ios); m_Acceptor.close();
m_acceptor.async_accept(*m_new_sock, m_Timer.cancel ();
boost::bind( ClearConnections ();
&SOCKS4AServer::HandleAccept, this, boost::asio::placeholders::error));
} }
void SOCKS4AServer::Start() void SOCKS4AServer::Accept ()
{ {
m_run = true; auto newSocket = new boost::asio::ip::tcp::socket (GetService ());
m_thread = new std::thread(std::bind(&SOCKS4AServer::Run, this)); m_Acceptor.async_accept (*newSocket, std::bind (&SOCKS4AServer::HandleAccept, this,
m_acceptor.listen(); std::placeholders::_1, newSocket));
Accept(); }
}
void SOCKS4AServer::HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket)
void SOCKS4AServer::Stop()
{
m_acceptor.close();
m_run = false;
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) { if (!ecode)
LogPrint("--- socks4a accepted"); {
new SOCKS4AHandler(&m_ios, m_new_sock); LogPrint(eLogDebug,"--- socks4a accepted");
new SOCKS4AHandler(this, socket);
Accept(); Accept();
} }
else
{
LogPrint (eLogError,"--- socks4a Closing socket on accept because: ", ecode.message ());
delete socket;
}
} }
} }
} }

64
SOCKS.h

@ -1,13 +1,11 @@
#ifndef SOCKS4A_H__ #ifndef SOCKS4A_H__
#define SOCKS4A_H__ #define SOCKS4A_H__
#include <thread>
#include <boost/asio.hpp>
#include <vector>
#include <mutex>
#include <memory> #include <memory>
#include <boost/asio.hpp>
#include "Identity.h" #include "Identity.h"
#include "Streaming.h" #include "Streaming.h"
#include "I2PTunnel.h"
namespace i2p namespace i2p
{ {
@ -16,6 +14,7 @@ namespace proxy
const size_t socks_buffer_size = 8192; const size_t socks_buffer_size = 8192;
class SOCKS4AServer;
class SOCKS4AHandler { class SOCKS4AHandler {
private: private:
@ -27,69 +26,54 @@ namespace proxy
void GotClientRequest(boost::system::error_code & ecode, std::string & host, uint16_t port); void GotClientRequest(boost::system::error_code & ecode, std::string & host, uint16_t port);
void HandleSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered); void HandleSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
void HandleSockForward(const boost::system::error_code & ecode, std::size_t bytes_transfered);
void HandleStreamRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
void Terminate(); void Terminate();
void CloseSock(); void CloseSock();
void CloseStream(); void CloseStream();
void AsyncSockRead(); void AsyncSockRead();
void AsyncStreamRead();
void SocksFailed(); void SocksFailed();
void LeaseSetTimeout(const boost::system::error_code & ecode); void SentSocksFailed(const boost::system::error_code & ecode);
void StreamWrote(const boost::system::error_code & ecode);
void SockWrote(const boost::system::error_code & ecode);
void SentConnectionSuccess(const boost::system::error_code & ecode); void SentConnectionSuccess(const boost::system::error_code & ecode);
void ConnectionSuccess(); void ConnectionSuccess();
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
uint8_t m_sock_buff[socks_buffer_size]; uint8_t m_sock_buff[socks_buffer_size];
uint8_t m_stream_buff[socks_buffer_size];
boost::asio::io_service * m_ios; SOCKS4AServer * m_parent;
boost::asio::ip::tcp::socket * m_sock; boost::asio::ip::tcp::socket * m_sock;
boost::asio::deadline_timer m_ls_timer;
std::shared_ptr<i2p::stream::Stream> m_stream; std::shared_ptr<i2p::stream::Stream> m_stream;
i2p::data::LeaseSet * m_ls;
i2p::data::IdentHash m_dest;
state m_state; state m_state;
public: public:
SOCKS4AHandler(boost::asio::io_service * ios, boost::asio::ip::tcp::socket * sock) : SOCKS4AHandler(SOCKS4AServer * parent, boost::asio::ip::tcp::socket * sock) :
m_ios(ios), m_sock(sock), m_ls_timer(*ios), m_parent(parent), m_sock(sock), m_stream(nullptr), m_state(INITIAL)
m_stream(nullptr), m_ls(nullptr), m_state(INITIAL) { AsyncSockRead(); } { AsyncSockRead(); }
~SOCKS4AHandler() { CloseSock(); CloseStream(); } ~SOCKS4AHandler() { CloseSock(); CloseStream(); }
bool isComplete() { return m_state == END; } bool isComplete() { return m_state == END; }
}; };
class SOCKS4AServer { class SOCKS4AServer: public i2p::client::I2PTunnel
{
public: public:
SOCKS4AServer(int port) : m_run(false), SOCKS4AServer(int port) : I2PTunnel(nullptr),
m_thread(nullptr), m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
m_work(m_ios), m_Timer (GetService ()) {};
m_acceptor(m_ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)),
m_new_sock(nullptr) { }
~SOCKS4AServer() { Stop(); } ~SOCKS4AServer() { Stop(); }
void Start();
void Stop(); void Start ();
void Stop ();
boost::asio::io_service& GetService () { return m_ios; };
private: private:
void Run();
void Accept(); void Accept();
void HandleAccept(const boost::system::error_code& ecode); void HandleAccept(const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket);
bool m_run;
std::thread * m_thread;
boost::asio::io_service m_ios;
boost::asio::io_service::work m_work;
boost::asio::ip::tcp::acceptor m_acceptor;
boost::asio::ip::tcp::socket * m_new_sock;
}; private:
boost::asio::ip::tcp::acceptor m_Acceptor;
boost::asio::deadline_timer m_Timer;
};
typedef SOCKS4AServer SOCKSProxy; typedef SOCKS4AServer SOCKSProxy;
} }

Loading…
Cancel
Save