From bf90f169102ab0c7d55e091ee6502fef3fc28e3e Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 3 Oct 2026 18:23:59 -0400 Subject: [PATCH] use common TunnelType for all types --- libi2pd_client/BOB.cpp | 114 ++++++++++++++++++++--------------------- libi2pd_client/BOB.h | 16 +++--- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/libi2pd_client/BOB.cpp b/libi2pd_client/BOB.cpp index 89756e9f..661916cb 100644 --- a/libi2pd_client/BOB.cpp +++ b/libi2pd_client/BOB.cpp @@ -261,7 +261,8 @@ namespace client m_Owner (owner), m_Socket (m_Owner.GetService ()), m_ReceiveBuffer(BOB_COMMAND_BUFFER_SIZE + 1), m_SendBuffer(BOB_COMMAND_BUFFER_SIZE + 1), m_IsOpen (true), m_IsQuiet (false), m_IsActive (false), - m_InPort (0), m_OutPort (0), m_CurrentDestination (nullptr) + m_InPort (0), m_OutPort (0), m_CurrentDestination (nullptr), + m_TunnelType (TunnelType::STANDARD) { } @@ -492,50 +493,52 @@ namespace client m_Nickname, m_InHost, m_OutHost, m_InPort, m_OutPort, m_IsQuiet); m_Owner.AddDestination (m_Nickname, m_CurrentDestination); } - if (!m_tunnelType.has_value()) + switch (m_TunnelType) { - if (m_InPort) - m_CurrentDestination->CreateInboundTunnel (m_InPort, m_InHost); - if (m_OutPort && !m_OutHost.empty ()) - m_CurrentDestination->CreateOutboundTunnel (m_OutHost, m_OutPort, m_IsQuiet); - m_CurrentDestination->Start (); - } - else - { - switch (*m_tunnelType) + case TunnelType::STANDARD: { - case TunnelType::SOCKS: - try - { - auto SocksProxy = std::make_shared(m_Nickname, m_InHost, m_InPort, - false, m_OutHost, m_OutPort, m_CurrentDestination->GetLocalDestination()); - SocksProxy->Start(); - m_Owner.SetProxy(m_Nickname, std::move(SocksProxy)); - } - catch (std::exception& e) - { - LogPrint(eLogCritical, "Clients: Exception in SOCKS Proxy: ", e.what()); - ThrowFatal ("Unable to start SOCKS Proxy at ", m_InHost, ":", m_InPort, ": ", e.what ()); - } - break; - case TunnelType::HTTP_PROXY: - try - { - auto HttpProxy = std::make_shared(m_Nickname, m_InHost, m_InPort, - m_OutHost, true, true, m_CurrentDestination->GetLocalDestination()); - HttpProxy->Start(); - m_Owner.SetProxy(m_Nickname, std::move(HttpProxy)); - } - catch (std::exception& e) - { - LogPrint(eLogCritical, "Clients: Exception in HTTP Proxy: ", e.what()); - ThrowFatal ("Unable to start HTTP Proxy at ", m_InHost, ":", m_InPort, ": ", e.what ()); - } - break; - default: - SendReplyError("Unsupported tunnel type."); - return; + if (m_InPort) + m_CurrentDestination->CreateInboundTunnel (m_InPort, m_InHost); + if (m_OutPort && !m_OutHost.empty ()) + m_CurrentDestination->CreateOutboundTunnel (m_OutHost, m_OutPort, m_IsQuiet); + m_CurrentDestination->Start (); + break; } + case TunnelType::SOCKS: + { + try + { + auto SocksProxy = std::make_shared(m_Nickname, m_InHost, m_InPort, + false, m_OutHost, m_OutPort, m_CurrentDestination->GetLocalDestination()); + SocksProxy->Start(); + m_Owner.SetProxy(m_Nickname, std::move(SocksProxy)); + } + catch (std::exception& e) + { + LogPrint(eLogCritical, "Clients: Exception in SOCKS Proxy: ", e.what()); + ThrowFatal ("Unable to start SOCKS Proxy at ", m_InHost, ":", m_InPort, ": ", e.what ()); + } + break; + } + case TunnelType::HTTP_PROXY: + { + try + { + auto HttpProxy = std::make_shared(m_Nickname, m_InHost, m_InPort, + m_OutHost, true, true, m_CurrentDestination->GetLocalDestination()); + HttpProxy->Start(); + m_Owner.SetProxy(m_Nickname, std::move(HttpProxy)); + } + catch (std::exception& e) + { + LogPrint(eLogCritical, "Clients: Exception in HTTP Proxy: ", e.what()); + ThrowFatal ("Unable to start HTTP Proxy at ", m_InHost, ":", m_InPort, ": ", e.what ()); + } + break; + } + default: + SendReplyError("Unsupported tunnel type."); + return; } SendReplyOK ("Tunnel starting"); m_IsActive = true; @@ -997,21 +1000,18 @@ namespace client { std::string_view sv(operand, len); LogPrint (eLogDebug, "BOB: settunneltype ", operand); - if (sv == "socks") - { - m_tunnelType = TunnelType::SOCKS; - SendReplyOK ("tunnel type set to SOCKS"); - } - else if (sv == "httpproxy") - { - m_tunnelType = TunnelType::HTTP_PROXY; - SendReplyOK ("tunnel type set to HTTP proxy"); - } - else - { - m_tunnelType.reset(); - SendReplyError ("no tunnel type has been set"); - } + if (sv == "socks") + { + m_TunnelType = TunnelType::SOCKS; + SendReplyOK ("tunnel type set to SOCKS"); + } + else if (sv == "httpproxy") + { + m_TunnelType = TunnelType::HTTP_PROXY; + SendReplyOK ("tunnel type set to HTTP proxy"); + } + else + SendReplyError ("no tunnel type has been set"); } BOBCommandChannel::BOBCommandChannel (const std::string& address, uint16_t port): diff --git a/libi2pd_client/BOB.h b/libi2pd_client/BOB.h index c215f7cf..d658997e 100644 --- a/libi2pd_client/BOB.h +++ b/libi2pd_client/BOB.h @@ -16,7 +16,6 @@ #include #include #include -#include #include #include "util.h" #include "I2PTunnel.h" @@ -212,6 +211,13 @@ namespace client class BOBCommandChannel; class BOBCommandSession: public std::enable_shared_from_this { + enum class TunnelType + { + STANDARD = 0, + SOCKS = 1, + HTTP_PROXY = 2 + }; + public: BOBCommandSession (BOBCommandChannel& owner); @@ -275,13 +281,7 @@ namespace client i2p::data::PrivateKeys m_Keys; i2p::util::Mapping m_Options; std::shared_ptr m_CurrentDestination; - - enum class TunnelType - { - SOCKS = 0, - HTTP_PROXY = 1 - }; - std::optional m_tunnelType; + TunnelType m_TunnelType; }; typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len);