From 3580e80783022d1fd8baecc38ab8a00490c16b0c Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Thu, 6 Apr 2017 21:24:19 +0800 Subject: [PATCH] Add support for the --max-clients option --- README.md | 1 + man/ttyd.1 | 4 ++++ man/ttyd.man.md | 3 +++ src/http.c | 2 +- src/protocol.c | 4 ++++ src/server.c | 9 ++++++++- src/server.h | 1 + 7 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 850cac1..9d70ec7 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ OPTIONS: --readonly, -R Do not allow clients to write to the TTY --client-option, -t Send option to client (format: key=value), repeat to add more options --check-origin, -O Do not allow websocket connection from different origin + --max-clients, -m Maximum clients to support (default: 0, no limit) --once, -o Accept only one client and exit on disconnection --browser, -B Open terminal with the default system browser --index, -I Custom index.html path diff --git a/man/ttyd.1 b/man/ttyd.1 index b481ed0..c6a2546 100644 --- a/man/ttyd.1 +++ b/man/ttyd.1 @@ -72,6 +72,10 @@ Cross platform: macOS, Linux, FreeBSD, OpenWrt/LEDE, Windows \-O, \-\-check\-origin Do not allow websocket connection from different origin +.PP +\-m, \-\-max\-clients + Maximum clients to support (default: 0, no limit) + .PP \-o, \-\-once Accept only one client and exit on disconnection diff --git a/man/ttyd.man.md b/man/ttyd.man.md index 1a7f3ba..b09aac1 100644 --- a/man/ttyd.man.md +++ b/man/ttyd.man.md @@ -51,6 +51,9 @@ ttyd 1 "September 2016" ttyd "User Manual" -O, --check-origin Do not allow websocket connection from different origin + -m, --max-clients + Maximum clients to support (default: 0, no limit) + -o, --once Accept only one client and exit on disconnection diff --git a/src/http.c b/src/http.c index e704bed..7c6dc94 100644 --- a/src/http.c +++ b/src/http.c @@ -61,7 +61,7 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi } lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), rip, sizeof(rip)); - lwsl_notice("HTTP %s - %s (%s)\n", in, rip, name); + lwsl_notice("HTTP %s - %s (%s)\n", (char *) in, rip, name); switch (check_auth(wsi)) { case 0: diff --git a/src/protocol.c b/src/protocol.c index 15a82ab..35128e7 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -188,6 +188,10 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, lwsl_warn("refuse to serve WS client due to the --once option.\n"); return 1; } + if (server->max_clients > 0 && server->client_count == server->max_clients) { + lwsl_warn("refuse to serve WS client due to the --max-clients option.\n"); + return 1; + } if (lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI) <= 0 || strcmp(buf, WS_PATH)) { lwsl_warn("refuse to serve WS client for illegal ws path: %s\n", buf); return 1; diff --git a/src/server.c b/src/server.c index d863df6..50838a3 100644 --- a/src/server.c +++ b/src/server.c @@ -35,6 +35,7 @@ static const struct option options[] = { {"ssl-ca", required_argument, NULL, 'A'}, {"readonly", no_argument, NULL, 'R'}, {"check-origin", no_argument, NULL, 'O'}, + {"max-clients", required_argument, NULL, 'm'}, {"once", no_argument, NULL, 'o'}, {"browser", no_argument, NULL, 'B'}, {"debug", required_argument, NULL, 'd'}, @@ -42,7 +43,7 @@ static const struct option options[] = { {"help", no_argument, NULL, 'h'}, {NULL, 0, 0, 0} }; -static const char *opt_string = "p:i:c:u:g:s:r:I:aSC:K:A:Rt:OoBd:vh"; +static const char *opt_string = "p:i:c:u:g:s:r:I:aSC:K:A:Rt:Om:oBd:vh"; void print_help() { fprintf(stderr, "ttyd is a tool for sharing terminal over the web\n\n" @@ -62,6 +63,7 @@ void print_help() { " --readonly, -R Do not allow clients to write to the TTY\n" " --client-option, -t Send option to client (format: key=value), repeat to add more options\n" " --check-origin, -O Do not allow websocket connection from different origin\n" + " --max-clients, -m Maximum clients to support (default: 0, no limit)\n" " --once, -o Accept only one client and exit on disconnection\n" " --browser, -B Open terminal with the default system browser\n" " --index, -I Custom index.html path\n" @@ -251,6 +253,9 @@ main(int argc, char **argv) { case 'O': server->check_origin = true; break; + case 'm': + server->max_clients = atoi(optarg); + break; case 'o': server->once = true; break; @@ -418,6 +423,8 @@ main(int argc, char **argv) { lwsl_notice(" check origin: true\n"); if (server->readonly) lwsl_notice(" readonly: true\n"); + if (server->max_clients > 0) + lwsl_notice(" max clients: %d\n", server->max_clients); if (server->once) lwsl_notice(" once: true\n"); if (server->index != NULL) { diff --git a/src/server.h b/src/server.h index 0ba7ab2..93dac07 100644 --- a/src/server.h +++ b/src/server.h @@ -110,6 +110,7 @@ struct tty_server { char *sig_name; // human readable signal string bool readonly; // whether not allow clients to write to the TTY bool check_origin; // whether allow websocket connection from different origin + int max_clients; // maximum clients to support bool once; // whether accept only one client and exit on disconnection char *socket_path; // UNIX domain socket path pthread_mutex_t lock; -- 2.43.4