2022-11-13 21:22:35 +00:00
|
|
|
#include "webfuse/ws/server.hpp"
|
2023-01-22 12:56:16 +00:00
|
|
|
#include "webfuse/ws/server_handler.hpp"
|
2022-11-19 21:57:32 +00:00
|
|
|
|
2022-11-13 21:22:35 +00:00
|
|
|
#include <libwebsockets.h>
|
2022-11-19 21:57:32 +00:00
|
|
|
|
|
|
|
#include <cinttypes>
|
2022-11-13 21:22:35 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <iostream>
|
2022-11-14 16:21:47 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
2022-11-19 21:57:32 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <future>
|
|
|
|
#include <chrono>
|
2022-11-14 18:02:46 +00:00
|
|
|
#include <stdexcept>
|
2022-11-13 21:22:35 +00:00
|
|
|
|
2022-11-19 21:57:32 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2023-01-06 15:50:32 +00:00
|
|
|
constexpr int64_t const timeout_secs = 10;
|
|
|
|
|
2022-11-19 21:57:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 21:22:35 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
|
|
|
static int ws_server_callback(struct lws *wsi, enum lws_callback_reasons reason,
|
|
|
|
void *user, void *in, size_t len)
|
|
|
|
{
|
2022-11-19 21:57:32 +00:00
|
|
|
auto const * protocol = lws_get_protocol(wsi);
|
|
|
|
if (nullptr == protocol) { return 0; }
|
|
|
|
if (&ws_server_callback != protocol->callback) { return 0; }
|
|
|
|
|
2023-01-22 12:56:16 +00:00
|
|
|
auto * handler = reinterpret_cast<webfuse::server_handler*>(protocol->user);
|
2022-11-19 21:57:32 +00:00
|
|
|
|
|
|
|
int result = 0;
|
2022-11-13 21:22:35 +00:00
|
|
|
switch(reason)
|
|
|
|
{
|
2023-01-15 13:55:31 +00:00
|
|
|
case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
|
2023-01-22 12:56:16 +00:00
|
|
|
result = handler->filter_connection(wsi);
|
2023-01-15 13:55:31 +00:00
|
|
|
break;
|
2022-11-13 21:22:35 +00:00
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
2023-01-22 12:56:16 +00:00
|
|
|
result = handler->on_established(wsi);
|
2022-11-13 21:22:35 +00:00
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_CLOSED:
|
2023-01-22 12:56:16 +00:00
|
|
|
handler->on_closed(wsi);
|
2022-11-13 21:22:35 +00:00
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_RECEIVE:
|
2023-01-22 12:56:16 +00:00
|
|
|
handler->on_receive(wsi, in, len);
|
2022-11-13 21:22:35 +00:00
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
2023-01-22 12:56:16 +00:00
|
|
|
handler->on_writable();
|
2022-11-13 21:22:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-19 21:57:32 +00:00
|
|
|
return result;
|
2022-11-13 21:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace webfuse
|
|
|
|
{
|
|
|
|
|
|
|
|
class ws_server::detail
|
|
|
|
{
|
|
|
|
detail(detail const &) = delete;
|
|
|
|
detail& operator=(detail const &) = delete;
|
|
|
|
detail(detail &&) = delete;
|
|
|
|
detail& operator=(detail &&) = delete;
|
|
|
|
public:
|
|
|
|
detail(ws_config const & config)
|
2022-11-14 16:21:47 +00:00
|
|
|
: shutdown_requested(false)
|
2023-01-22 12:56:16 +00:00
|
|
|
, data(config.authenticator, config.auth_header)
|
2022-11-13 21:22:35 +00:00
|
|
|
{
|
2023-01-14 22:58:22 +00:00
|
|
|
lws_set_log_level(0, nullptr);
|
|
|
|
|
2022-11-13 21:22:35 +00:00
|
|
|
memset(reinterpret_cast<void*>(protocols), 0, sizeof(protocols));
|
|
|
|
protocols[0].name = "webfuse2";
|
|
|
|
protocols[0].callback = &ws_server_callback;
|
|
|
|
protocols[0].per_session_data_size = 0;
|
2022-11-19 21:57:32 +00:00
|
|
|
protocols[0].user = reinterpret_cast<void*>(&data);
|
2022-11-13 21:22:35 +00:00
|
|
|
|
|
|
|
memset(reinterpret_cast<void*>(&info), 0, sizeof(info));
|
|
|
|
info.port = config.port;
|
|
|
|
info.protocols = protocols;
|
2023-01-14 22:58:22 +00:00
|
|
|
info.vhost_name = config.vhost_name.c_str();
|
2022-11-13 21:22:35 +00:00
|
|
|
info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE | LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
|
|
|
|
|
2023-01-14 22:58:22 +00:00
|
|
|
if (config.use_tls)
|
|
|
|
{
|
|
|
|
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
|
|
|
info.ssl_cert_filepath = config.cert_path.c_str();
|
|
|
|
info.ssl_private_key_filepath = config.key_path.c_str();
|
|
|
|
}
|
|
|
|
|
2022-11-13 21:22:35 +00:00
|
|
|
context = lws_create_context(&info);
|
|
|
|
|
2023-01-06 16:10:56 +00:00
|
|
|
lws_create_vhost(context, &info);
|
|
|
|
// lws_vhost * const vhost = lws_create_vhost(context, &info);
|
2023-01-14 22:58:22 +00:00
|
|
|
// int port = lws_get_vhost_port(vhost);
|
2022-11-14 16:21:47 +00:00
|
|
|
|
|
|
|
thread = std::thread([this]() {
|
|
|
|
while (!shutdown_requested)
|
|
|
|
{
|
2023-01-22 12:56:16 +00:00
|
|
|
data.poll();
|
2022-11-14 16:21:47 +00:00
|
|
|
lws_service(context, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2022-11-13 21:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~detail()
|
|
|
|
{
|
2022-11-14 16:21:47 +00:00
|
|
|
shutdown_requested = true;
|
|
|
|
lws_cancel_service(context);
|
|
|
|
thread.join();
|
2022-11-13 21:22:35 +00:00
|
|
|
lws_context_destroy(context);
|
|
|
|
}
|
|
|
|
|
2022-11-14 16:21:47 +00:00
|
|
|
std::thread thread;
|
|
|
|
std::atomic<bool> shutdown_requested;
|
2022-11-13 21:22:35 +00:00
|
|
|
lws_protocols protocols[2];
|
|
|
|
lws_context_creation_info info;
|
|
|
|
lws_context * context;
|
2023-01-22 12:56:16 +00:00
|
|
|
server_handler data;
|
2022-11-13 21:22:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ws_server::ws_server(ws_config const & config)
|
|
|
|
: d(new detail(config))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ws_server::~ws_server()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws_server::ws_server(ws_server && other)
|
|
|
|
{
|
|
|
|
this->d = other.d;
|
|
|
|
other.d = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws_server& ws_server::operator=(ws_server && other)
|
|
|
|
{
|
|
|
|
if (this != &other)
|
|
|
|
{
|
|
|
|
delete this->d;
|
|
|
|
this->d = other.d;
|
|
|
|
other.d = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-11-20 12:29:34 +00:00
|
|
|
messagereader ws_server::perform(messagewriter writer)
|
2022-11-14 18:02:46 +00:00
|
|
|
{
|
2023-01-22 12:56:16 +00:00
|
|
|
auto result = d->data.perform(std::move(writer));
|
2022-11-19 21:57:32 +00:00
|
|
|
|
|
|
|
lws_cancel_service(d->context);
|
2023-01-22 12:56:16 +00:00
|
|
|
if(std::future_status::timeout == result.wait_for(std::chrono::seconds(timeout_secs)))
|
2022-11-14 18:02:46 +00:00
|
|
|
{
|
2022-11-19 21:57:32 +00:00
|
|
|
throw std::runtime_error("timeout");
|
2022-11-14 18:02:46 +00:00
|
|
|
}
|
2022-11-19 21:57:32 +00:00
|
|
|
|
2023-01-22 12:56:16 +00:00
|
|
|
return std::move(result.get());
|
2022-11-14 18:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-13 21:22:35 +00:00
|
|
|
}
|