Add an Unraid plugin with a Settings page for Fluxer.

The stack can be installed from a plugin URL and started from Settings, the same shape as other Unraid plugin installers.
This commit is contained in:
Michael Sloan committed 2026-11-23 22:03:04 -05:00
1 parent 028c71e5a9
commit a19983df7b
10 files changed
+437 -9

No files matched your search

+208
View File
@@ -0,0 +1,208 @@
Menu="OtherSettings"
Icon="icon.svg"
Title="Fluxer"
---
<?php
$cfg_file = '/boot/config/plugins/fluxer/fluxer.cfg';
$log_file = '/var/log/fluxer-plugin.log';
$plugin_scripts = '/usr/local/emhttp/plugins/fluxer/scripts';
function fluxer_quote(string $value): string
{
return "'" . str_replace("'", "'\\''", $value) . "'";
}
function fluxer_unquote(string $value): string
{
$value = trim($value);
if (strlen($value) >= 2 && $value[0] === "'" && substr($value, -1) === "'") {
return str_replace("'\\''", "'", substr($value, 1, -1));
}
return $value;
}
function fluxer_defaults(): array
{
return [
'DOMAIN' => '',
'MODE' => 'proxy',
'APPDATA' => '/mnt/user/appdata/fluxer',
'STACK' => '/mnt/user/appdata/fluxer/stack',
];
}
function fluxer_read_cfg(string $file): array
{
$cfg = fluxer_defaults();
if (!is_file($file)) {
return $cfg;
}
foreach (file($file, FILE_IGNORE_NEW_LINES) as $line) {
if (!str_contains($line, '=')) {
continue;
}
[$key, $value] = explode('=', $line, 2);
if (array_key_exists($key, $cfg)) {
$cfg[$key] = fluxer_unquote($value);
}
}
return $cfg;
}
function fluxer_valid_domain(string $domain): bool
{
return (bool) preg_match('/^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$/', $domain);
}
function fluxer_valid_path(string $path): bool
{
return str_starts_with($path, '/mnt/') && !str_contains($path, '..') && !str_starts_with($path, '/boot');
}
function fluxer_write_cfg(string $file, array $cfg): void
{
$dir = dirname($file);
if (!is_dir($dir)) {
mkdir($dir, 0700, true);
}
$body = '';
foreach (['DOMAIN', 'MODE', 'APPDATA', 'STACK'] as $key) {
$body .= $key . '=' . fluxer_quote($cfg[$key]) . "\n";
}
$tmp = $file . '.tmp';
file_put_contents($tmp, $body);
rename($tmp, $file);
}
function fluxer_post_ok(): bool
{
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
return false;
}
$expected = $_SESSION['csrf_token'] ?? '';
if ($expected === '') {
return true;
}
$got = $_POST['csrf_token'] ?? '';
return is_string($got) && hash_equals($expected, $got);
}
function fluxer_setup_token(string $env_file): string
{
if (!is_file($env_file)) {
return '';
}
foreach (file($env_file, FILE_IGNORE_NEW_LINES) as $line) {
if (str_starts_with($line, 'FLUXER_SETUP_TOKEN=')) {
return substr($line, strlen('FLUXER_SETUP_TOKEN='));
}
}
return '';
}
$cfg = fluxer_read_cfg($cfg_file);
$notice = '';
$error = '';
$token = '';
$action = $_POST['action'] ?? '';
if ($action !== '' && !fluxer_post_ok()) {
$error = 'The form expired. Reload the page and try again.';
$action = '';
}
if ($action === 'Save' || $action === 'Start' || $action === 'Show token') {
$cfg['DOMAIN'] = trim($_POST['domain'] ?? '');
$cfg['MODE'] = trim($_POST['mode'] ?? 'proxy');
$cfg['APPDATA'] = trim($_POST['appdata'] ?? $cfg['APPDATA']);
$cfg['STACK'] = trim($_POST['stack'] ?? $cfg['STACK']);
if (($action === 'Start' || $cfg['DOMAIN'] !== '') && !fluxer_valid_domain($cfg['DOMAIN'])) {
$error = 'Domain must be a hostname such as chat.example.com, with no https:// and no path.';
} elseif ($cfg['MODE'] !== 'proxy' && $cfg['MODE'] !== 'direct') {
$error = 'Mode must be proxy or direct.';
} elseif (!fluxer_valid_path($cfg['APPDATA']) || !fluxer_valid_path($cfg['STACK'])) {
$error = 'Data and stack paths must stay under /mnt and off the USB flash drive.';
} else {
fluxer_write_cfg($cfg_file, $cfg);
$notice = 'Saved.';
}
}
if ($error === '' && $action === 'Start') {
$cmd = 'nohup /bin/bash ' . escapeshellarg($plugin_scripts . '/start.sh') . ' >/dev/null 2>&1 &';
exec($cmd);
$notice = 'Start is running. The first image download takes several minutes. Refresh this page for the log.';
}
if ($error === '' && $action === 'Stop') {
$cmd = 'nohup /bin/bash ' . escapeshellarg($plugin_scripts . '/stop.sh') . ' >/dev/null 2>&1 &';
exec($cmd);
$notice = 'Stop is running. Refresh this page for the log. Data is kept.';
}
if ($error === '' && $action === 'Show token') {
$token = fluxer_setup_token($cfg['STACK'] . '/.env');
if ($token === '') {
$error = 'No setup token yet. Start Fluxer once, then show the token again.';
}
}
$csrf = $_SESSION['csrf_token'] ?? '';
$ps = '';
if (is_file($cfg['STACK'] . '/docker-compose.yml')) {
$lines = [];
exec('docker compose --project-directory ' . escapeshellarg($cfg['STACK']) . ' ps 2>&1', $lines);
$ps = implode("\n", $lines);
}
$log = '';
if (is_file($log_file)) {
$all = file($log_file, FILE_IGNORE_NEW_LINES);
$log = implode("\n", array_slice($all, -40));
}
?>
<form method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?=htmlspecialchars($csrf, ENT_QUOTES)?>">
<p>This page installs the official Fluxer Compose bundle. Proxy mode serves plain HTTP on port <b>18080</b> for Nginx Proxy Manager, SWAG, or Traefik. Forward <b>TCP 7881</b> and <b>UDP 7882</b> to this server for voice and video.</p>
<?php if ($notice !== ''): ?><p><?=htmlspecialchars($notice, ENT_QUOTES)?></p><?php endif; ?>
<?php if ($error !== ''): ?><p><b><?=htmlspecialchars($error, ENT_QUOTES)?></b></p><?php endif; ?>
<table>
<tr>
<td>Hostname</td>
<td><input type="text" name="domain" value="<?=htmlspecialchars($cfg['DOMAIN'], ENT_QUOTES)?>" placeholder="chat.example.com" style="width:24em"></td>
</tr>
<tr>
<td>Mode</td>
<td>
<select name="mode">
<option value="proxy" <?php if ($cfg['MODE'] === 'proxy') echo 'selected'; ?>>proxy (port 18080)</option>
<option value="direct" <?php if ($cfg['MODE'] === 'direct') echo 'selected'; ?>>direct (ports 80 and 443)</option>
</select>
</td>
</tr>
<tr>
<td>Data path</td>
<td><input type="text" name="appdata" value="<?=htmlspecialchars($cfg['APPDATA'], ENT_QUOTES)?>" style="width:32em"></td>
</tr>
<tr>
<td>Stack path</td>
<td><input type="text" name="stack" value="<?=htmlspecialchars($cfg['STACK'], ENT_QUOTES)?>" style="width:32em"></td>
</tr>
</table>
<p>
<input type="submit" name="action" value="Save">
<input type="submit" name="action" value="Start">
<input type="submit" name="action" value="Stop">
<input type="submit" name="action" value="Show token">
</p>
</form>
<?php if ($token !== ''): ?>
<p>Setup token. It is also <code>FLUXER_SETUP_TOKEN</code> in the stack <code>.env</code>. Copy the <code>.env</code> file somewhere safe before you rely on this server.</p>
<pre><?=htmlspecialchars($token, ENT_QUOTES)?></pre>
<?php endif; ?>
<?php if ($cfg['DOMAIN'] !== ''): ?>
<p>Public address: <a href="https://<?=htmlspecialchars($cfg['DOMAIN'], ENT_QUOTES)?>" target="_blank">https://<?=htmlspecialchars($cfg['DOMAIN'], ENT_QUOTES)?></a></p>
<?php endif; ?>
<h3>Stack</h3>
<pre><?=htmlspecialchars($ps !== '' ? $ps : 'Not started yet.', ENT_QUOTES)?></pre>
<h3>Log</h3>
<pre><?=htmlspecialchars($log !== '' ? $log : 'No log yet.', ENT_QUOTES)?></pre>
+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# Shared paths for the Fluxer Unraid plugin. Source this file. Do not execute it.
CFG=/boot/config/plugins/fluxer/fluxer.cfg
PLUGIN_ROOT=/usr/local/emhttp/plugins/fluxer
LOG=/var/log/fluxer-plugin.log
LOCK=/var/run/fluxer-plugin.lock
fluxer_load_cfg() {
DOMAIN=""
MODE="proxy"
APPDATA="/mnt/user/appdata/fluxer"
STACK="/mnt/user/appdata/fluxer/stack"
if [[ -f "$CFG" ]]; then
# shellcheck disable=SC1090
source "$CFG"
fi
}
fluxer_valid_domain() {
[[ "$1" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]]
}
fluxer_valid_path() {
local path="$1"
[[ "$path" == /mnt/* && "$path" != /boot && "$path" != /boot/* && "$path" != *..* ]]
}
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
# Start Fluxer from the saved Settings page values.
set -euo pipefail
source /usr/local/emhttp/plugins/fluxer/scripts/common.sh
mkdir -p "$(dirname "$LOG")" "$(dirname "$LOCK")"
exec 9>"$LOCK"
if ! flock -n 9; then
echo "A Fluxer start or stop is already running."
exit 0
fi
exec >>"$LOG" 2>&1
echo "=== $(date -u +%Y-%m-%dT%H:%M:%SZ) start ==="
fluxer_load_cfg
if ! fluxer_valid_domain "$DOMAIN"; then
echo "Set a real hostname on Settings, Fluxer before starting. Current value: ${DOMAIN:-empty}"
exit 1
fi
if ! fluxer_valid_path "$APPDATA" || ! fluxer_valid_path "$STACK"; then
echo "Data and stack paths must be under /mnt and not on the USB flash drive."
exit 1
fi
case "$MODE" in
proxy|direct) ;;
*)
echo "Mode must be proxy or direct."
exit 1
;;
esac
export FLUXER_APPDATA="$APPDATA"
export FLUXER_STACK="$STACK"
bash "$PLUGIN_ROOT/scripts/prepare-unraid.sh" "$DOMAIN" "--$MODE" --up
echo "=== $(date -u +%Y-%m-%dT%H:%M:%SZ) start finished ==="
+23
View File
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# Stop the Fluxer compose project without deleting its data.
set -euo pipefail
source /usr/local/emhttp/plugins/fluxer/scripts/common.sh
mkdir -p "$(dirname "$LOG")" "$(dirname "$LOCK")"
exec 9>"$LOCK"
if ! flock -n 9; then
echo "A Fluxer start or stop is already running."
exit 0
fi
exec >>"$LOG" 2>&1
echo "=== $(date -u +%Y-%m-%dT%H:%M:%SZ) stop ==="
fluxer_load_cfg
if [[ ! -f "$STACK/docker-compose.yml" ]]; then
echo "No stack at $STACK yet."
exit 0
fi
docker compose --project-directory "$STACK" stop
echo "=== $(date -u +%Y-%m-%dT%H:%M:%SZ) stop finished ==="