From ab5c966fdf87c1da22831becae315934234c4771 Mon Sep 17 00:00:00 2001 From: R4SAS Date: Sat, 25 Dec 2021 02:20:01 +0300 Subject: [PATCH] [socks] changes Signed-off-by: R4SAS --- libi2pd_client/ClientContext.cpp | 3 +- libi2pd_client/SOCKS.cpp | 56 ++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/libi2pd_client/ClientContext.cpp b/libi2pd_client/ClientContext.cpp index d0074f62..1f485259 100644 --- a/libi2pd_client/ClientContext.cpp +++ b/libi2pd_client/ClientContext.cpp @@ -634,7 +634,8 @@ namespace client { // socks proxy std::string outproxy = section.second.get("outproxy", ""); - auto tun = std::make_shared(name, address, port, !outproxy.empty(), outproxy, destinationPort, localDestination); + uint32_t outproxyport = section.second.get("outproxyport", 0); + auto tun = std::make_shared(name, address, port, !outproxy.empty(), outproxy, outproxyport, localDestination); clientTunnel = tun; clientEndpoint = tun->GetLocalEndpoint (); } diff --git a/libi2pd_client/SOCKS.cpp b/libi2pd_client/SOCKS.cpp index e0aa5d48..b7ca7a72 100644 --- a/libi2pd_client/SOCKS.cpp +++ b/libi2pd_client/SOCKS.cpp @@ -31,7 +31,6 @@ namespace proxy static const size_t SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE = 8; static const size_t SOCKS_UPSTREAM_SOCKS5_INIT_REPLY_SIZE = 2; - static const size_t SOCKS_UPSTREAM_SOCKS5_CONNECT_REPLY_MIN_SIZE = 10; struct SOCKSDnsAddress { @@ -258,19 +257,19 @@ namespace proxy break; case ADDR_DNS: std::string address(addr.dns.value, addr.dns.size); - if(address.substr(addr.dns.size - 4, 4) == ".i2p") // overwrite if requested address inside I2P + //if(address.substr(addr.dns.size - 4, 4) == ".i2p") // overwrite if requested address inside I2P { m_response[3] = ADDR_IPV4; size += 4; memset(m_response + 4, 0, 6); // six HEX zeros } - else + /*else { - size += (1 + addr.dns.size); /* name length + resolved address */ + size += (1 + addr.dns.size); // name length + resolved address m_response[4] = addr.dns.size; - memcpy(m_response + 5, addr.dns.value, addr.dns.size); + memcpy(m_response + 5, addr.dns.value, addr.dns.size); // 4 - header + 1 - record size htobe16buf(m_response + size - 2, port); - } + }*/ break; } return boost::asio::const_buffers_1(m_response, size); @@ -738,32 +737,43 @@ namespace proxy void SOCKSHandler::HandleUpstreamData(uint8_t * dataptr, std::size_t len) { - if (m_state == UPSTREAM_HANDSHAKE) { + if (m_state == UPSTREAM_HANDSHAKE) + { m_upstream_response_len += len; // handle handshake data - if (m_upstream_response_len == SOCKS_UPSTREAM_SOCKS5_INIT_REPLY_SIZE) { - if (m_upstream_response[0] == '\x05' && m_upstream_response[1] != AUTH_UNACCEPTABLE) { - LogPrint(eLogInfo, "SOCKS: upstream proxy: success greeting"); + if (m_upstream_response_len == SOCKS_UPSTREAM_SOCKS5_INIT_REPLY_SIZE) + { + if (m_upstream_response[0] == '\x05' && m_upstream_response[1] != AUTH_UNACCEPTABLE) + { + LogPrint(eLogInfo, "SOCKS: upstream SOCKS5 proxy: success greeting"); SendUpstreamRequest(m_socksv, false); - } else { - LogPrint(eLogError, "SOCKS: upstream proxy failure: no acceptable methods were offered"); + } + else + { + LogPrint(eLogError, "SOCKS: upstream SOCKS5 proxy failure: no acceptable methods were offered"); SocksRequestFailed(SOCKS5_GEN_FAIL); } } - else if (m_upstream_response[0] == '\x05' && m_upstream_response_len >= SOCKS_UPSTREAM_SOCKS5_CONNECT_REPLY_MIN_SIZE) + else if (m_upstream_response[0] == '\x05') { - uint8_t resp = m_upstream_response[1]; - if (resp == SOCKS5_OK) { + if (m_upstream_response[1] == SOCKS5_OK) + { + LogPrint(eLogInfo, "SOCKS: upstream SOCKS5 proxy: successully established"); SocksUpstreamSuccess(); - } else { - LogPrint(eLogError, "SOCKS: upstream proxy failure: ", (int) resp); + } + else + { + LogPrint(eLogError, "SOCKS: upstream proxy failure: ", (int) m_upstream_response[1]); SocksRequestFailed(SOCKS5_GEN_FAIL); } } - else if (m_upstream_response_len < SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) { + else if (m_upstream_response_len < SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) + { // too small, continue reading AsyncUpstreamSockRead(); - } else if (len == SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) { + } + else if (len == SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) + { // just right uint8_t resp = m_upstream_response[1]; if (resp == SOCKS4_OK) { @@ -775,11 +785,15 @@ namespace proxy // TODO: runtime error? SocksRequestFailed(SOCKS5_GEN_FAIL); } - } else { + } + else + { // too big SocksRequestFailed(SOCKS5_GEN_FAIL); } - } else { + } + else + { // invalid state LogPrint(eLogError, "SOCKS: Invalid state reading from upstream: ", (int) m_state); }