Compare commits
2
Commits
7f0d43943c
...
7bbd969fa2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bbd969fa2 | ||
|
|
4f377970d7 |
+15
@@ -2,6 +2,8 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
|
|
||||||
# Runtime state/config files (generated, not source)
|
# Runtime state/config files (generated, not source)
|
||||||
|
# Note: data/ subdir contains runtime state (credentials, secrets, history) — never commit
|
||||||
|
dashcaddy-api/data/
|
||||||
dashcaddy-api/credentials.json
|
dashcaddy-api/credentials.json
|
||||||
dashcaddy-api/.env
|
dashcaddy-api/.env
|
||||||
.env
|
.env
|
||||||
@@ -17,6 +19,19 @@ dashcaddy-api/update-config.json
|
|||||||
dashcaddy-api/update-history.json
|
dashcaddy-api/update-history.json
|
||||||
dashcaddy-api/dashcaddy-errors.log
|
dashcaddy-api/dashcaddy-errors.log
|
||||||
|
|
||||||
|
# Auto-updater backups (created by dashcaddy-update.sh when rolling back)
|
||||||
|
start.sh.bak*
|
||||||
|
scripts/*.bak*
|
||||||
|
|
||||||
|
# Auto-updater runtime state (history + secrets + staging)
|
||||||
|
updates/
|
||||||
|
|
||||||
|
# Scratch / debug scripts (left over from past sessions)
|
||||||
|
cm_check*.js
|
||||||
|
full_test.js
|
||||||
|
login_test.js
|
||||||
|
login_backup_test.js
|
||||||
|
|
||||||
# Build output
|
# Build output
|
||||||
dashcaddy-installer/build-output/
|
dashcaddy-installer/build-output/
|
||||||
dashcaddy-installer/dist/
|
dashcaddy-installer/dist/
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
dev
|
c64bbe2
|
||||||
|
|||||||
@@ -9,9 +9,24 @@ const http = require('http');
|
|||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const paths = require('./platform-paths');
|
||||||
|
|
||||||
const HEALTH_CONFIG_FILE = process.env.HEALTH_CONFIG_FILE || path.join(__dirname, 'health-config.json');
|
// Persist health config + history alongside the other state files (services.json,
|
||||||
const HEALTH_HISTORY_FILE = process.env.HEALTH_HISTORY_FILE || path.join(__dirname, 'health-history.json');
|
// config.json) rather than next to the source. In a container that data dir is the
|
||||||
|
// mounted /app/data volume, so uptime history survives container recreates/updates;
|
||||||
|
// previously these defaulted to __dirname (unmounted /app) and every recreate wiped
|
||||||
|
// the accumulated history, blanking the dashboard uptime bars. Explicit env vars
|
||||||
|
// still override.
|
||||||
|
const HEALTH_DATA_DIR = process.env.HEALTH_DATA_DIR || path.dirname(paths.configFile);
|
||||||
|
const HEALTH_CONFIG_FILE = process.env.HEALTH_CONFIG_FILE || path.join(HEALTH_DATA_DIR, 'health-config.json');
|
||||||
|
const HEALTH_HISTORY_FILE = process.env.HEALTH_HISTORY_FILE || path.join(HEALTH_DATA_DIR, 'health-history.json');
|
||||||
|
|
||||||
|
// Legacy locations (next to the source) used before the data-dir default. Read these
|
||||||
|
// once on first load if the new files are absent, so upgrading installs migrate their
|
||||||
|
// accumulated history/config instead of starting empty. The next save() rewrites to
|
||||||
|
// the new location.
|
||||||
|
const LEGACY_HEALTH_CONFIG_FILE = path.join(__dirname, 'health-config.json');
|
||||||
|
const LEGACY_HEALTH_HISTORY_FILE = path.join(__dirname, 'health-history.json');
|
||||||
const CHECK_INTERVAL = parseInt(process.env.HEALTH_CHECK_INTERVAL || '30000', 10); // 30 seconds
|
const CHECK_INTERVAL = parseInt(process.env.HEALTH_CHECK_INTERVAL || '30000', 10); // 30 seconds
|
||||||
const MAX_CHECK_INTERVAL = parseInt(process.env.HEALTH_CHECK_MAX_INTERVAL || '300000', 10); // 5 minutes max backoff
|
const MAX_CHECK_INTERVAL = parseInt(process.env.HEALTH_CHECK_MAX_INTERVAL || '300000', 10); // 5 minutes max backoff
|
||||||
const HISTORY_RETENTION_DAYS = parseInt(process.env.HEALTH_HISTORY_RETENTION || '30', 10);
|
const HISTORY_RETENTION_DAYS = parseInt(process.env.HEALTH_HISTORY_RETENTION || '30', 10);
|
||||||
@@ -541,8 +556,10 @@ class HealthChecker extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
loadConfig() {
|
loadConfig() {
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync(HEALTH_CONFIG_FILE)) {
|
const file = fs.existsSync(HEALTH_CONFIG_FILE) ? HEALTH_CONFIG_FILE
|
||||||
return JSON.parse(fs.readFileSync(HEALTH_CONFIG_FILE, 'utf8'));
|
: (HEALTH_CONFIG_FILE !== LEGACY_HEALTH_CONFIG_FILE && fs.existsSync(LEGACY_HEALTH_CONFIG_FILE) ? LEGACY_HEALTH_CONFIG_FILE : null);
|
||||||
|
if (file) {
|
||||||
|
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.emit('log', 'error', `Error loading config: ${error.message}`);
|
this.emit('log', 'error', `Error loading config: ${error.message}`);
|
||||||
@@ -566,8 +583,10 @@ class HealthChecker extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
loadHistory() {
|
loadHistory() {
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync(HEALTH_HISTORY_FILE)) {
|
const file = fs.existsSync(HEALTH_HISTORY_FILE) ? HEALTH_HISTORY_FILE
|
||||||
return JSON.parse(fs.readFileSync(HEALTH_HISTORY_FILE, 'utf8'));
|
: (HEALTH_HISTORY_FILE !== LEGACY_HEALTH_HISTORY_FILE && fs.existsSync(LEGACY_HEALTH_HISTORY_FILE) ? LEGACY_HEALTH_HISTORY_FILE : null);
|
||||||
|
if (file) {
|
||||||
|
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.emit('log', 'error', `Error loading history: ${error.message}`);
|
this.emit('log', 'error', `Error loading history: ${error.message}`);
|
||||||
|
|||||||
@@ -304,6 +304,15 @@ module.exports = function configureMiddleware(app, {
|
|||||||
{ path: '/api/v1/license/feature/', prefix: true, method: 'GET' },
|
{ path: '/api/v1/license/feature/', prefix: true, method: 'GET' },
|
||||||
{ path: '/api/v1/config', exact: true, method: 'GET' },
|
{ path: '/api/v1/config', exact: true, method: 'GET' },
|
||||||
{ path: '/api/v1/services/status', exact: true, method: 'GET' },
|
{ path: '/api/v1/services/status', exact: true, method: 'GET' },
|
||||||
|
{ path: '/api/v1/health-checks/status', exact: true, method: 'GET' },
|
||||||
|
// Read-only update/version info shown on the dashboard view (verification
|
||||||
|
// modal, topbar version, update badges). Mutating actions — update-apply,
|
||||||
|
// rollback (POST) — are NOT listed here and stay TOTP-protected.
|
||||||
|
{ path: '/api/v1/system/version', exact: true, method: 'GET' },
|
||||||
|
{ path: '/api/v1/system/update-status', exact: true, method: 'GET' },
|
||||||
|
{ path: '/api/v1/system/update-history', exact: true, method: 'GET' },
|
||||||
|
{ path: '/api/v1/system/update-check', exact: true, method: 'GET' },
|
||||||
|
{ path: '/api/v1/updates/available', exact: true, method: 'GET' },
|
||||||
{ path: '/api/v1/system/update-notify', exact: true, method: 'POST' },
|
{ path: '/api/v1/system/update-notify', exact: true, method: 'POST' },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "dashcaddy-api",
|
"name": "dashcaddy-api",
|
||||||
"version": "1.6.0",
|
"version": "1.7.8",
|
||||||
"description": "DashCaddy API server - Dashboard backend for Docker, Caddy & DNS management",
|
"description": "DashCaddy API server - Dashboard backend for Docker, Caddy & DNS management",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const { success } = require('../response-helpers');
|
const fsp = require('fs').promises;
|
||||||
const fs = require('fs');
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
const { success } = require('../response-helpers');
|
||||||
|
|
||||||
const DEFAULT_BACKUP_DIR = process.env.BACKUP_DIR || path.join(__dirname, 'backups');
|
const DEFAULT_BACKUP_DIR = process.env.BACKUP_DIR || path.join(__dirname, '..', 'backups');
|
||||||
|
const DEFAULT_MAX_STORAGE_BYTES = process.env.BACKUP_MAX_STORAGE_BYTES
|
||||||
|
? parseInt(process.env.BACKUP_MAX_STORAGE_BYTES, 10)
|
||||||
|
: 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Backups routes factory
|
* Backups routes factory
|
||||||
@@ -41,6 +45,7 @@ module.exports = function({ backupManager, licenseManager, asyncHandler }) {
|
|||||||
runImmediately: backup.runImmediately || false,
|
runImmediately: backup.runImmediately || false,
|
||||||
destination: backup.destination || 'local',
|
destination: backup.destination || 'local',
|
||||||
destinationPath: backup.destinationPath || DEFAULT_BACKUP_DIR,
|
destinationPath: backup.destinationPath || DEFAULT_BACKUP_DIR,
|
||||||
|
maxStorageBytes: backup.maxStorageBytes || null,
|
||||||
lastRun: lastRun ? lastRun.toISOString() : null,
|
lastRun: lastRun ? lastRun.toISOString() : null,
|
||||||
nextRun: nextRun ? nextRun.toISOString() : null,
|
nextRun: nextRun ? nextRun.toISOString() : null,
|
||||||
lastBackupId: appHistory.length > 0 ? appHistory[0].id : null
|
lastBackupId: appHistory.length > 0 ? appHistory[0].id : null
|
||||||
@@ -52,7 +57,7 @@ module.exports = function({ backupManager, licenseManager, asyncHandler }) {
|
|||||||
|
|
||||||
// Create or update a scheduled backup for an app
|
// Create or update a scheduled backup for an app
|
||||||
router.post('/backups/schedule', premiumGating, asyncHandler(async (req, res) => {
|
router.post('/backups/schedule', premiumGating, asyncHandler(async (req, res) => {
|
||||||
const { appId, enabled, schedule, retention, runImmediately, destination, destinationPath } = req.body;
|
const { appId, enabled, schedule, retention, runImmediately, destination, destinationPath, maxStorageBytes } = req.body;
|
||||||
|
|
||||||
if (!appId) {
|
if (!appId) {
|
||||||
const { ValidationError } = require('../errors');
|
const { ValidationError } = require('../errors');
|
||||||
@@ -62,6 +67,11 @@ module.exports = function({ backupManager, licenseManager, asyncHandler }) {
|
|||||||
const config = backupManager.getConfig();
|
const config = backupManager.getConfig();
|
||||||
if (!config.backups) config.backups = {};
|
if (!config.backups) config.backups = {};
|
||||||
|
|
||||||
|
// Parse maxStorageBytes if provided as string (e.g. "10GB")
|
||||||
|
const parsedMaxStorage = maxStorageBytes
|
||||||
|
? (typeof maxStorageBytes === 'string' ? parseStorageSize(maxStorageBytes) : maxStorageBytes)
|
||||||
|
: null;
|
||||||
|
|
||||||
// Build the backup config for this app
|
// Build the backup config for this app
|
||||||
const backupConfig = {
|
const backupConfig = {
|
||||||
enabled: enabled !== undefined ? enabled : true,
|
enabled: enabled !== undefined ? enabled : true,
|
||||||
@@ -71,7 +81,8 @@ module.exports = function({ backupManager, licenseManager, asyncHandler }) {
|
|||||||
destination: destination || 'local',
|
destination: destination || 'local',
|
||||||
destinationPath: destinationPath || DEFAULT_BACKUP_DIR,
|
destinationPath: destinationPath || DEFAULT_BACKUP_DIR,
|
||||||
include: ['all'],
|
include: ['all'],
|
||||||
destinations: [{ type: destination || 'local', path: destinationPath || DEFAULT_BACKUP_DIR }]
|
destinations: [{ type: destination || 'local', path: destinationPath || DEFAULT_BACKUP_DIR }],
|
||||||
|
maxStorageBytes: parsedMaxStorage
|
||||||
};
|
};
|
||||||
|
|
||||||
config.backups[appId] = backupConfig;
|
config.backups[appId] = backupConfig;
|
||||||
@@ -490,6 +501,39 @@ module.exports = function({ backupManager, licenseManager, asyncHandler }) {
|
|||||||
success(res, { history });
|
success(res, { history });
|
||||||
}, 'backups-history'));
|
}, 'backups-history'));
|
||||||
|
|
||||||
|
// Get storage info for backups destination
|
||||||
|
router.get('/backups/storage-info', asyncHandler(async (req, res) => {
|
||||||
|
const storageInfo = await getStorageInfo();
|
||||||
|
success(res, storageInfo);
|
||||||
|
}, 'backups-storage-info'));
|
||||||
|
|
||||||
|
// Schedule a backup
|
||||||
|
router.post('/backups/schedule', asyncHandler(async (req, res) => {
|
||||||
|
const { name, schedule, maxStorageBytes, ...backupConfig } = req.body;
|
||||||
|
|
||||||
|
if (!name || !schedule) {
|
||||||
|
return res.status(400).json({ error: 'name and schedule are required' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const config = backupManager.getConfig();
|
||||||
|
|
||||||
|
// Store maxStorageBytes in the backup config (converted to bytes)
|
||||||
|
const maxBytes = typeof maxStorageBytes === 'number' && maxStorageBytes > 0
|
||||||
|
? maxStorageBytes
|
||||||
|
: (typeof maxStorageBytes === 'string' ? parseStorageSize(maxStorageBytes) : 0);
|
||||||
|
|
||||||
|
config.backups[name] = {
|
||||||
|
...backupConfig,
|
||||||
|
enabled: true,
|
||||||
|
schedule,
|
||||||
|
maxStorageBytes: maxBytes,
|
||||||
|
destinations: backupConfig.destinations || [{ type: 'local' }]
|
||||||
|
};
|
||||||
|
|
||||||
|
backupManager.updateConfig(config);
|
||||||
|
success(res, { message: `Backup '${name}' scheduled`, maxStorageBytes: maxBytes });
|
||||||
|
}, 'backups-schedule'));
|
||||||
|
|
||||||
// Restore from backup
|
// Restore from backup
|
||||||
router.post('/backups/restore/:backupId', asyncHandler(async (req, res) => {
|
router.post('/backups/restore/:backupId', asyncHandler(async (req, res) => {
|
||||||
const result = await backupManager.restoreBackup(req.params.backupId, req.body);
|
const result = await backupManager.restoreBackup(req.params.backupId, req.body);
|
||||||
@@ -653,3 +697,121 @@ function formatBytes(bytes) {
|
|||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get storage information for the backup directory
|
||||||
|
*/
|
||||||
|
async function getStorageInfo() {
|
||||||
|
const result = {
|
||||||
|
destination: DEFAULT_BACKUP_DIR,
|
||||||
|
maxStorageBytes: DEFAULT_MAX_STORAGE_BYTES,
|
||||||
|
usedBytes: 0,
|
||||||
|
availableBytes: 0,
|
||||||
|
usagePercent: 0,
|
||||||
|
backupCount: 0,
|
||||||
|
oldestBackup: null,
|
||||||
|
newestBackup: null
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get disk space info
|
||||||
|
const diskSpace = await getDiskSpaceInfo(DEFAULT_BACKUP_DIR);
|
||||||
|
result.availableBytes = diskSpace.available;
|
||||||
|
|
||||||
|
// Scan for backup files
|
||||||
|
if (DEFAULT_MAX_STORAGE_BYTES > 0) {
|
||||||
|
result.maxStorageBytes = DEFAULT_MAX_STORAGE_BYTES;
|
||||||
|
} else {
|
||||||
|
result.maxStorageBytes = diskSpace.total || 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let totalSize = 0;
|
||||||
|
let oldestTime = null;
|
||||||
|
let newestTime = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const entries = await fsp.readdir(DEFAULT_BACKUP_DIR);
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (entry.endsWith('.backup')) {
|
||||||
|
const filePath = path.join(DEFAULT_BACKUP_DIR, entry);
|
||||||
|
try {
|
||||||
|
const stats = await fsp.stat(filePath);
|
||||||
|
totalSize += stats.size;
|
||||||
|
result.backupCount++;
|
||||||
|
|
||||||
|
const fileTime = new Date(stats.mtime);
|
||||||
|
if (!oldestTime || fileTime < oldestTime) oldestTime = fileTime;
|
||||||
|
if (!newestTime || fileTime > newestTime) newestTime = fileTime;
|
||||||
|
} catch (e) {
|
||||||
|
// Skip files we can't stat
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Backup directory might not exist yet
|
||||||
|
}
|
||||||
|
|
||||||
|
result.usedBytes = totalSize;
|
||||||
|
result.oldestBackup = oldestTime ? oldestTime.toISOString() : null;
|
||||||
|
result.newestBackup = newestTime ? newestTime.toISOString() : null;
|
||||||
|
|
||||||
|
// Calculate available (total limit - used), or from disk space if no limit set
|
||||||
|
if (result.maxStorageBytes > 0) {
|
||||||
|
result.availableBytes = Math.max(0, result.maxStorageBytes - totalSize);
|
||||||
|
result.usagePercent = parseFloat(((totalSize / result.maxStorageBytes) * 100).toFixed(2));
|
||||||
|
} else if (diskSpace.total) {
|
||||||
|
result.availableBytes = diskSpace.available;
|
||||||
|
result.usagePercent = diskSpace.total > 0
|
||||||
|
? parseFloat((((diskSpace.total - diskSpace.available) / diskSpace.total) * 100).toFixed(2))
|
||||||
|
: 0;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[BackupsRouter] Error getting storage info:', error.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get disk space info (filesystem-agnostic)
|
||||||
|
*/
|
||||||
|
async function getDiskSpaceInfo(dirPath) {
|
||||||
|
try {
|
||||||
|
const diskInfo = await fsp.statfs(dirPath);
|
||||||
|
return {
|
||||||
|
total: diskInfo.blocks * diskInfo.bsize,
|
||||||
|
available: diskInfo.bfree * diskInfo.bsize,
|
||||||
|
used: (diskInfo.blocks - diskInfo.bfree) * diskInfo.bsize
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
// Directory might not exist or be accessible
|
||||||
|
return { total: 0, available: 0, used: 0 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse storage size string like "10GB" to bytes
|
||||||
|
*/
|
||||||
|
function parseStorageSize(sizeStr) {
|
||||||
|
if (!sizeStr || typeof sizeStr === 'number') return sizeStr || 0;
|
||||||
|
|
||||||
|
const match = String(sizeStr).match(/^(\d+(?:\.\d+)?)\s*(B|KB|MB|GB|TB|K|M|G|T)?$/i);
|
||||||
|
if (!match) return 0;
|
||||||
|
|
||||||
|
const value = parseFloat(match[1]);
|
||||||
|
const unit = (match[2] || 'B').toUpperCase();
|
||||||
|
|
||||||
|
const multipliers = {
|
||||||
|
'B': 1,
|
||||||
|
'K': 1024,
|
||||||
|
'KB': 1024,
|
||||||
|
'M': 1024 * 1024,
|
||||||
|
'MB': 1024 * 1024,
|
||||||
|
'G': 1024 * 1024 * 1024,
|
||||||
|
'GB': 1024 * 1024 * 1024,
|
||||||
|
'T': 1024 * 1024 * 1024 * 1024,
|
||||||
|
'TB': 1024 * 1024 * 1024 * 1024
|
||||||
|
};
|
||||||
|
|
||||||
|
return Math.floor(value * (multipliers[unit] || 1));
|
||||||
|
}
|
||||||
|
|||||||
@@ -134,11 +134,16 @@ restart_container() {
|
|||||||
# Stop and remove existing container so new env var is applied
|
# Stop and remove existing container so new env var is applied
|
||||||
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
|
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
|
||||||
|
|
||||||
# Re-create with same volumes and the SERVICES_FILE env var
|
# Re-create with same volumes. CRITICAL: must include CREDENTIALS_FILE +
|
||||||
|
# ENCRYPTION_KEY_FILE pointing at /app/data/ so the container reads the TOTP
|
||||||
|
# secret from the bind-mounted host data dir (not image-local /app/credentials.json
|
||||||
|
# which gets a fresh encryption key on every container recreate = TOTP breaks).
|
||||||
docker run -d --restart unless-stopped --name "$CONTAINER_NAME" \
|
docker run -d --restart unless-stopped --name "$CONTAINER_NAME" \
|
||||||
-p 127.0.0.1:3001:3001 \
|
-p 127.0.0.1:3001:3001 \
|
||||||
-v /opt/dashcaddy/dashcaddy-api/data:/app/data \
|
-v /opt/dashcaddy/dashcaddy-api/data:/app/data \
|
||||||
-e SERVICES_FILE=/app/data/services.json \
|
-e SERVICES_FILE=/app/data/services.json \
|
||||||
|
-e CREDENTIALS_FILE=/app/d...son \
|
||||||
|
-e ENCRYPTION_KEY_FILE=/app/data/.encryption-key \
|
||||||
"$image"
|
"$image"
|
||||||
log "Container restarted with fresh env"
|
log "Container restarted with fresh env"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,13 @@ let dockerMaintenance, logDigest;
|
|||||||
try { dockerMaintenance = require('../docker-maintenance'); } catch (_) { /* optional module */ }
|
try { dockerMaintenance = require('../docker-maintenance'); } catch (_) { /* optional module */ }
|
||||||
try { logDigest = require('../log-digest'); } catch (_) { /* optional module */ }
|
try { logDigest = require('../log-digest'); } catch (_) { /* optional module */ }
|
||||||
|
|
||||||
|
// Workflow engine (bundled workflows)
|
||||||
|
let bundledWorkflowsModule;
|
||||||
|
let workflowEngine = null;
|
||||||
|
try {
|
||||||
|
bundledWorkflowsModule = require('../bundled-workflows');
|
||||||
|
} catch (_) { /* optional module */ }
|
||||||
|
|
||||||
// Templates
|
// Templates
|
||||||
const { APP_TEMPLATES, TEMPLATE_CATEGORIES, DIFFICULTY_LEVELS } = require('../app-templates');
|
const { APP_TEMPLATES, TEMPLATE_CATEGORIES, DIFFICULTY_LEVELS } = require('../app-templates');
|
||||||
const { RECIPE_TEMPLATES, RECIPE_CATEGORIES } = require('../recipe-templates');
|
const { RECIPE_TEMPLATES, RECIPE_CATEGORIES } = require('../recipe-templates');
|
||||||
@@ -69,6 +76,7 @@ const recipesRoutes = require('../routes/recipes');
|
|||||||
const themesRoutes = require('../routes/themes');
|
const themesRoutes = require('../routes/themes');
|
||||||
const dockerResourcesRoutes = require('../routes/docker-resources');
|
const dockerResourcesRoutes = require('../routes/docker-resources');
|
||||||
const eventsRoutes = require('../routes/events');
|
const eventsRoutes = require('../routes/events');
|
||||||
|
const workflowsRoutes = require('../routes/workflows');
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
const { APP } = require('../constants');
|
const { APP } = require('../constants');
|
||||||
@@ -156,6 +164,21 @@ async function createApp() {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Back-compat: reverse-proxy SSO snippets (Caddy forward_auth + per-service
|
||||||
|
// auto-login pages) historically call these endpoints under the pre-1.5.0
|
||||||
|
// prefix `/api/auth/...`. The canonical mount is `/api/v1`. Hand-maintained
|
||||||
|
// Caddyfiles have repeatedly drifted back to the old prefix and 404'd the SSO
|
||||||
|
// gate (breaking Plex/Jellyfin/Emby/chat). Transparently rewrite ONLY these two
|
||||||
|
// auth paths to the v1 mount so the gate is tolerant of that drift. Must run
|
||||||
|
// before configureMiddleware() so CSRF/auth see the canonical path. This is
|
||||||
|
// deliberately narrow — NOT a general `/api` -> `/api/v1` alias.
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
if (req.url.startsWith('/api/auth/gate/') || req.url.startsWith('/api/auth/app-token/')) {
|
||||||
|
req.url = '/api/v1' + req.url.slice(4); // '/api'.length === 4
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
// Configure middleware
|
// Configure middleware
|
||||||
const middlewareResult = configureMiddleware(app, {
|
const middlewareResult = configureMiddleware(app, {
|
||||||
siteConfig: config.siteConfig,
|
siteConfig: config.siteConfig,
|
||||||
@@ -308,9 +331,55 @@ async function createApp() {
|
|||||||
app,
|
app,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Initialize workflow engine if bundled-workflows is available
|
||||||
|
if (bundledWorkflowsModule && ctx.docker) {
|
||||||
|
try {
|
||||||
|
const { WorkflowEngine } = bundledWorkflowsModule;
|
||||||
|
const workflowCtx = {
|
||||||
|
docker: ctx.docker,
|
||||||
|
notification: ctx.notification,
|
||||||
|
backupManager: ctx.backupManager,
|
||||||
|
resourceMonitor: ctx.resourceMonitor,
|
||||||
|
servicesStateManager: ctx.servicesStateManager
|
||||||
|
};
|
||||||
|
workflowEngine = new WorkflowEngine(workflowCtx);
|
||||||
|
ctx.workflowEngine = workflowEngine;
|
||||||
|
log.info('app', 'Workflow engine initialized');
|
||||||
|
} catch (err) {
|
||||||
|
log.error('app', 'Failed to initialize workflow engine', { error: err.message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Build versioned API router
|
// Build versioned API router
|
||||||
const apiRouter = express.Router();
|
const apiRouter = express.Router();
|
||||||
|
|
||||||
|
// Wire up notification listeners for resourceMonitor and backupManager
|
||||||
|
if (ctx.notification && ctx.resourceMonitor) {
|
||||||
|
ctx.resourceMonitor.on('alert', (alertData) => {
|
||||||
|
ctx.notification.sendAlert(alertData).catch(err => {
|
||||||
|
log.error('notification', 'Failed to send alert', { error: err.message });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
ctx.resourceMonitor.on('auto-restart', (data) => {
|
||||||
|
ctx.notification.sendServiceEvent('auto-restart', data).catch(err => {
|
||||||
|
log.error('notification', 'Failed to send auto-restart notification', { error: err.message });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ctx.notification && ctx.backupManager) {
|
||||||
|
ctx.backupManager.on('backup-complete', (data) => {
|
||||||
|
ctx.notification.send('backup-complete', data).catch(err => {
|
||||||
|
log.error('notification', 'Failed to send backup-complete', { error: err.message });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
ctx.backupManager.on('backup-failed', (data) => {
|
||||||
|
ctx.notification.send('backup-failed', data).catch(err => {
|
||||||
|
log.error('notification', 'Failed to send backup-failed', { error: err.message });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Mount route modules
|
// Mount route modules
|
||||||
apiRouter.use(authRoutes(ctx));
|
apiRouter.use(authRoutes(ctx));
|
||||||
apiRouter.use(configRoutes(ctx));
|
apiRouter.use(configRoutes(ctx));
|
||||||
@@ -330,7 +399,8 @@ async function createApp() {
|
|||||||
apiRouter.use('/containers', containerRoutes({
|
apiRouter.use('/containers', containerRoutes({
|
||||||
docker: ctx.docker,
|
docker: ctx.docker,
|
||||||
log: ctx.log,
|
log: ctx.log,
|
||||||
asyncHandler: ctx.asyncHandler
|
asyncHandler: ctx.asyncHandler,
|
||||||
|
workflowEngine: ctx.workflowEngine
|
||||||
}));
|
}));
|
||||||
apiRouter.use(serviceRoutes({
|
apiRouter.use(serviceRoutes({
|
||||||
servicesStateManager: ctx.servicesStateManager,
|
servicesStateManager: ctx.servicesStateManager,
|
||||||
@@ -361,7 +431,8 @@ async function createApp() {
|
|||||||
resourceMonitor: ctx.resourceMonitor,
|
resourceMonitor: ctx.resourceMonitor,
|
||||||
docker: ctx.docker,
|
docker: ctx.docker,
|
||||||
asyncHandler: ctx.asyncHandler,
|
asyncHandler: ctx.asyncHandler,
|
||||||
log: ctx.log
|
log: ctx.log,
|
||||||
|
notificationManager: ctx.notification
|
||||||
}));
|
}));
|
||||||
apiRouter.use(updatesRoutes({
|
apiRouter.use(updatesRoutes({
|
||||||
updateManager: ctx.updateManager,
|
updateManager: ctx.updateManager,
|
||||||
@@ -404,8 +475,8 @@ async function createApp() {
|
|||||||
}));
|
}));
|
||||||
apiRouter.use(backupsRoutes({
|
apiRouter.use(backupsRoutes({
|
||||||
backupManager: ctx.backupManager,
|
backupManager: ctx.backupManager,
|
||||||
asyncHandler: ctx.asyncHandler,
|
licenseManager: ctx.licenseManager,
|
||||||
licenseManager: ctx.licenseManager
|
asyncHandler: ctx.asyncHandler
|
||||||
}));
|
}));
|
||||||
apiRouter.use('/ca', caRoutes(ctx));
|
apiRouter.use('/ca', caRoutes(ctx));
|
||||||
apiRouter.use(browseRoutes({
|
apiRouter.use(browseRoutes({
|
||||||
@@ -435,6 +506,11 @@ async function createApp() {
|
|||||||
updateManager: ctx.updateManager,
|
updateManager: ctx.updateManager,
|
||||||
logError: ctx.logError
|
logError: ctx.logError
|
||||||
}));
|
}));
|
||||||
|
apiRouter.use(workflowsRoutes({
|
||||||
|
workflowEngine: ctx.workflowEngine,
|
||||||
|
licenseManager: ctx.licenseManager,
|
||||||
|
asyncHandler: ctx.asyncHandler
|
||||||
|
}));
|
||||||
|
|
||||||
// Inline API routes
|
// Inline API routes
|
||||||
apiRouter.get('/health', (req, res) => {
|
apiRouter.get('/health', (req, res) => {
|
||||||
|
|||||||
@@ -1,272 +0,0 @@
|
|||||||
const express = require('express');
|
|
||||||
const http = require('http');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* OpenClaw management routes
|
|
||||||
* Proxies gateway API calls through DashCaddy so the token never leaves the server.
|
|
||||||
*
|
|
||||||
* GET /openclaw/status → container info + gateway health
|
|
||||||
* POST /openclaw/deploy → deploy OpenClaw container
|
|
||||||
* GET /openclaw/proxy/* → proxy GET to gateway
|
|
||||||
* POST /openclaw/proxy/* → proxy POST to gateway
|
|
||||||
* DELETE /openclaw → remove container
|
|
||||||
*/
|
|
||||||
module.exports = function openClawRoutes(ctx) {
|
|
||||||
const router = express.Router();
|
|
||||||
const docker = ctx.docker;
|
|
||||||
const asyncHandler = ctx.asyncHandler;
|
|
||||||
const log = ctx.log || console;
|
|
||||||
|
|
||||||
// ── helpers ──────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
async function findOpenClawContainer() {
|
|
||||||
const containers = await docker.client.listContainers({ all: true });
|
|
||||||
return containers.find(function(c) {
|
|
||||||
return c.Image === 'ghcr.io/nousresearch/openclaw:latest' ||
|
|
||||||
(c.Labels && c.Labels['dashcaddy.managed'] === 'true' &&
|
|
||||||
c.Names.some(function(n) { return n.includes('openclaw'); }));
|
|
||||||
}) || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getGatewayToken(containerId) {
|
|
||||||
try {
|
|
||||||
const info = await docker.client.containerInfo(containerId);
|
|
||||||
const entry = (info.Config.Env || []).find(function(e) {
|
|
||||||
return e.startsWith('OPENCLAW_GATEWAY_TOKEN=');
|
|
||||||
});
|
|
||||||
return entry ? entry.split('=')[1] : null;
|
|
||||||
} catch(err) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getContainerPort(containerId) {
|
|
||||||
try {
|
|
||||||
const containers = await docker.client.listContainers({ all: true });
|
|
||||||
const c = containers.find(function(x) {
|
|
||||||
return x.Id === containerId || x.Id.startsWith(containerId);
|
|
||||||
});
|
|
||||||
if (c && c.Ports) {
|
|
||||||
const p = c.Ports.find(function(x) { return x.PrivatePort === 18792; });
|
|
||||||
if (p && p.PublicPort) return String(p.PublicPort);
|
|
||||||
}
|
|
||||||
return '18792';
|
|
||||||
} catch(err) {
|
|
||||||
return '18792';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function gatewayHealth(baseUrl, token) {
|
|
||||||
return new Promise(function(resolve) {
|
|
||||||
const headers = {};
|
|
||||||
if (token) headers['Authorization'] = 'Bearer ' + token;
|
|
||||||
const req = http.get(baseUrl + '/health', { headers: headers }, function(res) {
|
|
||||||
let data = '';
|
|
||||||
res.on('data', function(d) { data += d; });
|
|
||||||
res.on('end', function() {
|
|
||||||
try { resolve({ ok: true, data: JSON.parse(data) }); }
|
|
||||||
catch(e) { resolve({ ok: true, data: data }); }
|
|
||||||
});
|
|
||||||
});
|
|
||||||
req.on('error', function(e) { resolve({ ok: false, error: e.message }); });
|
|
||||||
req.setTimeout(5000, function() { req.destroy(); resolve({ ok: false, error: 'timeout' }); });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function proxyRequest(req, res, targetBase, path, token) {
|
|
||||||
const headers = {};
|
|
||||||
if (token) headers['Authorization'] = 'Bearer ' + token;
|
|
||||||
headers['X-Forwarded-For'] = req.ip;
|
|
||||||
headers['X-Forwarded-Proto'] = req.protocol;
|
|
||||||
|
|
||||||
const url = targetBase + '/' + path;
|
|
||||||
const method = req.method;
|
|
||||||
|
|
||||||
if (['POST', 'PUT', 'PATCH'].includes(method)) {
|
|
||||||
const body = JSON.stringify(req.body);
|
|
||||||
headers['Content-Type'] = 'application/json';
|
|
||||||
headers['Content-Length'] = Buffer.byteLength(body);
|
|
||||||
|
|
||||||
const proxyReq = http.request(url, { method: method, headers: headers }, function(proxyRes) {
|
|
||||||
res.set(proxyRes.headers);
|
|
||||||
res.status(proxyRes.statusCode);
|
|
||||||
proxyRes.on('data', function(d) { res.write(d); });
|
|
||||||
proxyRes.on('end', function() { res.end(); });
|
|
||||||
});
|
|
||||||
proxyReq.on('error', function(e) { res.status(502).json({ success: false, error: e.message }); });
|
|
||||||
proxyReq.setTimeout(15000, function() { proxyReq.destroy(); res.status(504).json({ success: false, error: 'gateway timeout' }); });
|
|
||||||
proxyReq.write(body);
|
|
||||||
proxyReq.end();
|
|
||||||
} else {
|
|
||||||
const proxyReq = http.get(url, { headers: headers }, function(proxyRes) {
|
|
||||||
res.set(proxyRes.headers);
|
|
||||||
res.status(proxyRes.statusCode);
|
|
||||||
proxyRes.on('data', function(d) { res.write(d); });
|
|
||||||
proxyRes.on('end', function() { res.end(); });
|
|
||||||
});
|
|
||||||
proxyReq.on('error', function(e) { res.status(502).json({ success: false, error: e.message }); });
|
|
||||||
proxyReq.setTimeout(15000, function() { proxyReq.destroy(); res.status(504).json({ success: false, error: 'gateway timeout' }); });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── GET /openclaw/status ────────────────────────────────────────────────
|
|
||||||
|
|
||||||
router.get('/status', asyncHandler(async function(req, res) {
|
|
||||||
const container = await findOpenClawContainer();
|
|
||||||
|
|
||||||
if (!container) {
|
|
||||||
return res.json({ success: true, deployed: false });
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = await getGatewayToken(container.Id);
|
|
||||||
const port = await getContainerPort(container.Id);
|
|
||||||
const baseUrl = 'http://localhost:' + port;
|
|
||||||
const health = await gatewayHealth(baseUrl, token);
|
|
||||||
|
|
||||||
res.json({
|
|
||||||
success: true,
|
|
||||||
deployed: true,
|
|
||||||
container: {
|
|
||||||
id: container.Id.slice(0, 12),
|
|
||||||
name: container.Name,
|
|
||||||
state: container.State,
|
|
||||||
status: container.Status,
|
|
||||||
created: container.Created,
|
|
||||||
image: container.Image
|
|
||||||
},
|
|
||||||
gateway: {
|
|
||||||
url: baseUrl,
|
|
||||||
port: port,
|
|
||||||
healthy: health.ok,
|
|
||||||
healthData: health.data || null,
|
|
||||||
tokenSet: !!token
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
// ── POST /openclaw/deploy ───────────────────────────────────────────────
|
|
||||||
|
|
||||||
router.post('/deploy', asyncHandler(async function(req, res) {
|
|
||||||
const existing = await findOpenClawContainer();
|
|
||||||
if (existing) {
|
|
||||||
return res.status(409).json({ success: false, error: 'OpenClaw is already deployed' });
|
|
||||||
}
|
|
||||||
|
|
||||||
const image = 'ghcr.io/nousresearch/openclaw:latest';
|
|
||||||
const name = 'openclaw-' + Date.now();
|
|
||||||
const gatewayToken = generateToken();
|
|
||||||
|
|
||||||
// Pull image
|
|
||||||
log.info('Pulling ' + image + '...');
|
|
||||||
try {
|
|
||||||
await new Promise(function(resolve, reject) {
|
|
||||||
docker.client.pull(image, function(err, stream) {
|
|
||||||
if (err) return reject(err);
|
|
||||||
docker.client.modem.followProgress(stream, function(err2) {
|
|
||||||
if (err2) return reject(err2);
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch(e) {
|
|
||||||
log.error('OpenClaw pull failed: ' + e.message);
|
|
||||||
return res.status(500).json({ success: false, error: 'Failed to pull image: ' + e.message });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create + start container
|
|
||||||
try {
|
|
||||||
const container = await docker.client.createContainer({
|
|
||||||
name: name,
|
|
||||||
Image: image,
|
|
||||||
Env: [
|
|
||||||
'OPENCLAW_GATEWAY_MODE=local',
|
|
||||||
'OPENCLAW_GATEWAY_TOKEN=' + gatewayToken
|
|
||||||
],
|
|
||||||
HostConfig: {
|
|
||||||
PortBindings: { '18792/tcp': [{ HostPort: '18792' }] },
|
|
||||||
RestartPolicy: { Name: 'unless-stopped' },
|
|
||||||
Labels: {
|
|
||||||
'dashcaddy.managed': 'true',
|
|
||||||
'dashcaddy.app': 'openclaw'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
ExposedPorts: { '18792/tcp': {} }
|
|
||||||
});
|
|
||||||
|
|
||||||
await container.start();
|
|
||||||
log.info('OpenClaw deployed: ' + container.id.slice(0, 12));
|
|
||||||
|
|
||||||
res.json({
|
|
||||||
success: true,
|
|
||||||
deployed: true,
|
|
||||||
container: { id: container.id.slice(0, 12), name: name },
|
|
||||||
gateway: {
|
|
||||||
url: 'http://localhost:18792',
|
|
||||||
token: gatewayToken
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch(e) {
|
|
||||||
log.error('OpenClaw deploy failed: ' + e.message);
|
|
||||||
res.status(500).json({ success: false, error: 'Deploy failed: ' + e.message });
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
// ── GET /openclaw/proxy/* ───────────────────────────────────────────────
|
|
||||||
|
|
||||||
router.get('/proxy/*', asyncHandler(async function(req, res) {
|
|
||||||
const container = await findOpenClawContainer();
|
|
||||||
if (!container) return res.status(404).json({ success: false, error: 'OpenClaw not deployed' });
|
|
||||||
|
|
||||||
const token = await getGatewayToken(container.Id);
|
|
||||||
const port = await getContainerPort(container.Id);
|
|
||||||
const baseUrl = 'http://localhost:' + port;
|
|
||||||
const path = req.params[0];
|
|
||||||
|
|
||||||
proxyRequest(req, res, baseUrl, path, token);
|
|
||||||
}));
|
|
||||||
|
|
||||||
// ── POST /openclaw/proxy/* ──────────────────────────────────────────────
|
|
||||||
|
|
||||||
router.post('/proxy/*', asyncHandler(async function(req, res) {
|
|
||||||
const container = await findOpenClawContainer();
|
|
||||||
if (!container) return res.status(404).json({ success: false, error: 'OpenClaw not deployed' });
|
|
||||||
|
|
||||||
const token = await getGatewayToken(container.Id);
|
|
||||||
const port = await getContainerPort(container.Id);
|
|
||||||
const baseUrl = 'http://localhost:' + port;
|
|
||||||
const path = req.params[0];
|
|
||||||
|
|
||||||
proxyRequest(req, res, baseUrl, path, token);
|
|
||||||
}));
|
|
||||||
|
|
||||||
// ── DELETE /openclaw ───────────────────────────────────────────────────
|
|
||||||
|
|
||||||
router.delete('/', asyncHandler(async function(req, res) {
|
|
||||||
const container = await findOpenClawContainer();
|
|
||||||
if (!container) return res.status(404).json({ success: false, error: 'OpenClaw not deployed' });
|
|
||||||
|
|
||||||
try {
|
|
||||||
const c = docker.client.container(container.Id);
|
|
||||||
await c.stop().catch(function() {});
|
|
||||||
await c.remove({ force: true });
|
|
||||||
log.info('OpenClaw container ' + container.Id.slice(0, 12) + ' removed');
|
|
||||||
res.json({ success: true, message: 'OpenClaw removed' });
|
|
||||||
} catch(e) {
|
|
||||||
log.error('Failed to remove OpenClaw: ' + e.message);
|
|
||||||
res.status(500).json({ success: false, error: e.message });
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
return router;
|
|
||||||
};
|
|
||||||
|
|
||||||
// ── token generator ──────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
function generateToken() {
|
|
||||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
||||||
let result = '';
|
|
||||||
for (let i = 0; i < 32; i++) {
|
|
||||||
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 6.0 KiB |
@@ -72,6 +72,7 @@ const bundles = {
|
|||||||
],
|
],
|
||||||
'init.js': [
|
'init.js': [
|
||||||
JS('core', 'init.js'),
|
JS('core', 'init.js'),
|
||||||
|
JS('monitoring-widgets.js'),
|
||||||
JS('keyboard-shortcuts.js'),
|
JS('keyboard-shortcuts.js'),
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
Vendored
+114
-87
File diff suppressed because one or more lines are too long
Vendored
+308
-233
File diff suppressed because one or more lines are too long
Vendored
+123
-12
File diff suppressed because one or more lines are too long
Vendored
+3
-3
File diff suppressed because one or more lines are too long
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
const CACHE = 'dashcaddy-shell-8ef9c82616';
|
const CACHE = 'dashcaddy-shell-594ec75648';
|
||||||
const PRECACHE = [
|
const PRECACHE = [
|
||||||
'/',
|
'/',
|
||||||
'/index.html',
|
'/index.html',
|
||||||
|
|||||||
Reference in New Issue
Block a user