-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "net/proxy/http/tls-stream.h" | ||
|
||
#include <utility> | ||
|
||
#include "absl/container/fixed_array.h" | ||
|
||
namespace net { | ||
namespace proxy { | ||
namespace http { | ||
|
||
TlsStream::TlsStream( | ||
const any_io_executor &executor, | ||
Stream &base_stream, | ||
boost::asio::ssl::context &ssl_context) | ||
: base_stream_wrapper_(base_stream, executor), | ||
ssl_stream_(base_stream_wrapper_, ssl_context) { | ||
// TODO: SSL_set_tlsext_host_name if not numeric host | ||
} | ||
|
||
void TlsStream::handshake( | ||
absl::AnyInvocable<void(std::error_code) &&> callback) { | ||
ssl_stream_.async_handshake( | ||
boost::asio::ssl::stream_base::client, | ||
std::move(callback)); | ||
} | ||
|
||
void TlsStream::read( | ||
absl::Span<mutable_buffer const> buffers, | ||
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) { | ||
ssl_stream_.async_read_some( | ||
absl::FixedArray<mutable_buffer, 1>(buffers.begin(), buffers.end()), | ||
std::move(callback)); | ||
} | ||
|
||
void TlsStream::write( | ||
absl::Span<const_buffer const> buffers, | ||
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) { | ||
ssl_stream_.async_write_some( | ||
absl::FixedArray<const_buffer, 1>(buffers.begin(), buffers.end()), | ||
std::move(callback)); | ||
} | ||
|
||
std::string_view TlsStream::alpn_selected() { | ||
const unsigned char *data; | ||
unsigned int len; | ||
SSL_get0_alpn_selected(ssl_stream_.native_handle(), &data, &len); | ||
return std::string_view(reinterpret_cast<const char *>(data), len); | ||
} | ||
|
||
} // namespace http | ||
} // namespace proxy | ||
} // namespace net |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef _NET_PROXY_HTTP_TLS_STREAM_H | ||
#define _NET_PROXY_HTTP_TLS_STREAM_H | ||
|
||
#include "boost/asio/ssl.hpp" | ||
#include "net/proxy/stream.h" | ||
#include "net/proxy/util/stream-wrapper.h" | ||
|
||
namespace net { | ||
namespace proxy { | ||
namespace http { | ||
|
||
class TlsStream : public Stream { | ||
public: | ||
TlsStream( | ||
const any_io_executor &executor, | ||
Stream &base_stream, | ||
boost::asio::ssl::context &ssl_context); | ||
|
||
void handshake(absl::AnyInvocable<void(std::error_code) &&> callback); | ||
|
||
void read( | ||
absl::Span<mutable_buffer const> buffers, | ||
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) override; | ||
|
||
void write( | ||
absl::Span<const_buffer const> buffers, | ||
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) override; | ||
|
||
void close() override { base_stream_wrapper_.stream().close(); } | ||
|
||
std::string_view alpn_selected(); | ||
|
||
private: | ||
StreamWrapper base_stream_wrapper_; | ||
boost::asio::ssl::stream<StreamWrapper> ssl_stream_; | ||
}; | ||
|
||
} // namespace http | ||
} // namespace proxy | ||
} // namespace net | ||
|
||
#endif // _NET_PROXY_HTTP_TLS_STREAM_H |