/** Simple WebSocket relay facilitating client-to-client connections. * * @author Steffen Vogel * @copyright 2018, Institute for Automation of Complex Power Systems, EONERC * @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 . *********************************************************************************/ #include #include #include #include #include #include #include #include #include #include /** The libwebsockets server context. */ static lws_context *context; /** The libwebsockets vhost. */ static lws_vhost *vhost; /* Forward declarations */ lws_callback_function protocol_cb; struct Session; struct Connection; static std::map> sessions; class InvalidUrlException { }; struct Options { bool loopback; } opts; class Frame : public std::vector { public: Frame() { // lws_write() requires LWS_PRE bytes in front of the payload insert(end(), LWS_PRE, 0); } uint8_t * data() { return std::vector::data() + LWS_PRE; } size_type size() { return std::vector::size() - LWS_PRE; } }; class Session { public: typedef std::string Identifier; static std::shared_ptr get(lws *wsi) { char uri[64]; /* 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' */ /* Get path of incoming request */ lws_hdr_copy(wsi, uri, sizeof(uri), WSI_TOKEN_GET_URI); if (strlen(uri) <= 0) throw InvalidUrlException(); Identifier sid = uri;//&uri[1]; auto it = sessions.find(sid); if (it == sessions.end()) { auto s = std::make_shared(sid); sessions[sid] = s; info("Creating new session: %s", sid.c_str()); return s; } else { info("Reusing existing session: %s", sid.c_str()); return it->second; } } Session(Identifier sid) : identifier(sid) { } ~Session() { } Identifier identifier; std::map> connections; }; class Connection { protected: lws *wsi; std::shared_ptr currentFrame; std::queue> outgoingFrames; std::shared_ptr session; public: Connection(lws *w) : wsi(w), currentFrame(std::make_shared()), outgoingFrames() { session = Session::get(sid); session->connections[wsi] = std::shared_ptr(this); info("New connection established to session: %s", session->identifier.c_str()); } ~Connection() { info("Connection closed"); session->connections.erase(wsi); } void write() { while (!outgoingFrames.empty()) { std::shared_ptr fr = outgoingFrames.front(); lws_write(wsi, fr->data(), fr->size(), LWS_WRITE_BINARY); outgoingFrames.pop(); } } void read(void *in, size_t len) { currentFrame->insert(currentFrame->end(), (uint8_t *) in, (uint8_t *) in + len); if (lws_is_final_fragment(wsi)) { debug(5, "Received frame, relaying to %zu connections", session->connections.size() - (opts.loopback ? 0 : 1)); for (auto p : session->connections) { auto c = p.second; /* We skip the current connection in order * to avoid receiving our own data */ if (opts.loopback == false && c.get() == this) continue; c->outgoingFrames.push(currentFrame); lws_callback_on_writable(c->wsi); } currentFrame = std::make_shared(); } } }; /** List of libwebsockets protocols. */ lws_protocols protocols[] = { { .name = "live", .callback = protocol_cb, .per_session_data_size = sizeof(Connection), .rx_buffer_size = 0 }, { NULL /* terminator */ } }; /** List of libwebsockets extensions. */ static const lws_extension extensions[] = { { "permessage-deflate", lws_extension_callback_pm_deflate, "permessage-deflate" }, { "deflate-frame", lws_extension_callback_pm_deflate, "deflate_frame" }, { NULL /* terminator */ } }; extern log *global_log; static void logger(int level, const char *msg) { int len = strlen(msg); if (strchr(msg, '\n')) len -= 1; /* Decrease severity for some errors. */ if (strstr(msg, "Unable to open") == msg) level = LLL_WARN; switch (level) { case LLL_ERR: log_print(global_log, CLR_RED("Web "), "%.*s", len, msg); break; case LLL_WARN: log_print(global_log, CLR_YEL("Web "), "%.*s", len, msg); break; case LLL_INFO: log_print(global_log, CLR_WHT("Web "), "%.*s", len, msg); break; default: log_print(global_log, "Web ", "%.*s", len, msg); break; } } int protocol_cb(lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { Connection *c = reinterpret_cast(user); switch (reason) { case LWS_CALLBACK_ESTABLISHED: auto s = Session::get(wsi); try { new (c) Connection(wsi); } catch (InvalidUrlException e) { lws_close_reason(wsi, LWS_CLOSE_STATUS_PROTOCOL_ERR, (unsigned char *) "Invalid URL", strlen("Invalid URL")); return -1; } break; case LWS_CALLBACK_CLOSED: c->~Connection(); break; case LWS_CALLBACK_SERVER_WRITEABLE: c->write(); break; case LWS_CALLBACK_RECEIVE: c->read(in, len); break; default: break; } return 0; } void usage() { std::cout << "Usage: villas-relay [OPTIONS]" << std::endl; std::cout << " OPTIONS is one or more of the following options:" << std::endl; std::cout << " -d LVL set debug level" << std::endl; std::cout << " -p PORT the port number to listen on" << std::endl; std::cout << " -p PROT the websocket protocol" << std::endl; std::cout << " -V show version and exit" << std::endl; std::cout << " -h show usage and exit" << std::endl; std::cout << std::endl; print_copyright(); } int main(int argc, char *argv[]) { lws_set_log_level((1 << LLL_COUNT) - 1, logger); /* Start server */ lws_context_creation_info ctx_info = { 0 }; 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; ctx_info.extensions = extensions; ctx_info.port = 8088; char c, *endptr; while ((c = getopt (argc, argv, "hVp:P:l")) != -1) { switch (c) { case 'p': ctx_info.port = strtoul(optarg, &endptr, 10); goto check; case 'P': protocols[0].name = optarg; break; case 'l': opts.loopback = true; break; case 'V': print_version(); exit(EXIT_SUCCESS); case 'h': case '?': usage(); exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS); } continue; check: if (optarg == endptr) error("Failed to parse parse option argument '-%c %s'", c, optarg); } if (argc - optind < 0) { usage(); exit(EXIT_FAILURE); } context = lws_create_context(&ctx_info); if (context == NULL) error("WebSocket: failed to initialize server context"); vhost = lws_create_vhost(context, &ctx_info); if (vhost == NULL) error("WebSocket: failed to initialize virtual host"); for (;;) lws_service(context, 100); return 0; }