mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-23 05:47:17 +01:00
commit
ce853786b5
|
@ -67,6 +67,8 @@ namespace proxy {
|
||||||
void ForwardToUpstreamProxy();
|
void ForwardToUpstreamProxy();
|
||||||
void HandleUpstreamHTTPProxyConnect(const boost::system::error_code & ec);
|
void HandleUpstreamHTTPProxyConnect(const boost::system::error_code & ec);
|
||||||
void HandleUpstreamSocksProxyConnect(const boost::system::error_code & ec);
|
void HandleUpstreamSocksProxyConnect(const boost::system::error_code & ec);
|
||||||
|
void HTTPConnect(const std::string & host, uint16_t port);
|
||||||
|
void HandleHTTPConnectStreamRequestComplete(std::shared_ptr<i2p::stream::Stream> stream);
|
||||||
|
|
||||||
void HandleSocksProxySendHandshake(const boost::system::error_code & ec, std::size_t bytes_transfered);
|
void HandleSocksProxySendHandshake(const boost::system::error_code & ec, std::size_t bytes_transfered);
|
||||||
void HandleSocksProxyReply(const boost::system::error_code & ec, std::size_t bytes_transfered);
|
void HandleSocksProxyReply(const boost::system::error_code & ec, std::size_t bytes_transfered);
|
||||||
|
@ -257,43 +259,63 @@ namespace proxy {
|
||||||
GenericProxyInfo("Addresshelper found", ss.str().c_str());
|
GenericProxyInfo("Addresshelper found", ss.str().c_str());
|
||||||
return true; /* request processed */
|
return true; /* request processed */
|
||||||
}
|
}
|
||||||
|
std::string dest_host;
|
||||||
SanitizeHTTPRequest(m_ClientRequest);
|
uint16_t dest_port;
|
||||||
|
bool useConnect = false;
|
||||||
std::string dest_host = m_RequestURL.host;
|
if(m_ClientRequest.method == "CONNECT")
|
||||||
uint16_t dest_port = m_RequestURL.port;
|
|
||||||
/* always set port, even if missing in request */
|
|
||||||
if (!dest_port)
|
|
||||||
dest_port = (m_RequestURL.schema == "https") ? 443 : 80;
|
|
||||||
/* detect dest_host, set proper 'Host' header in upstream request */
|
|
||||||
if (dest_host != "")
|
|
||||||
{
|
{
|
||||||
/* absolute url, replace 'Host' header */
|
std::string uri(m_ClientRequest.uri);
|
||||||
std::string h = dest_host;
|
auto pos = uri.find(":");
|
||||||
if (dest_port != 0 && dest_port != 80)
|
if(pos == std::string::npos || pos == uri.size() - 1)
|
||||||
h += ":" + std::to_string(dest_port);
|
|
||||||
m_ClientRequest.UpdateHeader("Host", h);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
auto h = m_ClientRequest.GetHeader ("Host");
|
|
||||||
if (h.length () > 0)
|
|
||||||
{
|
{
|
||||||
/* relative url and 'Host' header provided. transparent proxy mode? */
|
GenericProxyError("Invalid Request", "invalid request uri");
|
||||||
i2p::http::URL u;
|
return true;
|
||||||
std::string t = "http://" + h;
|
|
||||||
u.parse(t);
|
|
||||||
dest_host = u.host;
|
|
||||||
dest_port = u.port;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* relative url and missing 'Host' header */
|
useConnect = true;
|
||||||
GenericProxyError("Invalid request", "Can't detect destination host from request");
|
dest_port = std::stoi(uri.substr(pos+1));
|
||||||
return true;
|
dest_host = uri.substr(0, pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SanitizeHTTPRequest(m_ClientRequest);
|
||||||
|
|
||||||
|
dest_host = m_RequestURL.host;
|
||||||
|
dest_port = m_RequestURL.port;
|
||||||
|
/* always set port, even if missing in request */
|
||||||
|
if (!dest_port)
|
||||||
|
dest_port = (m_RequestURL.schema == "https") ? 443 : 80;
|
||||||
|
/* detect dest_host, set proper 'Host' header in upstream request */
|
||||||
|
if (dest_host != "")
|
||||||
|
{
|
||||||
|
/* absolute url, replace 'Host' header */
|
||||||
|
std::string h = dest_host;
|
||||||
|
if (dest_port != 0 && dest_port != 80)
|
||||||
|
h += ":" + std::to_string(dest_port);
|
||||||
|
m_ClientRequest.UpdateHeader("Host", h);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto h = m_ClientRequest.GetHeader ("Host");
|
||||||
|
if (h.length () > 0)
|
||||||
|
{
|
||||||
|
/* relative url and 'Host' header provided. transparent proxy mode? */
|
||||||
|
i2p::http::URL u;
|
||||||
|
std::string t = "http://" + h;
|
||||||
|
u.parse(t);
|
||||||
|
dest_host = u.host;
|
||||||
|
dest_port = u.port;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* relative url and missing 'Host' header */
|
||||||
|
GenericProxyError("Invalid request", "Can't detect destination host from request");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/* check dest_host really exists and inside I2P network */
|
/* check dest_host really exists and inside I2P network */
|
||||||
i2p::data::IdentHash identHash;
|
i2p::data::IdentHash identHash;
|
||||||
if (str_rmatch(dest_host, ".i2p")) {
|
if (str_rmatch(dest_host, ".i2p")) {
|
||||||
|
@ -316,6 +338,11 @@ namespace proxy {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if(useConnect)
|
||||||
|
{
|
||||||
|
HTTPConnect(dest_host, dest_port);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/* make relative url */
|
/* make relative url */
|
||||||
m_RequestURL.schema = "";
|
m_RequestURL.schema = "";
|
||||||
|
@ -427,6 +454,37 @@ namespace proxy {
|
||||||
Terminate();
|
Terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HTTPReqHandler::HTTPConnect(const std::string & host, uint16_t port)
|
||||||
|
{
|
||||||
|
LogPrint(eLogDebug, "HTTPProxy: CONNECT ",host, ":", port);
|
||||||
|
std::string hostname(host);
|
||||||
|
if(str_rmatch(hostname, ".i2p"))
|
||||||
|
GetOwner()->CreateStream (std::bind (&HTTPReqHandler::HandleHTTPConnectStreamRequestComplete,
|
||||||
|
shared_from_this(), std::placeholders::_1), host, port);
|
||||||
|
else
|
||||||
|
ForwardToUpstreamProxy();
|
||||||
|
}
|
||||||
|
|
||||||
|
void HTTPReqHandler::HandleHTTPConnectStreamRequestComplete(std::shared_ptr<i2p::stream::Stream> stream)
|
||||||
|
{
|
||||||
|
if(stream)
|
||||||
|
{
|
||||||
|
m_ClientResponse.code = 200;
|
||||||
|
m_ClientResponse.status = "OK";
|
||||||
|
m_send_buf = m_ClientResponse.to_string();
|
||||||
|
m_sock->send(boost::asio::buffer(m_send_buf));
|
||||||
|
auto connection = std::make_shared<i2p::client::I2PTunnelConnection>(GetOwner(), m_sock, stream);
|
||||||
|
GetOwner()->AddHandler(connection);
|
||||||
|
connection->I2PConnect();
|
||||||
|
m_sock = nullptr;
|
||||||
|
Terminate();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
GenericProxyError("CONNECT error", "Failed to Connect");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void HTTPReqHandler::SocksProxySuccess()
|
void HTTPReqHandler::SocksProxySuccess()
|
||||||
{
|
{
|
||||||
if(m_ClientRequest.method == "CONNECT") {
|
if(m_ClientRequest.method == "CONNECT") {
|
||||||
|
|
Loading…
Reference in a new issue