Browse Source

request b32-coded I2P address from HTTP server

pull/11/merge
orignal 11 years ago
parent
commit
9b92641112
  1. 54
      HTTPServer.cpp
  2. 1
      HTTPServer.h
  3. 13
      Streaming.cpp
  4. 4
      Streaming.h

54
HTTPServer.cpp

@ -1,8 +1,11 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include "base64.h"
#include "Tunnel.h" #include "Tunnel.h"
#include "TransitTunnel.h" #include "TransitTunnel.h"
#include "Transports.h" #include "Transports.h"
#include "NetDb.h"
#include "Streaming.h"
#include "HTTPServer.h" #include "HTTPServer.h"
namespace i2p 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 = "<html>LeaseSet not found</html>";
m_Reply.headers.resize(2);
m_Reply.headers[0].name = "Content-Length";
m_Reply.headers[0].value = boost::lexical_cast<std::string>(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 << "<html>Not responding</html>";
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<std::string>(m_Reply.content.size());
m_Reply.headers[1].name = "Content-Type";
m_Reply.headers[1].value = "text/html";
}
}
HTTPServer::HTTPServer (int port): HTTPServer::HTTPServer (int port):
m_Thread (nullptr), m_Work (m_Service), m_Thread (nullptr), m_Work (m_Service),
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
m_NewSocket (nullptr) m_NewSocket (nullptr)
{ {
} }
HTTPServer::~HTTPServer () HTTPServer::~HTTPServer ()

1
HTTPServer.h

@ -48,6 +48,7 @@ namespace util
void HandleWrite(const boost::system::error_code& ecode); void HandleWrite(const boost::system::error_code& ecode);
void HandleRequest (); void HandleRequest ();
void HandleDestinationRequest (std::string b32);
void FillContent (std::stringstream& s); void FillContent (std::stringstream& s);
private: private:

13
Streaming.cpp

@ -73,12 +73,16 @@ namespace stream
LogPrint ("Payload: ", str); LogPrint ("Payload: ", str);
packet->offset = buf - packet->buf; 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) if (flags & PACKET_FLAG_CLOSE)
{ {
LogPrint ("Closed"); LogPrint ("Closed");
m_IsOpen = false; m_IsOpen = false;
m_ReceiveQueue.WakeUp ();
} }
else else
SendQuickAck (); SendQuickAck ();
@ -212,13 +216,16 @@ namespace stream
} }
else else
DeleteI2NPMessage (msg); DeleteI2NPMessage (msg);
m_ReceiveQueue.WakeUp ();
} }
} }
size_t Stream::Receive (uint8_t * buf, size_t len, int timeout) size_t Stream::Receive (uint8_t * buf, size_t len, int timeout)
{ {
if (!m_IsOpen) return 0;
if (m_ReceiveQueue.IsEmpty ()) if (m_ReceiveQueue.IsEmpty ())
{ {
if (!timeout) return 0;
if (!m_ReceiveQueue.Wait (timeout, 0)) if (!m_ReceiveQueue.Wait (timeout, 0))
return 0; return 0;
} }
@ -358,7 +365,7 @@ namespace stream
return sharedLocalDestination->CreateNewStream (remote); return sharedLocalDestination->CreateNewStream (remote);
} }
void CloseStream (Stream * stream) void DeleteStream (Stream * stream)
{ {
if (sharedLocalDestination) if (sharedLocalDestination)
sharedLocalDestination->DeleteStream (stream); sharedLocalDestination->DeleteStream (stream);

4
Streaming.h

@ -54,7 +54,7 @@ namespace stream
void HandleNextPacket (Packet * packet); void HandleNextPacket (Packet * packet);
size_t Send (uint8_t * buf, size_t len, int timeout); // timeout in seconds 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 (); void Close ();
private: private:
@ -105,7 +105,7 @@ namespace stream
}; };
Stream * CreateStream (const i2p::data::LeaseSet * remote); Stream * CreateStream (const i2p::data::LeaseSet * remote);
void CloseStream (Stream * stream); void DeleteStream (Stream * stream);
// assuming data is I2CP message // assuming data is I2CP message
void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len); void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len);

Loading…
Cancel
Save