diff --git a/ClientContext.h b/ClientContext.h index 0cffcf33..6da2cd30 100644 --- a/ClientContext.h +++ b/ClientContext.h @@ -47,6 +47,7 @@ namespace client ClientDestination * LoadLocalDestination (const std::string& filename, bool isPublic); AddressBook& GetAddressBook () { return m_AddressBook; }; + const SAMBridge * GetSAMBridge () const { return m_SamBridge; }; private: diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 77585039..c83894aa 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -469,6 +469,9 @@ namespace util const char HTTP_COMMAND_LOCAL_DESTINATIONS[] = "local_destinations"; const char HTTP_COMMAND_LOCAL_DESTINATION[] = "local_destination"; const char HTTP_PARAM_BASE32_ADDRESS[] = "b32"; + const char HTTP_COMMAND_SAM_SESSIONS[] = "sam_sessions"; + const char HTTP_COMMAND_SAM_SESSION[] = "sam_session"; + const char HTTP_PARAM_SAM_SESSION_ID[] = "id"; namespace misc_strings { @@ -673,7 +676,10 @@ namespace util s << "
Local destinations"; s << "
Tunnels"; s << "
Transit tunnels"; - s << "
Transports
"; + s << "
Transports"; + if (i2p::client::context.GetSAMBridge ()) + s << "
SAM sessions"; + s << "
"; if (i2p::context.AcceptsTunnels ()) s << "
Stop accepting tunnels
"; @@ -706,6 +712,15 @@ namespace util auto b32 = params[HTTP_PARAM_BASE32_ADDRESS]; ShowLocalDestination (b32, s); } + else if (cmd == HTTP_COMMAND_SAM_SESSIONS) + ShowSAMSessions (s); + else if (cmd == HTTP_COMMAND_SAM_SESSION) + { + std::map params; + ExtractParams (command.substr (paramsPos), params); + auto id = params[HTTP_PARAM_SAM_SESSION_ID]; + ShowSAMSession (id, s); + } } void HTTPConnection::ShowTransports (std::stringstream& s) @@ -850,6 +865,51 @@ namespace util } } } + + void HTTPConnection::ShowSAMSessions (std::stringstream& s) + { + auto sam = i2p::client::context.GetSAMBridge (); + if (sam) + { + for (auto& it: sam->GetSessions ()) + { + s << ""; + s << it.first << "
" << std::endl; + } + } + } + + void HTTPConnection::ShowSAMSession (const std::string& id, std::stringstream& s) + { + auto sam = i2p::client::context.GetSAMBridge (); + if (sam) + { + auto session = sam->FindSession (id); + if (session) + { + for (auto it: session->sockets) + { + switch (it->GetSocketType ()) + { + case i2p::client::eSAMSocketTypeSession: + s << "session"; + break; + case i2p::client::eSAMSocketTypeStream: + s << "stream"; + break; + case i2p::client::eSAMSocketTypeAcceptor: + s << "acceptor"; + break; + default: + s << "unknown"; + } + s << " [" << it->GetSocket ().remote_endpoint() << "]"; + s << "
" << std::endl; + } + } + } + } void HTTPConnection::StartAcceptingTunnels (std::stringstream& s) { diff --git a/HTTPServer.h b/HTTPServer.h index 658a9f7f..4db3486b 100644 --- a/HTTPServer.h +++ b/HTTPServer.h @@ -69,6 +69,8 @@ namespace util void ShowTransitTunnels (std::stringstream& s); void ShowLocalDestinations (std::stringstream& s); void ShowLocalDestination (const std::string& b32, std::stringstream& s); + void ShowSAMSessions (std::stringstream& s); + void ShowSAMSession (const std::string& id, std::stringstream& s); void StartAcceptingTunnels (std::stringstream& s); void StopAcceptingTunnels (std::stringstream& s); void FillContent (std::stringstream& s); diff --git a/SAM.cpp b/SAM.cpp index e2ba5c15..44702245 100644 --- a/SAM.cpp +++ b/SAM.cpp @@ -738,7 +738,7 @@ namespace client } } - SAMSession * SAMBridge::FindSession (const std::string& id) + SAMSession * SAMBridge::FindSession (const std::string& id) const { std::unique_lock l(m_SessionsMutex); auto it = m_Sessions.find (id); diff --git a/SAM.h b/SAM.h index 0660f35c..07583153 100644 --- a/SAM.h +++ b/SAM.h @@ -79,6 +79,7 @@ namespace client boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; }; void ReceiveHandshake (); void SetSocketType (SAMSocketType socketType) { m_SocketType = socketType; }; + SAMSocketType GetSocketType () const { return m_SocketType; }; private: @@ -150,7 +151,7 @@ namespace client SAMSession * CreateSession (const std::string& id, const std::string& destination, // empty string means transient const std::map * params); void CloseSession (const std::string& id); - SAMSession * FindSession (const std::string& id); + SAMSession * FindSession (const std::string& id) const; private: @@ -170,9 +171,14 @@ namespace client boost::asio::ip::tcp::acceptor m_Acceptor; boost::asio::ip::udp::endpoint m_DatagramEndpoint, m_SenderEndpoint; boost::asio::ip::udp::socket m_DatagramSocket; - std::mutex m_SessionsMutex; + mutable std::mutex m_SessionsMutex; std::map m_Sessions; uint8_t m_DatagramReceiveBuffer[i2p::datagram::MAX_DATAGRAM_SIZE+1]; + + public: + + // for HTTP + const decltype(m_Sessions)& GetSessions () const { return m_Sessions; }; }; } }