From 2c694149fa6996eaeb23e378d5f83c9d3232c22f Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 4 Oct 2026 10:14:46 -0400 Subject: [PATCH] clean after neuroslope --- libi2pd_client/BOB.cpp | 120 ++++++++++++++++------------------------- libi2pd_client/BOB.h | 8 +-- 2 files changed, 50 insertions(+), 78 deletions(-) diff --git a/libi2pd_client/BOB.cpp b/libi2pd_client/BOB.cpp index 98dd0d07..c52fb1ec 100644 --- a/libi2pd_client/BOB.cpp +++ b/libi2pd_client/BOB.cpp @@ -386,50 +386,34 @@ namespace client os << data << std::endl; } - void BOBCommandSession::BuildStatusLine(bool currentTunnel, std::shared_ptr dest, std::string &out) + std::string BOBCommandSession::BuildStatusLine(bool currentTunnel, std::shared_ptr dest) { // helper lambdas - const auto issetStr = [](const std::string &str) { return str.empty() ? "not_set" : str; }; // for inhost, outhost - const auto issetNum = [&issetStr](const int p) { return issetStr(p == 0 ? "" : std::to_string(p)); }; // for inport, outport - const auto destExists = [](const BOBDestination * const dest) { return dest != nullptr; }; - const auto destReady = [](const BOBDestination * const dest) { return dest && dest->IsRunning(); }; - const auto bool_str = [](const bool v) { return v ? "true" : "false"; }; // bool -> str - const auto getProxyType = [](const i2p::client::I2PService* proxy) -> std::string { - if (!proxy) return "NONE"; - if (dynamic_cast(proxy)) return "SOCKS"; - if (dynamic_cast(proxy)) return "HTTPPROXY"; - return "UNKNOWN"; - }; - const auto isProxyRunning = [](const i2p::client::I2PService* proxy) -> bool { - return proxy != nullptr; - }; + constexpr auto issetStr = [](std::string_view str) { return str.empty() ? str : "not_set"; }; // for inhost, outhost + const auto issetNum = [](int p) { return p ? std::to_string (p) : "not_set"; }; // for inport, outport // tunnel info - const std::string nickname = currentTunnel ? m_Nickname : dest->GetNickname(); - const bool quiet = currentTunnel ? m_IsQuiet : dest->GetQuiet(); - const std::string inhost = issetStr(currentTunnel ? m_InHost : dest->GetInHost()); - const std::string outhost = issetStr(currentTunnel ? m_OutHost : dest->GetOutHost()); - const std::string inport = issetNum(currentTunnel ? m_InPort : dest->GetInPort()); - const std::string outport = issetNum(currentTunnel ? m_OutPort : dest->GetOutPort()); - const bool keys = destExists(dest.get ()); // key must exist when destination is created - const bool starting = destExists(dest.get ()) && !destReady(dest.get ()); - const bool running = destExists(dest.get ()) && destReady(dest.get ()); - const bool stopping = false; - - const i2p::client::I2PService* proxy = m_Owner.GetProxy(nickname); - const std::string proxyType = getProxyType(proxy); - const bool proxyStatus = isProxyRunning(proxy); + auto nickname = currentTunnel ? m_Nickname : (dest ? dest->GetNickname() : ""); + bool quiet = currentTunnel ? m_IsQuiet : (dest ? dest->GetQuiet() : true); + auto inhost = issetStr(currentTunnel ? m_InHost : (dest ? dest->GetInHost() : "")); + auto outhost = issetStr(currentTunnel ? m_OutHost : (dest ? dest->GetOutHost() : "")); + auto inport = issetNum(currentTunnel ? m_InPort : (dest ? dest->GetInPort() : 0)); + auto outport = issetNum(currentTunnel ? m_OutPort : (dest ? dest->GetOutPort() : 0)); + bool keys = (bool)dest; // key must exist when destination is created + bool starting = dest && !dest->IsRunning (); + bool running = dest && !dest->IsRunning (); + auto [proxy, proxyType] = m_Owner.GetProxy(nickname); // build line std::stringstream ss; ss << "DATA " - << "NICKNAME: " << nickname << " " << "STARTING: " << bool_str(starting) << " " - << "RUNNING: " << bool_str(running) << " " << "STOPPING: " << bool_str(stopping) << " " - << "KEYS: " << bool_str(keys) << " " << "QUIET: " << bool_str(quiet) << " " + << "NICKNAME: " << nickname << " " << "STARTING: " << std::boolalpha << starting << " " + << "RUNNING: " << std::boolalpha << running << " " << "STOPPING: " << "false " + << "KEYS: " << std::boolalpha << keys << " " << "QUIET: " << std::boolalpha << quiet << " " << "INPORT: " << inport << " " << "INHOST: " << inhost << " " << "OUTPORT: " << outport << " " << "OUTHOST: " << outhost << " " - << "PROXYTYPE: "<< proxyType << " " << "PROXYSTART: " << bool_str(proxyStatus); - out = ss.str(); + << "PROXYTYPE: "<< proxyType << " " << "PROXYSTART: " << std::boolalpha << (bool)proxy; + return ss.str(); } void BOBCommandSession::ZapCommandHandler (const char * operand, size_t len) @@ -508,10 +492,10 @@ namespace client { try { - auto SocksProxy = std::make_shared(m_Nickname, m_InHost, m_InPort, + 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)); + socksProxy->Start(); + m_Owner.AddProxy(m_Nickname, std::move(socksProxy), "SOCKS"); } catch (std::exception& e) { @@ -524,10 +508,10 @@ namespace client { try { - auto HttpProxy = std::make_shared(m_Nickname, m_InHost, m_InPort, + 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)); + httpProxy->Start(); + m_Owner.AddProxy(m_Nickname, std::move(httpProxy), "HTTPPROXY"); } catch (std::exception& e) { @@ -553,15 +537,13 @@ namespace client return; } auto dest = m_Owner.FindDestination (m_Nickname); - auto proxy = m_Owner.GetProxy (m_Nickname); if (dest) { dest->StopTunnels (); SendReplyOK ("Tunnel stopping"); + auto [proxy, proxyType] = m_Owner.GetProxy (m_Nickname); if (proxy) - { m_Owner.RemoveProxy (m_Nickname); - } } else SendReplyError ("tunnel not found"); @@ -592,13 +574,16 @@ namespace client if(*operand) { m_CurrentDestination = m_Owner.FindDestination (operand); - auto proxy = m_Owner.GetProxy (operand); if (m_CurrentDestination) { m_Keys = m_CurrentDestination->GetKeys (); m_IsActive = m_CurrentDestination->IsRunning (); - if(proxy) - m_IsActive = true; + if (!m_IsActive) + { + auto [proxy, proxyType] = m_Owner.GetProxy (operand); + if (proxy) + m_IsActive = true; + } m_Nickname = operand; } if (m_Nickname == operand) @@ -908,23 +893,19 @@ namespace client void BOBCommandSession::ListCommandHandler (const char * operand, size_t len) { LogPrint (eLogDebug, "BOB: list"); - std::string statusLine; bool sentCurrent = false; const auto& destinations = m_Owner.GetDestinations (); for (const auto& it: destinations) { - BuildStatusLine(false, it.second, statusLine); - SendRaw(statusLine); + SendRaw (BuildStatusLine(false, it.second)); if(m_Nickname.compare(it.second->GetNickname()) == 0) sentCurrent = true; } if(!sentCurrent && !m_Nickname.empty()) - { // add the current tunnel to the list. // this is for the incomplete tunnel which has not been started yet. - BuildStatusLine(true, m_CurrentDestination, statusLine); - SendRaw(statusLine); - } + SendRaw (BuildStatusLine(true, m_CurrentDestination)); + SendReplyOK ("Listing done"); } @@ -947,28 +928,19 @@ namespace client { LogPrint (eLogDebug, "BOB: status ", operand); const std::string name = operand; - std::string statusLine; // always prefer destination auto dest = m_Owner.FindDestination(name); if(dest) - { // tunnel destination exists - BuildStatusLine(false, dest, statusLine); - SendReplyOK(statusLine); - } + SendReplyOK (BuildStatusLine(false, dest)); else { - if(m_Nickname == name && !name.empty()) - { + if (m_Nickname == name && !name.empty()) // tunnel is incomplete / has not been started yet - BuildStatusLine(true, nullptr, statusLine); - SendReplyOK(statusLine); - } + SendReplyOK (BuildStatusLine(true, nullptr)); else - { - SendReplyError("no nickname has been set"); - } + SendReplyError ("no nickname has been set"); } } void BOBCommandSession::HelpCommandHandler (const char * operand, size_t len) @@ -1116,22 +1088,22 @@ namespace client return nullptr; } - void BOBCommandChannel::SetProxy (const std::string& name, std::shared_ptr proxy) + void BOBCommandChannel::AddProxy (const std::string& name, std::shared_ptr&& proxy, std::string_view type) { - m_proxy[name] = std::move(proxy); + m_Proxies.emplace (name, std::make_pair(std::move(proxy), type)); } - const I2PService* BOBCommandChannel::GetProxy(const std::string& name) const + std::pair, std::string> BOBCommandChannel::GetProxy(const std::string& name) const { - auto it = m_proxy.find(name); - if (it != m_proxy.end() && it->second) - return it->second.get(); - return nullptr; + auto it = m_Proxies.find(name); + if (it != m_Proxies.end() && it->second.first) + return it->second; + return { nullptr, "" }; } void BOBCommandChannel::RemoveProxy(const std::string& name) { - m_proxy.erase (name); + m_Proxies.erase (name); } void BOBCommandChannel::Accept () diff --git a/libi2pd_client/BOB.h b/libi2pd_client/BOB.h index d658997e..3d834bea 100644 --- a/libi2pd_client/BOB.h +++ b/libi2pd_client/BOB.h @@ -266,7 +266,7 @@ namespace client void SendReplyError (std::string_view msg); void SendRaw (std::string_view data); - void BuildStatusLine(bool currentTunnel, std::shared_ptr destination, std::string &out); + std::string BuildStatusLine(bool currentTunnel, std::shared_ptr destination); void SendPing (std::shared_ptr ls); @@ -299,8 +299,8 @@ namespace client void AddDestination (const std::string& name, std::shared_ptr dest); void DeleteDestination (const std::string& name); std::shared_ptr FindDestination (const std::string& name); - void SetProxy (const std::string& name, std::shared_ptr proxy); - const I2PService* GetProxy(const std::string& name) const; + void AddProxy (const std::string& name, std::shared_ptr&& proxy, std::string_view type); + std::pair, std::string> GetProxy(const std::string& name) const; void RemoveProxy(const std::string& name); private: @@ -315,7 +315,7 @@ namespace client std::map m_CommandHandlers; std::map m_HelpStrings; // shared, a proxy takes a reference to itself when it accepts a connection - std::map> m_proxy; + std::map, std::string> > m_Proxies; // name ->(tunnel, proxy type) public: