<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="robots" content="noindex">
    <title>RPort RDP</title>
    <link rel="icon" href="data:,">
    <script src="https://cdn.jsdelivr.net/npm/guacamole-common-js@1.3.1/dist/guacamole-common.min.js" integrity="sha384-deUYIyC5VFNZ+fFDSZM1Ya51Rr66lZgHPlgVBf+pEKFLLvVP0JC58ORoShD1OJUh" crossorigin="anonymous"></script>
</head>

<body>
    <form method="POST" action="/" id="guacForm">
        <input type="hidden" name="token" id="token" value="{{.token}}"/>
        <input type="hidden" name="username" id="username" value="{{.username}}"/>
        <input type="hidden" name="domain" id="domain" value="{{.domain}}"/>
        <input type="hidden" name="security" id="security" value="{{.security}}"/>
        <input type="hidden" name="keyboard" id="keyboard" value="{{.keyboard}}"/>
        <input type="hidden" name="guac-error" id="guac-error" value=""/>
    </form>
    <div id="display">
    </div>
</body>
<script>
        const tunnel = new Guacamole.WebSocketTunnel('websocket-tunnel');

        const client = new Guacamole.Client(tunnel);

        client.onerror = (error) => {
            console.error(error);
            document.getElementById("guac-error").value = "Connection error: " + error.message;
            document.getElementById("guacForm").submit();
        };

        window.onunload = () => {
            client.disconnect();
        };

        document.getElementById("display").append(
            client.getDisplay().getElement());

        try {
            client.connect(queryString());
        } catch (error) {
            console.error(error);
        }

        let mouse = new Guacamole.Mouse(client.getDisplay().getElement());

        mouse.onmousedown = mouse.onmouseup = mouse.onmousemove = (mouseState) => {
            client.sendMouseState(mouseState);
        };

        let keyboard = new Guacamole.Keyboard(document);

        keyboard.onkeydown = (keysym) => {
            client.sendKeyEvent(1, keysym);
            return false;
        };

        keyboard.onkeyup = (keysym) => {
            client.sendKeyEvent(0, keysym);
            return false;
        };

        function queryString() {
            let token = document.getElementById("token").value

            return "token=" + token
        }

</script>
</html>