vadimpronin / guacamole-lite

Node.js library for creating Guacamole-compatible servers. Guacamole is a RDP/VNC/SSH/Telnet client for HTML5 browsers.
Apache License 2.0
247 stars 78 forks source link

wss Error #47

Open pawansingh00 opened 2 years ago

pawansingh00 commented 2 years ago

Hi,

I have the following code --

I run this on the remote machine which I want to access using - VNC

#!/usr/bin/env node

const GuacamoleLite = require('guacamole-lite');

const websocketOptions = {
    port: 8080 // we will accept connections to this port
};

const guacdOptions = {
    port: 4822 // port of guacd
};

const clientOptions = {
    log: {
        level: 'VERBOSE'
    },
    crypt: {
        cypher: 'AES-256-CBC',
        key: 'MySuperSecretKeyForParamsToken12'
    }
};

const guacServer = new GuacamoleLite(websocketOptions, guacdOptions, clientOptions);

And then

I try to connect to it using below code --

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Page Title</title>
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <link rel="stylesheet" href="">
        <style></style>

        <!-- Guacamole -->
        <script type="text/javascript"
            src="https://unpkg.com/browse/guacamole-common-js@1.3.0/dist/guacamole-common.js"></script>
    </head>

    <body>

        <!-- Display -->
        <div id="display"></div>

        <!-- Init -->
        <script type="text/javascript"> /* <![CDATA[ */

            // Get display div from document
            var display = document.getElementById("display");

            // Instantiate client, using an HTTP tunnel for communications.
            var guac = new Guacamole.Client(
                new Guacamole.WebSocketTunnel("ws://<PUBLIC_IP_OF_MACHINE>:8080")
            );

            // Add client to display div
            display.appendChild(guac.getDisplay().getElement());

            // Error handler
            guac.onerror = function(error) {
                console.log("Error :: ", error);
                alert(error);
            };

            // Connect
           guac.connect('token=<generated_token>');

            // Disconnect on close
            window.onunload = function() {
                guac.disconnect();
            }

        /* ]]> */ </script>

        <!-- Init -->
        <script type="text/javascript"> /* <![CDATA[ */

            // Mouse
            var mouse = new Guacamole.Mouse(guac.getDisplay().getElement());

            mouse.onmousedown = 
            mouse.onmouseup   =
            mouse.onmousemove = function(mouseState) {
                guac.sendMouseState(mouseState);
            };

            // Keyboard
            var keyboard = new Guacamole.Keyboard(document);

            keyboard.onkeydown = function (keysym) {
                guac.sendKeyEvent(1, keysym);
            };

            keyboard.onkeyup = function (keysym) {
                guac.sendKeyEvent(0, keysym);
            };

        /* ]]> */ </script>

    </body>

</html>

In above code - - this is public IP of the machine which I want to access using VNC and - - this is a token generated using the below script ---

const crypto = require('crypto');

const clientOptions = {
    cypher: 'AES-256-CBC',
    key: 'MySuperSecretKeyForParamsToken12'
}

const encrypt = (value) => {
    const iv = crypto.randomBytes(16);
    const cipher = crypto.createCipheriv(clientOptions.cypher, clientOptions.key, iv);

    let crypted = cipher.update(JSON.stringify(value), 'utf8', 'base64');
    crypted += cipher.final('base64');

    const data = {
        iv: iv.toString('base64'),
        value: crypted
    };

    return new Buffer(JSON.stringify(data)).toString('base64');
};

console.log("Token : " + encrypt({
    "connection": {
        "type": "vnc",
        "settings": {
            "hostname": "<PUBLIC_IP_OF_MACHINE>",
            "port": "5900",
            "username": "pawan",
            "password": "pawan"           
        }
    }
}));

It works when -- above index.html file is served over http.

When I serve the above - index.html file over https and modify
this

            var guac = new Guacamole.Client(
                new Guacamole.WebSocketTunnel("ws://<PUBLIC_IP_OF_MACHINE>:8080")
            );

as

            var guac = new Guacamole.Client(
                new Guacamole.WebSocketTunnel("ws://<PUBLIC_IP_OF_MACHINE>:8080")
            );

I get the below error in Browser console -

wss -- guacamole-common.js:13108 WebSocket connection to 'wss://<PUBLIC_IP_OF_MACHINE>:8080/?token=<token>' failed: connect@guacamole-common.js:13108connect@guacamole-common.js:3336(anonymous)@?userId=9had87:50

Any help is much appreciated.