Browse Source

send original user's request through the proxy

pull/97/head
orignal 10 years ago
parent
commit
52fddadc98
  1. 8
      HTTPProxy.cpp
  2. 54
      HTTPServer.cpp
  3. 10
      HTTPServer.h

8
HTTPProxy.cpp

@ -63,14 +63,8 @@ namespace proxy @@ -63,14 +63,8 @@ namespace proxy
request r;
ExtractRequest(r);
parseHeaders(m_Buffer, r.headers);
const char * data = nullptr;
if (r.method == "POST")
{
data = strstr (m_Buffer, "\r\n\r\n");
if (data) data += 4;
}
LogPrint("Requesting ", r.host, " with path ", r.uri, " and method ", r.method);
HandleDestinationRequest(r.host, r.method, data ? std::string (data) : "" , r.uri);
SendToAddress (r.host, m_Buffer, m_BufferLen);
}
}

54
HTTPServer.cpp

@ -512,7 +512,7 @@ namespace util @@ -512,7 +512,7 @@ namespace util
void HTTPConnection::Receive ()
{
m_Socket->async_read_some (boost::asio::buffer (m_Buffer, 8192),
m_Socket->async_read_some (boost::asio::buffer (m_Buffer, 8191),
boost::bind(&HTTPConnection::HandleReceive, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
@ -522,6 +522,7 @@ namespace util @@ -522,6 +522,7 @@ namespace util
if (!ecode)
{
m_Buffer[bytes_transferred] = 0;
m_BufferLen = bytes_transferred;
RunRequest();
}
else if (ecode != boost::asio::error::operation_aborted)
@ -684,18 +685,19 @@ namespace util @@ -684,18 +685,19 @@ namespace util
s << "<br>";
}
}
s << "<p><a href=\"zmw2cyw2vj7f6obx3msmdvdepdhnw2ctc4okza2zjxlukkdfckhq\">Flibusta</a></p>";
s << "<p><a href=\"zmw2cyw2vj7f6obx3msmdvdepdhnw2ctc4okza2zjxlukkdfckhq.b32.i2p\">Flibusta</a></p>";
}
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& uri)
{
HandleDestinationRequest(address, "GET", "", uri);
std::string request = "GET " + uri + " HTTP/1.1\r\nHost:" + address + "\r\n";
LogPrint("HTTP Client Request: ", request);
SendToAddress (address, request.c_str (), request.size ());
}
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& method, const std::string& data, const std::string& uri)
void HTTPConnection::SendToAddress (const std::string& address, const char * buf, size_t len)
{
const i2p::data::LeaseSet * leaseSet = nullptr;
i2p::data::IdentHash destination;
std::string fullAddress;
if (address.find(".b32.i2p") != std::string::npos)
{
if (i2p::data::Base32ToByteStream(address.c_str(), address.length() - strlen(".b32.i2p"), (uint8_t *)destination, 32) != 32)
@ -704,7 +706,6 @@ namespace util @@ -704,7 +706,6 @@ namespace util
SendReply ("<html>" + itoopieImage + "<br>Invalid Base32 address</html>", 400);
return;
}
fullAddress = address;
}
else
{
@ -718,33 +719,15 @@ namespace util @@ -718,33 +719,15 @@ namespace util
return;
}
destination = *addr;
fullAddress = address;
}
else
{
if (address == "local")
{
// TODO: remove later
fullAddress = "local.i2p";
auto destination = i2p::stream::GetSharedLocalDestination ();
leaseSet = destination->GetLeaseSet ();
EepAccept (destination);
}
else
{
if (i2p::data::Base32ToByteStream(address.c_str(), address.length(), (uint8_t *)destination, 32) != 32)
{
LogPrint("Invalid Base32 address ", address);
SendReply("<html>" + itoopieImage + "<br>Invalid Base32 address</html>", 400);
return;
}
fullAddress = address + ".b32.i2p";
}
}
SendToDestination (destination, buf, len);
}
if (!leaseSet)
leaseSet = i2p::data::netdb.FindLeaseSet (destination);
void HTTPConnection::SendToDestination (const i2p::data::IdentHash& destination, const char * buf, size_t len)
{
auto leaseSet = i2p::data::netdb.FindLeaseSet (destination);
if (!leaseSet || !leaseSet->HasNonExpiredLeases ())
{
i2p::data::netdb.Subscribe(destination);
@ -760,16 +743,7 @@ namespace util @@ -760,16 +743,7 @@ namespace util
m_Stream = i2p::stream::CreateStream (*leaseSet);
if (m_Stream)
{
std::string request = method + " " + uri + " HTTP/1.1\r\nHost:" + fullAddress + "\r\n";
if (method == "POST" && data.size () > 0)
{
// POST/PUT, apply body
request += "Content-Type: application/x-www-form-urlencoded\r\n";
request += "Content-Length: " + boost::lexical_cast<std::string>(data.size ()) + "\r\n";
request += "\r\n" + data;
}
LogPrint("HTTP Client Request: ", request);
m_Stream->Send ((uint8_t *)request.c_str (), request.size (), 10);
m_Stream->Send ((uint8_t *)buf, len, 10);
AsyncStreamReceive ();
}
}

10
HTTPServer.h

@ -41,7 +41,8 @@ namespace util @@ -41,7 +41,8 @@ namespace util
public:
HTTPConnection (boost::asio::ip::tcp::socket * socket): m_Socket (socket), m_Stream (nullptr) { Receive (); };
HTTPConnection (boost::asio::ip::tcp::socket * socket):
m_Socket (socket), m_Stream (nullptr), m_BufferLen (0) { Receive (); };
virtual ~HTTPConnection() { delete m_Socket; }
private:
@ -68,15 +69,16 @@ namespace util @@ -68,15 +69,16 @@ namespace util
boost::asio::ip::tcp::socket * m_Socket;
i2p::stream::Stream * m_Stream;
char m_Buffer[8192], m_StreamBuffer[8192];
size_t m_BufferLen;
request m_Request;
reply m_Reply;
protected:
virtual void HandleDestinationRequest(const std::string& address, const std::string& uri);
virtual void HandleDestinationRequest(const std::string& address, const std::string& method, const std::string& data, const std::string& uri);
virtual void RunRequest ();
void HandleDestinationRequest(const std::string& address, const std::string& uri);
void SendToAddress (const std::string& address, const char * buf, size_t len);
void SendToDestination (const i2p::data::IdentHash& destination, const char * buf, size_t len);
public:

Loading…
Cancel
Save