2013-12-10 13:03:22 +00:00
|
|
|
#include <boost/bind.hpp>
|
2014-03-27 17:24:23 +00:00
|
|
|
#include <boost/bind/protect.hpp>
|
2013-12-10 13:03:22 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-01-13 02:41:25 +00:00
|
|
|
#include "base64.h"
|
2014-01-15 00:56:34 +00:00
|
|
|
#include "Log.h"
|
2013-12-10 13:03:22 +00:00
|
|
|
#include "Tunnel.h"
|
|
|
|
#include "TransitTunnel.h"
|
|
|
|
#include "Transports.h"
|
2014-01-13 02:41:25 +00:00
|
|
|
#include "NetDb.h"
|
2013-12-10 13:03:22 +00:00
|
|
|
#include "HTTPServer.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
|
|
|
namespace misc_strings
|
|
|
|
{
|
|
|
|
|
|
|
|
const char name_value_separator[] = { ':', ' ' };
|
|
|
|
const char crlf[] = { '\r', '\n' };
|
|
|
|
|
|
|
|
} // namespace misc_strings
|
|
|
|
|
|
|
|
std::vector<boost::asio::const_buffer> HTTPConnection::reply::to_buffers()
|
|
|
|
{
|
|
|
|
std::vector<boost::asio::const_buffer> buffers;
|
2014-01-18 22:43:04 +00:00
|
|
|
if (headers.size () > 0)
|
|
|
|
{
|
|
|
|
buffers.push_back (boost::asio::buffer ("HTTP/1.0 200 OK\r\n")); // always OK
|
|
|
|
for (std::size_t i = 0; i < headers.size(); ++i)
|
|
|
|
{
|
|
|
|
header& h = headers[i];
|
|
|
|
buffers.push_back(boost::asio::buffer(h.name));
|
|
|
|
buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator));
|
|
|
|
buffers.push_back(boost::asio::buffer(h.value));
|
|
|
|
buffers.push_back(boost::asio::buffer(misc_strings::crlf));
|
|
|
|
}
|
2013-12-10 13:03:22 +00:00
|
|
|
buffers.push_back(boost::asio::buffer(misc_strings::crlf));
|
2014-01-18 22:43:04 +00:00
|
|
|
}
|
2013-12-10 13:03:22 +00:00
|
|
|
buffers.push_back(boost::asio::buffer(content));
|
|
|
|
return buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::Terminate ()
|
|
|
|
{
|
2014-03-27 19:42:23 +00:00
|
|
|
if (m_Stream)
|
|
|
|
{
|
|
|
|
m_Stream->Close ();
|
|
|
|
DeleteStream (m_Stream);
|
|
|
|
}
|
2013-12-10 13:03:22 +00:00
|
|
|
m_Socket->close ();
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::Receive ()
|
|
|
|
{
|
2014-01-15 00:56:34 +00:00
|
|
|
m_Socket->async_read_some (boost::asio::buffer (m_Buffer, 8192),
|
2013-12-10 13:03:22 +00:00
|
|
|
boost::bind(&HTTPConnection::HandleReceive, this,
|
|
|
|
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (!ecode)
|
|
|
|
{
|
2014-01-15 00:56:34 +00:00
|
|
|
m_Buffer[bytes_transferred] = 0;
|
|
|
|
auto address = ExtractAddress ();
|
2014-01-20 00:19:09 +00:00
|
|
|
if (address.length () > 1) // not just '/'
|
2014-02-17 22:47:21 +00:00
|
|
|
{
|
|
|
|
std::string uri ("/"), b32;
|
|
|
|
size_t pos = address.find ('/', 1);
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
b32 = address.substr (1); // excluding leading '/' to end of line
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b32 = address.substr (1, pos - 1); // excluding leading '/' to next '/'
|
|
|
|
uri = address.substr (pos); // rest of line
|
|
|
|
}
|
|
|
|
|
|
|
|
HandleDestinationRequest (b32, uri);
|
|
|
|
}
|
2014-01-15 00:56:34 +00:00
|
|
|
else
|
|
|
|
HandleRequest ();
|
2013-12-10 13:03:22 +00:00
|
|
|
}
|
|
|
|
else if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
|
2014-01-15 00:56:34 +00:00
|
|
|
std::string HTTPConnection::ExtractAddress ()
|
|
|
|
{
|
|
|
|
char * get = strstr (m_Buffer, "GET");
|
|
|
|
if (get)
|
|
|
|
{
|
|
|
|
char * http = strstr (get, "HTTP");
|
|
|
|
if (http)
|
2014-01-20 00:19:09 +00:00
|
|
|
return std::string (get + 4, http - get - 5);
|
2014-01-15 00:56:34 +00:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-03-27 19:42:23 +00:00
|
|
|
void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
Terminate ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::HandleWrite (const boost::system::error_code& ecode)
|
2013-12-10 13:03:22 +00:00
|
|
|
{
|
2014-03-27 19:42:23 +00:00
|
|
|
if (ecode || (m_Stream && !m_Stream->IsOpen ()))
|
2014-03-27 17:24:23 +00:00
|
|
|
Terminate ();
|
2014-03-27 19:42:23 +00:00
|
|
|
else // data keeps coming
|
|
|
|
AsyncStreamReceive ();
|
2013-12-10 13:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::HandleRequest ()
|
|
|
|
{
|
|
|
|
std::stringstream s;
|
|
|
|
s << "<html>";
|
|
|
|
FillContent (s);
|
|
|
|
s << "</html>";
|
2014-03-27 17:24:23 +00:00
|
|
|
SendReply (s.str ());
|
2013-12-10 13:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::FillContent (std::stringstream& s)
|
|
|
|
{
|
2014-03-13 04:10:05 +00:00
|
|
|
s << "Our external address:" << "<BR>" << "<BR>";
|
|
|
|
for (auto& address : i2p::context.GetRouterInfo().GetAddresses())
|
|
|
|
{
|
2014-03-13 11:43:54 +00:00
|
|
|
switch (address.transportStyle)
|
|
|
|
{
|
|
|
|
case i2p::data::RouterInfo::eTransportNTCP:
|
|
|
|
s << "NTCP ";
|
2014-03-13 04:10:05 +00:00
|
|
|
break;
|
2014-03-13 11:43:54 +00:00
|
|
|
case i2p::data::RouterInfo::eTransportSSU:
|
|
|
|
s << "SSU ";
|
2014-03-13 04:10:05 +00:00
|
|
|
break;
|
2014-03-13 11:43:54 +00:00
|
|
|
default:
|
|
|
|
s << "Unknown ";
|
2014-03-13 04:10:05 +00:00
|
|
|
}
|
|
|
|
s << address.host.to_string() << ":" << address.port << "<BR>";
|
|
|
|
}
|
|
|
|
|
2013-12-10 13:03:22 +00:00
|
|
|
s << "<P>Tunnels</P>";
|
|
|
|
for (auto it: i2p::tunnel::tunnels.GetOutboundTunnels ())
|
|
|
|
{
|
|
|
|
it->GetTunnelConfig ()->Print (s);
|
2014-03-16 20:03:20 +00:00
|
|
|
if (it->GetTunnelPool ())
|
|
|
|
s << " " << "Pool";
|
2014-03-21 22:26:11 +00:00
|
|
|
if (it->IsFailed ())
|
|
|
|
s << " " << "Failed";
|
2013-12-10 13:03:22 +00:00
|
|
|
s << " " << (int)it->GetNumSentBytes () << "<BR>";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it: i2p::tunnel::tunnels.GetInboundTunnels ())
|
|
|
|
{
|
|
|
|
it.second->GetTunnelConfig ()->Print (s);
|
2014-03-15 00:51:51 +00:00
|
|
|
if (it.second->GetTunnelPool ())
|
|
|
|
s << " " << "Pool";
|
2014-03-21 22:26:11 +00:00
|
|
|
if (it.second->IsFailed ())
|
|
|
|
s << " " << "Failed";
|
2013-12-10 13:03:22 +00:00
|
|
|
s << " " << (int)it.second->GetNumReceivedBytes () << "<BR>";
|
|
|
|
}
|
2014-03-15 00:24:12 +00:00
|
|
|
|
2013-12-10 13:03:22 +00:00
|
|
|
s << "<P>Transit tunnels</P>";
|
|
|
|
for (auto it: i2p::tunnel::tunnels.GetTransitTunnels ())
|
|
|
|
{
|
|
|
|
if (dynamic_cast<i2p::tunnel::TransitTunnelGateway *>(it.second))
|
|
|
|
s << it.second->GetTunnelID () << "-->";
|
|
|
|
else if (dynamic_cast<i2p::tunnel::TransitTunnelEndpoint *>(it.second))
|
|
|
|
s << "-->" << it.second->GetTunnelID ();
|
|
|
|
else
|
|
|
|
s << "-->" << it.second->GetTunnelID () << "-->";
|
2014-01-06 03:52:17 +00:00
|
|
|
s << " " << it.second->GetNumTransmittedBytes () << "<BR>";
|
2013-12-10 13:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s << "<P>Transports</P>";
|
2014-02-25 03:28:28 +00:00
|
|
|
s << "NTCP<BR>";
|
2013-12-10 13:03:22 +00:00
|
|
|
for (auto it: i2p::transports.GetNTCPSessions ())
|
|
|
|
{
|
2014-01-06 03:52:17 +00:00
|
|
|
// RouterInfo of incoming connection doesn't have address
|
|
|
|
bool outgoing = it.second->GetRemoteRouterInfo ().GetNTCPAddress ();
|
2013-12-10 13:03:22 +00:00
|
|
|
if (it.second->IsEstablished ())
|
2014-01-06 03:52:17 +00:00
|
|
|
{
|
|
|
|
if (outgoing) s << "-->";
|
2013-12-10 13:03:22 +00:00
|
|
|
s << it.second->GetRemoteRouterInfo ().GetIdentHashAbbreviation () << ": "
|
2014-01-06 03:52:17 +00:00
|
|
|
<< it.second->GetSocket ().remote_endpoint().address ().to_string ();
|
|
|
|
if (!outgoing) s << "-->";
|
|
|
|
s << "<BR>";
|
|
|
|
}
|
2013-12-10 13:03:22 +00:00
|
|
|
}
|
2014-02-25 03:28:28 +00:00
|
|
|
auto ssuServer = i2p::transports.GetSSUServer ();
|
|
|
|
if (ssuServer)
|
|
|
|
{
|
|
|
|
s << "<BR>SSU<BR>";
|
|
|
|
for (auto it: ssuServer->GetSessions ())
|
|
|
|
{
|
2014-03-12 11:37:43 +00:00
|
|
|
// incoming connections don't have remote router
|
|
|
|
bool outgoing = it.second->GetRemoteRouter ();
|
2014-02-25 03:28:28 +00:00
|
|
|
auto endpoint = it.second->GetRemoteEndpoint ();
|
2014-03-12 11:37:43 +00:00
|
|
|
if (outgoing) s << "-->";
|
|
|
|
s << endpoint.address ().to_string () << ":" << endpoint.port ();
|
|
|
|
if (!outgoing) s << "-->";
|
|
|
|
s << "<BR>";
|
2014-02-25 03:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-20 00:19:09 +00:00
|
|
|
s << "<p><a href=\"zmw2cyw2vj7f6obx3msmdvdepdhnw2ctc4okza2zjxlukkdfckhq\">Flibusta</a></p>";
|
2013-12-10 13:03:22 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 01:18:23 +00:00
|
|
|
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& uri)
|
2014-01-13 02:41:25 +00:00
|
|
|
{
|
2014-03-17 01:18:23 +00:00
|
|
|
i2p::data::IdentHash destination;
|
|
|
|
std::string fullAddress;
|
|
|
|
if (address.find (".i2p") != std::string::npos)
|
|
|
|
{
|
|
|
|
auto addr = i2p::data::netdb.FindAddress(address);
|
|
|
|
if (!addr)
|
|
|
|
{
|
|
|
|
LogPrint ("Unknown address ", address);
|
2014-03-27 17:24:23 +00:00
|
|
|
SendReply ("Unknown address");
|
2014-03-17 01:18:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
destination = *addr;
|
|
|
|
fullAddress = address;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i2p::data::Base32ToByteStream (address.c_str (), address.length (), (uint8_t *)destination, 32) != 32)
|
|
|
|
{
|
|
|
|
LogPrint ("Invalid Base32 address ", address);
|
2014-03-29 12:11:00 +00:00
|
|
|
SendReply ("Invalid Base32 address");
|
2014-03-17 01:18:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fullAddress = address + ".b32.i2p";
|
2014-02-17 22:47:21 +00:00
|
|
|
}
|
2014-03-17 01:18:23 +00:00
|
|
|
|
2014-01-13 02:41:25 +00:00
|
|
|
auto leaseSet = i2p::data::netdb.FindLeaseSet (destination);
|
2014-01-15 01:57:33 +00:00
|
|
|
if (!leaseSet || !leaseSet->HasNonExpiredLeases ())
|
2014-01-13 02:41:25 +00:00
|
|
|
{
|
2014-02-14 21:10:25 +00:00
|
|
|
i2p::data::netdb.Subscribe(destination);
|
2014-01-13 02:41:25 +00:00
|
|
|
std::this_thread::sleep_for (std::chrono::seconds(10)); // wait for 10 seconds
|
|
|
|
leaseSet = i2p::data::netdb.FindLeaseSet (destination);
|
2014-01-15 01:57:33 +00:00
|
|
|
if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) // still no LeaseSet
|
2014-01-13 02:41:25 +00:00
|
|
|
{
|
2014-03-27 17:24:23 +00:00
|
|
|
SendReply (leaseSet ? "<html>Leases expired</html>" : "<html>LeaseSet not found</html>");
|
2014-01-13 02:41:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-27 19:42:23 +00:00
|
|
|
}
|
|
|
|
if (!m_Stream)
|
|
|
|
m_Stream = i2p::stream::CreateStream (*leaseSet);
|
|
|
|
if (m_Stream)
|
2014-01-13 02:41:25 +00:00
|
|
|
{
|
2014-03-17 01:18:23 +00:00
|
|
|
std::string request = "GET " + uri + " HTTP/1.1\n Host:" + fullAddress + "\n";
|
2014-03-27 19:42:23 +00:00
|
|
|
m_Stream->Send ((uint8_t *)request.c_str (), request.length (), 10);
|
2014-03-29 12:11:00 +00:00
|
|
|
AsyncStreamReceive ();
|
2014-01-13 02:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 19:42:23 +00:00
|
|
|
void HTTPConnection::AsyncStreamReceive ()
|
|
|
|
{
|
|
|
|
if (m_Stream)
|
|
|
|
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192),
|
|
|
|
boost::protect (boost::bind (&HTTPConnection::HandleStreamReceive, this,
|
|
|
|
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)),
|
2014-03-29 12:11:00 +00:00
|
|
|
45); // 45 seconds timeout
|
2014-03-27 19:42:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 19:45:08 +00:00
|
|
|
void HTTPConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
2014-03-27 17:24:23 +00:00
|
|
|
if (bytes_transferred)
|
|
|
|
{
|
2014-03-27 19:42:23 +00:00
|
|
|
boost::asio::async_write (*m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred),
|
|
|
|
boost::bind (&HTTPConnection::HandleWrite, this, boost::asio::placeholders::error));
|
2014-03-27 17:24:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-27 19:42:23 +00:00
|
|
|
if (m_Stream && m_Stream->IsOpen ())
|
|
|
|
SendReply ("Not responding");
|
|
|
|
else
|
|
|
|
Terminate ();
|
2014-03-27 17:24:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::SendReply (const std::string& content)
|
|
|
|
{
|
|
|
|
m_Reply.content = content;
|
|
|
|
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";
|
|
|
|
|
|
|
|
boost::asio::async_write (*m_Socket, m_Reply.to_buffers(),
|
2014-03-27 19:42:23 +00:00
|
|
|
boost::bind (&HTTPConnection::HandleWriteReply, this,
|
|
|
|
boost::asio::placeholders::error));
|
2014-03-26 19:45:08 +00:00
|
|
|
}
|
2013-12-10 13:03:22 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-01-13 02:41:25 +00:00
|
|
|
|
2013-12-10 13:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTPServer::~HTTPServer ()
|
|
|
|
{
|
|
|
|
Stop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::Start ()
|
|
|
|
{
|
|
|
|
m_Thread = new std::thread (std::bind (&HTTPServer::Run, this));
|
|
|
|
m_Acceptor.listen ();
|
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::Stop ()
|
|
|
|
{
|
|
|
|
m_Acceptor.close();
|
|
|
|
m_Service.stop ();
|
|
|
|
if (m_Thread)
|
|
|
|
{
|
|
|
|
m_Thread->join ();
|
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::Run ()
|
|
|
|
{
|
|
|
|
m_Service.run ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::Accept ()
|
|
|
|
{
|
|
|
|
m_NewSocket = new boost::asio::ip::tcp::socket (m_Service);
|
|
|
|
m_Acceptor.async_accept (*m_NewSocket, boost::bind (&HTTPServer::HandleAccept, this,
|
|
|
|
boost::asio::placeholders::error));
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServer::HandleAccept(const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (!ecode)
|
|
|
|
{
|
|
|
|
new HTTPConnection (m_NewSocket);
|
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|