request b32-coded I2P address from HTTP server

This commit is contained in:
orignal 2014-01-12 21:41:25 -05:00
parent 07eb5baac0
commit 9b92641112
4 changed files with 67 additions and 5 deletions

View File

@ -1,8 +1,11 @@
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#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 = "<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):
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 ()

View File

@ -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:

View File

@ -73,12 +73,16 @@ namespace stream
LogPrint ("Payload: ", str);
packet->offset = buf - packet->buf;
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);

View File

@ -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);