summaryrefslogtreecommitdiff
path: root/src/components/transport_manager/src/websocket_server/websocket_listener.cc
blob: 87fff3acbc4930a46be1be104324da11a52e0e48 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "transport_manager/websocket_server/websocket_listener.h"
#include "transport_manager/transport_adapter/transport_adapter_controller.h"
#include "transport_manager/websocket_server/websocket_device.h"
#include "utils/file_system.h"

namespace transport_manager {
namespace transport_adapter {
CREATE_LOGGERPTR_GLOBAL(logger_, "WebSocketListener")

WebSocketListener::WebSocketListener(TransportAdapterController* controller,
                                     const TransportManagerSettings& settings,
                                     const int num_threads)
    : controller_(controller)
    , ioc_(num_threads)
#ifdef ENABLE_SECURITY
    , ctx_(ssl::context::sslv23)
    , start_secure_(false)
#endif  // ENABLE_SECURITY
    , acceptor_(ioc_)
    , socket_(ioc_)
    , io_pool_(new boost::asio::thread_pool(num_threads))
    , num_threads_(num_threads)
    , shutdown_(false)
    , settings_(settings) {
}

WebSocketListener::~WebSocketListener() {
  Terminate();
}

TransportAdapter::Error WebSocketListener::Init() {
  LOG4CXX_AUTO_TRACE(logger_);
  const auto old_shutdown_value = shutdown_.exchange(false);
  if (old_shutdown_value) {
    ioc_.restart();
    io_pool_.reset(new boost::asio::thread_pool(num_threads_));
  }
  return StartListening();
}

void WebSocketListener::Terminate() {
  LOG4CXX_AUTO_TRACE(logger_);
  Shutdown();
}

TransportAdapter::Error WebSocketListener::StartListening() {
  LOG4CXX_AUTO_TRACE(logger_);
  if (acceptor_.is_open()) {
    return TransportAdapter::OK;
  }

#ifdef ENABLE_SECURITY
  auto const ta_error = AddCertificateAuthority();
  if (TransportAdapter::OK != ta_error) {
    return ta_error;
  }
#endif

  auto const address =
      boost::asio::ip::make_address(settings_.websocket_server_address());
  tcp::endpoint endpoint = {address, settings_.websocket_server_port()};

  // Open the acceptor
  boost::system::error_code ec;
  acceptor_.open(endpoint.protocol(), ec);
  if (ec) {
    auto str_err = "ErrorOpen: " + ec.message();
    LOG4CXX_ERROR(logger_,
                  str_err << " host/port: " << endpoint.address().to_string()
                          << "/" << endpoint.port());
    return TransportAdapter::FAIL;
  }

  acceptor_.set_option(boost::asio::socket_base::reuse_address(true), ec);
  if (ec) {
    std::string str_err = "ErrorSetOption: " + ec.message();
    LOG4CXX_ERROR(logger_,
                  str_err << " host/port: " << endpoint.address().to_string()
                          << "/" << endpoint.port());
    return TransportAdapter::FAIL;
  }

  // Bind to the server address
  acceptor_.bind(endpoint, ec);
  if (ec) {
    std::string str_err = "ErrorBind: " + ec.message();
    LOG4CXX_ERROR(logger_,
                  str_err << " host/port: " << endpoint.address().to_string()
                          << "/" << endpoint.port());
    return TransportAdapter::FAIL;
  }

  // Start listening for connections
  acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
  if (ec) {
    std::string str_err = "ErrorListen: " + ec.message();
    LOG4CXX_ERROR(logger_,
                  str_err << " host/port: " << endpoint.address().to_string()
                          << "/" << endpoint.port());
    return TransportAdapter::FAIL;
  }

  if (false == Run()) {
    return TransportAdapter::FAIL;
  }

  return TransportAdapter::OK;
}

#ifdef ENABLE_SECURITY
TransportAdapter::Error WebSocketListener::AddCertificateAuthority() {
  LOG4CXX_AUTO_TRACE(logger_);

  const auto cert_path = settings_.ws_server_cert_path();
  LOG4CXX_DEBUG(logger_, "Path to certificate : " << cert_path);
  const auto key_path = settings_.ws_server_key_path();
  LOG4CXX_DEBUG(logger_, "Path to key : " << key_path);
  const auto ca_cert_path = settings_.ws_server_ca_cert_path();
  LOG4CXX_DEBUG(logger_, "Path to ca cert : " << ca_cert_path);
  start_secure_ = settings_.wss_server_supported();

  if (start_secure_ && (!file_system::FileExists(cert_path) ||
                        !file_system::FileExists(key_path) ||
                        !file_system::FileExists(ca_cert_path))) {
    LOG4CXX_ERROR(logger_, "Certificate or key file not found");
    return TransportAdapter::FAIL;
  }

  if (!start_secure_) {
    auto check_config = [](const std::string& config,
                           const std::string config_name) {
      bool start_unsecure = config.empty();
      if (!start_unsecure) {
        LOG4CXX_ERROR(logger_,
                      "Configuration for secure WS is incomplete. "
                          << config_name
                          << " config is "
                             "present, meanwhile others may be missing. Please "
                             "check INI file");
      }
      return start_unsecure;
    };
    if (!check_config(cert_path, "Server cert") ||
        !check_config(key_path, "Server key") ||
        !check_config(ca_cert_path, "CA cert")) {
      return TransportAdapter::FAIL;
    }
  } else {
    LOG4CXX_INFO(logger_, "WebSocket server will start secure connection");
    ctx_.add_verify_path(cert_path);
    ctx_.set_options(boost::asio::ssl::context::default_workarounds);
    using context = boost::asio::ssl::context_base;
    ctx_.set_verify_mode(ssl::verify_peer | ssl::verify_fail_if_no_peer_cert);
    boost::system::error_code sec_ec;
    ctx_.use_certificate_chain_file(cert_path, sec_ec);
    ctx_.load_verify_file(ca_cert_path);
    if (sec_ec) {
      LOG4CXX_ERROR(
          logger_,
          "Loading WS server certificate failed: " << sec_ec.message());
      return TransportAdapter::FAIL;
    }
    sec_ec.clear();
    ctx_.use_private_key_file(key_path, context::pem, sec_ec);
    if (sec_ec) {
      LOG4CXX_ERROR(logger_,
                    "Loading WS server key failed: " << sec_ec.message());
      return TransportAdapter::FAIL;
    }
  }

  return TransportAdapter::OK;
}
#endif  // ENABLE_SECURITY

bool WebSocketListener::Run() {
  LOG4CXX_AUTO_TRACE(logger_);
  const bool is_connection_open = WaitForConnection();
  if (is_connection_open) {
    boost::asio::post(*io_pool_.get(), [&]() { ioc_.run(); });
  } else {
    LOG4CXX_ERROR(logger_, "Connection is shutdown or acceptor isn't open");
  }

  return is_connection_open;
}

bool WebSocketListener::WaitForConnection() {
  LOG4CXX_AUTO_TRACE(logger_);
  if (!shutdown_ && acceptor_.is_open()) {
    acceptor_.async_accept(
        socket_,
        std::bind(
            &WebSocketListener::StartSession, this, std::placeholders::_1));
    return true;
  }
  return false;
}

template <>
void WebSocketListener::ProcessConnection(
    std::shared_ptr<WebSocketConnection<WebSocketSession<> > > connection,
    const DeviceSptr device,
    const ApplicationHandle app_handle) {
  LOG4CXX_AUTO_TRACE(logger_);
  controller_->ConnectionCreated(
      connection, device->unique_device_id(), app_handle);

  controller_->ConnectDone(device->unique_device_id(), app_handle);

  connection->Run();

  connection_list_lock.Acquire();
  connection_list_.push_back(connection);
  connection_list_lock.Release();

  WaitForConnection();
}

#ifdef ENABLE_SECURITY
template <>
void WebSocketListener::ProcessConnection(
    std::shared_ptr<WebSocketConnection<WebSocketSecureSession<> > > connection,
    const DeviceSptr device,
    const ApplicationHandle app_handle) {
  LOG4CXX_AUTO_TRACE(logger_);
  controller_->ConnectionCreated(
      connection, device->unique_device_id(), app_handle);

  controller_->ConnectDone(device->unique_device_id(), app_handle);

  connection->Run();

  connection_list_lock.Acquire();
  connection_list_.push_back(connection);
  connection_list_lock.Release();

  WaitForConnection();
}
#endif  // ENABLE_SECURITY

void WebSocketListener::StartSession(boost::system::error_code ec) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (ec) {
    std::string str_err = "ErrorAccept: " + ec.message();
    LOG4CXX_ERROR(logger_, str_err);
    return;
  }

  if (shutdown_) {
    return;
  }

  const ApplicationHandle app_handle = socket_.native_handle();

  std::shared_ptr<WebSocketDevice> device =
      std::static_pointer_cast<WebSocketDevice>(
          controller_->GetWebEngineDevice());

  LOG4CXX_INFO(logger_, "Connected client: " << app_handle);

#ifdef ENABLE_SECURITY
  if (start_secure_) {
    auto connection =
        std::make_shared<WebSocketConnection<WebSocketSecureSession<> > >(
            device->unique_device_id(),
            app_handle,
            std::move(socket_),
            ctx_,
            controller_);
    ProcessConnection(connection, device, app_handle);
    return;
  }
#endif  // ENABLE_SECURITY

  auto connection = std::make_shared<WebSocketConnection<WebSocketSession<> > >(
      device->unique_device_id(), app_handle, std::move(socket_), controller_);
  ProcessConnection(connection, device, app_handle);
}

void WebSocketListener::Shutdown() {
  LOG4CXX_AUTO_TRACE(logger_);
  if (false == shutdown_.exchange(true)) {
    ioc_.stop();
    socket_.close();
    boost::system::error_code ec;
    acceptor_.close(ec);

    if (ec) {
      LOG4CXX_ERROR(logger_, "Acceptor closed with error: " << ec);
    }

    io_pool_->stop();
    io_pool_->join();
  }
}

}  // namespace transport_adapter
}  // namespace transport_manager