Add support for the --client-option option

This commit is contained in:
Shuanglei Tao
2016-10-29 21:56:45 +08:00
parent a16fdfdbd4
commit 24c3f2a880
4 changed files with 30 additions and 2 deletions

View File

@@ -65,6 +65,7 @@ OPTIONS:
--signal, -s Signal to send to the command when exit it (default: SIGHUP) --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) --reconnect, -r Time to reconnect for the client in seconds (default: 10)
--readonly, -R Do not allow clients to write to the TTY --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 --check-origin, -O Do not allow websocket connection from different origin
--once, -o Accept only one client and exit on disconnection --once, -o Accept only one client and exit on disconnection
--ssl, -S Enable ssl --ssl, -S Enable ssl

View File

@@ -34,7 +34,11 @@ send_initial_message(struct lws *wsi) {
if (lws_write(wsi, p, (size_t) n, LWS_WRITE_TEXT) < n) { if (lws_write(wsi, p, (size_t) n, LWS_WRITE_TEXT) < n) {
return -1; 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; return 0;
} }
@@ -54,6 +58,7 @@ parse_window_size(const char *json) {
return NULL; return NULL;
} }
rows = json_object_get_int(o); rows = json_object_get_int(o);
json_object_put(obj);
struct winsize *size = t_malloc(sizeof(struct winsize)); struct winsize *size = t_malloc(sizeof(struct winsize));
memset(size, 0, sizeof(struct winsize)); memset(size, 0, sizeof(struct winsize));

View File

@@ -41,7 +41,7 @@ static const struct option options[] = {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{NULL, 0, 0, 0} {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() { void print_help() {
fprintf(stderr, "ttyd is a tool for sharing terminal over the web\n\n" 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" " --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" " --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" " --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" " --check-origin, -O Do not allow websocket connection from different origin\n"
" --once, -o Accept only one client and exit on disconnection\n" " --once, -o Accept only one client and exit on disconnection\n"
" --ssl, -S Enable ssl\n" " --ssl, -S Enable ssl\n"
@@ -193,6 +194,8 @@ main(int argc, char **argv) {
char key_path[1024] = ""; char key_path[1024] = "";
char ca_path[1024] = ""; char ca_path[1024] = "";
struct json_object *client_prefs = json_object_new_object();
// parse command line options // parse command line options
int c; int c;
while ((c = getopt_long(start, argv, opt_string, options, NULL)) != -1) { while ((c = getopt_long(start, argv, opt_string, options, NULL)) != -1) {
@@ -274,11 +277,28 @@ main(int argc, char **argv) {
break; break;
case '?': case '?':
break; 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: default:
print_help(); print_help();
return -1; 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) { if (server->command == NULL || strlen(server->command) == 0) {
fprintf(stderr, "ttyd: missing start command\n"); fprintf(stderr, "ttyd: missing start command\n");
@@ -360,6 +380,7 @@ main(int argc, char **argv) {
if (server->credential != NULL) if (server->credential != NULL)
t_free(server->credential); t_free(server->credential);
t_free(server->command); t_free(server->command);
t_free(server->prefs_json);
int i = 0; int i = 0;
do { do {
t_free(server->argv[i++]); t_free(server->argv[i++]);

View File

@@ -67,6 +67,7 @@ struct tty_client {
struct tty_server { struct tty_server {
LIST_HEAD(client, tty_client) clients; // client list LIST_HEAD(client, tty_client) clients; // client list
int client_count; // client count int client_count; // client count
char *prefs_json; // client preferences
char *credential; // encoded basic auth credential char *credential; // encoded basic auth credential
int reconnect; // reconnect timeout int reconnect; // reconnect timeout
char *command; // full command line char *command; // full command line