From e832002795062649d9bb4ecc07c46595d637cca9 Mon Sep 17 00:00:00 2001 From: jeff Date: Mon, 14 Jul 2014 12:40:06 -0400 Subject: [PATCH] add socks proxy --- Daemon.cpp | 21 ++-- Makefile | 2 +- SOCKS.cpp | 282 +++++++++++++++++++++++++++++++++++++++++++++++++++++ SOCKS.h | 98 +++++++++++++++++++ 4 files changed, 395 insertions(+), 8 deletions(-) create mode 100644 SOCKS.cpp create mode 100644 SOCKS.h diff --git a/Daemon.cpp b/Daemon.cpp index b3d74de6..2cc8b9e7 100644 --- a/Daemon.cpp +++ b/Daemon.cpp @@ -15,6 +15,7 @@ #include "Streaming.h" #include "HTTPServer.h" #include "HTTPProxy.h" +#include "SOCKS.h" namespace i2p { @@ -23,14 +24,16 @@ namespace i2p class Daemon_Singleton::Daemon_Singleton_Private { public: - Daemon_Singleton_Private() : httpServer(nullptr), httpProxy(nullptr) { }; + Daemon_Singleton_Private() : httpServer(nullptr), httpProxy(nullptr), socksProxy(nullptr) { }; ~Daemon_Singleton_Private() { delete httpServer; delete httpProxy; + delete socksProxy; }; i2p::util::HTTPServer *httpServer; i2p::proxy::HTTPProxy *httpProxy; + i2p::proxy::SOCKSProxy *socksProxy; }; Daemon_Singleton::Daemon_Singleton() : running(1), d(*new Daemon_Singleton_Private()) {}; @@ -83,7 +86,7 @@ namespace i2p d.httpServer = new i2p::util::HTTPServer(i2p::util::config::GetArg("-httpport", 7070)); d.httpServer->Start(); - LogPrint("HTTPServer started"); + LogPrint("HTTP Server started"); i2p::data::netdb.Start(); LogPrint("NetDB started"); @@ -98,8 +101,10 @@ namespace i2p d.httpProxy = new i2p::proxy::HTTPProxy(i2p::util::config::GetArg("-httpproxyport", 4446)); d.httpProxy->Start(); - LogPrint("Proxy started"); - + LogPrint("HTTP Proxy started"); + d.socksProxy = new i2p::proxy::SOCKSProxy(i2p::util::config::GetArg("-socksproxyport", 4447)); + d.socksProxy->Start(); + LogPrint("SOCKS Proxy Started"); return true; } @@ -108,7 +113,9 @@ namespace i2p LogPrint("Shutdown started."); d.httpProxy->Stop(); - LogPrint("HTTPProxy stoped"); + LogPrint("HTTP Proxy stoped"); + d.socksProxy->Stop(); + LogPrint("SOCKS Proxy stoped"); i2p::stream::StopStreaming(); LogPrint("Streaming stoped"); i2p::garlic::routing.Stop(); @@ -120,9 +127,9 @@ namespace i2p i2p::data::netdb.Stop(); LogPrint("NetDB stoped"); d.httpServer->Stop(); - LogPrint("HTTPServer stoped"); + LogPrint("HTTP Server stoped"); StopLog (); - + delete d.socksProxy; d.socksProxy = nullptr; delete d.httpProxy; d.httpProxy = nullptr; delete d.httpServer; d.httpServer = nullptr; diff --git a/Makefile b/Makefile index 149ade28..5c04f3fd 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ OBJECTS = obj/CryptoConst.o obj/base64.o obj/NTCPSession.o obj/RouterInfo.o obj/ obj/TunnelGateway.o obj/TransitTunnel.o obj/I2NPProtocol.o obj/Log.o obj/Garlic.o \ obj/HTTPServer.o obj/Streaming.o obj/Identity.o obj/SSU.o obj/util.o obj/Reseed.o \ obj/UPnP.o obj/TunnelPool.o obj/HTTPProxy.o obj/AddressBook.o obj/Daemon.o \ - obj/DaemonLinux.o obj/SSUData.o obj/i2p.o obj/aes.o + obj/DaemonLinux.o obj/SSUData.o obj/i2p.o obj/aes.o obj/SOCKS.o INCFLAGS = LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem -lboost_regex -lboost_program_options -lpthread LIBS = diff --git a/SOCKS.cpp b/SOCKS.cpp new file mode 100644 index 00000000..7243a8d8 --- /dev/null +++ b/SOCKS.cpp @@ -0,0 +1,282 @@ +#include "SOCKS.h" +#include "Identity.h" +#include "NetDb.h" +#include +#include +#include +#include + +namespace i2p +{ +namespace proxy +{ + constexpr uint8_t socks_leaseset_timeout = 10; + constexpr uint8_t socks_timeout = 60; + + void SOCKS4AHandler::AsyncSockRead() + { + LogPrint("--- socks4a async sock read"); + if(m_sock) { + if (m_state == INITIAL) { + m_sock->async_receive(boost::asio::buffer(m_sock_buff, socks_buffer_size), + boost::bind(&SOCKS4AHandler::HandleSockRecv, this, + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred)); + } else { + 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 { + 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() { + CloseStream(); + CloseSock(); + delete this; // ew + } + + void SOCKS4AHandler::SocksFailed() + { + LogPrint("--- socks4a failed"); + m_sock->send(boost::asio::buffer("\x00\x5b 12345")); + Terminate(); + } + + void SOCKS4AHandler::CloseSock() + { + if (m_sock) { + LogPrint("--- socks4a close sock"); + m_sock->close(); + delete m_sock; + m_sock = nullptr; + } + } + + void SOCKS4AHandler::CloseStream() + { + if (m_stream) { + LogPrint("--- socks4a close stream"); + delete m_stream; + m_stream = nullptr; + } + } + + constexpr size_t socks_hostname_size = 1024; + constexpr size_t socks_ident_size = 1024; + constexpr 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, 1); + } + + void SOCKS4AHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len) + { + LogPrint("--- socks4a sock recv: ", len); + + if(ecode) { + LogPrint(" --- sock recv got error: ", ecode); + Terminate(); + 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; + // get port + uint16_t port = 0; + uint16_t idx1 = 0; + uint16_t idx2 = 0; + + LogPrint("--- socks4a state initial ", len); + + // check valid request + if( m_sock_buff[0] != 4 || m_sock_buff[1] != 1 || m_sock_buff[len-1] ) { + LogPrint("--- socks4a rejected invalid"); + SocksFailed(); + return; + } + + // get port + port = m_sock_buff[3] | m_sock_buff[2] << 8; + + // read ident + do { + LogPrint("--- socks4a ", (int) m_sock_buff[9+idx1]); + identbuff[idx1] = m_sock_buff[8+idx1]; + } while( identbuff[idx1++] && idx1 < socks_ident_size ); + + LogPrint("--- socks4a ident ", identbuff); + // read hostname + do { + hostbuff[idx2] = m_sock_buff[8+idx1+idx2]; + } while( hostbuff[idx2++] && idx2 < socks_hostname_size ); + + LogPrint("--- socks4a requested ", hostbuff, ":" , port); + + dest = std::string(hostbuff); + if(dest.find(".b32.i2p") == std::string::npos) { + LogPrint("--- socks4a invalid hostname: ", dest); + SocksFailed(); + return; + } + + if ( i2p::data::Base32ToByteStream(hostbuff, destb32_len, (uint8_t *) m_dest, 32) != 32 ) { + LogPrint("--- sock4a invalid b32: ", dest); + } + + LogPrint("--- sock4a find lease set"); + m_ls = i2p::data::netdb.FindLeaseSet(m_dest); + if (!m_ls || m_ls->HasNonExpiredLeases()) { + i2p::data::netdb.Subscribe(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); + } + } + + void SOCKS4AHandler::HandleStreamRecv(const boost::system::error_code & ecode, std::size_t len) + { + if(ecode) { LogPrint("--- socks4a stream recv error: ", ecode); m_state = END; } + switch(m_state) { + case INITIAL: + case END: + 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) + { + LogPrint("--- socks4a sock wrote"); + if(ecode) { LogPrint("--- socks4a SockWrote error: ",ecode); } + else { AsyncSockRead(); } + } + + void SOCKS4AHandler::StreamWrote(const boost::system::error_code & ecode) + { + + LogPrint("--- socks4a stream wrote"); + if(ecode) { LogPrint("--- socks4a StreamWrote error: ",ecode); } + else { AsyncStreamRead(); } + } + + void SOCKS4AHandler::LeaseSetTimeout(const boost::system::error_code & ecode) + { + m_ls = i2p::data::netdb.FindLeaseSet(m_dest); + if(m_ls) { + ConnectionSuccess(); + } else { + LogPrint("--- socks4a ls timeout"); + 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) + { + LogPrint("--- socks4a making connection"); + m_stream = i2p::stream::CreateStream(*m_ls); + m_state = OKAY; + LogPrint("--- socks4a state is ", m_state); + AsyncSockRead(); + AsyncStreamRead(); + } + + void SOCKS4AServer::Run() + { + 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.async_accept(*m_new_sock, + boost::bind( + &SOCKS4AServer::HandleAccept, this, boost::asio::placeholders::error)); + } + + void SOCKS4AServer::Start() + { + m_run = true; + m_thread = new std::thread(std::bind(&SOCKS4AServer::Run, this)); + m_acceptor.listen(); + Accept(); + } + + 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) { + LogPrint("--- socks4a accepted"); + new SOCKS4AHandler(&m_ios, m_new_sock); + Accept(); + } + } +} +} diff --git a/SOCKS.h b/SOCKS.h new file mode 100644 index 00000000..af9e4788 --- /dev/null +++ b/SOCKS.h @@ -0,0 +1,98 @@ +#ifndef SOCKS4A_H__ +#define SOCKS4A_H__ + +#include +#include +#include +#include + +#include "Identity.h" +#include "Streaming.h" + +namespace i2p +{ +namespace proxy +{ + + constexpr size_t socks_buffer_size = 8192; + + class SOCKS4AHandler { + + private: + enum state { + INITIAL, + OKAY, + END + }; + + 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 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 CloseSock(); + void CloseStream(); + void AsyncSockRead(); + void AsyncStreamRead(); + void SocksFailed(); + void LeaseSetTimeout(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 ConnectionSuccess(); + + uint8_t m_sock_buff[socks_buffer_size]; + uint8_t m_stream_buff[socks_buffer_size]; + + boost::asio::io_service * m_ios; + boost::asio::ip::tcp::socket * m_sock; + boost::asio::deadline_timer m_ls_timer; + i2p::stream::Stream * m_stream; + i2p::data::LeaseSet * m_ls; + i2p::data::IdentHash m_dest; + state m_state; + + + public: + SOCKS4AHandler(boost::asio::io_service * ios, boost::asio::ip::tcp::socket * sock) : + m_ios(ios), m_sock(sock), m_ls_timer(*ios), + m_stream(nullptr), m_ls(nullptr), m_state(INITIAL) { AsyncSockRead(); } + + ~SOCKS4AHandler() { CloseSock(); CloseStream(); } + bool isComplete() { return m_state == END; } + }; + + class SOCKS4AServer { + public: + SOCKS4AServer(int port) : m_run(false), + m_thread(nullptr), + m_work(m_ios), + m_acceptor(m_ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)), + m_new_sock(nullptr) { } + ~SOCKS4AServer() { Stop(); } + void Start(); + void Stop(); + + + private: + + void Run(); + void Accept(); + void HandleAccept(const boost::system::error_code& ecode); + + 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; + + + }; + + typedef SOCKS4AServer SOCKSProxy; +} +} + + +#endif