Skip to content

Commit c5a21e0

Browse files
angrycubmatifali
andauthored
feat: add path-based sharing for kasm (#115)
Co-authored-by: Atif Ali <[email protected]>
1 parent 8b6a80b commit c5a21e0

File tree

4 files changed

+177
-16
lines changed

4 files changed

+177
-16
lines changed

registry/coder/modules/kasmvnc/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ Automatically install [KasmVNC](https://kasmweb.com/kasmvnc) in a workspace, and
1515
module "kasmvnc" {
1616
count = data.coder_workspace.me.start_count
1717
source = "registry.coder.com/coder/kasmvnc/coder"
18-
version = "1.0.23"
18+
version = "1.1.0"
1919
agent_id = coder_agent.example.id
2020
desktop_environment = "xfce"
21+
subdomain = true
2122
}
2223
```
2324

registry/coder/modules/kasmvnc/main.tf

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,42 @@ variable "kasm_version" {
2929
variable "desktop_environment" {
3030
type = string
3131
description = "Specifies the desktop environment of the workspace. This should be pre-installed on the workspace."
32+
3233
validation {
3334
condition = contains(["xfce", "kde", "gnome", "lxde", "lxqt"], var.desktop_environment)
3435
error_message = "Invalid desktop environment. Please specify a valid desktop environment."
3536
}
3637
}
3738

39+
variable "subdomain" {
40+
type = bool
41+
default = true
42+
description = "Is subdomain sharing enabled in your cluster?"
43+
}
44+
3845
resource "coder_script" "kasm_vnc" {
3946
agent_id = var.agent_id
4047
display_name = "KasmVNC"
4148
icon = "/icon/kasmvnc.svg"
49+
run_on_start = true
4250
script = templatefile("${path.module}/run.sh", {
43-
PORT : var.port,
44-
DESKTOP_ENVIRONMENT : var.desktop_environment,
45-
KASM_VERSION : var.kasm_version
51+
PORT = var.port,
52+
DESKTOP_ENVIRONMENT = var.desktop_environment,
53+
KASM_VERSION = var.kasm_version
54+
SUBDOMAIN = tostring(var.subdomain)
55+
PATH_VNC_HTML = var.subdomain ? "" : file("${path.module}/path_vnc.html")
4656
})
47-
run_on_start = true
4857
}
4958

5059
resource "coder_app" "kasm_vnc" {
5160
agent_id = var.agent_id
5261
slug = "kasm-vnc"
53-
display_name = "kasmVNC"
62+
display_name = "KasmVNC"
5463
url = "http://localhost:${var.port}"
5564
icon = "/icon/kasmvnc.svg"
56-
subdomain = true
65+
subdomain = var.subdomain
5766
share = "owner"
67+
5868
healthcheck {
5969
url = "http://localhost:${var.port}/app"
6070
interval = 5
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Path-Sharing Bounce Page</title>
5+
<style type="text/css">
6+
:root {
7+
color-scheme: light dark;
8+
--dark: #121212;
9+
--header-bg: rgba(127,127,127,0.2);
10+
--light: white;
11+
--rule-color: light-dark(rgba(0,0,0,0.8), rgba(255,255,255,0.8));
12+
background-color: light-dark(var(--light), var(--dark));
13+
color: light-dark(var(--dark), var(--light));
14+
}
15+
body, h1, p {
16+
box-sizing: border-box;
17+
margin:0; padding:0;
18+
}
19+
body{
20+
font-family:Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
21+
}
22+
h1{
23+
width: 100%;
24+
padding: 1rem;
25+
letter-spacing: -1.5pt;
26+
padding-bottom:10px;
27+
border-bottom: 1px solid var(--rule-color);
28+
background-color: var(--header-bg);
29+
}
30+
p {
31+
padding: 1rem; letter-spacing: -0.5pt;}
32+
a.indent { display:inline-block; padding-top:0.5rem; padding-left: 2rem; font-size:0.8rem }
33+
</style>
34+
<meta charset="UTF-8" />
35+
</head>
36+
<body>
37+
<h1>Path-Sharing Bounce Page</h1>
38+
<p>
39+
This application is being served via path sharing.
40+
If you are not redirected, <span id="help">check the
41+
Javascript console in your browser's developer tools
42+
for more information.</span>
43+
</p>
44+
</body>
45+
<script language="javascript">
46+
// This page exists to satisfy the querystring driven client API
47+
// specified here - https://raw.githubusercontent.com/kasmtech/noVNC/bce2d6a7048025c6e6c05df9d98b206c23f6dbab/docs/EMBEDDING.md
48+
// tl;dr:
49+
// * `host` - The WebSocket host to connect to.
50+
// This is just the hostname component of the original URL
51+
// * `port` - The WebSocket port to connect to.
52+
// It doesn't look like we need to set this unless it's different
53+
// than the incoming http request.
54+
// * `encrypt` - If TLS should be used for the WebSocket connection.
55+
// we base this on whether or not the protocol is `https`, seems
56+
// reasonable for now.
57+
// * `path` - The WebSocket path to use.
58+
// This apparently doesn't tolerate a leading `/` so we use a
59+
// function to tidy that up.
60+
function trimFirstCharIf(str, char) {
61+
return str.charAt(0) === char ? str.slice(1) : str;
62+
}
63+
function trimLastCharIf(str, char) {
64+
return str.endsWith("/") ? str.slice(0,str.length-1) : str;
65+
}
66+
const newloc = new URL(window.location);
67+
const h = document.getElementById("help")
68+
69+
// Building the websockify path must happen before we append the filename to newloc.pathname
70+
newloc.searchParams.append("path",
71+
trimLastCharIf(trimFirstCharIf(newloc.pathname,"/"),"/")+"/websockify");
72+
newloc.searchParams.append("encrypted", newloc.protocol==="https:"? true : false);
73+
74+
newloc.pathname += "vnc.html"
75+
console.log(newloc);
76+
77+
h.innerHTML = `click <a id="link" href="${newloc.toString()}">here</a> to go to the application.
78+
<br/><br/>The rewritten URL is:<br/><a id="link" class="indent" href="${newloc.toString()}">${newloc.toString()}</a>`
79+
window.location = newloc.href;
80+
</script>
81+
</html>

registry/coder/modules/kasmvnc/run.sh

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Exit on error, undefined variables, and pipe failures
44
set -euo pipefail
55

6+
error() { printf "💀 ERROR: %s\n" "$@"; exit 1; }
7+
68
# Function to check if vncserver is already installed
79
check_installed() {
810
if command -v vncserver &> /dev/null; then
@@ -188,7 +190,7 @@ if command -v sudo &> /dev/null && sudo -n true 2> /dev/null; then
188190
SUDO=sudo
189191
else
190192
kasm_config_file="$HOME/.vnc/kasmvnc.yaml"
191-
SUDO=
193+
SUDO=""
192194

193195
echo "WARNING: Sudo access not available, using user config dir!"
194196

@@ -206,6 +208,7 @@ echo "Writing KasmVNC config to $kasm_config_file"
206208
$SUDO tee "$kasm_config_file" > /dev/null << EOF
207209
network:
208210
protocol: http
211+
interface: 127.0.0.1
209212
websocket_port: ${PORT}
210213
ssl:
211214
require_ssl: false
@@ -220,16 +223,82 @@ EOF
220223
# and does not listen publicly
221224
echo -e "password\npassword\n" | vncpasswd -wo -u "$USER"
222225

226+
get_http_dir() {
227+
# determine the served file path
228+
# Start with the default
229+
httpd_directory="/usr/share/kasmvnc/www"
230+
231+
# Check the system configuration path
232+
if [[ -e /etc/kasmvnc/kasmvnc.yaml ]]; then
233+
d=($(grep -E "^\s*httpd_directory:.*$" /etc/kasmvnc/kasmvnc.yaml))
234+
# If this grep is successful, it will return:
235+
# httpd_directory: /usr/share/kasmvnc/www
236+
if [[ $${#d[@]} -eq 2 && -d "$${d[1]}" ]]; then
237+
httpd_directory="$${d[1]}"
238+
fi
239+
fi
240+
241+
# Check the home directory for overriding values
242+
if [[ -e "$HOME/.vnc/kasmvnc.yaml" ]]; then
243+
d=($(grep -E "^\s*httpd_directory:.*$" /etc/kasmvnc/kasmvnc.yaml))
244+
if [[ $${#d[@]} -eq 2 && -d "$${d[1]}" ]]; then
245+
httpd_directory="$${d[1]}"
246+
fi
247+
fi
248+
echo $httpd_directory
249+
}
250+
251+
fix_server_index_file(){
252+
local fname=$${FUNCNAME[0]} # gets current function name
253+
if [[ $# -ne 1 ]]; then
254+
error "$fname requires exactly 1 parameter:\n\tpath to KasmVNC httpd_directory"
255+
fi
256+
local httpdir="$1"
257+
if [[ ! -d "$httpdir" ]]; then
258+
error "$fname: $httpdir is not a directory"
259+
fi
260+
pushd "$httpdir" > /dev/null
261+
262+
cat <<'EOH' > /tmp/path_vnc.html
263+
${PATH_VNC_HTML}
264+
EOH
265+
$SUDO mv /tmp/path_vnc.html .
266+
# check for the switcheroo
267+
if [[ -f "index.html" && -L "vnc.html" ]]; then
268+
$SUDO mv $httpdir/index.html $httpdir/vnc.html
269+
fi
270+
$SUDO ln -s -f path_vnc.html index.html
271+
popd > /dev/null
272+
}
273+
274+
patch_kasm_http_files(){
275+
homedir=$(get_http_dir)
276+
fix_server_index_file "$homedir"
277+
}
278+
279+
if [[ "${SUBDOMAIN}" == "false" ]]; then
280+
echo "🩹 Patching up webserver files to support path-sharing..."
281+
patch_kasm_http_files
282+
fi
283+
284+
VNC_LOG="/tmp/kasmvncserver.log"
223285
# Start the server
224286
printf "🚀 Starting KasmVNC server...\n"
225-
vncserver -select-de "${DESKTOP_ENVIRONMENT}" -disableBasicAuth > /tmp/kasmvncserver.log 2>&1 &
226-
pid=$!
227-
228-
# Wait for server to start
229-
sleep 5
230-
grep -v '^[[:space:]]*$' /tmp/kasmvncserver.log | tail -n 10
231-
if ps -p $pid | grep -q "^$pid"; then
232-
echo "ERROR: Failed to start KasmVNC server. Check full logs at /tmp/kasmvncserver.log"
287+
288+
set +e
289+
vncserver -select-de "${DESKTOP_ENVIRONMENT}" -disableBasicAuth > "$VNC_LOG" 2>&1
290+
RETVAL=$?
291+
set -e
292+
293+
if [[ $RETVAL -ne 0 ]]; then
294+
echo "ERROR: Failed to start KasmVNC server. Return code: $RETVAL"
295+
if [[ -f "$VNC_LOG" ]]; then
296+
echo "Full logs:"
297+
cat "$VNC_LOG"
298+
else
299+
echo "ERROR: Log file not found: $VNC_LOG"
300+
fi
233301
exit 1
234302
fi
303+
235304
printf "🚀 KasmVNC server started successfully!\n"

0 commit comments

Comments
 (0)