Browse Source

* HTTPProxy.cpp : HandleJumpServices() -> ExtractAddressHelper()

pull/572/head
hagen 9 years ago
parent
commit
66c09fc44c
  1. 57
      HTTPProxy.cpp

57
HTTPProxy.cpp

@ -52,7 +52,7 @@ namespace proxy {
void HTTPRequestFailed(const char *message); void HTTPRequestFailed(const char *message);
void RedirectToJumpService(std::string & host); void RedirectToJumpService(std::string & host);
bool ValidateHTTPRequest(); bool ValidateHTTPRequest();
void HandleJumpServices(); bool ExtractAddressHelper(i2p::http::URL & url, std::string & b64);
bool CreateHTTPRequest(uint8_t *http_buff, std::size_t len); bool CreateHTTPRequest(uint8_t *http_buff, std::size_t len);
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);
@ -123,12 +123,14 @@ namespace proxy {
/* TODO: don't redirect to webconsole, it's not always work, handle jumpservices here */ /* 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.address", url.host);
i2p::config::GetOption("http.port", url.port); i2p::config::GetOption("http.port", url.port);
url.schema = "http";
url.path = "/"; url.path = "/";
url.query = "page=jumpservices&address="; url.query = "page=jumpservices&address=";
url.query += host; url.query += host;
res.code = 302; /* redirect */ res.code = 302; /* redirect */
res.add_header("Location", url.to_string().c_str()); res.add_header("Location", url.to_string().c_str());
res.add_header("Connection", "close");
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),
@ -151,40 +153,28 @@ namespace proxy {
return true; return true;
} }
void HTTPReqHandler::HandleJumpServices() bool HTTPReqHandler::ExtractAddressHelper(i2p::http::URL & url, std::string & b64)
{ {
static const char * helpermark1 = "?i2paddresshelper="; const char *param = "i2paddresshelper=";
static const char * helpermark2 = "&i2paddresshelper="; std::size_t pos = url.query.find(param);
size_t addressHelperPos1 = m_path.rfind (helpermark1); std::size_t len = std::strlen(param);
size_t addressHelperPos2 = m_path.rfind (helpermark2); std::map<std::string, std::string> params;
size_t addressHelperPos;
if (addressHelperPos1 == std::string::npos) if (pos == std::string::npos)
{ return false; /* not found */
if (addressHelperPos2 == std::string::npos) if (!url.parse_query(params))
return; //Not a jump service return false;
else
addressHelperPos = addressHelperPos2; std::string value = params["i2paddresshelper"];
} len += value.length();
else b64 = i2p::http::UrlDecode(value);
{ url.query.replace(pos, len, "");
if (addressHelperPos2 == std::string::npos) return true;
addressHelperPos = addressHelperPos1;
else if ( addressHelperPos1 > addressHelperPos2 )
addressHelperPos = addressHelperPos1;
else
addressHelperPos = addressHelperPos2;
}
auto base64 = m_path.substr (addressHelperPos + strlen(helpermark1));
base64 = i2p::util::http::urlDecode(base64); //Some of the symbols may be urlencoded
LogPrint (eLogInfo, "HTTPProxy: jump service for ", m_address, ", inserting to address book");
//TODO: this is very dangerous and broken. We should ask the user before doing anything see http://pastethis.i2p/raw/pn5fL4YNJL7OSWj3Sc6N/
//TODO: we could redirect the user again to avoid dirtiness in the browser
i2p::client::context.GetAddressBook ().InsertAddress (m_address, base64);
m_path.erase(addressHelperPos);
} }
bool HTTPReqHandler::CreateHTTPRequest(uint8_t *http_buff, std::size_t len) bool HTTPReqHandler::CreateHTTPRequest(uint8_t *http_buff, std::size_t len)
{ {
std::string b64;
i2p::http::URL url; i2p::http::URL url;
url.parse(m_url); url.parse(m_url);
m_address = url.host; /* < compatibility */ m_address = url.host; /* < compatibility */
@ -196,7 +186,12 @@ namespace proxy {
url.host = ""; url.host = "";
m_path = url.to_string(); /* < compatibility */ m_path = url.to_string(); /* < compatibility */
if (!ValidateHTTPRequest()) return false; if (!ValidateHTTPRequest()) return false;
HandleJumpServices();
/* TODO: notify user */
if (ExtractAddressHelper(url, b64)) {
i2p::client::context.GetAddressBook ().InsertAddress (url.host, b64);
LogPrint (eLogInfo, "HTTPProxy: added b64 from addresshelper for ", url.host, " to address book");
}
i2p::data::IdentHash identHash; i2p::data::IdentHash identHash;
if (str_rmatch(m_address, ".i2p")) if (str_rmatch(m_address, ".i2p"))

Loading…
Cancel
Save