-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·210 lines (169 loc) · 6.52 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·210 lines (169 loc) · 6.52 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
#!/usr/bin/env bash
set -euo pipefail
: "${VNC_PASSWD:=vncpasswd}"
: "${SNOWLUMA_UID:=1000}"
: "${SNOWLUMA_GID:=1000}"
: "${SNOWLUMA_HOME:=/app/snowluma}"
: "${SNOWLUMA_DATA:=/app/snowluma-data}"
: "${SNOWLUMA_WEBUI_PORT:=5099}"
: "${SNOWLUMA_LOG_LEVEL:=info}"
: "${SNOWLUMA_SCREEN:=1920x1080x24}"
: "${SNOWLUMA_HOOK_AUTOLOAD:=1}"
: "${SNOWLUMA_EXTRA_QQ_HOMES:=}"
: "${SNOWLUMA_QQ_FLAGS:=--disable-gpu --disable-software-rasterizer --disable-gpu-compositing}"
export DISPLAY="${DISPLAY:-:1}"
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp/snowluma-${SNOWLUMA_UID}}"
export SNOWLUMA_HOOK_RUNTIME_DIR="${SNOWLUMA_HOOK_RUNTIME_DIR:-${XDG_RUNTIME_DIR}}"
export SNOWLUMA_LOG_LEVEL SNOWLUMA_HOOK_AUTOLOAD SNOWLUMA_EXTRA_QQ_HOMES SNOWLUMA_QQ_FLAGS
DISPLAY_NUM="${DISPLAY#:}"
DISPLAY_NUM="${DISPLAY_NUM%%.*}"
chmod 1777 /tmp || true
mkdir -p /tmp/.X11-unix
chmod 1777 /tmp/.X11-unix || true
rm -f /run/dbus/pid /run/dbus/system_bus_socket "/tmp/.X${DISPLAY_NUM}-lock" "/tmp/.X11-unix/X${DISPLAY_NUM}" /tmp/dbus-*
mkdir -p \
/var/run/dbus \
/root/.vnc \
"${XDG_RUNTIME_DIR}" \
"${SNOWLUMA_DATA}/config" \
/app/.cache \
/app/.config \
/app/.local/share \
/etc/supervisor/conf.d
ensure_machine_id() {
local persistent="${SNOWLUMA_DATA}/config/machine-id"
mkdir -p "$(dirname "$persistent")" || { echo "FATAL: cannot create machine-id persistent directory" >&2; exit 1; }
if [ ! -f "$persistent" ]; then
dbus-uuidgen > "$persistent" || { echo "FATAL: dbus-uuidgen failed to generate machine-id" >&2; exit 1; }
fi
ln -sf "$persistent" /etc/machine-id || { echo "FATAL: cannot symlink machine-id" >&2; exit 1; }
}
ensure_machine_id
groupmod -o -g "${SNOWLUMA_GID}" snowluma
usermod -o -u "${SNOWLUMA_UID}" -g "${SNOWLUMA_GID}" snowluma
chown -R "${SNOWLUMA_UID}:${SNOWLUMA_GID}" \
/app \
/opt/QQ \
"${XDG_RUNTIME_DIR}"
chmod 700 "${XDG_RUNTIME_DIR}"
# Docker restart can preserve stale AF_UNIX socket nodes in the container
# writable layer. Remove them before QQ starts so PID reuse cannot make a dead
# hook socket look like a live SnowLuma injection.
find "${XDG_RUNTIME_DIR}" -maxdepth 1 -type s -name 'mojo.*.*.sock' -delete 2>/dev/null || true
# Freeze the bundled QQ version. QQ has two update paths: the "立即更新" full-update
# dialog (click-gated -> never fires headless) and a SILENT hot-update channel
# (hotUpdateApi -> qqpatch.gtimg.cn) that swaps QQ files in the background. Only
# the silent one can change bytes without us, which would break the version-pinned
# nnphook. Black-hole that single host so the hot-update download can never land.
# QQ's main-process hot-update resolves via getaddrinfo, which honours /etc/hosts;
# Docker writes /etc/hosts once at container start and never rewrites it after, so
# this append is durable for the container's lifetime. Runs as root before QQ.
if ! grep -q 'qqpatch\.gtimg\.cn' /etc/hosts 2>/dev/null; then
printf '0.0.0.0 qqpatch.gtimg.cn\n' >> /etc/hosts
fi
generate_extra_qq_supervisor_conf() {
local conf="/etc/supervisor/conf.d/extra-qq.conf"
local homes="${SNOWLUMA_EXTRA_QQ_HOMES//,/ }"
local home
local index=1
local delay
rm -f "${conf}"
for home in ${homes}; do
[ -n "${home}" ] || continue
case "${home}" in
/app/*) ;;
*)
echo "Skipping extra QQ HOME '${home}': path must be under /app." >&2
continue
;;
esac
case "${home}" in
*[!A-Za-z0-9_@%+=:,./-]*)
echo "Skipping extra QQ HOME '${home}': unsupported characters in path." >&2
continue
;;
esac
mkdir -p "${home}"
chown -R "${SNOWLUMA_UID}:${SNOWLUMA_GID}" "${home}"
delay=$(( (index - 1) * 10 ))
cat >> "${conf}" <<EOF
[program:qq-extra-${index}]
command=/bin/sh -c 'sleep ${delay}; exec qq --no-sandbox %(ENV_SNOWLUMA_QQ_FLAGS)s'
directory=/app
user=snowluma
priority=15
autostart=true
autorestart=true
startsecs=3
stopasgroup=true
killasgroup=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
environment=HOME="${home}",DISPLAY="%(ENV_DISPLAY)s",XDG_RUNTIME_DIR="%(ENV_XDG_RUNTIME_DIR)s",DBUS_SESSION_BUS_ADDRESS="%(ENV_DBUS_SESSION_BUS_ADDRESS)s",SNOWLUMA_HOOK_RUNTIME_DIR="%(ENV_SNOWLUMA_HOOK_RUNTIME_DIR)s"
EOF
index=$((index + 1))
done
if [ "${index}" -gt 1 ]; then
echo "Configured $((index - 1)) extra QQ instance(s): ${SNOWLUMA_EXTRA_QQ_HOMES}"
fi
}
generate_extra_qq_supervisor_conf
node <<'NODE'
const fs = require('fs');
const path = require('path');
const dataDir = process.env.SNOWLUMA_DATA || '/app/snowluma-data';
const configDir = path.join(dataDir, 'config');
const runtimeConfigPath = path.join(configDir, 'runtime.json');
const requestedPort = Number(process.env.SNOWLUMA_WEBUI_PORT || 5099);
const webuiPort = Number.isInteger(requestedPort) && requestedPort > 0 && requestedPort <= 65535
? requestedPort
: 5099;
fs.mkdirSync(configDir, { recursive: true });
let runtimeConfig = {};
try {
runtimeConfig = JSON.parse(fs.readFileSync(runtimeConfigPath, 'utf8'));
} catch {
runtimeConfig = {};
}
runtimeConfig.webuiPort = webuiPort;
fs.writeFileSync(runtimeConfigPath, `${JSON.stringify(runtimeConfig, null, 2)}\n`, 'utf8');
NODE
# Node.js block above ran as root, so runtime.json is owned by root.
# SnowLuma runs as snowluma user and needs write access to config/.
chown "${SNOWLUMA_UID}:${SNOWLUMA_GID}" "${SNOWLUMA_DATA}/config" "${SNOWLUMA_DATA}/config/runtime.json"
x11vnc -storepasswd "${VNC_PASSWD}" /root/.vnc/passwd >/dev/null
wait_for_xvfb() {
local socket="/tmp/.X11-unix/X${DISPLAY_NUM}"
for _ in {1..200}; do
if [ -S "${socket}" ]; then
return 0
fi
if ! kill -0 "${XVFB_PID}" 2>/dev/null; then
echo "Xvfb exited before display ${DISPLAY} became ready." >&2
return 1
fi
sleep 0.1
done
echo "Timed out waiting for Xvfb display ${DISPLAY}." >&2
return 1
}
export DBUS_SESSION_BUS_ADDRESS=""
if DBUS_SESSION_BUS_ADDRESS=$(su -s /bin/bash -c 'dbus-daemon --session --fork --print-address' snowluma 2>/dev/null); then
export DBUS_SESSION_BUS_ADDRESS
fi
dbus-daemon --config-file=/usr/share/dbus-1/system.conf --print-address &
Xvfb "${DISPLAY}" -screen 0 "${SNOWLUMA_SCREEN}" &
XVFB_PID=$!
wait_for_xvfb
fluxbox &
x11vnc -display "${DISPLAY}" -noxrecord -noxfixes -noxdamage -forever -rfbauth /root/.vnc/passwd &
X11VNC_PID=$!
sleep 0.5
if ! kill -0 "${X11VNC_PID}" 2>/dev/null; then
echo "x11vnc failed to start for display ${DISPLAY}." >&2
exit 1
fi
nohup /opt/noVNC/utils/novnc_proxy --vnc localhost:5900 --listen 6081 --file-only >/var/log/novnc.log 2>&1 &
exec supervisord -c /etc/supervisord.conf