Skip to content

Commit 3b26e1a

Browse files
committed
Merge branch 'main' into lilac/group
2 parents 0766f8d + c5a21e0 commit 3b26e1a

File tree

4 files changed

+176
-15
lines changed

4 files changed

+176
-15
lines changed

registry/coder/modules/kasmvnc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module "kasmvnc" {
1818
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,6 +29,7 @@ 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."
@@ -47,28 +48,37 @@ variable "group" {
4748
default = null
4849
}
4950

51+
variable "subdomain" {
52+
type = bool
53+
default = true
54+
description = "Is subdomain sharing enabled in your cluster?"
55+
}
56+
5057
resource "coder_script" "kasm_vnc" {
5158
agent_id = var.agent_id
5259
display_name = "KasmVNC"
5360
icon = "/icon/kasmvnc.svg"
61+
run_on_start = true
5462
script = templatefile("${path.module}/run.sh", {
55-
PORT : var.port,
56-
DESKTOP_ENVIRONMENT : var.desktop_environment,
57-
KASM_VERSION : var.kasm_version
63+
PORT = var.port,
64+
DESKTOP_ENVIRONMENT = var.desktop_environment,
65+
KASM_VERSION = var.kasm_version
66+
SUBDOMAIN = tostring(var.subdomain)
67+
PATH_VNC_HTML = var.subdomain ? "" : file("${path.module}/path_vnc.html")
5868
})
59-
run_on_start = true
6069
}
6170

6271
resource "coder_app" "kasm_vnc" {
6372
agent_id = var.agent_id
6473
slug = "kasm-vnc"
65-
display_name = "kasmVNC"
74+
display_name = "KasmVNC"
6675
url = "http://localhost:${var.port}"
6776
icon = "/icon/kasmvnc.svg"
68-
subdomain = true
77+
subdomain = var.subdomain
6978
share = "owner"
7079
order = var.order
7180
group = var.group
81+
7282
healthcheck {
7383
url = "http://localhost:${var.port}/app"
7484
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)