From 24c3f2a880df947f55f091d9b6486b349fa98eed Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Sat, 29 Oct 2016 21:56:45 +0800 Subject: [PATCH] Add support for the --client-option option --- README.md | 1 + src/protocol.c | 7 ++++++- src/server.c | 23 ++++++++++++++++++++++- src/server.h | 1 + 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 80d8083..4d2ee54 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ OPTIONS: --signal, -s Signal to send to the command when exit it (default: SIGHUP) --reconnect, -r Time to reconnect for the client in seconds (default: 10) --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 --once, -o Accept only one client and exit on disconnection --ssl, -S Enable ssl diff --git a/src/protocol.c b/src/protocol.c index d7b5c6f..2ea254b 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -34,7 +34,11 @@ send_initial_message(struct lws *wsi) { if (lws_write(wsi, p, (size_t) n, LWS_WRITE_TEXT) < n) { return -1; } - + // client preferences + n = sprintf((char *) p, "%c%s", SET_PREFERENCES, server->prefs_json); + if (lws_write(wsi, p, (size_t) n, LWS_WRITE_TEXT) < n) { + return -1; + } return 0; } @@ -54,6 +58,7 @@ parse_window_size(const char *json) { return NULL; } rows = json_object_get_int(o); + json_object_put(obj); struct winsize *size = t_malloc(sizeof(struct winsize)); memset(size, 0, sizeof(struct winsize)); diff --git a/src/server.c b/src/server.c index 465d227..86a72cb 100644 --- a/src/server.c +++ b/src/server.c @@ -41,7 +41,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:aSC:K:A:ROod:vh"; +static const char *opt_string = "p:i:c:u:g:s:r:aSC:K:A:Rt:Ood:vh"; void print_help() { fprintf(stderr, "ttyd is a tool for sharing terminal over the web\n\n" @@ -58,6 +58,7 @@ void print_help() { " --signal, -s Signal to send to the command when exit it (default: SIGHUP)\n" " --reconnect, -r Time to reconnect for the client in seconds (default: 10)\n" " --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" " --once, -o Accept only one client and exit on disconnection\n" " --ssl, -S Enable ssl\n" @@ -193,6 +194,8 @@ main(int argc, char **argv) { char key_path[1024] = ""; char ca_path[1024] = ""; + struct json_object *client_prefs = json_object_new_object(); + // parse command line options int c; while ((c = getopt_long(start, argv, opt_string, options, NULL)) != -1) { @@ -274,11 +277,28 @@ main(int argc, char **argv) { break; case '?': break; + case 't': + optind--; + for(;optind < start && *argv[optind] != '-'; optind++){ + char *option =strdup(optarg); + char *key = strsep(&option, "="); + if (key == NULL) { + fprintf(stderr, "ttyd: invalid client option: %s, format: key=value\n", optarg); + return -1; + } + char *value = strsep(&option, "="); + t_free(option); + struct json_object *obj = json_tokener_parse(value); + json_object_object_add(client_prefs, key, obj != NULL ? obj : json_object_new_string(value)); + } + break; default: print_help(); return -1; } } + server->prefs_json = strdup(json_object_to_json_string(client_prefs)); + json_object_put(client_prefs); if (server->command == NULL || strlen(server->command) == 0) { fprintf(stderr, "ttyd: missing start command\n"); @@ -360,6 +380,7 @@ main(int argc, char **argv) { if (server->credential != NULL) t_free(server->credential); t_free(server->command); + t_free(server->prefs_json); int i = 0; do { t_free(server->argv[i++]); diff --git a/src/server.h b/src/server.h index b61077c..eb1235a 100644 --- a/src/server.h +++ b/src/server.h @@ -67,6 +67,7 @@ struct tty_client { struct tty_server { LIST_HEAD(client, tty_client) clients; // client list int client_count; // client count + char *prefs_json; // client preferences char *credential; // encoded basic auth credential int reconnect; // reconnect timeout char *command; // full command line -- 2.43.4