From 09fb04545f7bf4acaa93c58cf85f35c05a1dfea3 Mon Sep 17 00:00:00 2001 From: LyokoJeremie Date: Mon, 14 Aug 2023 20:46:36 +0800 Subject: [PATCH] fix do_handshake_client_write --- src/ProxyHandshakeUtils/Socks5ServerImpl.cpp | 57 +++++++++++++++++--- src/ProxyHandshakeUtils/Socks5ServerImpl.h | 2 + 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/ProxyHandshakeUtils/Socks5ServerImpl.cpp b/src/ProxyHandshakeUtils/Socks5ServerImpl.cpp index d1d4ebe..3d9bc39 100644 --- a/src/ProxyHandshakeUtils/Socks5ServerImpl.cpp +++ b/src/ProxyHandshakeUtils/Socks5ServerImpl.cpp @@ -65,7 +65,8 @@ void Socks5ServerImpl::do_analysis_client_first_socks5_header() { return; } else { // do socks5 handshake with client (downside) - do_handshake_client_read(); + BOOST_LOG_S5B(trace) << "do socks5 handshake with client (downside)"; + do_handshake_client_write(); return; } } else if (d[0] == 0x05 && d[1] > 0) { @@ -104,11 +105,13 @@ void Socks5ServerImpl::do_analysis_client_first_socks5_header() { // client support user/pwd auth method if (ptr->authClientManager->needAuth()) { // do socks5 auth with client (downside) + BOOST_LOG_S5B(trace) << "do socks5 auth with client (downside)"; do_auth_client_write(); return; } else { // do socks5 handshake with client (downside) - do_handshake_client_read(); + BOOST_LOG_S5B(trace) << "do socks5 handshake with client (downside)"; + do_handshake_client_write(); return; } } else { @@ -154,11 +157,11 @@ void Socks5ServerImpl::do_auth_client_write() { const boost::system::error_code &ec, std::size_t bytes_transferred_) { if (ec) { - return fail(ec, "do_auth_client_error"); + return fail(ec, "do_auth_client_write"); } if (bytes_transferred_ != data_send->size()) { std::stringstream ss; - ss << "do_auth_client_error with bytes_transferred_:" + ss << "do_auth_client_write with bytes_transferred_:" << bytes_transferred_ << " but data_send->size():" << data_send->size(); return fail(ec, ss.str()); } @@ -288,11 +291,11 @@ void Socks5ServerImpl::do_auth_client_ok() { const boost::system::error_code &ec, std::size_t bytes_transferred_) { if (ec) { - return fail(ec, "do_auth_client_error"); + return fail(ec, "do_auth_client_ok"); } if (bytes_transferred_ != data_send->size()) { std::stringstream ss; - ss << "do_auth_client_error with bytes_transferred_:" + ss << "do_auth_client_ok with bytes_transferred_:" << bytes_transferred_ << " but data_send->size():" << data_send->size(); return fail(ec, ss.str()); } @@ -351,6 +354,48 @@ void Socks5ServerImpl::do_auth_client_error() { } } +void Socks5ServerImpl::do_handshake_client_write() { + // https://datatracker.ietf.org/doc/html/rfc1928 + // server auth none + // +----+---------+ + // |VER | METHOD | + // +----+---------+ + // | 1 | 1(0x02) | + // +----+---------+ + // + // 0x00 means no auth + + // do_downstream_write + auto ptr = parents.lock(); + if (ptr) { + + auto data_send = std::make_shared( + "\x05\x00", 2 + ); + + boost::asio::async_write( + ptr->downstream_socket_, + boost::asio::buffer(*data_send), + [this, self = shared_from_this(), data_send, ptr]( + const boost::system::error_code &ec, + std::size_t bytes_transferred_) { + if (ec) { + return fail(ec, "do_handshake_client_write"); + } + if (bytes_transferred_ != data_send->size()) { + std::stringstream ss; + ss << "do_handshake_client_write with bytes_transferred_:" + << bytes_transferred_ << " but data_send->size():" << data_send->size(); + return fail(ec, ss.str()); + } + + do_handshake_client_read(); + } + ); + } else { + badParentPtr(); + } +} void Socks5ServerImpl::do_handshake_client_read() { // do_downstream_read auto ptr = parents.lock(); diff --git a/src/ProxyHandshakeUtils/Socks5ServerImpl.h b/src/ProxyHandshakeUtils/Socks5ServerImpl.h index ef25de5..1569d59 100644 --- a/src/ProxyHandshakeUtils/Socks5ServerImpl.h +++ b/src/ProxyHandshakeUtils/Socks5ServerImpl.h @@ -68,6 +68,8 @@ class Socks5ServerImpl : public std::enable_shared_from_this { void do_auth_client_error(); + void do_handshake_client_write(); + void do_handshake_client_read(); void do_ready_to_send_last_ok_package(const std::shared_ptr &ptr);