diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 577ef748..a30f589f 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -1,8 +1,11 @@ #include #include +#include "base64.h" #include "Tunnel.h" #include "TransitTunnel.h" #include "Transports.h" +#include "NetDb.h" +#include "Streaming.h" #include "HTTPServer.h" namespace i2p @@ -123,12 +126,63 @@ namespace util } } + void HTTPConnection::HandleDestinationRequest (std::string b32) + { + uint8_t destination[32]; + i2p::data::Base32ToByteStream (b32.c_str (), b32.length (), destination, 32); + auto leaseSet = i2p::data::netdb.FindLeaseSet (destination); + if (!leaseSet) + { + i2p::data::netdb.RequestDestination (i2p::data::IdentHash (destination), true); + std::this_thread::sleep_for (std::chrono::seconds(10)); // wait for 10 seconds + leaseSet = i2p::data::netdb.FindLeaseSet (destination); + if (!leaseSet) // still no LeaseSet + { + m_Reply.content = "LeaseSet not found"; + m_Reply.headers.resize(2); + m_Reply.headers[0].name = "Content-Length"; + m_Reply.headers[0].value = boost::lexical_cast(m_Reply.content.size()); + m_Reply.headers[1].name = "Content-Type"; + m_Reply.headers[1].value = "text/html"; + return; + } + } + // we found LeaseSet + auto s = i2p::stream::CreateStream (leaseSet); + if (s) + { + std::string request = "GET / HTTP/1.1\n Host:" + b32 + ".b32.i2p\n"; + s->Send ((uint8_t *)request.c_str (), request.length (), 30); + std::stringstream ss; + uint8_t buf[8192]; + size_t r = s->Receive (buf, 8192, 30); // 30 seconds + if (r) // we recieved data + { + ss << std::string ((char *)buf, r); + while (s->IsOpen () && (r = s->Receive (buf, 8192, 30)) > 0) + ss << std::string ((char *)buf,r); + } + else // nothing received + ss << "Not responding"; + s->Close (); + //DeleteStream (s); + + m_Reply.content = ss.str (); + m_Reply.headers.resize(2); + m_Reply.headers[0].name = "Content-Length"; + m_Reply.headers[0].value = boost::lexical_cast(m_Reply.content.size()); + m_Reply.headers[1].name = "Content-Type"; + m_Reply.headers[1].value = "text/html"; + } + } + HTTPServer::HTTPServer (int port): m_Thread (nullptr), m_Work (m_Service), m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_NewSocket (nullptr) { + } HTTPServer::~HTTPServer () diff --git a/HTTPServer.h b/HTTPServer.h index 84439c2a..76393f27 100644 --- a/HTTPServer.h +++ b/HTTPServer.h @@ -48,6 +48,7 @@ namespace util void HandleWrite(const boost::system::error_code& ecode); void HandleRequest (); + void HandleDestinationRequest (std::string b32); void FillContent (std::stringstream& s); private: diff --git a/Streaming.cpp b/Streaming.cpp index 5f3b3228..a737e622 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -73,12 +73,16 @@ namespace stream LogPrint ("Payload: ", str); packet->offset = buf - packet->buf; - m_ReceiveQueue.Put (packet); - + if (packet->GetLength () > 0) + m_ReceiveQueue.Put (packet); + else + delete packet; + if (flags & PACKET_FLAG_CLOSE) { LogPrint ("Closed"); m_IsOpen = false; + m_ReceiveQueue.WakeUp (); } else SendQuickAck (); @@ -212,13 +216,16 @@ namespace stream } else DeleteI2NPMessage (msg); + m_ReceiveQueue.WakeUp (); } } size_t Stream::Receive (uint8_t * buf, size_t len, int timeout) { + if (!m_IsOpen) return 0; if (m_ReceiveQueue.IsEmpty ()) { + if (!timeout) return 0; if (!m_ReceiveQueue.Wait (timeout, 0)) return 0; } @@ -358,7 +365,7 @@ namespace stream return sharedLocalDestination->CreateNewStream (remote); } - void CloseStream (Stream * stream) + void DeleteStream (Stream * stream) { if (sharedLocalDestination) sharedLocalDestination->DeleteStream (stream); diff --git a/Streaming.h b/Streaming.h index 06dc1f44..cc772190 100644 --- a/Streaming.h +++ b/Streaming.h @@ -54,7 +54,7 @@ namespace stream void HandleNextPacket (Packet * packet); size_t Send (uint8_t * buf, size_t len, int timeout); // timeout in seconds - size_t Receive (uint8_t * buf, size_t len, int timeout); // returns 0 if timeout expired + size_t Receive (uint8_t * buf, size_t len, int timeout = 0); // returns 0 if timeout expired void Close (); private: @@ -105,7 +105,7 @@ namespace stream }; Stream * CreateStream (const i2p::data::LeaseSet * remote); - void CloseStream (Stream * stream); + void DeleteStream (Stream * stream); // assuming data is I2CP message void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len);