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. 60
      HTTPServer.cpp
  3. 10
      HTTPServer.h

8
HTTPProxy.cpp

@ -63,14 +63,8 @@ namespace proxy
request r; request r;
ExtractRequest(r); ExtractRequest(r);
parseHeaders(m_Buffer, r.headers); 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); 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);
} }
} }

60
HTTPServer.cpp

@ -512,7 +512,7 @@ namespace util
void HTTPConnection::Receive () 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::bind(&HTTPConnection::HandleReceive, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
} }
@ -522,6 +522,7 @@ namespace util
if (!ecode) if (!ecode)
{ {
m_Buffer[bytes_transferred] = 0; m_Buffer[bytes_transferred] = 0;
m_BufferLen = bytes_transferred;
RunRequest(); RunRequest();
} }
else if (ecode != boost::asio::error::operation_aborted) else if (ecode != boost::asio::error::operation_aborted)
@ -684,18 +685,19 @@ namespace util
s << "<br>"; 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) 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; i2p::data::IdentHash destination;
std::string fullAddress;
if (address.find(".b32.i2p") != std::string::npos) 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) if (i2p::data::Base32ToByteStream(address.c_str(), address.length() - strlen(".b32.i2p"), (uint8_t *)destination, 32) != 32)
@ -704,7 +706,6 @@ namespace util
SendReply ("<html>" + itoopieImage + "<br>Invalid Base32 address</html>", 400); SendReply ("<html>" + itoopieImage + "<br>Invalid Base32 address</html>", 400);
return; return;
} }
fullAddress = address;
} }
else else
{ {
@ -718,33 +719,15 @@ namespace util
return; return;
} }
destination = *addr; 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";
}
} }
} }
if (!leaseSet) SendToDestination (destination, buf, len);
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 ()) if (!leaseSet || !leaseSet->HasNonExpiredLeases ())
{ {
i2p::data::netdb.Subscribe(destination); i2p::data::netdb.Subscribe(destination);
@ -760,16 +743,7 @@ namespace util
m_Stream = i2p::stream::CreateStream (*leaseSet); m_Stream = i2p::stream::CreateStream (*leaseSet);
if (m_Stream) if (m_Stream)
{ {
std::string request = method + " " + uri + " HTTP/1.1\r\nHost:" + fullAddress + "\r\n"; m_Stream->Send ((uint8_t *)buf, len, 10);
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);
AsyncStreamReceive (); AsyncStreamReceive ();
} }
} }

10
HTTPServer.h

@ -41,7 +41,8 @@ namespace util
public: 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; } virtual ~HTTPConnection() { delete m_Socket; }
private: private:
@ -68,15 +69,16 @@ namespace util
boost::asio::ip::tcp::socket * m_Socket; boost::asio::ip::tcp::socket * m_Socket;
i2p::stream::Stream * m_Stream; i2p::stream::Stream * m_Stream;
char m_Buffer[8192], m_StreamBuffer[8192]; char m_Buffer[8192], m_StreamBuffer[8192];
size_t m_BufferLen;
request m_Request; request m_Request;
reply m_Reply; reply m_Reply;
protected: 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 (); 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: public:

Loading…
Cancel
Save