2018-06-05 09:14:38 +02:00
|
|
|
/** Simple WebSocket relay facilitating client-to-client connections.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2018-06-05 09:14:38 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2018-06-20 20:55:17 +02:00
|
|
|
#include <iostream>
|
2018-06-05 09:14:38 +02:00
|
|
|
#include <map>
|
2018-06-20 20:55:17 +02:00
|
|
|
#include <queue>
|
2018-06-05 09:14:38 +02:00
|
|
|
#include <string>
|
2018-06-20 20:55:17 +02:00
|
|
|
#include <utility>
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
#include <jansson.h>
|
2020-10-16 11:08:40 +02:00
|
|
|
#include <unistd.h>
|
2018-07-17 17:56:56 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
#include <villas/node/config.h>
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/compat.hpp>
|
2019-04-19 14:52:52 +02:00
|
|
|
#include <villas/memory.h>
|
|
|
|
#include <villas/tool.hpp>
|
2019-01-21 10:33:32 +01:00
|
|
|
#include <villas/log.hpp>
|
2020-10-16 11:08:40 +02:00
|
|
|
#include <villas/utils.hpp>
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
#include "villas-relay.hpp"
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
typedef char uuid_string_t[37];
|
|
|
|
|
|
|
|
using namespace villas::utils;
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
namespace tools {
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
RelaySession::RelaySession(Relay *r, Identifier sid) :
|
2019-01-21 10:33:32 +01:00
|
|
|
identifier(sid),
|
|
|
|
connects(0)
|
|
|
|
{
|
2021-02-16 14:15:14 +01:00
|
|
|
auto loggerName = fmt::format("relay:{}", sid);
|
|
|
|
logger = villas::logging.get(loggerName);
|
2019-04-17 18:46:18 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
logger->info("RelaySession created: {}", identifier);
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
sessions[sid] = this;
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-04-07 13:23:43 +02:00
|
|
|
created = time(nullptr);
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
uuid_generate_from_str(uuid, identifier, r->uuid);
|
2019-01-21 10:33:32 +01:00
|
|
|
}
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
RelaySession::~RelaySession()
|
2019-01-21 10:33:32 +01:00
|
|
|
{
|
2019-04-19 14:52:52 +02:00
|
|
|
logger->info("RelaySession destroyed: {}", identifier);
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
sessions.erase(identifier);
|
|
|
|
}
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
RelaySession * RelaySession::get(Relay *r, lws *wsi)
|
2019-01-21 10:33:32 +01:00
|
|
|
{
|
|
|
|
char uri[64];
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
/* We use the URI to associate this connection to a session
|
|
|
|
* Example: ws://example.com/node_1
|
|
|
|
* Will select the session with the name 'node_1'
|
|
|
|
*/
|
2018-07-16 22:19:55 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
/* Get path of incoming request */
|
|
|
|
lws_hdr_copy(wsi, uri, sizeof(uri), WSI_TOKEN_GET_URI);
|
2020-09-30 16:07:07 +02:00
|
|
|
if (strlen(uri) <= 1)
|
2019-01-21 10:33:32 +01:00
|
|
|
throw InvalidUrlException();
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2020-09-30 16:07:07 +02:00
|
|
|
Identifier sid = uri + 1;
|
2019-01-12 20:31:16 +01:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
auto it = sessions.find(sid);
|
|
|
|
if (it == sessions.end()) {
|
2020-10-16 11:08:40 +02:00
|
|
|
auto *rs = new RelaySession(r, sid);
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!rs)
|
|
|
|
throw MemoryAllocationError();
|
|
|
|
|
|
|
|
return rs;
|
2019-01-12 20:31:16 +01:00
|
|
|
}
|
2019-01-21 10:33:32 +01:00
|
|
|
else {
|
2021-02-16 14:15:14 +01:00
|
|
|
auto logger = logging.get("villas-relay");
|
2019-04-17 18:46:18 +02:00
|
|
|
logger->info("Found existing session: {}", sid);
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
return it->second;
|
2019-01-12 20:31:16 +01:00
|
|
|
}
|
2019-01-21 10:33:32 +01:00
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
json_t * RelaySession::toJson() const
|
2019-01-21 10:33:32 +01:00
|
|
|
{
|
|
|
|
json_t *json_connections = json_array();
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
for (auto it : connections) {
|
|
|
|
auto conn = it.second;
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
json_array_append(json_connections, conn->toJson());
|
|
|
|
}
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
uuid_string_t uuid_str;
|
2019-01-21 10:33:32 +01:00
|
|
|
uuid_unparse_lower(uuid, uuid_str);
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
return json_pack("{ s: s, s: s, s: o, s: I, s: i }",
|
|
|
|
"identifier", identifier.c_str(),
|
|
|
|
"uuid", uuid_str,
|
|
|
|
"connections", json_connections,
|
|
|
|
"created", created,
|
|
|
|
"connects", connects
|
|
|
|
);
|
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
std::map<std::string, RelaySession *> RelaySession::sessions;
|
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
RelayConnection::RelayConnection(Relay *r, lws *w, bool lo) :
|
2019-01-21 10:33:32 +01:00
|
|
|
wsi(w),
|
|
|
|
currentFrame(std::make_shared<Frame>()),
|
|
|
|
outgoingFrames(),
|
|
|
|
bytes_recv(0),
|
|
|
|
bytes_sent(0),
|
|
|
|
frames_recv(0),
|
2019-04-19 14:52:52 +02:00
|
|
|
frames_sent(0),
|
|
|
|
loopback(lo)
|
2019-01-21 10:33:32 +01:00
|
|
|
{
|
2020-10-16 11:08:40 +02:00
|
|
|
session = RelaySession::get(r, wsi);
|
2019-01-21 10:33:32 +01:00
|
|
|
session->connections[wsi] = this;
|
|
|
|
session->connects++;
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), ip, sizeof(ip));
|
2019-01-12 20:31:16 +01:00
|
|
|
|
2019-04-07 13:23:43 +02:00
|
|
|
created = time(nullptr);
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
session->logger->info("New connection established: {} ({})", name, ip);
|
2019-01-21 10:33:32 +01:00
|
|
|
}
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
RelayConnection::~RelayConnection()
|
2019-01-21 10:33:32 +01:00
|
|
|
{
|
2021-02-16 14:15:14 +01:00
|
|
|
session->logger->info("RelayConnection closed: {} ({})", name, ip);
|
2019-01-12 20:31:16 +01:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
session->connections.erase(wsi);
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
if (session->connections.empty())
|
|
|
|
delete session;
|
|
|
|
}
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
json_t * RelayConnection::toJson() const
|
2019-01-21 10:33:32 +01:00
|
|
|
{
|
|
|
|
return json_pack("{ s: s, s: s, s: I, s: I, s: I, s: I, s: I }",
|
|
|
|
"name", name,
|
|
|
|
"ip", ip,
|
|
|
|
"created", created,
|
|
|
|
"bytes_recv", bytes_recv,
|
|
|
|
"bytes_sent", bytes_sent,
|
|
|
|
"frames_recv", frames_recv,
|
|
|
|
"frames_sent", frames_sent
|
|
|
|
);
|
|
|
|
}
|
2019-01-12 20:31:16 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
void RelayConnection::write()
|
2019-01-21 10:33:32 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
std::shared_ptr<Frame> fr = outgoingFrames.front();
|
2019-01-14 10:00:09 +01:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
ret = lws_write(wsi, fr->data(), fr->size(), LWS_WRITE_BINARY);
|
|
|
|
if (ret < 0)
|
|
|
|
return;
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
bytes_sent += fr->size();
|
|
|
|
frames_sent++;
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
outgoingFrames.pop();
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
if (outgoingFrames.size() > 0)
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
}
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
void RelayConnection::read(void *in, size_t len)
|
2019-01-21 10:33:32 +01:00
|
|
|
{
|
|
|
|
currentFrame->insert(currentFrame->end(), (uint8_t *) in, (uint8_t *) in + len);
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
bytes_recv += len;
|
2018-07-16 22:19:55 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
if (lws_is_final_fragment(wsi)) {
|
|
|
|
frames_recv++;
|
2021-02-16 14:15:14 +01:00
|
|
|
session->logger->debug("Received frame, relaying to {} connections", session->connections.size() - (loopback ? 0 : 1));
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
for (auto p : session->connections) {
|
|
|
|
auto c = p.second;
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
/* We skip the current connection in order
|
|
|
|
* to avoid receiving our own data */
|
2019-04-19 14:52:52 +02:00
|
|
|
if (loopback == false && c == this)
|
2019-01-21 10:33:32 +01:00
|
|
|
continue;
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
c->outgoingFrames.push(currentFrame);
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
lws_callback_on_writable(c->wsi);
|
2018-06-20 20:55:17 +02:00
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
currentFrame = std::make_shared<Frame>();
|
|
|
|
}
|
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
Relay::Relay(int argc, char *argv[]) :
|
|
|
|
Tool(argc, argv, "relay"),
|
|
|
|
stop(false),
|
2020-09-11 14:57:05 +02:00
|
|
|
context(nullptr),
|
|
|
|
vhost(nullptr),
|
2019-04-19 14:52:52 +02:00
|
|
|
loopback(false),
|
|
|
|
port(8088),
|
|
|
|
protocol("live")
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
char hname[128];
|
|
|
|
ret = gethostname(hname, sizeof(hname));
|
|
|
|
if (ret)
|
|
|
|
throw SystemError("Failed to get hostname");
|
|
|
|
|
|
|
|
hostname = hname;
|
|
|
|
|
|
|
|
/* Default UUID is derived from hostname */
|
|
|
|
uuid_generate_from_str(uuid, hname);
|
|
|
|
|
2019-11-09 11:35:24 +01:00
|
|
|
ret = memory_init(0);
|
2019-04-19 14:52:52 +02:00
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to initialize memory");
|
|
|
|
|
|
|
|
/* Initialize logging */
|
2020-10-16 11:08:40 +02:00
|
|
|
lws_set_log_level((1 << LLL_COUNT) - 1, loggerCallback);
|
2019-04-19 14:52:52 +02:00
|
|
|
|
|
|
|
protocols = {
|
|
|
|
{
|
|
|
|
.name = "http",
|
|
|
|
.callback = lws_callback_http_dummy,
|
|
|
|
.per_session_data_size = 0,
|
|
|
|
.rx_buffer_size = 1024
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "http-api",
|
2020-10-16 11:08:40 +02:00
|
|
|
.callback = httpProtocolCallback,
|
2019-04-19 14:52:52 +02:00
|
|
|
.per_session_data_size = 0,
|
|
|
|
.rx_buffer_size = 1024
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "live",
|
2020-10-16 11:08:40 +02:00
|
|
|
.callback = protocolCallback,
|
2019-04-19 14:52:52 +02:00
|
|
|
.per_session_data_size = sizeof(RelayConnection),
|
|
|
|
.rx_buffer_size = 0
|
|
|
|
},
|
|
|
|
{ nullptr /* terminator */ }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
void Relay::loggerCallback(int level, const char *msg)
|
2019-01-14 09:59:51 +01:00
|
|
|
{
|
2021-02-16 14:15:14 +01:00
|
|
|
auto logger = logging.get("lws");
|
2018-07-17 17:56:56 +02:00
|
|
|
|
2019-01-12 19:06:41 +01:00
|
|
|
char *nl = (char *) strchr(msg, '\n');
|
|
|
|
if (nl)
|
|
|
|
*nl = 0;
|
2018-06-05 09:14:38 +02:00
|
|
|
|
|
|
|
/* Decrease severity for some errors. */
|
|
|
|
if (strstr(msg, "Unable to open") == msg)
|
|
|
|
level = LLL_WARN;
|
|
|
|
|
|
|
|
switch (level) {
|
2019-04-19 14:52:52 +02:00
|
|
|
case LLL_ERR:
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->error("{}", msg);
|
2019-04-19 14:52:52 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LLL_WARN:
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->warn( "{}", msg);
|
2019-04-19 14:52:52 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LLL_INFO:
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->info( "{}", msg);
|
2019-04-19 14:52:52 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-02-16 14:15:14 +01:00
|
|
|
logger->debug("{}", msg);
|
2019-04-19 14:52:52 +02:00
|
|
|
break;
|
2018-06-05 09:14:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
int Relay::httpProtocolCallback(lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
|
2019-01-21 10:33:32 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t json_len;
|
|
|
|
json_t *json_sessions, *json_body;
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
lws_context *ctx = lws_get_context(wsi);
|
|
|
|
void *user_ctx = lws_context_user(ctx);
|
|
|
|
|
|
|
|
Relay *r = reinterpret_cast<Relay *>(user_ctx);
|
2019-04-17 18:46:18 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
unsigned char buf[LWS_PRE + 2048], *start = &buf[LWS_PRE], *end = &buf[sizeof(buf) - LWS_PRE - 1], *p = start;
|
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
case LWS_CALLBACK_HTTP:
|
|
|
|
if (lws_add_http_common_headers(wsi, HTTP_STATUS_OK,
|
|
|
|
"application/json",
|
|
|
|
LWS_ILLEGAL_HTTP_CONTENT_LEN, /* no content len */
|
|
|
|
&p, end))
|
|
|
|
return 1;
|
2019-04-19 14:52:52 +02:00
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
if (lws_finalize_write_http_header(wsi, start, &p, end))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Write the body separately */
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_HTTP_WRITEABLE:
|
|
|
|
|
|
|
|
json_sessions = json_array();
|
2019-04-19 14:52:52 +02:00
|
|
|
for (auto it : RelaySession::sessions) {
|
2019-01-21 10:33:32 +01:00
|
|
|
auto &session = it.second;
|
|
|
|
|
|
|
|
json_array_append(json_sessions, session->toJson());
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
uuid_string_t uuid_str;
|
|
|
|
uuid_unparse(r->uuid, uuid_str);
|
2019-01-21 10:33:32 +01:00
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
json_body = json_pack("{ s: o, s: s, s: s, s: s, s: { s: b, s: i, s: s } }",
|
2019-01-21 10:33:32 +01:00
|
|
|
"sessions", json_sessions,
|
|
|
|
"version", PROJECT_VERSION_STR,
|
2020-10-16 11:08:40 +02:00
|
|
|
"hostname", r->hostname.c_str(),
|
|
|
|
"uuid", uuid_str,
|
2019-01-21 10:33:32 +01:00
|
|
|
"options",
|
2019-04-19 14:52:52 +02:00
|
|
|
"loopback", r->loopback,
|
|
|
|
"port", r->port,
|
|
|
|
"protocol", r->protocol.c_str()
|
2019-01-21 10:33:32 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
json_len = json_dumpb(json_body, (char *) buf + LWS_PRE, sizeof(buf) - LWS_PRE, JSON_INDENT(4));
|
|
|
|
|
|
|
|
ret = lws_write(wsi, buf + LWS_PRE, json_len, LWS_WRITE_HTTP_FINAL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
r->logger->info("Handled API request");
|
2019-01-21 10:33:32 +01:00
|
|
|
|
|
|
|
//if (lws_http_transaction_completed(wsi))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lws_callback_http_dummy(wsi, reason, user, in, len);
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
int Relay::protocolCallback(lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
|
2018-06-05 09:14:38 +02:00
|
|
|
{
|
2019-04-19 14:52:52 +02:00
|
|
|
lws_context *ctx = lws_get_context(wsi);
|
|
|
|
void *user_ctx = lws_context_user(ctx);
|
|
|
|
|
|
|
|
Relay *r = reinterpret_cast<Relay *>(user_ctx);
|
|
|
|
RelayConnection *c = reinterpret_cast<RelayConnection *>(user);
|
2018-06-05 09:14:38 +02:00
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
|
2019-01-21 10:33:32 +01:00
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
2018-06-20 20:55:17 +02:00
|
|
|
try {
|
2020-10-16 11:08:40 +02:00
|
|
|
new (c) RelayConnection(r, wsi, r->loopback);
|
2018-06-20 20:55:17 +02:00
|
|
|
}
|
2020-08-17 17:09:55 +02:00
|
|
|
catch (const InvalidUrlException &e) {
|
2018-06-05 09:14:38 +02:00
|
|
|
lws_close_reason(wsi, LWS_CLOSE_STATUS_PROTOCOL_ERR, (unsigned char *) "Invalid URL", strlen("Invalid URL"));
|
2019-03-26 17:04:57 +01:00
|
|
|
|
2018-06-05 09:14:38 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLOSED:
|
2019-04-19 14:52:52 +02:00
|
|
|
c->~RelayConnection();
|
2018-06-05 09:14:38 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
2018-06-20 20:55:17 +02:00
|
|
|
c->write();
|
2018-06-05 09:14:38 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_RECEIVE:
|
2018-06-20 20:55:17 +02:00
|
|
|
c->read(in, len);
|
2018-06-05 09:14:38 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
void Relay::usage()
|
2018-06-20 20:55:17 +02:00
|
|
|
{
|
2019-03-31 19:58:53 +02:00
|
|
|
std::cout << "Usage: villas-relay [OPTIONS]" << std::endl
|
2019-04-19 14:52:52 +02:00
|
|
|
<< " OPTIONS is one or more of the following options:" << std::endl
|
|
|
|
<< " -d LVL set debug level" << std::endl
|
|
|
|
<< " -p PORT the port number to listen on" << std::endl
|
|
|
|
<< " -P PROT the websocket protocol" << std::endl
|
|
|
|
<< " -l enable loopback of own data" << std::endl
|
2020-10-16 11:08:40 +02:00
|
|
|
<< " -u UUID unique instance id" << std::endl
|
2019-04-19 14:52:52 +02:00
|
|
|
<< " -V show version and exit" << std::endl
|
|
|
|
<< " -h show usage and exit" << std::endl << std::endl;
|
|
|
|
|
|
|
|
printCopyright();
|
2018-06-20 20:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
void Relay::parse()
|
2018-06-05 09:14:38 +02:00
|
|
|
{
|
2020-10-16 11:08:40 +02:00
|
|
|
int ret;
|
2019-04-19 14:52:52 +02:00
|
|
|
char c, *endptr;
|
2020-10-16 11:08:40 +02:00
|
|
|
while ((c = getopt (argc, argv, "hVp:P:ld:u:")) != -1) {
|
2019-04-19 14:52:52 +02:00
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
2021-02-16 14:15:14 +01:00
|
|
|
logging.setLevel(optarg);
|
2019-04-19 14:52:52 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
port = strtoul(optarg, &endptr, 10);
|
|
|
|
goto check;
|
|
|
|
|
|
|
|
case 'P':
|
|
|
|
protocol = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
loopback = true;
|
|
|
|
break;
|
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
case 'u':
|
|
|
|
ret = uuid_parse(optarg, uuid);
|
|
|
|
if (ret) {
|
|
|
|
logger->error("Failed to parse UUID: {}", optarg);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
case 'V':
|
|
|
|
printVersion();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
2019-04-17 18:46:18 +02:00
|
|
|
}
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
check: if (optarg == endptr) {
|
|
|
|
logger->error("Failed to parse parse option argument '-{} {}'", c, optarg);
|
2019-01-12 13:48:29 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2019-04-19 14:52:52 +02:00
|
|
|
}
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
if (argc - optind < 0) {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 20:55:17 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
int Relay::main() {
|
|
|
|
/* Start server */
|
|
|
|
lws_context_creation_info ctx_info = { 0 };
|
|
|
|
|
|
|
|
protocols[2].name = protocol.c_str();
|
|
|
|
|
|
|
|
ctx_info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS | LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
|
|
|
ctx_info.gid = -1;
|
|
|
|
ctx_info.uid = -1;
|
|
|
|
ctx_info.protocols = protocols.data();
|
|
|
|
ctx_info.extensions = extensions.data();
|
|
|
|
ctx_info.port = port;
|
|
|
|
ctx_info.mounts = &mount;
|
|
|
|
ctx_info.user = (void *) this;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
auto lwsLogger = logging.get("lws");
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
context = lws_create_context(&ctx_info);
|
|
|
|
if (context == nullptr) {
|
2021-02-16 14:15:14 +01:00
|
|
|
lwsLogger->error("Failed to initialize server context");
|
2019-04-19 14:52:52 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2019-01-21 10:33:32 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
vhost = lws_create_vhost(context, &ctx_info);
|
|
|
|
if (vhost == nullptr) {
|
2021-02-16 14:15:14 +01:00
|
|
|
lwsLogger->error("Failed to initialize virtual host");
|
2019-04-19 14:52:52 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2019-01-21 10:33:32 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
while (!stop)
|
|
|
|
lws_service(context, 100);
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
const std::vector<lws_extension> Relay::extensions = {
|
2020-11-11 21:20:16 +01:00
|
|
|
#ifdef LWS_DEFLATE_FOUND
|
2019-04-19 14:52:52 +02:00
|
|
|
{
|
|
|
|
"permessage-deflate",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"permessage-deflate"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"deflate-frame",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"deflate_frame"
|
|
|
|
},
|
2020-11-11 21:20:16 +01:00
|
|
|
#endif /* LWS_DEFLATE_FOUND */
|
2019-04-19 14:52:52 +02:00
|
|
|
{ nullptr /* terminator */ }
|
|
|
|
};
|
2018-06-05 09:14:38 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
const lws_http_mount Relay::mount = {
|
|
|
|
.mount_next = nullptr, /* linked-list "next" */
|
|
|
|
.mountpoint = "/api/v1", /* mountpoint URL */
|
|
|
|
.origin = nullptr, /* protocol */
|
|
|
|
.def = nullptr,
|
|
|
|
.protocol = "http-api",
|
|
|
|
.cgienv = nullptr,
|
|
|
|
.extra_mimetypes = nullptr,
|
|
|
|
.interpret = nullptr,
|
|
|
|
.cgi_timeout = 0,
|
|
|
|
.cache_max_age = 0,
|
|
|
|
.auth_mask = 0,
|
|
|
|
.cache_reusable = 0,
|
|
|
|
.cache_revalidate = 0,
|
|
|
|
.cache_intermediaries = 0,
|
|
|
|
.origin_protocol = LWSMPRO_CALLBACK, /* dynamic */
|
|
|
|
.mountpoint_len = 7, /* char count */
|
|
|
|
.basic_auth_login_file =nullptr,
|
|
|
|
};
|
|
|
|
|
2020-06-15 22:16:38 +02:00
|
|
|
} /* namespace tools */
|
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|
2019-04-19 14:52:52 +02:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-06-18 17:52:54 +02:00
|
|
|
villas::node::tools::Relay t(argc, argv);
|
2019-04-19 14:52:52 +02:00
|
|
|
|
|
|
|
return t.run();
|
2018-06-05 09:14:38 +02:00
|
|
|
}
|
2019-04-19 14:52:52 +02:00
|
|
|
|
|
|
|
|