diff --git a/minimal-examples/minimal-ws-proxy/CMakeLists.txt b/minimal-examples/minimal-ws-proxy/CMakeLists.txt new file mode 100644 index 00000000..3ec2949e --- /dev/null +++ b/minimal-examples/minimal-ws-proxy/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 2.8) + +set(SAMP lws-minimal-ws-proxy) +set(SRCS minimal-ws-proxy.c) + +if (UNIX) + set(CMAKE_C_FLAGS "-Wall -Wsign-compare -Wignored-qualifiers -Wtype-limits -Wuninitialized -Werror -Wundef ${CMAKE_C_FLAGS}" ) +endif() + +add_executable(${SAMP} ${SRCS}) +target_link_libraries(${SAMP} -lwebsockets) diff --git a/minimal-examples/minimal-ws-proxy/README.md b/minimal-examples/minimal-ws-proxy/README.md new file mode 100644 index 00000000..5c655007 --- /dev/null +++ b/minimal-examples/minimal-ws-proxy/README.md @@ -0,0 +1,38 @@ +# lws minimal ws proxy + +## Build + +``` + $ cmake . && make +``` + +## Description + +This is the same as minimal-ws-server-ring, but with the +inclusion of a ws client connection to https://libwebsockets.org +using the dumb-increment protocol feeding the ringbuffer. + +Each client that connect to this server receives the content that +had arrived on the client connection feeding the ringbuffer proxied +to their browser window over a ws connection. + +## Usage + +``` + $ ./lws-minimal-ws-proxy +[2018/03/14 17:50:10:6938] USER: LWS minimal ws proxy | visit http://localhost:7681 +[2018/03/14 17:50:10:6955] NOTICE: Creating Vhost 'default' port 7681, 2 protocols, IPv6 off +[2018/03/14 17:50:10:6955] NOTICE: Using non-SSL mode +[2018/03/14 17:50:10:7035] NOTICE: created client ssl context for default +[2018/03/14 17:50:11:7047] NOTICE: binding to lws-minimal-proxy +[2018/03/14 17:50:11:7047] NOTICE: lws_client_connect_2: 0x872e60: address libwebsockets.org +[2018/03/14 17:50:12:3282] NOTICE: lws_client_connect_2: 0x872e60: address libwebsockets.org +[2018/03/14 17:50:13:8195] USER: callback_minimal: established +``` + +Visit http://localhost:7681 on multiple browser windows + +Data received on the remote wss connection is copied to all open browser windows. + +A ringbuffer holds up to 8 lines of text in the server, and the browser shows +the last 20 lines of received text. diff --git a/minimal-examples/minimal-ws-proxy/minimal-ws-proxy.c b/minimal-examples/minimal-ws-proxy/minimal-ws-proxy.c new file mode 100644 index 00000000..2227b7d3 --- /dev/null +++ b/minimal-examples/minimal-ws-proxy/minimal-ws-proxy.c @@ -0,0 +1,90 @@ +/* + * lws-minimal-ws-proxy + * + * Copyright (C) 2018 Andy Green + * + * This file is made available under the Creative Commons CC0 1.0 + * Universal Public Domain Dedication. + * + * This demonstrates the most minimal http server you can make with lws, + * with an added websocket proxy distributing what is received on a + * dumb-increment wss connection to https://libwebsockets.org to all + * browsers connected to this server. + * + * To keep it simple, it serves stuff in the subdirectory "./mount-origin" of + * the directory it was started in. + * You can change that by changing mount.origin. + */ + +#include +#include +#include + +#define LWS_PLUGIN_STATIC +#include "protocol_lws_minimal.c" + +static struct lws_protocols protocols[] = { + { "http", lws_callback_http_dummy, 0, 0 }, + LWS_PLUGIN_PROTOCOL_MINIMAL, + { NULL, NULL, 0, 0 } /* terminator */ +}; + +static int interrupted; + +static const struct lws_http_mount mount = { + /* .mount_next */ NULL, /* linked-list "next" */ + /* .mountpoint */ "/", /* mountpoint URL */ + /* .origin */ "./mount-origin", /* serve from dir */ + /* .def */ "index.html", /* default filename */ + /* .protocol */ NULL, + /* .cgienv */ NULL, + /* .extra_mimetypes */ NULL, + /* .interpret */ NULL, + /* .cgi_timeout */ 0, + /* .cache_max_age */ 0, + /* .auth_mask */ 0, + /* .cache_reusable */ 0, + /* .cache_revalidate */ 0, + /* .cache_intermediaries */ 0, + /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */ + /* .mountpoint_len */ 1, /* char count */ + /* .basic_auth_login_file */ NULL, +}; + +void sigint_handler(int sig) +{ + interrupted = 1; +} + +int main(int argc, char **argv) +{ + struct lws_context_creation_info info; + struct lws_context *context; + int n = 0; + + signal(SIGINT, sigint_handler); + + memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ + info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; + info.port = 7681; + info.mounts = &mount; + info.protocols = protocols; + + lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_USER + /* | LLL_INFO */ /* | LLL_DEBUG */, NULL); + + lwsl_user("LWS minimal ws proxy | visit http://localhost:7681\n"); + + context = lws_create_context(&info); + if (!context) { + lwsl_err("lws init failed\n"); + return 1; + } + + while (n >= 0 && !interrupted) + n = lws_service(context, 1000); + + lws_context_destroy(context); + + return 0; +} diff --git a/minimal-examples/minimal-ws-proxy/mount-origin/favicon.ico b/minimal-examples/minimal-ws-proxy/mount-origin/favicon.ico new file mode 100644 index 00000000..c0cc2e3d Binary files /dev/null and b/minimal-examples/minimal-ws-proxy/mount-origin/favicon.ico differ diff --git a/minimal-examples/minimal-ws-proxy/mount-origin/index.html b/minimal-examples/minimal-ws-proxy/mount-origin/index.html new file mode 100644 index 00000000..0a312d88 --- /dev/null +++ b/minimal-examples/minimal-ws-proxy/mount-origin/index.html @@ -0,0 +1,91 @@ + + + +
+ + Minimal ws server proxy example.
+ The server makes a dumb-increment-protocol wss connection
+ to libwebsockets.org. It proxies what it was sent to
+ all browsers open on this page.
+ The textarea show the last 20 lines received. +
+
+
+ + + + + + diff --git a/minimal-examples/minimal-ws-proxy/mount-origin/libwebsockets.org-logo.png b/minimal-examples/minimal-ws-proxy/mount-origin/libwebsockets.org-logo.png new file mode 100644 index 00000000..2060a10c Binary files /dev/null and b/minimal-examples/minimal-ws-proxy/mount-origin/libwebsockets.org-logo.png differ diff --git a/minimal-examples/minimal-ws-proxy/protocol_lws_minimal.c b/minimal-examples/minimal-ws-proxy/protocol_lws_minimal.c new file mode 100644 index 00000000..2c55c9e8 --- /dev/null +++ b/minimal-examples/minimal-ws-proxy/protocol_lws_minimal.c @@ -0,0 +1,295 @@ +/* + * ws protocol handler plugin for "lws-minimal" + * + * Copyright (C) 2010-2018 Andy Green + * + * This file is made available under the Creative Commons CC0 1.0 + * Universal Public Domain Dedication. + * + * This version uses an lws_ring ringbuffer to cache up to 8 messages at a time, + * so it's not so easy to lose messages. + */ + +#if !defined (LWS_PLUGIN_STATIC) +#define LWS_DLL +#define LWS_INTERNAL +#include +#endif + +#include + +/* one of these created for each message */ + +struct msg { + void *payload; /* is malloc'd */ + size_t len; +}; + +/* one of these is created for each client connecting to us */ + +struct per_session_data__minimal { + struct per_session_data__minimal *pss_list; + struct lws *wsi; + uint32_t tail; +}; + +/* one of these is created for each vhost our protocol is used with */ + +struct per_vhost_data__minimal { + struct lws_context *context; + struct lws_vhost *vhost; + const struct lws_protocols *protocol; + + struct per_session_data__minimal *pss_list; /* linked-list of live pss*/ + + struct lws_ring *ring; /* ringbuffer holding unsent messages */ + struct lws_client_connect_info i; + struct lws *client_wsi; +}; + +/* destroys the message when everyone has had a copy of it */ + +static void +__minimal_destroy_message(void *_msg) +{ + struct msg *msg = _msg; + + free(msg->payload); + msg->payload = NULL; + msg->len = 0; +} + +static int +connect_client(struct per_vhost_data__minimal *vhd) +{ + vhd->i.context = vhd->context; + vhd->i.port = 443; + vhd->i.address = "libwebsockets.org"; + vhd->i.path = "/"; + vhd->i.host = vhd->i.address; + vhd->i.origin = vhd->i.address; + vhd->i.ssl_connection = 1; + + vhd->i.protocol = "dumb-increment-protocol"; + vhd->i.local_protocol_name = "lws-minimal-proxy"; + vhd->i.pwsi = &vhd->client_wsi; + + return !lws_client_connect_via_info(&vhd->i); +} + +static int +callback_minimal(struct lws *wsi, enum lws_callback_reasons reason, + void *user, void *in, size_t len) +{ + struct per_session_data__minimal *pss = + (struct per_session_data__minimal *)user; + struct per_vhost_data__minimal *vhd = + (struct per_vhost_data__minimal *) + lws_protocol_vh_priv_get(lws_get_vhost(wsi), + lws_get_protocol(wsi)); + const struct msg *pmsg; + struct msg amsg; + uint32_t oldest; + int n, m; + + switch (reason) { + + /* --- protocol lifecycle callbacks --- */ + + case LWS_CALLBACK_PROTOCOL_INIT: + vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi), + lws_get_protocol(wsi), + sizeof(struct per_vhost_data__minimal)); + vhd->context = lws_get_context(wsi); + vhd->protocol = lws_get_protocol(wsi); + vhd->vhost = lws_get_vhost(wsi); + + vhd->ring = lws_ring_create(sizeof(struct msg), 8, + __minimal_destroy_message); + if (connect_client(vhd)) + lws_timed_callback_vh_protocol(vhd->vhost, + vhd->protocol, + LWS_CALLBACK_USER, 1); + break; + + case LWS_CALLBACK_PROTOCOL_DESTROY: + lws_ring_destroy(vhd->ring); + break; + + /* --- serving callbacks --- */ + + case LWS_CALLBACK_ESTABLISHED: + /* add ourselves to the list of live pss held in the vhd */ + pss->pss_list = vhd->pss_list; + vhd->pss_list = pss; + pss->tail = lws_ring_get_oldest_tail(vhd->ring); + pss->wsi = wsi; + break; + + case LWS_CALLBACK_CLOSED: + /* remove our closing pss from the list of live pss */ + lws_start_foreach_llp(struct per_session_data__minimal **, + ppss, vhd->pss_list) { + if (*ppss == pss) { + *ppss = pss->pss_list; + break; + } + } lws_end_foreach_llp(ppss, pss_list); + break; + + case LWS_CALLBACK_SERVER_WRITEABLE: + pmsg = lws_ring_get_element(vhd->ring, &pss->tail); + if (!pmsg) + break; + + /* notice we allowed for LWS_PRE in the payload already */ + m = lws_write(wsi, pmsg->payload + LWS_PRE, pmsg->len, + LWS_WRITE_TEXT); + if (m < (int)pmsg->len) { + lwsl_err("ERROR %d writing to di socket\n", n); + return -1; + } + + n = lws_ring_get_oldest_tail(vhd->ring) == pss->tail; + lws_ring_consume(vhd->ring, &pss->tail, NULL, 1); + + if (n) { /* we may have been the oldest tail */ + n = 0; + oldest = pss->tail; + lws_start_foreach_llp( + struct per_session_data__minimal **, + ppss, vhd->pss_list) { + m = lws_ring_get_count_waiting_elements( + vhd->ring, &(*ppss)->tail); + if (m > n) { + n = m; + oldest = (*ppss)->tail; + } + } lws_end_foreach_llp(ppss, pss_list); + + /* this will delete any entries behind the new oldest */ + lws_ring_update_oldest_tail(vhd->ring, oldest); + } + + /* more to do? */ + if (lws_ring_get_element(vhd->ring, &pss->tail)) + /* come back as soon as we can write more */ + lws_callback_on_writable(pss->wsi); + break; + + /* --- client callbacks --- */ + + case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: + lwsl_err("CLIENT_CONNECTION_ERROR: %s\n", + in ? (char *)in : "(null)"); + vhd->client_wsi = NULL; + lws_timed_callback_vh_protocol(vhd->vhost, vhd->protocol, + LWS_CALLBACK_USER, 1); + break; + + case LWS_CALLBACK_CLIENT_ESTABLISHED: + lwsl_user("%s: established\n", __func__); + break; + + case LWS_CALLBACK_CLIENT_RECEIVE: + /* if no clients, just drop incoming */ + if (!vhd->pss_list) + break; + + n = (int)lws_ring_get_count_free_elements(vhd->ring); + if (!n) { + lwsl_user("dropping!\n"); + break; + } + + amsg.len = len; + /* notice we over-allocate by LWS_PRE */ + amsg.payload = malloc(LWS_PRE + len); + if (!amsg.payload) { + lwsl_user("OOM: dropping\n"); + break; + } + + memcpy((char *)amsg.payload + LWS_PRE, in, len); + if (!lws_ring_insert(vhd->ring, &amsg, 1)) { + __minimal_destroy_message(&amsg); + lwsl_user("dropping!\n"); + break; + } + + /* + * let everybody know we want to write something on them + * as soon as they are ready + */ + lws_start_foreach_llp(struct per_session_data__minimal **, + ppss, vhd->pss_list) { + lws_callback_on_writable((*ppss)->wsi); + } lws_end_foreach_llp(ppss, pss_list); + + break; + + case LWS_CALLBACK_CLIENT_CLOSED: + vhd->client_wsi = NULL; + lws_timed_callback_vh_protocol(vhd->vhost, vhd->protocol, + LWS_CALLBACK_USER, 1); + break; + + /* rate-limited client connect retries */ + + case LWS_CALLBACK_USER: + lwsl_notice("%s: LWS_CALLBACK_USER\n", __func__); + if (connect_client(vhd)) + lws_timed_callback_vh_protocol(vhd->vhost, + vhd->protocol, + LWS_CALLBACK_USER, 1); + break; + + default: + break; + } + + return 0; +} + +#define LWS_PLUGIN_PROTOCOL_MINIMAL \ + { \ + "lws-minimal-proxy", \ + callback_minimal, \ + sizeof(struct per_session_data__minimal), \ + 128, \ + 0, NULL, 0 \ + } + + +#if !defined (LWS_PLUGIN_STATIC) + +/* boilerplate needed if we are built as a dynamic plugin */ + +static const struct lws_protocols protocols[] = { + LWS_PLUGIN_PROTOCOL_MINIMAL +}; + +LWS_EXTERN LWS_VISIBLE int +init_protocol_minimal(struct lws_context *context, + struct lws_plugin_capability *c) +{ + if (c->api_magic != LWS_PLUGIN_API_MAGIC) { + lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC, + c->api_magic); + return 1; + } + + c->protocols = protocols; + c->count_protocols = ARRAY_SIZE(protocols); + c->extensions = NULL; + c->count_extensions = 0; + + return 0; +} + +LWS_EXTERN LWS_VISIBLE int +destroy_protocol_minimal(struct lws_context *context) +{ + return 0; +} +#endif