Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: support https protocol via caddy #982

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions src/cli/cli_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,12 +1034,18 @@ std::shared_ptr<IOBuf> CliConnection::GetNextDownstreamBuf(asio::error_code& ec,
} else
#endif
if (upstream_https_fallback_) {
if (upstream_handshake_) {
if (upstream_https_handshake_) {
ReadUpstreamHttpsHandshake(buf, ec);
if (ec) {
return nullptr;
}
}
if (upstream_https_chunked_) {
ReadUpstreamHttpsChunk(buf, ec);
if (ec) {
return nullptr;
}
}
downstream_.push_back(buf);
} else {
if (socks5_method_select_handshake_) {
Expand Down Expand Up @@ -1088,9 +1094,9 @@ std::shared_ptr<IOBuf> CliConnection::GetNextDownstreamBuf(asio::error_code& ec,
}

void CliConnection::ReadUpstreamHttpsHandshake(std::shared_ptr<IOBuf> buf, asio::error_code& ec) {
DCHECK(upstream_handshake_);
DCHECK(upstream_https_handshake_);

upstream_handshake_ = false;
upstream_https_handshake_ = false;
HttpResponseParser parser;

bool ok;
Expand All @@ -1103,6 +1109,10 @@ void CliConnection::ReadUpstreamHttpsHandshake(std::shared_ptr<IOBuf> buf, asio:
if (ok && parser.status_code() == 200) {
buf->trimStart(nparsed);
buf->retreat(nparsed);
if (parser.transfer_encoding_is_chunked()) {
upstream_https_chunked_ = true;
VLOG(1) << "Connection (client) " << connection_id() << " upstream http chunked encoding";
}
} else {
if (!ok) {
LOG(WARNING) << "Connection (client) " << connection_id()
Expand All @@ -1121,6 +1131,44 @@ void CliConnection::ReadUpstreamHttpsHandshake(std::shared_ptr<IOBuf> buf, asio:
}
}

void CliConnection::ReadUpstreamHttpsChunk(std::shared_ptr<IOBuf> buf, asio::error_code& ec) {
DCHECK(upstream_https_chunked_);

HttpResponseParser parser;

bool ok;
int nparsed = parser.Parse(buf, &ok);

if (nparsed) {
VLOG(3) << "Connection (client) " << connection_id()
<< " chunked http: " << std::string(reinterpret_cast<const char*>(buf->data()), nparsed);
}
if (ok && parser.status_code() == 200) {
buf->trimStart(nparsed);
buf->retreat(nparsed);
upstream_https_chunked_ = false;
if (parser.content_length() != 0) {
LOG(WARNING) << "Connection (client) " << connection_id() << " upstream server returns unexpected body";
ec = asio::error::invalid_argument;
return;
}
if (buf->empty()) {
ec = asio::error::try_again;
return;
}
} else {
if (!ok) {
LOG(WARNING) << "Connection (client) " << connection_id()
<< " upstream server unhandled: " << parser.ErrorMessage() << ": "
<< std::string(reinterpret_cast<const char*>(buf->data()), nparsed);
} else {
LOG(WARNING) << "Connection (client) " << connection_id() << " upstream server returns: " << parser.status_code();
}
ec = asio::error::connection_refused;
return;
}
}

void CliConnection::WriteUpstreamMethodSelectRequest() {
socks5::method_select_request_header method_select_header;
method_select_header.ver = socks5::version;
Expand Down
5 changes: 4 additions & 1 deletion src/cli/cli_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ class CliConnection : public RefCountedThreadSafe<CliConnection>,

private:
void ReadUpstreamHttpsHandshake(std::shared_ptr<IOBuf> buf, asio::error_code& ec);
void ReadUpstreamHttpsChunk(std::shared_ptr<IOBuf> buf, asio::error_code& ec);

void WriteUpstreamMethodSelectRequest();
void ReadUpstreamMethodSelectResponse(std::shared_ptr<IOBuf> buf, asio::error_code& ec);
Expand All @@ -377,7 +378,9 @@ class CliConnection : public RefCountedThreadSafe<CliConnection>,
void ReadUpstreamSocksResponse(std::shared_ptr<IOBuf> buf, asio::error_code& ec);

/// the state of https fallback handshake (upstream)
bool upstream_handshake_ = true;
bool upstream_https_handshake_ = true;
/// the state of https chunked (upstream)
bool upstream_https_chunked_ = false;
/// the state of socks5 method select handshake (upstream)
bool socks5_method_select_handshake_ = false;
bool socks5_auth_handshake_ = false;
Expand Down
Loading