From a93a54590f76b470949cd85aa4719b9a79cb343f Mon Sep 17 00:00:00 2001 From: R4SAS Date: Tue, 4 Jan 2022 22:26:56 +0300 Subject: [PATCH] [socks] changes Signed-off-by: R4SAS --- libi2pd_client/HTTPProxy.cpp | 10 +++++++--- libi2pd_client/I2PService.cpp | 8 ++++---- libi2pd_client/SOCKS.cpp | 27 +++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/libi2pd_client/HTTPProxy.cpp b/libi2pd_client/HTTPProxy.cpp index 189b68f3..f61a652b 100644 --- a/libi2pd_client/HTTPProxy.cpp +++ b/libi2pd_client/HTTPProxy.cpp @@ -483,8 +483,10 @@ namespace proxy { void HTTPReqHandler::HandleUpstreamSocksProxyConnect(const boost::system::error_code & ec) { - if(!ec) { - if(m_RequestURL.host.size() > 255) { + if(!ec) + { + if(m_RequestURL.host.size() > 255) + { GenericProxyError(tr("hostname too long"), m_RequestURL.host); return; } @@ -512,7 +514,9 @@ namespace proxy { reqsize += host.size(); m_socks_buf[++reqsize] = 0; boost::asio::async_write(*m_proxysock, boost::asio::buffer(m_socks_buf, reqsize), boost::asio::transfer_all(), std::bind(&HTTPReqHandler::HandleSocksProxySendHandshake, this, std::placeholders::_1, std::placeholders::_2)); - } else GenericProxyError(tr("cannot connect to upstream socks proxy"), ec.message()); + } + else + GenericProxyError(tr("cannot connect to upstream socks proxy"), ec.message()); } void HTTPReqHandler::HandleSocksProxySendHandshake(const boost::system::error_code & ec, std::size_t bytes_transferred) diff --git a/libi2pd_client/I2PService.cpp b/libi2pd_client/I2PService.cpp index 2a6fe44e..9772b74f 100644 --- a/libi2pd_client/I2PService.cpp +++ b/libi2pd_client/I2PService.cpp @@ -243,7 +243,7 @@ namespace client LogPrint(eLogDebug, "TCPIPPipe: Downstream: ", (int) bytes_transfered, " bytes received"); if (ecode) { - LogPrint(eLogError, "TCPIPPipe: Downstream read error:" , ecode.message()); + LogPrint(eLogError, "TCPIPPipe: Downstream read error: " , ecode.message()); if (ecode != boost::asio::error::operation_aborted) Terminate(); } else { @@ -256,7 +256,7 @@ namespace client void TCPIPPipe::HandleDownstreamWrite(const boost::system::error_code & ecode) { if (ecode) { - LogPrint(eLogError, "TCPIPPipe: Downstream write error:" , ecode.message()); + LogPrint(eLogError, "TCPIPPipe: Downstream write error: " , ecode.message()); if (ecode != boost::asio::error::operation_aborted) Terminate(); } @@ -267,7 +267,7 @@ namespace client void TCPIPPipe::HandleUpstreamWrite(const boost::system::error_code & ecode) { if (ecode) { - LogPrint(eLogError, "TCPIPPipe: Upstream write error:" , ecode.message()); + LogPrint(eLogError, "TCPIPPipe: Upstream write error: " , ecode.message()); if (ecode != boost::asio::error::operation_aborted) Terminate(); } @@ -280,7 +280,7 @@ namespace client LogPrint(eLogDebug, "TCPIPPipe: Upstream ", (int)bytes_transfered, " bytes received"); if (ecode) { - LogPrint(eLogError, "TCPIPPipe: Upstream read error:" , ecode.message()); + LogPrint(eLogError, "TCPIPPipe: Upstream read error: " , ecode.message()); if (ecode != boost::asio::error::operation_aborted) Terminate(); } else { diff --git a/libi2pd_client/SOCKS.cpp b/libi2pd_client/SOCKS.cpp index b7ca7a72..cd47b8e3 100644 --- a/libi2pd_client/SOCKS.cpp +++ b/libi2pd_client/SOCKS.cpp @@ -290,7 +290,30 @@ namespace proxy } else { - return GenerateSOCKS5Response(SOCKS5_GEN_FAIL, m_addrtype, m_address, m_port); // dirty hack, SOCKS5_GEN_FAIL == 1, that's a "establish a TCP/IP stream connection" command + upstreamRequestSize = 6; // header + port + m_upstream_request[0] = '\x05'; // version + m_upstream_request[1] = '\x01'; // request tcp socket opening + m_upstream_request[2] = '\x00'; // reserved + m_upstream_request[3] = m_addrtype; // address type + switch (m_addrtype) + { + case ADDR_IPV4: + upstreamRequestSize += 4; + htobe32buf(m_upstream_request + 4, m_address.ip); + htobe16buf(m_upstream_request + upstreamRequestSize - 2, m_port); + break; + case ADDR_IPV6: + upstreamRequestSize += 16; + memcpy(m_upstream_request + 4, m_address.ipv6, 16); + htobe16buf(m_upstream_request + upstreamRequestSize - 2, m_port); + break; + case ADDR_DNS: + upstreamRequestSize += (1 + m_address.dns.size); // name length + resolved address + m_upstream_request[4] = m_address.dns.size; + memcpy(m_upstream_request + 5, m_address.dns.value, m_address.dns.size); // 4 - header + 1 - record size + htobe16buf(m_upstream_request + upstreamRequestSize - 2, m_port); + break; + } } } else if (version == 4) // SOCKS4a @@ -745,7 +768,7 @@ namespace proxy { if (m_upstream_response[0] == '\x05' && m_upstream_response[1] != AUTH_UNACCEPTABLE) { - LogPrint(eLogInfo, "SOCKS: upstream SOCKS5 proxy: success greeting"); + LogPrint(eLogInfo, "SOCKS: upstream SOCKS5 proxy: success greeting, sending connection request"); SendUpstreamRequest(m_socksv, false); } else