Browse Source

* HTTPProxy.cpp : html error messages

pull/574/head
hagen 8 years ago
parent
commit
8dd157d2eb
  1. 102
      HTTPProxy.cpp

102
HTTPProxy.cpp

@ -22,6 +22,21 @@
namespace i2p { namespace i2p {
namespace proxy { namespace proxy {
std::map<std::string, std::string> jumpservices = {
{ "inr.i2p", "http://joajgazyztfssty4w2on5oaqksz6tqoxbduy553y34mf4byv6gpq.b32.i2p/search/?q=" },
{ "stats.i2p", "http://7tbay5p4kzeekxvyvbf6v7eauazemsnnl2aoyqhg5jzpr5eke7tq.b32.i2p/cgi-bin/jump.cgi?a=" },
};
static const char *pageHead =
"<head>\r\n"
" <title>I2P HTTP proxy: error</title>\r\n"
" <style type=\"text/css\">\r\n"
" body { font: 100%/1.5em sans-serif; margin: 0; padding: 1.5em; background: #FAFAFA; color: #103456; }\r\n"
" .header { font-size: 2.5em; text-align: center; margin: 1.5em 0; color: #894C84; }\r\n"
" </style>\r\n"
"</head>\r\n"
;
bool str_rmatch(std::string & str, const char *suffix) { bool str_rmatch(std::string & str, const char *suffix) {
auto pos = str.rfind (suffix); auto pos = str.rfind (suffix);
if (pos == std::string::npos) if (pos == std::string::npos)
@ -39,12 +54,14 @@ namespace proxy {
void HandleSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered); void HandleSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
void Terminate(); void Terminate();
void AsyncSockRead(); void AsyncSockRead();
void HTTPRequestFailed(const char *message);
void RedirectToJumpService(std::string & host);
bool ExtractAddressHelper(i2p::http::URL & url, std::string & b64); bool ExtractAddressHelper(i2p::http::URL & url, std::string & b64);
void SanitizeHTTPRequest(i2p::http::HTTPReq & req); void SanitizeHTTPRequest(i2p::http::HTTPReq & req);
void SentHTTPFailed(const boost::system::error_code & ecode); void SentHTTPFailed(const boost::system::error_code & ecode);
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream); void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
/* error helpers */
void GenericProxyError(const char *title, const char *description);
void HostNotFound(std::string & host);
void SendProxyError(std::string & content);
uint8_t m_recv_chunk[8192]; uint8_t m_recv_chunk[8192];
std::string m_recv_buf; // from client std::string m_recv_buf; // from client
@ -82,36 +99,39 @@ namespace proxy {
Done(shared_from_this()); Done(shared_from_this());
} }
void HTTPReqHandler::HTTPRequestFailed(const char *message) void HTTPReqHandler::GenericProxyError(const char *title, const char *description) {
{ std::stringstream ss;
i2p::http::HTTPRes res; ss << "<h1>Proxy error: " << title << "</h1>\r\n";
res.code = 500; ss << "<p>" << description << "</p>\r\n";
res.add_header("Content-Type", "text/plain"); std::string content = ss.str();
res.add_header("Connection", "close"); SendProxyError(content);
res.body = message; }
res.body += "\r\n";
std::string response = res.to_string(); void HTTPReqHandler::HostNotFound(std::string & host) {
boost::asio::async_write(*m_sock, boost::asio::buffer(response), std::stringstream ss;
std::bind(&HTTPReqHandler::SentHTTPFailed, shared_from_this(), std::placeholders::_1)); ss << "<h1>Proxy error: Host not found</h1>\r\n"
} << "<p>Remote host not found in router's addressbook</p>\r\n"
<< "<p>You may try to find this host on jumpservices below:</p>\r\n"
<< "<ul>\r\n";
for (auto & js : jumpservices) {
ss << " <li><a href=\"" << js.second << host << "\">" << js.first << "</a></li>\r\n";
}
ss << "</ul>\r\n";
std::string content = ss.str();
SendProxyError(content);
}
void HTTPReqHandler::RedirectToJumpService(std::string & host) void HTTPReqHandler::SendProxyError(std::string & content)
{ {
i2p::http::HTTPRes res; i2p::http::HTTPRes res;
i2p::http::URL url; res.code = 500;
res.add_header("Content-Type", "text/html; charset=UTF-8");
/* 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.schema = "http";
url.path = "/";
url.query = "page=jumpservices&address=";
url.query += host;
res.code = 302; /* redirect */
res.add_header("Location", url.to_string().c_str());
res.add_header("Connection", "close"); res.add_header("Connection", "close");
std::stringstream ss;
ss << "<html>\r\n" << pageHead
<< "<body>" << content << "</body>\r\n"
<< "</html>\r\n";
res.body = ss.str();
std::string response = res.to_string(); std::string response = res.to_string();
boost::asio::async_write(*m_sock, boost::asio::buffer(response), boost::asio::async_write(*m_sock, boost::asio::buffer(response),
std::bind(&HTTPReqHandler::SentHTTPFailed, shared_from_this(), std::placeholders::_1)); std::bind(&HTTPReqHandler::SentHTTPFailed, shared_from_this(), std::placeholders::_1));
@ -181,7 +201,7 @@ namespace proxy {
if (req_len < 0) { if (req_len < 0) {
LogPrint(eLogError, "HTTPProxy: unable to parse request"); LogPrint(eLogError, "HTTPProxy: unable to parse request");
HTTPRequestFailed("invalid request"); GenericProxyError("Invalid request", "Proxy unable to parse your request");
return true; /* parse error */ return true; /* parse error */
} }
@ -189,13 +209,14 @@ namespace proxy {
LogPrint(eLogDebug, "HTTPProxy: requested: ", req.uri); LogPrint(eLogDebug, "HTTPProxy: requested: ", req.uri);
url.parse(req.uri); url.parse(req.uri);
/* TODO: show notice page with original link */
if (ExtractAddressHelper(url, b64)) { if (ExtractAddressHelper(url, b64)) {
i2p::client::context.GetAddressBook ().InsertAddress (url.host, b64); i2p::client::context.GetAddressBook ().InsertAddress (url.host, b64);
std::string message = "added b64 from addresshelper for " + url.host + " to address book"; LogPrint (eLogInfo, "HTTPProxy: added b64 from addresshelper for ", url.host);
LogPrint (eLogInfo, "HTTPProxy: ", message); std::string full_url = url.to_string();
message += ", please reload page"; std::stringstream ss;
HTTPRequestFailed(message.c_str()); ss << "Host " << url.host << " added to router's addressbook from helper. "
<< "Click <a href=\"" << full_url << "\">here</a> to proceed.";
GenericProxyError("Addresshelper found", ss.str().c_str());
return true; /* request processed */ return true; /* request processed */
} }
@ -224,8 +245,7 @@ namespace proxy {
dest_port = u.port; dest_port = u.port;
} else { } else {
/* relative url and missing 'Host' header */ /* relative url and missing 'Host' header */
const char *message = "Can't detect destination host from request"; GenericProxyError("Invalid request", "Can't detect destination host from request");
HTTPRequestFailed(message);
return true; return true;
} }
@ -233,14 +253,14 @@ namespace proxy {
i2p::data::IdentHash identHash; i2p::data::IdentHash identHash;
if (str_rmatch(dest_host, ".i2p")) { if (str_rmatch(dest_host, ".i2p")) {
if (!i2p::client::context.GetAddressBook ().GetIdentHash (dest_host, identHash)) { if (!i2p::client::context.GetAddressBook ().GetIdentHash (dest_host, identHash)) {
RedirectToJumpService(dest_host); /* unknown host */ HostNotFound(dest_host);
return true; /* request processed */ return true; /* request processed */
} }
/* TODO: outproxy handler here */ /* TODO: outproxy handler here */
} else { } else {
std::string message = "Host " + url.host + " not inside i2p network, but outproxy support still missing"; LogPrint (eLogWarning, "HTTPProxy: outproxy failure for ", dest_host, ": not implemented yet");
HTTPRequestFailed(message.c_str()); std::string message = "Host" + dest_host + "not inside I2P network, but outproxy support not implemented yet";
LogPrint (eLogWarning, "HTTPProxy: ", message); GenericProxyError("Outproxy failure", message.c_str());
return true; return true;
} }
@ -291,7 +311,7 @@ namespace proxy {
{ {
if (!stream) { if (!stream) {
LogPrint (eLogError, "HTTPProxy: error when creating the stream, check the previous warnings for more info"); LogPrint (eLogError, "HTTPProxy: error when creating the stream, check the previous warnings for more info");
HTTPRequestFailed("error when creating the stream, check logs"); GenericProxyError("Host is down", "Can't create connection to requested host, it may be down");
return; return;
} }
if (Kill()) if (Kill())

Loading…
Cancel
Save