From 2cb6283d004b37c145cfd08e8a1a1521651e7868 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 6 Apr 2018 15:23:56 -0400 Subject: [PATCH] outproxy authorization --- libi2pd/Base.cpp | 15 +++++++++++++++ libi2pd/Base.h | 9 ++++++--- libi2pd_client/HTTPProxy.cpp | 18 +++++++++++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/libi2pd/Base.cpp b/libi2pd/Base.cpp index 09f04c05..f80f2751 100644 --- a/libi2pd/Base.cpp +++ b/libi2pd/Base.cpp @@ -210,6 +210,21 @@ namespace data return 4*d.quot; } + std::string ToBase64Standard (const std::string& in) + { + auto len = Base64EncodingBufferSize (in.length ()); + char * str = new char[len+1]; + auto l = ByteStreamToBase64 ((const uint8_t *)in.c_str (), in.length (), str, len); + str[l] = 0; + // replace '-' by '+' and '~' by '/' + for (size_t i = 0; i < l; i++) + if (str[i] == '-') str[i] = '+'; + else if (str[i] == '~') str[i] = '/'; + std::string s(str); + delete[] str; + return s; + } + /* * * iT64 diff --git a/libi2pd/Base.h b/libi2pd/Base.h index bc92376f..a273f468 100644 --- a/libi2pd/Base.h +++ b/libi2pd/Base.h @@ -15,10 +15,13 @@ namespace data { size_t Base32ToByteStream (const char * inBuf, size_t len, uint8_t * outBuf, size_t outLen); size_t ByteStreamToBase32 (const uint8_t * InBuf, size_t len, char * outBuf, size_t outLen); - /** + /** Compute the size for a buffer to contain encoded base64 given that the size of the input is input_size bytes - */ - size_t Base64EncodingBufferSize(const size_t input_size); + */ + size_t Base64EncodingBufferSize(const size_t input_size); + + std::string ToBase64Standard (const std::string& in); // using standard table, for Proxy-Authorization + } // data } // i2p diff --git a/libi2pd_client/HTTPProxy.cpp b/libi2pd_client/HTTPProxy.cpp index ac5d907d..47f756d3 100644 --- a/libi2pd_client/HTTPProxy.cpp +++ b/libi2pd_client/HTTPProxy.cpp @@ -389,11 +389,19 @@ namespace proxy { m_ClientRequestURL.host = ""; m_ClientRequest.uri = m_ClientRequestURL.to_string(); + if (m_ProxyURL.schema == "http" && (!m_ProxyURL.user.empty () || !m_ProxyURL.pass.empty ())) + { + // http proxy authorization + std::string s = "basic " + i2p::data::ToBase64Standard (m_ProxyURL.user + ":" + m_ProxyURL.pass); + m_ClientRequest.AddHeader("Proxy-Authorization", s); + } + m_ClientRequest.write(m_ClientRequestBuffer); m_ClientRequestBuffer << m_recv_buf.substr(m_req_len); // assume http if empty schema - if (m_ProxyURL.schema == "" || m_ProxyURL.schema == "http") { + if (m_ProxyURL.schema == "" || m_ProxyURL.schema == "http") + { // handle upstream http proxy if (!m_ProxyURL.port) m_ProxyURL.port = 80; if (m_ProxyURL.is_i2p()) @@ -409,14 +417,18 @@ namespace proxy { m_proxysock->async_connect(ep, std::bind(&HTTPReqHandler::HandleUpstreamHTTPProxyConnect, this, std::placeholders::_1)); })); } - } else if (m_ProxyURL.schema == "socks") { + } + else if (m_ProxyURL.schema == "socks") + { // handle upstream socks proxy if (!m_ProxyURL.port) m_ProxyURL.port = 9050; // default to tor default if not specified boost::asio::ip::tcp::resolver::query q(m_ProxyURL.host, std::to_string(m_ProxyURL.port)); m_proxy_resolver.async_resolve(q, std::bind(&HTTPReqHandler::HandleUpstreamProxyResolved, this, std::placeholders::_1, std::placeholders::_2, [&](boost::asio::ip::tcp::endpoint ep) { m_proxysock->async_connect(ep, std::bind(&HTTPReqHandler::HandleUpstreamSocksProxyConnect, this, std::placeholders::_1)); })); - } else { + } + else + { // unknown type, complain GenericProxyError("unknown outproxy url", m_ProxyURL.to_string().c_str()); }