Add support for the --max-clients option

This commit is contained in:
Shuanglei Tao
2017-04-06 21:24:19 +08:00
parent 9cc5e4a03b
commit 3580e80783
7 changed files with 22 additions and 2 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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:

View File

@@ -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;

View File

@@ -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) {

View File

@@ -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;