protocol: rename tty_client to pss_tty

This commit is contained in:
Shuanglei Tao
2019-11-30 17:46:37 +08:00
parent 7911cc31e9
commit df6f3bd652
3 changed files with 115 additions and 115 deletions

View File

@@ -106,20 +106,20 @@ check_host_origin(struct lws *wsi) {
} }
void void
tty_client_free(struct tty_client *client) { pss_tty_free(struct pss_tty *pss) {
uv_read_stop((uv_stream_t *) &client->pipe); uv_read_stop((uv_stream_t *) &pss->pipe);
uv_signal_stop(&client->watcher); uv_signal_stop(&pss->watcher);
close(client->pty); close(pss->pty);
// free the buffer // free the buffer
if (client->buffer != NULL) if (pss->buffer != NULL)
free(client->buffer); free(pss->buffer);
if (client->pty_buffer != NULL) if (pss->pty_buffer != NULL)
free(client->pty_buffer); free(pss->pty_buffer);
for (int i = 0; i < client->argc; i++) { for (int i = 0; i < pss->argc; i++) {
free(client->args[i]); free(pss->args[i]);
} }
} }
@@ -131,56 +131,56 @@ alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
void void
read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) { read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
struct tty_client *client = (struct tty_client *) stream->data; struct pss_tty *pss = (struct pss_tty *) stream->data;
client->pty_len = nread; pss->pty_len = nread;
if (nread <= 0) { if (nread <= 0) {
if (nread == UV_ENOBUFS || nread == 0) if (nread == UV_ENOBUFS || nread == 0)
return; return;
if (nread == UV_EOF) if (nread == UV_EOF)
client->pty_len = 0; pss->pty_len = 0;
else else
lwsl_err("read_cb: %s\n", uv_err_name(nread)); lwsl_err("read_cb: %s\n", uv_err_name(nread));
client->pty_buffer = NULL; pss->pty_buffer = NULL;
} else { } else {
client->pty_buffer = xmalloc(LWS_PRE + 1 + (size_t ) nread); pss->pty_buffer = xmalloc(LWS_PRE + 1 + (size_t ) nread);
memcpy(client->pty_buffer + LWS_PRE + 1, buf->base, (size_t ) nread); memcpy(pss->pty_buffer + LWS_PRE + 1, buf->base, (size_t ) nread);
} }
free(buf->base); free(buf->base);
lws_callback_on_writable(client->wsi); lws_callback_on_writable(pss->wsi);
uv_read_stop(stream); uv_read_stop(stream);
} }
void void
child_cb(uv_signal_t *handle, int signum) { child_cb(uv_signal_t *handle, int signum) {
struct tty_client *client; struct pss_tty *pss;
pid_t pid; pid_t pid;
int status; int status;
client = (struct tty_client *) handle->data; pss = (struct pss_tty *) handle->data;
status = wait_proc(client->pid, &pid); status = wait_proc(pss->pid, &pid);
if (pid > 0) { if (pid > 0) {
lwsl_notice("process exited with code %d, pid: %d\n", status, pid); lwsl_notice("process exited with code %d, pid: %d\n", status, pid);
client->pid = 0; pss->pid = 0;
tty_client_free(client); pss_tty_free(pss);
} }
} }
int int
spawn_process(struct tty_client *client) { spawn_process(struct pss_tty *pss) {
// append url args to arguments // append url args to arguments
char *argv[server->argc + client->argc + 1]; char *argv[server->argc + pss->argc + 1];
int i, n = 0; int i, n = 0;
for (i = 0; i < server->argc; i++) { for (i = 0; i < server->argc; i++) {
argv[n++] = server->argv[i]; argv[n++] = server->argv[i];
} }
for (i = 0; i < client->argc; i++) { for (i = 0; i < pss->argc; i++) {
argv[n++] = client->args[i]; argv[n++] = pss->args[i];
} }
argv[n] = NULL; argv[n] = NULL;
uv_signal_start(&client->watcher, child_cb, SIGCHLD); uv_signal_start(&pss->watcher, child_cb, SIGCHLD);
int pty; int pty;
pid_t pid = forkpty(&pty, NULL, NULL, NULL); pid_t pid = forkpty(&pty, NULL, NULL, NULL);
@@ -191,7 +191,7 @@ spawn_process(struct tty_client *client) {
setenv("TERM", server->terminal_type, true); setenv("TERM", server->terminal_type, true);
#if LWS_LIBRARY_VERSION_NUMBER < 3001000 #if LWS_LIBRARY_VERSION_NUMBER < 3001000
// libwebsockets set FD_CLOEXEC since v3.1.0 // libwebsockets set FD_CLOEXEC since v3.1.0
close(lws_get_socket_fd(client->wsi)); close(lws_get_socket_fd(pss->wsi));
#endif #endif
if (execvp(argv[0], argv) < 0) { if (execvp(argv[0], argv) < 0) {
perror("execvp failed\n"); perror("execvp failed\n");
@@ -206,15 +206,15 @@ spawn_process(struct tty_client *client) {
} }
lwsl_notice("started process, pid: %d\n", pid); lwsl_notice("started process, pid: %d\n", pid);
client->pid = pid; pss->pid = pid;
client->pty = pty; pss->pty = pty;
if (client->size.ws_row > 0 && client->size.ws_col > 0) if (pss->size.ws_row > 0 && pss->size.ws_col > 0)
ioctl(client->pty, TIOCSWINSZ, &client->size); ioctl(pss->pty, TIOCSWINSZ, &pss->size);
client->pipe.data = client; pss->pipe.data = pss;
uv_pipe_open(&client->pipe, pty); uv_pipe_open(&pss->pipe, pty);
lws_callback_on_writable(client->wsi); lws_callback_on_writable(pss->wsi);
return 0; return 0;
} }
@@ -234,7 +234,7 @@ kill_process(pid_t pid, int sig) {
int int
callback_tty(struct lws *wsi, enum lws_callback_reasons reason, callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len) { void *user, void *in, size_t len) {
struct tty_client *client = (struct tty_client *) user; struct pss_tty *pss = (struct pss_tty *) user;
char buf[256]; char buf[256];
size_t n = 0; size_t n = 0;
@@ -260,25 +260,25 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
break; break;
case LWS_CALLBACK_ESTABLISHED: case LWS_CALLBACK_ESTABLISHED:
client->initialized = false; pss->initialized = false;
client->initial_cmd_index = 0; pss->initial_cmd_index = 0;
client->authenticated = false; pss->authenticated = false;
client->wsi = wsi; pss->wsi = wsi;
client->buffer = NULL; pss->buffer = NULL;
client->pty_len = 0; pss->pty_len = 0;
client->argc = 0; pss->argc = 0;
client->loop = server->loop; pss->loop = server->loop;
uv_pipe_init(client->loop, &client->pipe, 0); uv_pipe_init(pss->loop, &pss->pipe, 0);
uv_signal_init(client->loop, &client->watcher); uv_signal_init(pss->loop, &pss->watcher);
client->watcher.data = client; pss->watcher.data = pss;
if (server->url_arg) { if (server->url_arg) {
while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) { while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) {
if (strncmp(buf, "arg=", 4) == 0) { if (strncmp(buf, "arg=", 4) == 0) {
client->args = xrealloc(client->args, (client->argc + 1) * sizeof(char *)); pss->args = xrealloc(pss->args, (pss->argc + 1) * sizeof(char *));
client->args[client->argc] = strdup(&buf[4]); pss->args[pss->argc] = strdup(&buf[4]);
client->argc++; pss->argc++;
} }
} }
} }
@@ -288,69 +288,69 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI); lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI);
#if LWS_LIBRARY_VERSION_NUMBER >= 2004000 #if LWS_LIBRARY_VERSION_NUMBER >= 2004000
lws_get_peer_simple(lws_get_network_wsi(wsi), client->address, sizeof(client->address)); lws_get_peer_simple(lws_get_network_wsi(wsi), pss->address, sizeof(pss->address));
#else #else
char name[100]; char name[100];
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), client->address, sizeof(client->address)); lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), pss->address, sizeof(pss->address));
#endif #endif
lwsl_notice("WS %s - %s, clients: %d\n", buf, client->address, server->client_count); lwsl_notice("WS %s - %s, clients: %d\n", buf, pss->address, server->client_count);
break; break;
case LWS_CALLBACK_SERVER_WRITEABLE: case LWS_CALLBACK_SERVER_WRITEABLE:
if (!client->initialized) { if (!pss->initialized) {
if (client->initial_cmd_index == sizeof(initial_cmds)) { if (pss->initial_cmd_index == sizeof(initial_cmds)) {
client->initialized = true; pss->initialized = true;
uv_read_start((uv_stream_t *)& client->pipe, alloc_cb, read_cb); uv_read_start((uv_stream_t *)& pss->pipe, alloc_cb, read_cb);
break; break;
} }
if (send_initial_message(wsi, client->initial_cmd_index) < 0) { if (send_initial_message(wsi, pss->initial_cmd_index) < 0) {
lwsl_err("failed to send initial message, index: %d\n", client->initial_cmd_index); lwsl_err("failed to send initial message, index: %d\n", pss->initial_cmd_index);
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0); lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
return -1; return -1;
} }
client->initial_cmd_index++; pss->initial_cmd_index++;
lws_callback_on_writable(wsi); lws_callback_on_writable(wsi);
break; break;
} }
// read error or client exited, close connection // read error or client exited, close connection
if (client->pty_len == 0) { if (pss->pty_len == 0) {
lws_close_reason(wsi, LWS_CLOSE_STATUS_NORMAL, NULL, 0); lws_close_reason(wsi, LWS_CLOSE_STATUS_NORMAL, NULL, 0);
return 1; return 1;
} else if (client->pty_len < 0) { } else if (pss->pty_len < 0) {
lwsl_err("read error: %d (%s)\n", errno, strerror(errno)); lwsl_err("read error: %d (%s)\n", errno, strerror(errno));
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0); lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
return -1; return -1;
} }
if (client->pty_buffer == NULL) if (pss->pty_buffer == NULL)
break; break;
client->pty_buffer[LWS_PRE] = OUTPUT; pss->pty_buffer[LWS_PRE] = OUTPUT;
n = (size_t) (client->pty_len + 1); n = (size_t) (pss->pty_len + 1);
if (lws_write(wsi, (unsigned char *) client->pty_buffer + LWS_PRE, n, LWS_WRITE_BINARY) < n) { if (lws_write(wsi, (unsigned char *) pss->pty_buffer + LWS_PRE, n, LWS_WRITE_BINARY) < n) {
lwsl_err("write OUTPUT to WS\n"); lwsl_err("write OUTPUT to WS\n");
} }
free(client->pty_buffer); free(pss->pty_buffer);
client->pty_buffer = NULL; pss->pty_buffer = NULL;
uv_read_start((uv_stream_t *)& client->pipe, alloc_cb, read_cb); uv_read_start((uv_stream_t *)& pss->pipe, alloc_cb, read_cb);
break; break;
case LWS_CALLBACK_RECEIVE: case LWS_CALLBACK_RECEIVE:
if (client->buffer == NULL) { if (pss->buffer == NULL) {
client->buffer = xmalloc(len); pss->buffer = xmalloc(len);
client->len = len; pss->len = len;
memcpy(client->buffer, in, len); memcpy(pss->buffer, in, len);
} else { } else {
client->buffer = xrealloc(client->buffer, client->len + len); pss->buffer = xrealloc(pss->buffer, pss->len + len);
memcpy(client->buffer + client->len, in, len); memcpy(pss->buffer + pss->len, in, len);
client->len += len; pss->len += len;
} }
const char command = client->buffer[0]; const char command = pss->buffer[0];
// check auth // check auth
if (server->credential != NULL && !client->authenticated && command != JSON_DATA) { if (server->credential != NULL && !pss->authenticated && command != JSON_DATA) {
lwsl_warn("WS client not authenticated\n"); lwsl_warn("WS client not authenticated\n");
return 1; return 1;
} }
@@ -362,59 +362,59 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
switch (command) { switch (command) {
case INPUT: case INPUT:
if (client->pty == 0) if (pss->pty == 0)
break; break;
if (server->readonly) if (server->readonly)
return 0; return 0;
uv_buf_t b = { client->buffer + 1, client->len - 1 }; uv_buf_t b = { pss->buffer + 1, pss->len - 1 };
int err = uv_try_write((uv_stream_t *) &client->pipe, &b, 1); int err = uv_try_write((uv_stream_t *) &pss->pipe, &b, 1);
if (err < 0) { if (err < 0) {
lwsl_err("uv_try_write: %s\n", uv_err_name(err)); lwsl_err("uv_try_write: %s\n", uv_err_name(err));
return -1; return -1;
} }
break; break;
case RESIZE_TERMINAL: case RESIZE_TERMINAL:
if (parse_window_size(client->buffer + 1, &client->size) && client->pty > 0) { if (parse_window_size(pss->buffer + 1, &pss->size) && pss->pty > 0) {
if (ioctl(client->pty, TIOCSWINSZ, &client->size) == -1) { if (ioctl(pss->pty, TIOCSWINSZ, &pss->size) == -1) {
lwsl_err("ioctl TIOCSWINSZ: %d (%s)\n", errno, strerror(errno)); lwsl_err("ioctl TIOCSWINSZ: %d (%s)\n", errno, strerror(errno));
} }
} }
break; break;
case JSON_DATA: case JSON_DATA:
if (client->pid > 0) if (pss->pid > 0)
break; break;
if (server->credential != NULL) { if (server->credential != NULL) {
json_object *obj = json_tokener_parse(client->buffer); json_object *obj = json_tokener_parse(pss->buffer);
struct json_object *o = NULL; struct json_object *o = NULL;
if (json_object_object_get_ex(obj, "AuthToken", &o)) { if (json_object_object_get_ex(obj, "AuthToken", &o)) {
const char *token = json_object_get_string(o); const char *token = json_object_get_string(o);
if (token != NULL && !strcmp(token, server->credential)) if (token != NULL && !strcmp(token, server->credential))
client->authenticated = true; pss->authenticated = true;
else else
lwsl_warn("WS authentication failed with token: %s\n", token); lwsl_warn("WS authentication failed with token: %s\n", token);
} }
if (!client->authenticated) { if (!pss->authenticated) {
lws_close_reason(wsi, LWS_CLOSE_STATUS_POLICY_VIOLATION, NULL, 0); lws_close_reason(wsi, LWS_CLOSE_STATUS_POLICY_VIOLATION, NULL, 0);
return -1; return -1;
} }
} }
if (spawn_process(client) != 0) return 1; if (spawn_process(pss) != 0) return 1;
break; break;
default: default:
lwsl_warn("ignored unknown message type: %c\n", command); lwsl_warn("ignored unknown message type: %c\n", command);
break; break;
} }
if (client->buffer != NULL) { if (pss->buffer != NULL) {
free(client->buffer); free(pss->buffer);
client->buffer = NULL; pss->buffer = NULL;
} }
break; break;
case LWS_CALLBACK_CLOSED: case LWS_CALLBACK_CLOSED:
server->client_count--; server->client_count--;
lwsl_notice("WS closed from %s, clients: %d\n", client->address, server->client_count); lwsl_notice("WS closed from %s, clients: %d\n", pss->address, server->client_count);
kill_process(client->pid, server->sig_code); kill_process(pss->pid, server->sig_code);
if (server->once && server->client_count == 0) { if (server->once && server->client_count == 0) {
lwsl_notice("exiting due to the --once option.\n"); lwsl_notice("exiting due to the --once option.\n");
force_exit = true; force_exit = true;

View File

@@ -19,13 +19,13 @@
volatile bool force_exit = false; volatile bool force_exit = false;
struct lws_context *context; struct lws_context *context;
struct tty_server *server; struct server *server;
// websocket protocols // websocket protocols
static const struct lws_protocols protocols[] = { static const struct lws_protocols protocols[] = {
{"http-only", callback_http, sizeof(struct pss_http), 0}, {"http-only", callback_http, sizeof(struct pss_http), 0},
{"tty", callback_tty, sizeof(struct tty_client), 0}, {"tty", callback_tty, sizeof(struct pss_tty), 0},
{NULL, NULL, 0, 0} {NULL, NULL, 0, 0}
}; };
// websocket extensions // websocket extensions
@@ -101,14 +101,14 @@ void print_help() {
); );
} }
struct tty_server * struct server *
tty_server_new(int argc, char **argv, int start) { server_new(int argc, char **argv, int start) {
struct tty_server *ts; struct server *ts;
size_t cmd_len = 0; size_t cmd_len = 0;
ts = xmalloc(sizeof(struct tty_server)); ts = xmalloc(sizeof(struct server));
memset(ts, 0, sizeof(struct tty_server)); memset(ts, 0, sizeof(struct server));
ts->client_count = 0; ts->client_count = 0;
ts->sig_code = SIGHUP; ts->sig_code = SIGHUP;
sprintf(ts->terminal_type, "%s", "xterm-256color"); sprintf(ts->terminal_type, "%s", "xterm-256color");
@@ -146,7 +146,7 @@ tty_server_new(int argc, char **argv, int start) {
} }
void void
tty_server_free(struct tty_server *ts) { server_free(struct server *ts) {
if (ts == NULL) if (ts == NULL)
return; return;
if (ts->credential != NULL) if (ts->credential != NULL)
@@ -244,7 +244,7 @@ main(int argc, char **argv) {
} }
int start = calc_command_start(argc, argv); int start = calc_command_start(argc, argv);
server = tty_server_new(argc, argv, start); server = server_new(argc, argv, start);
struct lws_context_creation_info info; struct lws_context_creation_info info;
memset(&info, 0, sizeof(info)); memset(&info, 0, sizeof(info));
@@ -511,7 +511,7 @@ main(int argc, char **argv) {
lws_context_destroy(context); lws_context_destroy(context);
// cleanup // cleanup
tty_server_free(server); server_free(server);
return 0; return 0;
} }

View File

@@ -17,9 +17,16 @@
extern volatile bool force_exit; extern volatile bool force_exit;
extern struct lws_context *context; extern struct lws_context *context;
extern struct tty_server *server; extern struct server *server;
struct tty_client { struct pss_http {
char path[128];
char *buffer;
char *ptr;
size_t len;
};
struct pss_tty {
bool initialized; bool initialized;
int initial_cmd_index; int initial_cmd_index;
bool authenticated; bool authenticated;
@@ -42,14 +49,7 @@ struct tty_client {
uv_signal_t watcher; uv_signal_t watcher;
}; };
struct pss_http { struct server {
char path[128];
char *buffer;
char *ptr;
size_t len;
};
struct tty_server {
int client_count; // client count int client_count; // client count
char *prefs_json; // client preferences char *prefs_json; // client preferences
char *credential; // encoded basic auth credential char *credential; // encoded basic auth credential