Repository navigation
Expand file tree
/
Copy pathTlsSession.cpp
More file actions
171 lines (145 loc) · 3.66 KB
/
Copy pathTlsSession.cpp
File metadata and controls
171 lines (145 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
*
* @file TlsSession.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/session/TlsSession.hpp>
#include <memory>
#include <stdexcept>
#include <utility>
#include <string>
#include <vix/server/TlsConfig.hpp>
#include <vix/session/Session.hpp>
#include <vix/session/TlsTransport.hpp>
#include <vix/log/Logger.hpp>
namespace vix::session
{
using Logger = vix::log::Logger;
namespace
{
inline Logger &log()
{
return Logger::getInstance();
}
} // namespace
struct TlsSession::Impl
{
std::unique_ptr<tcp_stream> stream;
vix::router::Router &router;
const vix::config::Config &config;
std::shared_ptr<vix::executor::RuntimeExecutor> executor;
Impl(
std::unique_ptr<tcp_stream> input_stream,
vix::router::Router &input_router,
const vix::config::Config &input_config,
std::shared_ptr<vix::executor::RuntimeExecutor> input_executor)
: stream(std::move(input_stream)),
router(input_router),
config(input_config),
executor(std::move(input_executor))
{
if (!stream)
{
throw std::invalid_argument("TlsSession requires a valid TCP stream");
}
if (!executor)
{
throw std::invalid_argument("TlsSession requires a valid executor");
}
}
void close_stream_noexcept() noexcept
{
if (!stream)
{
return;
}
try
{
stream->close();
}
catch (...)
{
}
}
};
TlsSession::TlsSession(
std::unique_ptr<tcp_stream> stream,
vix::router::Router &router,
const vix::config::Config &config,
std::shared_ptr<vix::executor::RuntimeExecutor> executor)
: impl_(std::make_unique<Impl>(
std::move(stream),
router,
config,
std::move(executor)))
{
}
TlsSession::~TlsSession() = default;
task<void> TlsSession::run()
{
if (!impl_)
{
co_return;
}
const vix::server::TlsConfig tls = impl_->config.getTlsConfig();
if (!tls.is_enabled())
{
log().log(
Logger::Level::Warn,
"[tls] TlsSession started while TLS is disabled");
impl_->close_stream_noexcept();
co_return;
}
if (!tls.is_configured())
{
log().log(
Logger::Level::Error,
"[tls] TLS is enabled but certificate or private key file is missing");
impl_->close_stream_noexcept();
co_return;
}
try
{
auto transport = std::make_unique<TlsTransport>(
std::move(impl_->stream),
tls);
co_await transport->async_handshake();
auto session = std::make_shared<Session>(
std::move(transport),
impl_->router,
impl_->config,
impl_->executor);
co_await session->run();
}
catch (const std::exception &e)
{
const std::string msg = e.what();
if (msg.find("unexpected eof while reading") != std::string::npos ||
msg.find("connection reset") != std::string::npos ||
msg.find("broken pipe") != std::string::npos)
{
log().log(
Logger::Level::Debug,
"[tls] client closed connection during handshake: {}",
e.what());
}
else
{
log().log(
Logger::Level::Error,
"[tls] TLS session failed: {}",
e.what());
}
impl_->close_stream_noexcept();
}
co_return;
}
} // namespace vix::session