GUACAMOLE-221: Migrate Kubernetes handling of "argv" to guac_argv_*() convenience API.

This commit is contained in:
Michael Jumper 2020-07-01 19:11:52 -07:00
parent 08a57d3375
commit aa3a9cde6c
5 changed files with 52 additions and 169 deletions

View File

@ -29,172 +29,33 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
/** int guac_kubernetes_argv_callback(guac_user* user, const char* mimetype,
* All Kubernetes connection settings which may be updated by unprivileged const char* name, const char* value, void* data) {
* users through "argv" streams.
*/
typedef enum guac_kubernetes_argv_setting {
/**
* The color scheme of the terminal.
*/
GUAC_KUBERNETES_ARGV_SETTING_COLOR_SCHEME,
/**
* The name of the font family used by the terminal.
*/
GUAC_KUBERNETES_ARGV_SETTING_FONT_NAME,
/**
* The size of the font used by the terminal, in points.
*/
GUAC_KUBERNETES_ARGV_SETTING_FONT_SIZE
} guac_kubernetes_argv_setting;
/**
* The value or current status of a connection parameter received over an
* "argv" stream.
*/
typedef struct guac_kubernetes_argv {
/**
* The specific setting being updated.
*/
guac_kubernetes_argv_setting setting;
/**
* Buffer space for containing the received argument value.
*/
char buffer[GUAC_KUBERNETES_ARGV_MAX_LENGTH];
/**
* The number of bytes received so far.
*/
int length;
} guac_kubernetes_argv;
/**
* Handler for "blob" instructions which appends the data from received blobs
* to the end of the in-progress argument value buffer.
*
* @see guac_user_blob_handler
*/
static int guac_kubernetes_argv_blob_handler(guac_user* user,
guac_stream* stream, void* data, int length) {
guac_kubernetes_argv* argv = (guac_kubernetes_argv*) stream->data;
/* Calculate buffer size remaining, including space for null terminator,
* adjusting received length accordingly */
int remaining = sizeof(argv->buffer) - argv->length - 1;
if (length > remaining)
length = remaining;
/* Append received data to end of buffer */
memcpy(argv->buffer + argv->length, data, length);
argv->length += length;
return 0;
}
/**
* Handler for "end" instructions which applies the changes specified by the
* argument value buffer associated with the stream.
*
* @see guac_user_end_handler
*/
static int guac_kubernetes_argv_end_handler(guac_user* user,
guac_stream* stream) {
int size;
guac_client* client = user->client; guac_client* client = user->client;
guac_kubernetes_client* kubernetes_client = (guac_kubernetes_client*) client->data; guac_kubernetes_client* kubernetes_client = (guac_kubernetes_client*) client->data;
guac_terminal* terminal = kubernetes_client->term; guac_terminal* terminal = kubernetes_client->term;
/* Append null terminator to value */ /* Update color scheme */
guac_kubernetes_argv* argv = (guac_kubernetes_argv*) stream->data; if (strcmp(name, GUAC_KUBERNETES_ARGV_COLOR_SCHEME) == 0)
argv->buffer[argv->length] = '\0'; guac_terminal_apply_color_scheme(terminal, value);
/* Apply changes to chosen setting */ /* Update font name */
switch (argv->setting) { else if (strcmp(name, GUAC_KUBERNETES_ARGV_FONT_NAME) == 0)
guac_terminal_apply_font(terminal, value, -1, 0);
/* Update color scheme */
case GUAC_KUBERNETES_ARGV_SETTING_COLOR_SCHEME:
guac_terminal_apply_color_scheme(terminal, argv->buffer);
guac_client_stream_argv(client, client->socket, "text/plain",
"color-scheme", argv->buffer);
break;
/* Update font name */
case GUAC_KUBERNETES_ARGV_SETTING_FONT_NAME:
guac_terminal_apply_font(terminal, argv->buffer, -1, 0);
guac_client_stream_argv(client, client->socket, "text/plain",
"font-name", argv->buffer);
break;
/* Update font size */
case GUAC_KUBERNETES_ARGV_SETTING_FONT_SIZE:
/* Update only if font size is sane */
size = atoi(argv->buffer);
if (size > 0) {
guac_terminal_apply_font(terminal, NULL, size,
kubernetes_client->settings->resolution);
guac_client_stream_argv(client, client->socket, "text/plain",
"font-size", argv->buffer);
}
break;
/* Update only if font size is sane */
else if (strcmp(name, GUAC_KUBERNETES_ARGV_FONT_SIZE) == 0) {
int size = atoi(value);
if (size > 0)
guac_terminal_apply_font(terminal, NULL, size,
kubernetes_client->settings->resolution);
} }
/* Update Kubernetes terminal size */ /* Update Kubernetes terminal size */
guac_kubernetes_resize(client, terminal->term_height, guac_kubernetes_resize(client, terminal->term_height,
terminal->term_width); terminal->term_width);
free(argv);
return 0;
}
int guac_kubernetes_argv_handler(guac_user* user, guac_stream* stream,
char* mimetype, char* name) {
guac_kubernetes_argv_setting setting;
/* Allow users to update the color scheme and font details */
if (strcmp(name, "color-scheme") == 0)
setting = GUAC_KUBERNETES_ARGV_SETTING_COLOR_SCHEME;
else if (strcmp(name, "font-name") == 0)
setting = GUAC_KUBERNETES_ARGV_SETTING_FONT_NAME;
else if (strcmp(name, "font-size") == 0)
setting = GUAC_KUBERNETES_ARGV_SETTING_FONT_SIZE;
/* No other connection parameters may be updated */
else {
guac_protocol_send_ack(user->socket, stream, "Not allowed.",
GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN);
guac_socket_flush(user->socket);
return 0;
}
guac_kubernetes_argv* argv = malloc(sizeof(guac_kubernetes_argv));
argv->setting = setting;
argv->length = 0;
/* Prepare stream to receive argument value */
stream->blob_handler = guac_kubernetes_argv_blob_handler;
stream->end_handler = guac_kubernetes_argv_end_handler;
stream->data = argv;
/* Signal stream is ready */
guac_protocol_send_ack(user->socket, stream, "Ready for updated "
"parameter.", GUAC_PROTOCOL_STATUS_SUCCESS);
guac_socket_flush(user->socket);
return 0; return 0;
} }
@ -205,20 +66,21 @@ void* guac_kubernetes_send_current_argv(guac_user* user, void* data) {
guac_terminal* terminal = kubernetes_client->term; guac_terminal* terminal = kubernetes_client->term;
/* Send current color scheme */ /* Send current color scheme */
guac_user_stream_argv(user, user->socket, "text/plain", "color-scheme", guac_user_stream_argv(user, user->socket, "text/plain",
terminal->color_scheme); GUAC_KUBERNETES_ARGV_COLOR_SCHEME, terminal->color_scheme);
/* Send current font name */ /* Send current font name */
guac_user_stream_argv(user, user->socket, "text/plain", "font-name", guac_user_stream_argv(user, user->socket, "text/plain",
terminal->font_name); GUAC_KUBERNETES_ARGV_FONT_NAME, terminal->font_name);
/* Send current font size */ /* Send current font size */
char font_size[64]; char font_size[64];
sprintf(font_size, "%i", terminal->font_size); sprintf(font_size, "%i", terminal->font_size);
guac_user_stream_argv(user, user->socket, "text/plain", "font-size", guac_user_stream_argv(user, user->socket, "text/plain",
font_size); GUAC_KUBERNETES_ARGV_FONT_SIZE, font_size);
return NULL; return NULL;
} }

View File

@ -23,19 +23,32 @@
#include "config.h" #include "config.h"
#include <guacamole/argv.h>
#include <guacamole/user.h> #include <guacamole/user.h>
/** /**
* The maximum number of bytes to allow for any argument value received via an * The name of the parameter that specifies/updates the color scheme used by
* argv stream, including null terminator. * the terminal emulator.
*/ */
#define GUAC_KUBERNETES_ARGV_MAX_LENGTH 16384 #define GUAC_KUBERNETES_ARGV_COLOR_SCHEME "color-scheme"
/** /**
* Handles an incoming stream from a Guacamole "argv" instruction, updating the * The name of the parameter that specifies/updates the name of the font used
* given connection parameter if that parameter is allowed to be updated. * by the terminal emulator.
*/ */
guac_user_argv_handler guac_kubernetes_argv_handler; #define GUAC_KUBERNETES_ARGV_FONT_NAME "font-name"
/**
* The name of the parameter that specifies/updates the font size used by the
* terminal emulator.
*/
#define GUAC_KUBERNETES_ARGV_FONT_SIZE "font-size"
/**
* Handles a received argument value from a Guacamole "argv" instruction,
* updating the given connection parameter.
*/
guac_argv_callback guac_kubernetes_argv_callback;
/** /**
* Sends the current values of all non-sensitive parameters which may be set * Sends the current values of all non-sensitive parameters which may be set

View File

@ -17,12 +17,14 @@
* under the License. * under the License.
*/ */
#include "argv.h"
#include "client.h" #include "client.h"
#include "common/clipboard.h" #include "common/clipboard.h"
#include "kubernetes.h" #include "kubernetes.h"
#include "settings.h" #include "settings.h"
#include "user.h" #include "user.h"
#include <guacamole/argv.h>
#include <guacamole/client.h> #include <guacamole/client.h>
#include <libwebsockets.h> #include <libwebsockets.h>
@ -100,6 +102,11 @@ int guac_client_init(guac_client* client) {
client->join_handler = guac_kubernetes_user_join_handler; client->join_handler = guac_kubernetes_user_join_handler;
client->free_handler = guac_kubernetes_client_free_handler; client->free_handler = guac_kubernetes_client_free_handler;
/* Register handlers for argument values that may be sent after the handshake */
guac_argv_register(GUAC_KUBERNETES_ARGV_COLOR_SCHEME, guac_kubernetes_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
guac_argv_register(GUAC_KUBERNETES_ARGV_FONT_NAME, guac_kubernetes_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
guac_argv_register(GUAC_KUBERNETES_ARGV_FONT_SIZE, guac_kubernetes_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
/* Set locale and warn if not UTF-8 */ /* Set locale and warn if not UTF-8 */
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) { if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) {

View File

@ -17,6 +17,7 @@
* under the License. * under the License.
*/ */
#include "argv.h"
#include "settings.h" #include "settings.h"
#include <guacamole/user.h> #include <guacamole/user.h>
@ -35,9 +36,9 @@ const char* GUAC_KUBERNETES_CLIENT_ARGS[] = {
"client-key", "client-key",
"ca-cert", "ca-cert",
"ignore-cert", "ignore-cert",
"font-name", GUAC_KUBERNETES_ARGV_FONT_NAME,
"font-size", GUAC_KUBERNETES_ARGV_FONT_SIZE,
"color-scheme", GUAC_KUBERNETES_ARGV_COLOR_SCHEME,
"typescript-path", "typescript-path",
"typescript-name", "typescript-name",
"create-typescript-path", "create-typescript-path",

View File

@ -93,7 +93,7 @@ int guac_kubernetes_user_join_handler(guac_user* user, int argc, char** argv) {
user->pipe_handler = guac_kubernetes_pipe_handler; user->pipe_handler = guac_kubernetes_pipe_handler;
/* Updates to connection parameters */ /* Updates to connection parameters */
user->argv_handler = guac_kubernetes_argv_handler; user->argv_handler = guac_argv_handler;
/* Display size change events */ /* Display size change events */
user->size_handler = guac_kubernetes_user_size_handler; user->size_handler = guac_kubernetes_user_size_handler;