cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

user.c (3607B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "argv.h"
     21#include "clipboard.h"
     22#include "common/cursor.h"
     23#include "input.h"
     24#include "kubernetes.h"
     25#include "pipe.h"
     26#include "settings.h"
     27#include "terminal/terminal.h"
     28#include "user.h"
     29
     30#include <guacamole/client.h>
     31#include <guacamole/protocol.h>
     32#include <guacamole/socket.h>
     33#include <guacamole/user.h>
     34
     35#include <pthread.h>
     36#include <stdlib.h>
     37
     38int guac_kubernetes_user_join_handler(guac_user* user, int argc, char** argv) {
     39
     40    guac_client* client = user->client;
     41    guac_kubernetes_client* kubernetes_client =
     42        (guac_kubernetes_client*) client->data;
     43
     44    /* Parse provided arguments */
     45    guac_kubernetes_settings* settings = guac_kubernetes_parse_args(user,
     46            argc, (const char**) argv);
     47
     48    /* Fail if settings cannot be parsed */
     49    if (settings == NULL) {
     50        guac_user_log(user, GUAC_LOG_INFO,
     51                "Badly formatted client arguments.");
     52        return 1;
     53    }
     54
     55    /* Store settings at user level */
     56    user->data = settings;
     57
     58    /* Connect to Kubernetes if owner */
     59    if (user->owner) {
     60
     61        /* Store owner's settings at client level */
     62        kubernetes_client->settings = settings;
     63
     64        /* Start client thread */
     65        if (pthread_create(&(kubernetes_client->client_thread), NULL,
     66                    guac_kubernetes_client_thread, (void*) client)) {
     67            guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
     68                    "Unable to start Kubernetes client thread");
     69            return 1;
     70        }
     71
     72    }
     73
     74    /* Only handle events if not read-only */
     75    if (!settings->read_only) {
     76
     77        /* General mouse/keyboard events */
     78        user->key_handler = guac_kubernetes_user_key_handler;
     79        user->mouse_handler = guac_kubernetes_user_mouse_handler;
     80
     81        /* Inbound (client to server) clipboard transfer */
     82        if (!settings->disable_paste)
     83            user->clipboard_handler = guac_kubernetes_clipboard_handler;
     84
     85        /* STDIN redirection */
     86        user->pipe_handler = guac_kubernetes_pipe_handler;
     87
     88        /* Updates to connection parameters */
     89        user->argv_handler = guac_argv_handler;
     90
     91        /* Display size change events */
     92        user->size_handler = guac_kubernetes_user_size_handler;
     93
     94    }
     95
     96    return 0;
     97
     98}
     99
    100int guac_kubernetes_user_leave_handler(guac_user* user) {
    101
    102    guac_kubernetes_client* kubernetes_client =
    103        (guac_kubernetes_client*) user->client->data;
    104
    105    /* Remove the user from the terminal */
    106    guac_terminal_remove_user(kubernetes_client->term, user);
    107
    108    /* Free settings if not owner (owner settings will be freed with client) */
    109    if (!user->owner) {
    110        guac_kubernetes_settings* settings =
    111            (guac_kubernetes_settings*) user->data;
    112        guac_kubernetes_settings_free(settings);
    113    }
    114
    115    return 0;
    116}
    117