-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
280 lines (252 loc) · 11.2 KB
/
Copy pathstart.sh
File metadata and controls
280 lines (252 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
# =============================================================================
# Remote Browser Kit — Production Startup Script
# =============================================================================
set -euo pipefail
# --------------------------------------------------------------------------- #
# Logging helpers #
# Fix: log to stdout only — /logs is a Windows volume mount and may be #
# owned by root. tee to file is optional and gracefully skipped if denied. #
# --------------------------------------------------------------------------- #
LOG_FILE="/logs/startup.log"
log() {
local msg
msg="[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] $*"
echo "$msg"
echo "$msg" >> "$LOG_FILE" 2>/dev/null || true
}
warn() {
local msg
msg="[$(date '+%Y-%m-%d %H:%M:%S')] [WARN] $*"
echo "$msg"
echo "$msg" >> "$LOG_FILE" 2>/dev/null || true
}
err() {
local msg
msg="[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR] $*"
echo "$msg" >&2
echo "$msg" >> "$LOG_FILE" 2>/dev/null || true
}
# Helper: redirect a background process log to file if writable, else /dev/null
logfile() {
local f="$1"
if touch "$f" 2>/dev/null; then
echo "$f"
else
echo "/dev/null"
fi
}
log "============================================================"
log "Remote Browser Kit starting up"
log "============================================================"
# --------------------------------------------------------------------------- #
# Configuration — all values come from environment with sane defaults #
# --------------------------------------------------------------------------- #
START_URL="${START_URL:-https://example.com}"
SCREEN_WIDTH="${SCREEN_WIDTH:-1920}"
SCREEN_HEIGHT="${SCREEN_HEIGHT:-1080}"
SCREEN_DEPTH="${SCREEN_DEPTH:-24}"
VNC_PORT="${VNC_PORT:-5900}"
NOVNC_PORT="${NOVNC_PORT:-6080}"
BROWSER_MODE="${BROWSER_MODE:-kiosk}" # kiosk | fullscreen | normal
INCOGNITO="${INCOGNITO:-false}"
VNC_PASS="${VNC_PASS:-}" # empty = no VNC password
SESSION_MODE="${SESSION_MODE:-fresh}" # fresh | persistent
BROWSER_ZOOM="${BROWSER_ZOOM:-1.0}"
DISPLAY_NAME=":1"
# --------------------------------------------------------------------------- #
# Browser selection #
# --------------------------------------------------------------------------- #
SUPPORTED_BROWSERS=(chromium firefox edge webkit)
BROWSER="$(echo "${BROWSER:-chromium}" | tr '[:upper:]' '[:lower:]')"
_supported=false
for b in "${SUPPORTED_BROWSERS[@]}"; do
[ "$b" = "$BROWSER" ] && _supported=true
done
if [ "$_supported" != "true" ]; then
err "Unsupported BROWSER='$BROWSER'. Supported values: ${SUPPORTED_BROWSERS[*]}"
exit 1
fi
# Chromium is always built in; Firefox/Edge/WebKit are opt-in build args
# (INCLUDE_FIREFOX/INCLUDE_EDGE/INCLUDE_WEBKIT) — /opt/browsers/.available-*
# is written at build time only for what was actually installed, so a
# requested-but-not-built browser fails clearly here instead of as a raw
# "command not found" once launch_browser() tries to run it.
if [ ! -f "/opt/browsers/.available-${BROWSER}" ]; then
err "BROWSER='$BROWSER' was requested, but this image was built without it."
err "Rebuild with the matching flag set to true in 'env', e.g. INCLUDE_${BROWSER^^}=true, then 'browser.cmd build'. See docs/browsers.md for the build matrix."
exit 1
fi
BROWSER_SCRIPT="/opt/browsers/${BROWSER}.sh"
if [ ! -f "$BROWSER_SCRIPT" ]; then
err "Internal error: no launcher script for '$BROWSER' at $BROWSER_SCRIPT"
exit 1
fi
# shellcheck source=/dev/null
source "$BROWSER_SCRIPT"
log "Configuration:"
log " BROWSER = $BROWSER ($BROWSER_LABEL)"
log " START_URL = $START_URL"
log " RESOLUTION = ${SCREEN_WIDTH}x${SCREEN_HEIGHT}x${SCREEN_DEPTH}"
log " BROWSER_MODE = $BROWSER_MODE"
log " INCOGNITO = $INCOGNITO"
log " SESSION_MODE = $SESSION_MODE"
log " BROWSER_ZOOM = $BROWSER_ZOOM"
log " VNC_PORT = $VNC_PORT"
log " NOVNC_PORT = $NOVNC_PORT"
log " VNC_PASSWORD = $([ -n "$VNC_PASS" ] && echo 'SET' || echo 'NONE (open)')"
export DISPLAY="$DISPLAY_NAME"
# --------------------------------------------------------------------------- #
# Clean up stale X server locks from previous runs #
# --------------------------------------------------------------------------- #
log "Cleaning up stale X locks..."
rm -f /tmp/.X1-lock /tmp/.X${DISPLAY_NAME#:}-lock
rm -rf /tmp/.X11-unix
mkdir -p /tmp/.X11-unix
chmod 1777 /tmp/.X11-unix
# --------------------------------------------------------------------------- #
# Start Xvfb virtual display #
# --------------------------------------------------------------------------- #
log "Starting Xvfb (${SCREEN_WIDTH}x${SCREEN_HEIGHT}x${SCREEN_DEPTH})..."
Xvfb "$DISPLAY_NAME" \
-screen 0 "${SCREEN_WIDTH}x${SCREEN_HEIGHT}x${SCREEN_DEPTH}" \
-ac \
-nolisten tcp \
> "$(logfile /logs/xvfb.log)" 2>&1 &
XVFB_PID=$!
log "Xvfb PID: $XVFB_PID"
# Wait until Xvfb is ready (up to 10s)
for i in $(seq 1 10); do
if xdpyinfo -display "$DISPLAY_NAME" >/dev/null 2>&1; then
log "Xvfb ready after ${i}s"
break
fi
if [ "$i" -eq 10 ]; then
err "Xvfb failed to start after 10 seconds — aborting"
exit 1
fi
sleep 1
done
# --------------------------------------------------------------------------- #
# Start Openbox window manager #
# --------------------------------------------------------------------------- #
log "Starting Openbox..."
openbox > "$(logfile /logs/openbox.log)" 2>&1 &
sleep 1
# --------------------------------------------------------------------------- #
# Session mode / profile directory #
# #
# Profiles are namespaced per browser (/data/profile/<browser> or #
# /tmp/<browser>-profile) so switching BROWSER on a persistent volume can't #
# hand one browser another browser's profile format. #
# #
# browser_seed_profile() writes each browser's settings template (password #
# manager disabled, download dir, zoom) into the *actual* profile directory #
# on every start. It has to be the real directory, not a fixed default #
# profile path baked into the image — --user-data-dir/-profile always #
# points elsewhere at launch, so a template copied anywhere else would #
# never actually be read by the browser. #
# --------------------------------------------------------------------------- #
if [ "$SESSION_MODE" = "persistent" ]; then
PROFILE_DIR="/data/profile/$BROWSER"
log "Session mode: PERSISTENT — profile at $PROFILE_DIR"
else
PROFILE_DIR="/tmp/${BROWSER}-profile"
log "Session mode: FRESH — ephemeral profile"
rm -rf "$PROFILE_DIR"
fi
mkdir -p "$PROFILE_DIR"
browser_seed_profile "$PROFILE_DIR"
# --------------------------------------------------------------------------- #
# VNC password setup #
# --------------------------------------------------------------------------- #
if [ -n "$VNC_PASS" ]; then
log "Configuring VNC password..."
mkdir -p "$HOME/.vnc"
x11vnc -storepasswd "$VNC_PASS" "$HOME/.vnc/passwd" 2>/dev/null
VNC_AUTH=(-rfbauth "$HOME/.vnc/passwd")
log "VNC password set successfully"
else
warn "VNC_PASS not set — VNC is open. Set VNC_PASS in the 'env' file."
VNC_AUTH=(-nopw)
fi
# --------------------------------------------------------------------------- #
# Start x11vnc #
# #
# IMPORTANT: #
# -ncache is intentionally NOT used. It multiplies screen height #
# (ncache 10 = height × 12 = 1080 × 12 = 12960) which causes Eggplant #
# and VNC clients to see wrong resolution. #
# #
# -clip locks the advertised framebuffer to exactly SCREEN_WIDTHxHEIGHT #
# so Eggplant always connects at the correct resolution. #
# --------------------------------------------------------------------------- #
log "Starting x11vnc on port $VNC_PORT..."
x11vnc \
-display "$DISPLAY_NAME" \
-forever \
"${VNC_AUTH[@]}" \
-listen 0.0.0.0 \
-shared \
-rfbport "$VNC_PORT" \
-xkb \
-noxdamage \
-clip "${SCREEN_WIDTH}x${SCREEN_HEIGHT}+0+0" \
> "$(logfile /logs/vnc.log)" 2>&1 &
VNC_PID=$!
log "x11vnc PID: $VNC_PID"
# Wait for VNC port to be listening (up to 15s)
for i in $(seq 1 15); do
if nc -z 127.0.0.1 "$VNC_PORT" 2>/dev/null; then
log "VNC ready on port $VNC_PORT after ${i}s"
break
fi
if [ "$i" -eq 15 ]; then
err "x11vnc did not open port $VNC_PORT — aborting"
exit 1
fi
sleep 1
done
# --------------------------------------------------------------------------- #
# Start noVNC web portal #
# --------------------------------------------------------------------------- #
log "Starting noVNC on port $NOVNC_PORT..."
websockify \
--web=/usr/share/novnc/ \
"$NOVNC_PORT" \
"localhost:$VNC_PORT" \
> "$(logfile /logs/novnc.log)" 2>&1 &
NOVNC_PID=$!
log "noVNC PID: $NOVNC_PID"
# Wait for noVNC port (up to 10s)
for i in $(seq 1 10); do
if nc -z 127.0.0.1 "$NOVNC_PORT" 2>/dev/null; then
log "noVNC ready on port $NOVNC_PORT after ${i}s"
break
fi
if [ "$i" -eq 10 ]; then
warn "noVNC did not open port $NOVNC_PORT — continuing anyway"
break
fi
sleep 1
done
log "============================================================"
log "All services started. Launching $BROWSER_LABEL -> $START_URL"
log " VNC: vnc://localhost:$VNC_PORT"
log " Web: http://localhost:$NOVNC_PORT/vnc.html"
log "============================================================"
# --------------------------------------------------------------------------- #
# Launch the selected browser — single run, no auto-restart #
# If the user closes it, it stays closed. VNC and noVNC remain alive so the #
# desktop is still accessible. Restart the container for a fresh session. #
# launch_browser() (defined in the sourced browsers/<name>.sh) must run the #
# browser as a plain foreground command, NOT exec — this script needs #
# control back afterwards to log and fall through to `wait`. #
# --------------------------------------------------------------------------- #
log "Starting $BROWSER_LABEL..."
launch_browser > "$(logfile /logs/browser.log)" 2>&1 || true
log "$BROWSER_LABEL has exited. VNC session remains active."
log "To relaunch: docker restart <container>"
log "To reset: browser.cmd restart <N>"
# Keep container alive — VNC + noVNC stay accessible after browser closes
wait