|
|
|
@ -20,10 +20,18 @@
@@ -20,10 +20,18 @@
|
|
|
|
|
#include "I2PTunnel.h" |
|
|
|
|
#include "Config.h" |
|
|
|
|
#include "HTTP.h" |
|
|
|
|
#include "HTTPServer.h" |
|
|
|
|
|
|
|
|
|
namespace i2p { |
|
|
|
|
namespace proxy { |
|
|
|
|
bool str_rmatch(std::string & str, const char *suffix) { |
|
|
|
|
auto pos = str.rfind (suffix); |
|
|
|
|
if (pos == std::string::npos) |
|
|
|
|
return false; /* not found */ |
|
|
|
|
if (str.length() == (pos + std::strlen(suffix))) |
|
|
|
|
return true; /* match */ |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static const size_t http_buffer_size = 8192; |
|
|
|
|
class HTTPReqHandler: public i2p::client::I2PServiceHandler, public std::enable_shared_from_this<HTTPReqHandler> |
|
|
|
|
{ |
|
|
|
@ -43,9 +51,8 @@ namespace proxy {
@@ -43,9 +51,8 @@ namespace proxy {
|
|
|
|
|
void Terminate(); |
|
|
|
|
void AsyncSockRead(); |
|
|
|
|
void HTTPRequestFailed(const char *message); |
|
|
|
|
void RedirectToJumpService(); |
|
|
|
|
void RedirectToJumpService(std::string & host); |
|
|
|
|
void ExtractRequest(); |
|
|
|
|
bool IsI2PAddress(); |
|
|
|
|
bool ValidateHTTPRequest(); |
|
|
|
|
void HandleJumpServices(); |
|
|
|
|
bool CreateHTTPRequest(uint8_t *http_buff, std::size_t len); |
|
|
|
@ -99,23 +106,34 @@ namespace proxy {
@@ -99,23 +106,34 @@ namespace proxy {
|
|
|
|
|
//TODO: handle this apropriately
|
|
|
|
|
void HTTPReqHandler::HTTPRequestFailed(const char *message) |
|
|
|
|
{ |
|
|
|
|
std::size_t size = std::strlen(message); |
|
|
|
|
std::stringstream ss; |
|
|
|
|
ss << "HTTP/1.0 500 Internal Server Error\r\n" |
|
|
|
|
<< "Content-Type: text/plain\r\n"; |
|
|
|
|
ss << "Content-Length: " << std::to_string(size + 2) << "\r\n" |
|
|
|
|
<< "\r\n"; /* end of headers */ |
|
|
|
|
ss << message << "\r\n"; |
|
|
|
|
std::string response = ss.str(); |
|
|
|
|
i2p::http::HTTPRes res; |
|
|
|
|
res.code = 500; |
|
|
|
|
res.add_header("Content-Type", "text/plain"); |
|
|
|
|
res.add_header("Connection", "close"); |
|
|
|
|
res.body = message; |
|
|
|
|
res.body += "\r\n"; |
|
|
|
|
std::string response = res.to_string(); |
|
|
|
|
boost::asio::async_write(*m_sock, boost::asio::buffer(response), |
|
|
|
|
std::bind(&HTTPReqHandler::SentHTTPFailed, shared_from_this(), std::placeholders::_1)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void HTTPReqHandler::RedirectToJumpService(/*HTTPReqHandler::errTypes error*/) |
|
|
|
|
void HTTPReqHandler::RedirectToJumpService(std::string & host) |
|
|
|
|
{ |
|
|
|
|
std::stringstream ss; |
|
|
|
|
i2p::http::ShowJumpServices (ss, m_address); |
|
|
|
|
boost::asio::async_write(*m_sock, boost::asio::buffer(ss.str ()), |
|
|
|
|
i2p::http::HTTPRes res; |
|
|
|
|
i2p::http::URL url; |
|
|
|
|
|
|
|
|
|
/* TODO: don't redirect to webconsole, it's not always work, handle jumpservices here */ |
|
|
|
|
i2p::config::GetOption("http.address", url.host); |
|
|
|
|
i2p::config::GetOption("http.port", url.port); |
|
|
|
|
url.path = "/"; |
|
|
|
|
url.query = "page=jumpservices&address="; |
|
|
|
|
url.query += host; |
|
|
|
|
|
|
|
|
|
res.code = 302; /* redirect */ |
|
|
|
|
res.add_header("Location", url.to_string().c_str()); |
|
|
|
|
|
|
|
|
|
std::string response = res.to_string(); |
|
|
|
|
boost::asio::async_write(*m_sock, boost::asio::buffer(response), |
|
|
|
|
std::bind(&HTTPReqHandler::SentHTTPFailed, shared_from_this(), std::placeholders::_1)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -180,16 +198,6 @@ namespace proxy {
@@ -180,16 +198,6 @@ namespace proxy {
|
|
|
|
|
m_path.erase(addressHelperPos); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool HTTPReqHandler::IsI2PAddress() |
|
|
|
|
{ |
|
|
|
|
auto pos = m_address.rfind (".i2p"); |
|
|
|
|
if (pos != std::string::npos && (pos+4) == m_address.length ()) |
|
|
|
|
{ |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool HTTPReqHandler::CreateHTTPRequest(uint8_t *http_buff, std::size_t len) |
|
|
|
|
{ |
|
|
|
|
ExtractRequest(); //TODO: parse earlier
|
|
|
|
@ -197,14 +205,13 @@ namespace proxy {
@@ -197,14 +205,13 @@ namespace proxy {
|
|
|
|
|
HandleJumpServices(); |
|
|
|
|
|
|
|
|
|
i2p::data::IdentHash identHash; |
|
|
|
|
if (IsI2PAddress ()) |
|
|
|
|
if (str_rmatch(m_address, ".i2p")) |
|
|
|
|
{ |
|
|
|
|
if (!i2p::client::context.GetAddressBook ().GetIdentHash (m_address, identHash)){ |
|
|
|
|
RedirectToJumpService(); |
|
|
|
|
RedirectToJumpService(m_address); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_request = m_method; |
|
|
|
|
m_request.push_back(' '); |
|
|
|
@ -317,7 +324,6 @@ namespace proxy {
@@ -317,7 +324,6 @@ namespace proxy {
|
|
|
|
|
else |
|
|
|
|
AsyncSockRead(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void HTTPReqHandler::SentHTTPFailed(const boost::system::error_code & ecode) |
|
|
|
|