Initial commit: CTmine-client (ContainerMine Client)
Docker-WebApp, die einen echten Minecraft Java-Client (Prism Launcher) in einem Container mit virtuellem Display betreibt und per noVNC im Browser anzeigt. Gedacht, um Accounts AFK an Farmen zu stellen. - Multi-Stage Dockerfile: Vite/Vue-Build + Ubuntu-Runtime (Xvfb, x11vnc, websockify, noVNC, openbox, nginx, Prism Launcher 11.0.3) - Vue 3 + Vite + TypeScript Dashboard (noVNC-iframe, Start/Stop, Status-Anzeige, Schnellstart-Anleitung) - Node-Status-API ohne externe Dependencies (/api/status, /api/mc/*) - docker-compose.yml mit PUID/PGID, konfigurierbarer Auflösung, shm_size - Persistente Accounts/Instanzen in /config (Volume)
This commit is contained in:
commit
b8a99e6e9e
25 changed files with 1766 additions and 0 deletions
98
docker/entrypoint.sh
Normal file
98
docker/entrypoint.sh
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
#!/usr/bin/env bash
|
||||
# CTmine-client Entrypoint.
|
||||
# Richtet den Runtime-Benutzer, /config und die display-bezogenen Konfigurationen
|
||||
# ein und startet anschließend supervisord, der alle Prozesse verwaltet.
|
||||
set -euo pipefail
|
||||
|
||||
PUID="${PUID:-1000}"
|
||||
PGID="${PGID:-1000}"
|
||||
TZ="${TZ:-Europe/Berlin}"
|
||||
CONFIG_DIR="${CONFIG_DIR:-/config}"
|
||||
WEB_ROOT="${WEB_ROOT:-/var/www/ctmine}"
|
||||
|
||||
# TimeZone setzen, sofern tzdata vorhanden.
|
||||
if [ -f "/usr/share/zoneinfo/${TZ}" ]; then
|
||||
ln -snf "/usr/share/zoneinfo/${TZ}" /etc/localtime
|
||||
echo "${TZ}" > /etc/timezone
|
||||
fi
|
||||
|
||||
echo "[entrypoint] PUID=${PUID} PGID=${PGID} TZ=${TZ}"
|
||||
|
||||
# --- /config anlegen und dem Runtime-Benutzer übergeben ------------------
|
||||
mkdir -p "${CONFIG_DIR}/prism" "${CONFIG_DIR}/.local"
|
||||
|
||||
# Gruppe/Benutzer an PUID/PGID anpassen (der 'ctmine'-Benutzer wird im
|
||||
# Dockerfile mit UID 1000 angelegt). So gehören Dateien im Volume dem
|
||||
# Host-Benutzer.
|
||||
if [ "$(id -u)" = "0" ]; then
|
||||
groupmod -o -g "${PGID}" ctmine 2>/dev/null || true
|
||||
usermod -o -u "${PUID}" -g "${PGID}" -d "${CONFIG_DIR}" ctmine 2>/dev/null || true
|
||||
chown -R ctmine:ctmine "${CONFIG_DIR}"
|
||||
fi
|
||||
|
||||
# --- Display-Auflösung in Supervisor-Config schreiben --------------------
|
||||
# supervisord kann keine ENV-Substitution, darum generieren wir Xvfb-Start
|
||||
# und x11vnc-Optionen hier als feste Dateien.
|
||||
DISPLAY_WIDTH="${DISPLAY_WIDTH:-1280}"
|
||||
DISPLAY_HEIGHT="${DISPLAY_HEIGHT:-720}"
|
||||
DISPLAY_REFRESH="${DISPLAY_REFRESH:-60}"
|
||||
DISPLAY_DEPTH="${DISPLAY_DEPTH:-24}"
|
||||
|
||||
cat > /tmp/xvfb.conf <<EOF
|
||||
[program:xvfb]
|
||||
command=/usr/bin/Xvfb :0 -screen 0 ${DISPLAY_WIDTH}x${DISPLAY_HEIGHT}x${DISPLAY_DEPTH} -ac -nolisten tcp
|
||||
priority=10
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stdout_logfile=/dev/fd/1
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/fd/2
|
||||
stderr_logfile_maxbytes=0
|
||||
environment=HOME="/config"
|
||||
EOF
|
||||
|
||||
# VNC-Passwort-Datei (optional). Ohne Passwort läuft x11vnc ohne Auth —
|
||||
# nur für vertrauenswürdige lokale Netze gedacht.
|
||||
VNC_PASSWORD="${VNC_PASSWORD:-}"
|
||||
VNC_PWFILE="${CONFIG_DIR}/.vncpasswd"
|
||||
if [ -n "${VNC_PASSWORD}" ]; then
|
||||
printf '%s\n' "${VNC_PASSWORD}" | vncpasswd -f > "${VNC_PWFILE}"
|
||||
chmod 600 "${VNC_PWFILE}"
|
||||
chown ctmine:ctmine "${VNC_PWFILE}"
|
||||
VNC_PWFLAG="-rfbauth ${VNC_PWFILE}"
|
||||
else
|
||||
rm -f "${VNC_PWFILE}"
|
||||
VNC_PWFLAG="-nopw"
|
||||
fi
|
||||
|
||||
cat > /tmp/x11vnc.conf <<EOF
|
||||
[program:x11vnc]
|
||||
command=/usr/bin/x11vnc -display :0 -forever -shared ${VNC_PWFLAG} -rfbport 5900
|
||||
priority=20
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stdout_logfile=/dev/fd/1
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/fd/2
|
||||
stderr_logfile_maxbytes=0
|
||||
environment=HOME="/config"
|
||||
EOF
|
||||
|
||||
# nginx braucht /run für PID + Sockets, sicherstellen dass es existiert.
|
||||
mkdir -p /run /var/lib/nginx /var/log/nginx
|
||||
chown -R ctmine:ctmine /var/lib/nginx /var/log/nginx 2>/dev/null || true
|
||||
|
||||
# Wenn Web-Root existiert, Rechte setzen.
|
||||
if [ -d "${WEB_ROOT}" ]; then
|
||||
chown -R ctmine:ctmine "${WEB_ROOT}" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# --- Prism beim ersten Start ins persistente Volume kopieren --------------
|
||||
# /opt/prism enthält die frische Launcher-Installation; beim ersten Start
|
||||
# (oder nach Updates) wird sie nach /config/prism kopiert, damit Accounts
|
||||
# und Instanzen persistent bleiben.
|
||||
/opt/bootstrap-prism.sh
|
||||
echo "[entrypoint] Prism Launcher unter ${CONFIG_DIR}/prism bereit."
|
||||
|
||||
echo "[entrypoint] Starte supervisord …"
|
||||
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
|
||||
14
docker/mc-api/package.json
Normal file
14
docker/mc-api/package.json
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"name": "ctmine-client-api",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "Status- und Steuerungs-API für CTmine-client",
|
||||
"type": "module",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"start": "node server.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
}
|
||||
}
|
||||
178
docker/mc-api/server.js
Normal file
178
docker/mc-api/server.js
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
// CTmine-client Status- und Steuerungs-API.
|
||||
// Absichtlich ohne externe Abhängigkeiten (nur Node-Built-ins), damit der
|
||||
// Container schlank bleibt und kein npm install im Runtime-Stage nötig ist.
|
||||
//
|
||||
// Endpunkte:
|
||||
// GET /api/status → { display, vnc, minecraft, resolution, timestamp }
|
||||
// POST /api/mc/start → startet Prism Launcher
|
||||
// POST /api/mc/stop → beendet Prism + Minecraft
|
||||
//
|
||||
// nginx proxyt /api zu diesem Server (127.0.0.1:3000 im Container).
|
||||
|
||||
import { createServer } from 'node:http'
|
||||
import { exec, execFile, spawn } from 'node:child_process'
|
||||
import { promisify } from 'node:util'
|
||||
|
||||
const execAsync = promisify(exec)
|
||||
const PORT = Number(process.env.MC_API_PORT || 3000)
|
||||
|
||||
// --- Konfiguration aus Environment ---------------------------------------
|
||||
const CONFIG_DIR = process.env.CONFIG_DIR || '/config'
|
||||
const PRISM_DIR = process.env.PRISM_DIR || `${CONFIG_DIR}/prism`
|
||||
const DISPLAY = process.env.DISPLAY || ':0'
|
||||
const ACCOUNT = process.env.MC_ACCOUNT || '' // Profilname in Prism
|
||||
const MC_VERSION = process.env.MC_VERSION || '' // optional, z.B. "1.21"
|
||||
|
||||
// --- Hilfsfunktionen -----------------------------------------------------
|
||||
|
||||
/**
|
||||
* Liefert true, wenn mindestens ein Prozess auf das Muster passt.
|
||||
* Wichtig: pgrep -f per Shell matched sonst auf die Shell selbst, deren
|
||||
* Kommandozeile das Pattern enthält. Wir filtern deshalb die Shell-basierten
|
||||
* Aufrufe (sh/bash -c …) heraus und prüfen nur „echte“ Treffer.
|
||||
*/
|
||||
async function pgrep(pattern) {
|
||||
try {
|
||||
// -a gibt die volle Kommandozeile aus, sodass wir filtern können.
|
||||
const { stdout } = await execAsync(`pgrep -fa '${pattern}' || true`)
|
||||
const lines = stdout.trim().split('\n').filter(Boolean)
|
||||
// Nur Treffer zählen, die nicht themselves die Such-Shell sind.
|
||||
const real = lines.filter((line) => !/\b(?:sh|bash) -c .*pgrep/.test(line))
|
||||
return real.length > 0
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/** Führt ein Kommando als der Runtime-Benutzer aus, falls PUID gesetzt ist. */
|
||||
function asUser(cmd) {
|
||||
const uid = process.env.PUID
|
||||
const gid = process.env.PGID
|
||||
// Wenn wir root sind und PUID gesetzt ist → per runpuuids als Benutzer laufen.
|
||||
if (process.getuid && process.getuid() === 0 && uid && gid) {
|
||||
return `gosu ${uid}:${gid} bash -lc '${cmd.replace(/'/g, "'\\''")}'`
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
/** Sicheres JSON-Antwort-Helfer. */
|
||||
function sendJson(res, status, body) {
|
||||
const payload = JSON.stringify(body)
|
||||
res.writeHead(status, {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(payload),
|
||||
})
|
||||
res.end(payload)
|
||||
}
|
||||
|
||||
// --- Status --------------------------------------------------------------
|
||||
|
||||
async function readResolution() {
|
||||
try {
|
||||
const { stdout } = await execAsync('xdpyinfo -display ' + DISPLAY + ' 2>/dev/null | grep dimensions')
|
||||
const m = /(\d+)x(\d+)/.exec(stdout)
|
||||
if (m) return { width: Number(m[1]), height: Number(m[2]) }
|
||||
} catch {
|
||||
/* xdpyinfo nicht verfügbar */
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
async function getStatus() {
|
||||
const [display, vnc, minecraft, resolution] = await Promise.all([
|
||||
pgrep('Xvfb ' + DISPLAY),
|
||||
pgrep('x11vnc'),
|
||||
// Prism portable Binary heißt 'prismlauncher' (klein); der offizielle Build
|
||||
// 'PrismLauncher'. Minecraft selbst läuft als java-Prozess mit net.minecraft.
|
||||
pgrep('prismlauncher|PrismLauncher|java.*net\\.minecraft'),
|
||||
readResolution(),
|
||||
])
|
||||
return {
|
||||
display,
|
||||
vnc,
|
||||
minecraft,
|
||||
resolution,
|
||||
timestamp: Date.now(),
|
||||
}
|
||||
}
|
||||
|
||||
// --- Minecraft-Steuerung -------------------------------------------------
|
||||
|
||||
async function startMinecraft() {
|
||||
if (await pgrep('prismlauncher|PrismLauncher|java.*net\\.minecraft')) {
|
||||
return { ok: true, message: 'Minecraft läuft bereits.' }
|
||||
}
|
||||
|
||||
// PrismLauncher im Headless-CLI-Modus gibt es nicht zuverlässig über alle
|
||||
// Versionen. Wir starten daher die GUI-Anwendung auf dem virtuellen Display;
|
||||
// der Benutzer interagiert dann per VNC. Das ist der robuste Weg.
|
||||
//
|
||||
// Die API läuft bereits als ctmine-Benutzer (via gosu in supervisor.conf),
|
||||
// daher starten wir Prism direkt ohne weiteren gosu-Wrapper.
|
||||
const child = spawn(PRISM_DIR + '/PrismLauncher', [], {
|
||||
cwd: PRISM_DIR,
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
env: {
|
||||
...process.env,
|
||||
DISPLAY,
|
||||
HOME: CONFIG_DIR,
|
||||
},
|
||||
})
|
||||
child.on('error', () => {
|
||||
/* wird über Status-Polling sichtbar */
|
||||
})
|
||||
child.unref()
|
||||
|
||||
return { ok: true, message: 'Prism Launcher wird gestartet. Im VNC-Fenster sichtbar.' }
|
||||
}
|
||||
|
||||
async function stopMinecraft() {
|
||||
// Zuerst sauber Prism schließen, dann Restprozesse killen.
|
||||
try {
|
||||
await execAsync("pkill -INT -f 'prismlauncher|PrismLauncher' || true")
|
||||
await new Promise((r) => setTimeout(r, 1500))
|
||||
await execAsync("pkill -TERM -f 'prismlauncher|PrismLauncher|net.minecraft' || true")
|
||||
} catch {
|
||||
/* pkill liefert !=0 wenn nichts läuft — egal */
|
||||
}
|
||||
return { ok: true, message: 'Minecraft wurde beendet.' }
|
||||
}
|
||||
|
||||
// --- HTTP-Server ---------------------------------------------------------
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
// CORS frei für lokales Netz (Dashboard läuft gleicher Origin, aber sicher).
|
||||
res.setHeader('Access-Control-Allow-Origin', '*')
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.writeHead(204)
|
||||
return res.end()
|
||||
}
|
||||
|
||||
const url = new URL(req.url, `http://localhost:${PORT}`)
|
||||
|
||||
try {
|
||||
if (req.method === 'GET' && url.pathname === '/api/status') {
|
||||
return sendJson(res, 200, await getStatus())
|
||||
}
|
||||
|
||||
if (req.method === 'POST' && url.pathname === '/api/mc/start') {
|
||||
return sendJson(res, 200, await startMinecraft())
|
||||
}
|
||||
|
||||
if (req.method === 'POST' && url.pathname === '/api/mc/stop') {
|
||||
return sendJson(res, 200, await stopMinecraft())
|
||||
}
|
||||
|
||||
sendJson(res, 404, { ok: false, message: 'Not found' })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
sendJson(res, 500, { ok: false, message })
|
||||
}
|
||||
})
|
||||
|
||||
server.listen(PORT, '127.0.0.1', () => {
|
||||
console.log(`[ctmine-api] lauscht auf 127.0.0.1:${PORT}`)
|
||||
})
|
||||
66
docker/nginx.conf
Normal file
66
docker/nginx.conf
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
# CTmine-client nginx-Konfiguration.
|
||||
# Liefert das gebaute Vue-Dashboard aus und proxyt API + VNC-WebSocket.
|
||||
|
||||
user ctmine;
|
||||
worker_processes 1;
|
||||
pid /run/nginx.pid;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
access_log /dev/stdout;
|
||||
error_log /dev/stderr warn;
|
||||
|
||||
sendfile on;
|
||||
keepalive_timeout 65;
|
||||
client_max_body_size 4m;
|
||||
|
||||
server {
|
||||
listen 8080 default_server;
|
||||
listen [::]:8080 default_server;
|
||||
server_name _;
|
||||
|
||||
root /var/www/ctmine;
|
||||
index index.html;
|
||||
|
||||
# --- Vue SPA: Fallback auf index.html ---------------------------
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
# --- noVNC-Webclient (vom novnc-Debian-Paket unter /usr/share/novnc) -
|
||||
# Der VncViewer-iframe lädt /novnc/vnc.html und verbindet sich dann
|
||||
# selbsttätig über /websockify (s.u.).
|
||||
location /novnc/ {
|
||||
alias /usr/share/novnc/;
|
||||
try_files $uri $uri/ =404;
|
||||
}
|
||||
|
||||
# --- Status-/Steuerungs-API → Node ------------------------------
|
||||
location /api/ {
|
||||
proxy_pass http://127.0.0.1:3000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
# --- VNC über WebSocket (websockify lauscht auf :6080) ----------
|
||||
# Wichtig: Upgrade-Header weiterreichen, sonst kein WS-Handshake.
|
||||
location /websockify {
|
||||
proxy_pass http://127.0.0.1:6080;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header Host $host;
|
||||
proxy_read_timeout 3600s;
|
||||
proxy_send_timeout 3600s;
|
||||
}
|
||||
}
|
||||
}
|
||||
79
docker/supervisor.conf
Normal file
79
docker/supervisor.conf
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
; CTmine-client — Haupt-Supervisor-Konfiguration.
|
||||
; supervisord läuft als ROOT, damit es /dev/fd/1 (Container-stdout) nutzen und
|
||||
; die Programme sauber verwalten kann. Jedes Programm startet seinen Prozess
|
||||
; selbst als ctmine-Benutzer (per gosu), so dass die eigentliche Arbeit nie
|
||||
; als root läuft.
|
||||
;
|
||||
; Die Xvfb- und x11vnc-Programmblöcke werden vom entrypoint.sh als
|
||||
; /tmp/xvfb.conf und /tmp/x11vnc.conf generiert (wegen ENV-Substitution)
|
||||
; und hier per include gezogen.
|
||||
|
||||
[supervisord]
|
||||
nodaemon=true
|
||||
user=root
|
||||
logfile=/dev/null
|
||||
logfile_maxbytes=0
|
||||
pidfile=/var/run/supervisord.pid
|
||||
|
||||
[unix_http_server]
|
||||
file=/var/run/supervisor.sock
|
||||
chmod=0700
|
||||
|
||||
[rpcinterface:supervisor]
|
||||
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
|
||||
|
||||
[supervisorctl]
|
||||
serverurl=unix:///var/run/supervisor.sock
|
||||
|
||||
; Von entrypoint.sh generierte Programmblöcke (Xvfb, x11vnc):
|
||||
[include]
|
||||
files = /tmp/xvfb.conf /tmp/x11vnc.conf
|
||||
|
||||
; --- Windowmanager (sonst hat MC keinen Rahmen / Fokus) -------------------
|
||||
[program:openbox]
|
||||
command=/usr/sbin/gosu ctmine /usr/bin/openbox
|
||||
environment=DISPLAY=":0"
|
||||
priority=30
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stdout_logfile=/dev/fd/1
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/fd/2
|
||||
stderr_logfile_maxbytes=0
|
||||
|
||||
; --- websockify: VNC :5900 → WebSocket, von nginx unter /websockify exposed -
|
||||
[program:websockify]
|
||||
command=/usr/sbin/gosu ctmine /usr/bin/python3 -m websockify 0.0.0.0:6080 localhost:5900 --web=/usr/share/novnc
|
||||
priority=40
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stdout_logfile=/dev/fd/1
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/fd/2
|
||||
stderr_logfile_maxbytes=0
|
||||
|
||||
; --- Node Status-/Steuerungs-API ------------------------------------------
|
||||
[program:mc-api]
|
||||
command=/usr/sbin/gosu ctmine /usr/bin/node /app/mc-api/server.js
|
||||
directory=/app/mc-api
|
||||
environment=DISPLAY=":0",HOME="/config",NODE_ENV="production"
|
||||
priority=50
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stdout_logfile=/dev/fd/1
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/fd/2
|
||||
stderr_logfile_maxbytes=0
|
||||
|
||||
; --- nginx: liefert Vue-Dashboard + proxyt /api und /websockify -----------
|
||||
; nginx muss als root starten (Port-Bind + Config), wechselt intern per
|
||||
; 'user ctmine;' in der nginx.conf in den Worker-Prozessen.
|
||||
[program:nginx]
|
||||
command=/usr/sbin/nginx -g "daemon off;"
|
||||
priority=60
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stdout_logfile=/dev/fd/1
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/fd/2
|
||||
stderr_logfile_maxbytes=0
|
||||
Loading…
Add table
Add a link
Reference in a new issue