diff --git a/dashcaddy-api/.eslintignore b/dashcaddy-api/.eslintignore new file mode 100644 index 0000000..3063216 --- /dev/null +++ b/dashcaddy-api/.eslintignore @@ -0,0 +1,5 @@ +node_modules/ +coverage/ +dist/ +build/ +*.min.js diff --git a/dashcaddy-api/.eslintrc.js b/dashcaddy-api/.eslintrc.js new file mode 100644 index 0000000..0f0b03a --- /dev/null +++ b/dashcaddy-api/.eslintrc.js @@ -0,0 +1,57 @@ +module.exports = { + env: { + node: true, + es2021: true, + }, + extends: 'eslint:recommended', + parserOptions: { + ecmaVersion: 'latest', + sourceType: 'commonjs', + }, + rules: { + // Error Prevention + 'no-unused-vars': ['warn', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }], + 'no-console': 'off', // We use structured logging, but console is okay for debug + 'no-undef': 'error', + 'no-unreachable': 'error', + 'no-constant-condition': ['error', { checkLoops: false }], + + // Code Quality + 'prefer-const': 'warn', + 'no-var': 'warn', + 'eqeqeq': ['warn', 'always', { null: 'ignore' }], + 'curly': ['warn', 'multi-line'], + 'no-throw-literal': 'error', + + // Async/Await + 'require-await': 'warn', + 'no-async-promise-executor': 'error', + 'no-await-in-loop': 'off', // Sometimes intentional for sequential operations + + // Style (Prettier handles formatting, these are semantic) + 'consistent-return': 'off', // Express routes don't always return + 'no-nested-ternary': 'warn', + 'max-depth': ['warn', 4], + 'complexity': ['warn', 20], + + // Prevent common pitfalls + 'no-eval': 'error', + 'no-implied-eval': 'error', + 'no-new-func': 'error', + 'no-with': 'error', + 'no-proto': 'error', + }, + overrides: [ + { + // Test files can be more lenient + files: ['**/__tests__/**/*.js', '**/*.test.js', '**/*.spec.js'], + env: { + jest: true, + }, + rules: { + 'no-unused-expressions': 'off', + 'max-depth': 'off', + }, + }, + ], +}; diff --git a/dashcaddy-api/.prettierignore b/dashcaddy-api/.prettierignore new file mode 100644 index 0000000..8d806ca --- /dev/null +++ b/dashcaddy-api/.prettierignore @@ -0,0 +1,6 @@ +node_modules/ +coverage/ +dist/ +build/ +package-lock.json +*.min.js diff --git a/dashcaddy-api/.prettierrc b/dashcaddy-api/.prettierrc new file mode 100644 index 0000000..1e1e4fc --- /dev/null +++ b/dashcaddy-api/.prettierrc @@ -0,0 +1,10 @@ +{ + "semi": true, + "singleQuote": true, + "trailingComma": "es5", + "printWidth": 120, + "tabWidth": 2, + "useTabs": false, + "arrowParens": "avoid", + "endOfLine": "lf" +} diff --git a/dashcaddy-api/Dockerfile b/dashcaddy-api/Dockerfile index 54888f6..2680695 100644 --- a/dashcaddy-api/Dockerfile +++ b/dashcaddy-api/Dockerfile @@ -9,6 +9,7 @@ COPY package*.json ./ RUN npm install --production COPY *.js ./ +COPY src/ ./src/ COPY routes/ ./routes/ COPY openapi.yaml ./ diff --git a/dashcaddy-api/error-handler.js b/dashcaddy-api/error-handler.js index 3e0dca2..2e311a2 100644 --- a/dashcaddy-api/error-handler.js +++ b/dashcaddy-api/error-handler.js @@ -1,10 +1,14 @@ -// Error Handler Middleware -// Centralizes error handling logic to eliminate duplicate catch blocks +/** + * DashCaddy Error Handler Middleware + * Centralizes error handling logic to eliminate duplicate catch blocks + */ -const { HTTP_STATUS } = require('./constants'); +const { AppError } = require('./errors'); +const { logError } = require('./error-logger'); /** - * Async route handler wrapper - catches errors and passes to error middleware + * Async route handler wrapper + * Automatically catches errors and passes to error middleware * Usage: app.get('/route', asyncHandler(async (req, res) => { ... })) */ function asyncHandler(fn) { @@ -14,72 +18,70 @@ function asyncHandler(fn) { } /** - * Express error middleware - handles all errors consistently + * Global error handling middleware + * MUST be registered after all routes in server.js */ function errorMiddleware(err, req, res, next) { - const logger = req.app.get('logger'); - - // Log the error with context - logger.error('Request error', { - error: err.message, - stack: err.stack, - path: req.path, + // Log all errors with request context + logError(req.path, err, { method: req.method, ip: req.ip, - userId: req.user?.id + userId: req.user?.id, + body: req.body }); + + // Determine if this is an operational error (AppError) or programming error + const isOperational = err.isOperational || err instanceof AppError; - // Determine status code - const statusCode = err.statusCode || err.status || HTTP_STATUS.INTERNAL_ERROR; + // Status code + const statusCode = err.statusCode || 500; - // Send consistent error response - res.status(statusCode).json({ + // Error code (DC-XXX format) + const code = err.code || `DC-${statusCode}`; + + // Build response + const response = { success: false, - error: err.message || 'Internal server error', - ...(process.env.NODE_ENV === 'development' && { stack: err.stack }) - }); + error: isOperational ? err.message : 'Internal server error', + code + }; + + // Add optional fields if present + if (err.requiresTotp) response.requiresTotp = true; + if (err.retryAfter) response.retryAfter = err.retryAfter; + if (err.field) response.field = err.field; + if (err.resource) response.resource = err.resource; + if (err.details && Object.keys(err.details).length > 0) response.details = err.details; + + // Development mode: include stack trace + if (process.env.NODE_ENV === 'development') { + response.stack = err.stack; + } + + // Send response + res.status(statusCode).json(response); + + // For non-operational errors, log as fatal + if (!isOperational) { + console.error('FATAL: Non-operational error detected', { + error: err.message, + stack: err.stack, + path: req.path + }); + } } /** - * Custom error classes for specific scenarios + * 404 handler for routes not found + * Register this before the global error handler */ -class ValidationError extends Error { - constructor(message) { - super(message); - this.name = 'ValidationError'; - this.statusCode = HTTP_STATUS.BAD_REQUEST; - } -} - -class UnauthorizedError extends Error { - constructor(message = 'Unauthorized') { - super(message); - this.name = 'UnauthorizedError'; - this.statusCode = HTTP_STATUS.UNAUTHORIZED; - } -} - -class NotFoundError extends Error { - constructor(message = 'Not found') { - super(message); - this.name = 'NotFoundError'; - this.statusCode = HTTP_STATUS.NOT_FOUND; - } -} - -class ConflictError extends Error { - constructor(message) { - super(message); - this.name = 'ConflictError'; - this.statusCode = HTTP_STATUS.CONFLICT; - } +function notFoundHandler(req, res, next) { + const { NotFoundError } = require('./errors'); + next(new NotFoundError(`Route ${req.method} ${req.path}`)); } module.exports = { asyncHandler, errorMiddleware, - ValidationError, - UnauthorizedError, - NotFoundError, - ConflictError + notFoundHandler }; diff --git a/dashcaddy-api/errors.js b/dashcaddy-api/errors.js index 573231c..50d8285 100644 --- a/dashcaddy-api/errors.js +++ b/dashcaddy-api/errors.js @@ -1,48 +1,105 @@ /** - * Typed Error Classes for DashCaddy API - * Provides structured errors that the global error handler catches automatically. + * DashCaddy API Error Classes + * All errors inherit from AppError and provide consistent structure. */ class AppError extends Error { - constructor(message, statusCode = 500, code = 'INTERNAL_ERROR') { + constructor(message, statusCode = 500, code = null) { super(message); this.name = this.constructor.name; this.statusCode = statusCode; - this.code = code; + this.code = code || this.constructor.name.toUpperCase().replace(/ERROR$/, '_ERROR'); + this.isOperational = true; // Distinguishes from programming errors } } -class DockerError extends AppError { - constructor(message, details = {}) { - super(message, 500, 'DOCKER_ERROR'); - this.details = details; - } -} +// 4xx Client Errors -class CaddyError extends AppError { - constructor(message, details = {}) { - super(message, 502, 'CADDY_ERROR'); - this.details = details; - } -} - -class DNSError extends AppError { - constructor(message, details = {}) { - super(message, 502, 'DNS_ERROR'); - this.details = details; +class ValidationError extends AppError { + constructor(message, field = null) { + super(message, 400, 'DC-400'); + this.field = field; } } class AuthenticationError extends AppError { - constructor(message = 'Authentication required') { - super(message, 401, 'AUTH_REQUIRED'); + constructor(message = 'Authentication required', requiresTotp = false) { + super(message, 401, 'DC-401'); + this.requiresTotp = requiresTotp; + } +} + +class ForbiddenError extends AppError { + constructor(message = 'Forbidden') { + super(message, 403, 'DC-403'); } } class NotFoundError extends AppError { constructor(resource = 'Resource') { - super(`${resource} not found`, 404, 'NOT_FOUND'); + super(`${resource} not found`, 404, 'DC-404'); + this.resource = resource; } } -module.exports = { AppError, DockerError, CaddyError, DNSError, AuthenticationError, NotFoundError }; +class ConflictError extends AppError { + constructor(message, conflictingResource = null) { + super(message, 409, 'DC-409'); + this.conflictingResource = conflictingResource; + } +} + +class RateLimitError extends AppError { + constructor(retryAfter = 60) { + super('Rate limit exceeded', 429, 'DC-429'); + this.retryAfter = retryAfter; + } +} + +// 5xx Server Errors + +class DockerError extends AppError { + constructor(message, operation = null, details = {}) { + super(message, 500, 'DC-500-DOCKER'); + this.operation = operation; + this.details = details; + } +} + +class CaddyError extends AppError { + constructor(message, operation = null, details = {}) { + super(message, 502, 'DC-502-CADDY'); + this.operation = operation; + this.details = details; + } +} + +class DNSError extends AppError { + constructor(message, operation = null, details = {}) { + super(message, 502, 'DC-502-DNS'); + this.operation = operation; + this.details = details; + } +} + +class ServiceUnavailableError extends AppError { + constructor(service, retryAfter = null) { + super(`Service unavailable: ${service}`, 503, 'DC-503'); + this.service = service; + this.retryAfter = retryAfter; + } +} + +module.exports = { + AppError, + ValidationError, + AuthenticationError, + ForbiddenError, + NotFoundError, + ConflictError, + RateLimitError, + DockerError, + CaddyError, + DNSError, + ServiceUnavailableError +}; diff --git a/dashcaddy-api/package-lock.json b/dashcaddy-api/package-lock.json index b8943c5..d7431c5 100644 --- a/dashcaddy-api/package-lock.json +++ b/dashcaddy-api/package-lock.json @@ -1,12 +1,12 @@ { "name": "dashcaddy-api", - "version": "1.1.0", + "version": "1.1.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dashcaddy-api", - "version": "1.1.0", + "version": "1.1.5", "dependencies": { "compression": "^1.8.1", "cors": "^2.8.6", diff --git a/dashcaddy-api/routes/apps/deploy.js b/dashcaddy-api/routes/apps/deploy.js index 8026771..1d6ad40 100644 --- a/dashcaddy-api/routes/apps/deploy.js +++ b/dashcaddy-api/routes/apps/deploy.js @@ -6,29 +6,57 @@ const { REGEX, DOCKER } = require('../../constants'); const { isValidPort } = require('../../input-validator'); const { exists } = require('../../fs-helpers'); const platformPaths = require('../../platform-paths'); +const { ValidationError } = require('../../errors'); +const { logError } = require('../../src/utils/logging'); +/** + * Apps deployment routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.docker - Docker client wrapper + * @param {Object} deps.caddy - Caddy client + * @param {Object} deps.credentialManager - Credential manager + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Object} deps.portLockManager - Port lock manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.errorResponse - Error response helper + * @param {Object} deps.log - Logger instance + * @param {Object} deps.helpers - Apps helpers module + * @returns {express.Router} + */ -module.exports = function(ctx, helpers) { +module.exports = function({ docker, caddy, credentialManager, servicesStateManager, portLockManager, asyncHandler, errorResponse, log, helpers, APP_TEMPLATES, siteConfig, buildDomain, buildServiceUrl, addServiceToConfig, dns, notification, safeErrorMessage }) { const router = express.Router(); + + // Ctx shim for backward compatibility with existing route code + const ctx = { + APP_TEMPLATES, + siteConfig, + buildDomain, + buildServiceUrl, + addServiceToConfig, + dns, + notification, + safeErrorMessage + }; async function deployDashCAStaticSite(template, userConfig) { const destPath = platformPaths.caCertDir; try { - ctx.log.info('deploy', 'DashCA: Starting static site deployment'); + log.info('deploy', 'DashCA: Starting static site deployment'); if (!await exists(destPath)) { await fsp.mkdir(destPath, { recursive: true }); - ctx.log.info('deploy', 'DashCA: Created destination directory', { path: destPath }); + log.info('deploy', 'DashCA: Created destination directory', { path: destPath }); } - ctx.log.info('deploy', 'DashCA: Verifying certificate files'); + log.info('deploy', 'DashCA: Verifying certificate files'); const rootCertExists = await exists(`${destPath}/root.crt`); const intermediateCertExists = await exists(`${destPath}/intermediate.crt`); - if (rootCertExists) ctx.log.info('deploy', 'DashCA: Root certificate found'); - else ctx.log.warn('deploy', 'DashCA: Root certificate not found', { expected: path.join(destPath, 'root.crt') }); - if (intermediateCertExists) ctx.log.info('deploy', 'DashCA: Intermediate certificate found'); + if (rootCertExists) log.info('deploy', 'DashCA: Root certificate found'); + else log.warn('deploy', 'DashCA: Root certificate not found', { expected: path.join(destPath, 'root.crt') }); + if (intermediateCertExists) log.info('deploy', 'DashCA: Intermediate certificate found'); const indexPath = path.join(destPath, 'index.html'); if (!await exists(indexPath)) { - ctx.log.info('deploy', 'DashCA: Creating minimal landing page'); + log.info('deploy', 'DashCA: Creating minimal landing page'); const minimalHtml = `
@@ -57,15 +85,15 @@ module.exports = function(ctx, helpers) { `; await fsp.writeFile(indexPath, minimalHtml); - ctx.log.info('deploy', 'DashCA: Created minimal landing page'); + log.info('deploy', 'DashCA: Created minimal landing page'); } else { - ctx.log.info('deploy', 'DashCA: Using existing index.html'); + log.info('deploy', 'DashCA: Using existing index.html'); } - ctx.log.info('deploy', 'DashCA: For full features, copy certificate files to ' + destPath); - ctx.log.info('deploy', 'DashCA: Static site deployment completed successfully'); + log.info('deploy', 'DashCA: For full features, copy certificate files to ' + destPath); + log.info('deploy', 'DashCA: Static site deployment completed successfully'); } catch (error) { - ctx.log.error('deploy', 'DashCA deployment error', { error: error.message }); + log.error('deploy', 'DashCA deployment error', { error: error.message }); throw new Error(`DashCA deployment failed: ${error.message}`); } } @@ -81,9 +109,9 @@ module.exports = function(ctx, helpers) { let lockId = null; try { - ctx.log.info('deploy', 'Acquiring port locks', { ports: requestedPorts }); - lockId = await ctx.portLockManager.acquirePorts(requestedPorts); - ctx.log.info('deploy', 'Port locks acquired', { lockId }); + log.info('deploy', 'Acquiring port locks', { ports: requestedPorts }); + lockId = await portLockManager.acquirePorts(requestedPorts); + log.info('deploy', 'Port locks acquired', { lockId }); } catch (lockError) { throw new Error(`Failed to acquire port locks: ${lockError.message}`); } @@ -91,9 +119,9 @@ module.exports = function(ctx, helpers) { try { // Remove stale container with same name try { - const existingContainer = ctx.docker.client.getContainer(containerName); + const existingContainer = docker.client.getContainer(containerName); const info = await existingContainer.inspect(); - ctx.log.info('docker', 'Removing stale container', { containerName, status: info.State.Status }); + log.info('docker', 'Removing stale container', { containerName, status: info.State.Status }); await existingContainer.remove({ force: true }); await new Promise(r => setTimeout(r, 2000)); } catch (e) { @@ -143,43 +171,43 @@ module.exports = function(ctx, helpers) { } try { - ctx.log.info('docker', 'Pulling image', { image: processedTemplate.docker.image }); - await ctx.docker.pull(processedTemplate.docker.image); - ctx.log.info('docker', 'Image pulled successfully', { image: processedTemplate.docker.image }); + log.info('docker', 'Pulling image', { image: processedTemplate.docker.image }); + await docker.pull(processedTemplate.docker.image); + log.info('docker', 'Image pulled successfully', { image: processedTemplate.docker.image }); } catch (e) { - ctx.log.warn('docker', 'Image pull failed, checking if local image exists', { image: processedTemplate.docker.image, error: e.message }); + log.warn('docker', 'Image pull failed, checking if local image exists', { image: processedTemplate.docker.image, error: e.message }); try { - const images = await ctx.docker.client.listImages({ filters: { reference: [processedTemplate.docker.image] } }); + const images = await docker.client.listImages({ filters: { reference: [processedTemplate.docker.image] } }); if (images.length === 0) throw new Error(`[DC-201] Image ${processedTemplate.docker.image} not found locally and pull failed: ${e.message}`); - ctx.log.info('docker', 'Using existing local image', { image: processedTemplate.docker.image }); + log.info('docker', 'Using existing local image', { image: processedTemplate.docker.image }); } catch (listError) { throw new Error(`[DC-201] Failed to pull or find image ${processedTemplate.docker.image}: ${e.message}`); } } - const container = await ctx.docker.client.createContainer(containerConfig); + const container = await docker.client.createContainer(containerConfig); await container.start(); // Prune dangling images to prevent disk bloat try { - const pruneResult = await ctx.docker.client.pruneImages({ filters: { dangling: { true: true } } }); + const pruneResult = await docker.client.pruneImages({ filters: { dangling: { true: true } } }); if (pruneResult.SpaceReclaimed > 0) { - ctx.log.info('docker', 'Pruned dangling images after deploy', { spaceReclaimed: Math.round(pruneResult.SpaceReclaimed / 1024 / 1024) + 'MB' }); + log.info('docker', 'Pruned dangling images after deploy', { spaceReclaimed: Math.round(pruneResult.SpaceReclaimed / 1024 / 1024) + 'MB' }); } } catch (pruneErr) { - ctx.log.debug('docker', 'Image prune after deploy failed', { error: pruneErr.message }); + log.debug('docker', 'Image prune after deploy failed', { error: pruneErr.message }); } - await ctx.portLockManager.releasePorts(lockId); - ctx.log.info('deploy', 'Port locks released', { lockId }); + await portLockManager.releasePorts(lockId); + log.info('deploy', 'Port locks released', { lockId }); return container.id; } catch (deployError) { if (lockId) { try { - await ctx.portLockManager.releasePorts(lockId); - ctx.log.info('deploy', 'Port locks released after error', { lockId }); + await portLockManager.releasePorts(lockId); + log.info('deploy', 'Port locks released after error', { lockId }); } catch (releaseError) { - ctx.log.error('deploy', 'Failed to release port locks', { lockId, error: releaseError.message }); + log.error('deploy', 'Failed to release port locks', { lockId, error: releaseError.message }); } } throw deployError; @@ -187,10 +215,10 @@ module.exports = function(ctx, helpers) { } // Check for existing container before deployment - router.post('/apps/check-existing', ctx.asyncHandler(async (req, res) => { + router.post('/apps/check-existing', asyncHandler(async (req, res) => { const { appId } = req.body; const template = ctx.APP_TEMPLATES[appId]; - if (!template) return ctx.errorResponse(res, 400, 'Invalid app template'); + if (!template) throw new ValidationError('Invalid app template'); const existingContainer = await helpers.findExistingContainerByImage(template); if (existingContainer) { res.json({ success: true, exists: true, container: existingContainer, message: `Found existing ${template.name} container: ${existingContainer.name}` }); @@ -200,42 +228,42 @@ module.exports = function(ctx, helpers) { }, 'check-existing')); // Deploy new app - router.post('/apps/deploy', ctx.asyncHandler(async (req, res) => { + router.post('/apps/deploy', asyncHandler(async (req, res) => { const { appId, config } = req.body; if (!appId || typeof appId !== 'string') { - return ctx.errorResponse(res, 400, 'appId is required'); + throw new ValidationError('appId is required'); } if (!config || typeof config !== 'object') { - return ctx.errorResponse(res, 400, 'config object is required'); + throw new ValidationError('config object is required'); } if (!config.subdomain || typeof config.subdomain !== 'string') { - return ctx.errorResponse(res, 400, 'config.subdomain is required'); + throw new ValidationError('config.subdomain is required'); } try { - ctx.log.info('deploy', 'Deploying app', { appId, subdomain: config.subdomain }); + log.info('deploy', 'Deploying app', { appId, subdomain: config.subdomain }); const template = ctx.APP_TEMPLATES[appId]; if (!template) { - await ctx.logError('app-deploy', new Error('Invalid app template'), { appId, config }); - return ctx.errorResponse(res, 400, 'Invalid app template'); + await logError('app-deploy', new Error('Invalid app template'), { appId, config }); + throw new ValidationError('Invalid app template'); } if (config.subdomain) { if (!REGEX.SUBDOMAIN.test(config.subdomain)) { - return ctx.errorResponse(res, 400, '[DC-301] Invalid subdomain format'); + throw new ValidationError('[DC-301] Invalid subdomain format'); } // Block reserved path names in subdirectory mode if (ctx.siteConfig.routingMode === 'subdirectory' && helpers.RESERVED_SUBPATHS.includes(config.subdomain)) { - return ctx.errorResponse(res, 400, `[DC-301] "${config.subdomain}" is a reserved path and cannot be used in subdirectory mode`); + return errorResponse(res, 400, `[DC-301] "${config.subdomain}" is a reserved path and cannot be used in subdirectory mode`); } } if (config.port && !isValidPort(config.port)) { - return ctx.errorResponse(res, 400, 'Invalid port number (must be 1-65535)'); + throw new ValidationError('Invalid port number (must be 1-65535)'); } if (!template.isStaticSite) { const allowedHostnames = ['localhost', 'host.docker.internal']; if (config.ip && !validatorLib.isIP(config.ip) && !allowedHostnames.includes(config.ip)) { - return ctx.errorResponse(res, 400, '[DC-210] Invalid IP address. Use a valid IP (e.g., 192.168.x.x) or "localhost".'); + return errorResponse(res, 400, '[DC-210] Invalid IP address. Use a valid IP (e.g., 192.168.x.x) or "localhost".'); } if (!config.ip) config.ip = ctx.siteConfig.dnsServerIp || 'localhost'; } else { @@ -245,26 +273,29 @@ module.exports = function(ctx, helpers) { let containerId; let usedExisting = false; + + // Process template variables for manifest (only needed for Docker containers) + const processedTemplate = template.isStaticSite ? null : helpers.processTemplateVariables(template, config); if (template.isStaticSite) { - ctx.log.info('deploy', 'Deploying static site', { appId }); + log.info('deploy', 'Deploying static site', { appId }); if (appId === 'dashca') { await deployDashCAStaticSite(template, config); containerId = null; - ctx.log.info('deploy', 'Static site deployed', { appId }); + log.info('deploy', 'Static site deployed', { appId }); } else { throw new Error(`Unknown static site type: ${appId}`); } } else if (config.useExisting && config.existingContainerId) { containerId = config.existingContainerId; usedExisting = true; - ctx.log.info('deploy', 'Using existing container', { containerId }); + log.info('deploy', 'Using existing container', { containerId }); if (config.existingPort && !config.port) config.port = config.existingPort; } else { containerId = await deployContainer(appId, config, template); - ctx.log.info('deploy', 'Container deployed', { containerId }); + log.info('deploy', 'Container deployed', { containerId }); await helpers.waitForHealthCheck(containerId, template.healthCheck, config.port || template.defaultPort); - ctx.log.info('deploy', 'Container is healthy', { containerId }); + log.info('deploy', 'Container is healthy', { containerId }); } const isSubdirectoryMode = ctx.siteConfig.routingMode === 'subdirectory' && ctx.siteConfig.domain; @@ -274,11 +305,11 @@ module.exports = function(ctx, helpers) { if (config.createDns && !isSubdirectoryMode) { try { await ctx.dns.createRecord(config.subdomain, config.ip); - ctx.log.info('deploy', 'DNS record created', { domain: ctx.buildDomain(config.subdomain), ip: config.ip }); + log.info('deploy', 'DNS record created', { domain: ctx.buildDomain(config.subdomain), ip: config.ip }); } catch (dnsError) { - await ctx.logError('app-deploy-dns', dnsError, { appId, subdomain: config.subdomain, ip: config.ip }); + await logError('app-deploy-dns', dnsError, { appId, subdomain: config.subdomain, ip: config.ip }); dnsWarning = `DNS creation failed: ${dnsError.message}. You may need to create the DNS record manually.`; - ctx.log.warn('deploy', 'DNS creation failed during deploy', { error: dnsError.message }); + log.warn('deploy', 'DNS creation failed during deploy', { error: dnsError.message }); } } @@ -297,7 +328,7 @@ module.exports = function(ctx, helpers) { } caddyConfig = helpers.generateStaticSiteConfig(config.subdomain, sitePath, caddyOptions); } else { - caddyConfig = ctx.caddy.generateConfig(config.subdomain, config.ip, config.port || template.defaultPort, caddyOptions); + caddyConfig = caddy.generateConfig(config.subdomain, config.ip, config.port || template.defaultPort, caddyOptions); } // Write Caddy config (subdirectory: inject into main block; subdomain: append as new block) @@ -307,7 +338,7 @@ module.exports = function(ctx, helpers) { } else { await helpers.addCaddyConfig(config.subdomain, caddyConfig); } - ctx.log.info('deploy', 'Caddy config added', { domain: ctx.buildDomain(config.subdomain), routingMode: ctx.siteConfig.routingMode, tailscaleOnly: config.tailscaleOnly || false }); + log.info('deploy', 'Caddy config added', { domain: ctx.buildDomain(config.subdomain), routingMode: ctx.siteConfig.routingMode, tailscaleOnly: config.tailscaleOnly || false }); // Build service URL based on routing mode const serviceUrl = ctx.buildServiceUrl(config.subdomain); @@ -360,7 +391,7 @@ module.exports = function(ctx, helpers) { deployedAt: new Date().toISOString(), deploymentManifest }); - ctx.log.info('deploy', 'Service added to dashboard', { subdomain: config.subdomain }); + log.info('deploy', 'Service added to dashboard', { subdomain: config.subdomain }); const response = { success: true, containerId, usedExisting, @@ -377,11 +408,11 @@ module.exports = function(ctx, helpers) { res.json(response); } catch (error) { - await ctx.logError('app-deploy', error, { appId, config }); - ctx.log.error('deploy', 'Deployment failed', { appId, error: error.message }); + await logError('app-deploy', error, { appId, config }); + log.error('deploy', 'Deployment failed', { appId, error: error.message }); const template = ctx.APP_TEMPLATES[appId]; ctx.notification.send('deploymentFailed', 'Deployment Failed', `Failed to deploy **${template?.name || appId}**.\nError: ${error.message}`, 'error'); - ctx.errorResponse(res, 500, ctx.safeErrorMessage(error)); + errorResponse(res, 500, ctx.safeErrorMessage(error)); } }, 'apps-deploy')); diff --git a/dashcaddy-api/routes/apps/helpers.js b/dashcaddy-api/routes/apps/helpers.js index 6e9d76b..450e07f 100644 --- a/dashcaddy-api/routes/apps/helpers.js +++ b/dashcaddy-api/routes/apps/helpers.js @@ -6,12 +6,23 @@ const { REGEX, DOCKER } = require('../../constants'); const { exists } = require('../../fs-helpers'); const platformPaths = require('../../platform-paths'); -module.exports = function(ctx) { +/** + * Apps helpers factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.docker - Docker client wrapper + * @param {Object} deps.caddy - Caddy client + * @param {Object} deps.credentialManager - Credential manager + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Function} deps.fetchT - Timeout-wrapped fetch + * @param {Object} deps.log - Logger instance + * @returns {Object} Helper functions + */ +module.exports = function({ docker, caddy, credentialManager, servicesStateManager, fetchT, log }) { async function checkPortConflicts(ports, excludeContainerName = null) { const conflicts = []; try { - const containers = await ctx.docker.client.listContainers({ all: true }); + const containers = await docker.client.listContainers({ all: true }); for (const container of containers) { if (excludeContainerName && container.Names.some(n => n === `/${excludeContainerName}`)) continue; if (container.State !== 'running') continue; @@ -27,14 +38,14 @@ module.exports = function(ctx) { } } } catch (e) { - ctx.log.warn('docker', 'Could not check port conflicts', { error: e.message }); + log.warn('docker', 'Could not check port conflicts', { error: e.message }); } return conflicts; } async function findExistingContainerByImage(template) { try { - const containers = await ctx.docker.client.listContainers({ all: false }); + const containers = await docker.client.listContainers({ all: false }); const templateImage = template.docker.image.split(':')[0]; for (const container of containers) { const containerImage = container.Image.split(':')[0]; @@ -53,7 +64,7 @@ module.exports = function(ctx) { } return null; } catch (e) { - ctx.log.warn('docker', 'Could not check for existing containers', { error: e.message }); + log.warn('docker', 'Could not check for existing containers', { error: e.message }); return null; } } @@ -140,7 +151,7 @@ module.exports = function(ctx) { normalizedHost === root || normalizedHost.startsWith(root + path.sep) ); if (!isAllowed) { - ctx.log.warn('deploy', 'Custom volume host path rejected', { hostPath: override.hostPath, allowed: allowedRoots }); + log.warn('deploy', 'Custom volume host path rejected', { hostPath: override.hostPath, allowed: allowedRoots }); return vol; // Keep original volume, don't apply unsafe override } return `${toDockerDesktopPath(override.hostPath)}:${containerPath}`; @@ -243,39 +254,39 @@ module.exports = function(ctx) { for (let i = 0; i < maxAttempts; i++) { try { - const container = ctx.docker.client.getContainer(containerId); + const container = docker.client.getContainer(containerId); const info = await container.inspect(); if (info.State.Running) { if (info.State.Health) { if (info.State.Health.Status === 'healthy') { - ctx.log.info('docker', 'Container is healthy (Docker health check)', { containerId }); + log.info('docker', 'Container is healthy (Docker health check)', { containerId }); return true; } } else if (healthPath && port && httpCheckFailed < 5) { try { - const response = await ctx.fetchT(`http://localhost:${port}${healthPath}`, { + const response = await fetchT(`http://localhost:${port}${healthPath}`, { signal: AbortSignal.timeout(3000), redirect: 'manual' }); if (response.ok || (response.status >= 300 && response.status < 400)) { - ctx.log.info('docker', 'Health check passed', { containerId, status: response.status }); + log.info('docker', 'Health check passed', { containerId, status: response.status }); return true; } } catch (e) { httpCheckFailed++; - ctx.log.debug('docker', 'HTTP health check failed', { attempt: httpCheckFailed, error: e.message }); + log.debug('docker', 'HTTP health check failed', { attempt: httpCheckFailed, error: e.message }); } } else { if (i >= 5) { - ctx.log.info('docker', 'Container is running', { containerId, waitedSeconds: i * delay / 1000 }); + log.info('docker', 'Container is running', { containerId, waitedSeconds: i * delay / 1000 }); return true; } } } } catch (e) { - ctx.log.debug('docker', 'Health check attempt failed', { attempt: i + 1, error: e.message }); + log.debug('docker', 'Health check attempt failed', { attempt: i + 1, error: e.message }); } if (i < maxAttempts - 1) { - ctx.log.debug('docker', 'Waiting for container to be healthy', { attempt: i + 1, maxAttempts }); + log.debug('docker', 'Waiting for container to be healthy', { attempt: i + 1, maxAttempts }); await new Promise(resolve => setTimeout(resolve, delay)); } } @@ -284,15 +295,15 @@ module.exports = function(ctx) { async function addCaddyConfig(subdomain, config) { const domain = ctx.buildDomain(subdomain); - const existing = await ctx.caddy.read(); + const existing = await caddy.read(); if (existing.includes(`${domain} {`)) { - ctx.log.info('caddy', 'Caddy config already exists, skipping add', { domain }); - await ctx.caddy.reload(existing); + log.info('caddy', 'Caddy config already exists, skipping add', { domain }); + await caddy.reload(existing); return; } - const result = await ctx.caddy.modify(c => c + `\n${config}\n`); + const result = await caddy.modify(c => c + `\n${config}\n`); if (!result.success) throw new Error(`[DC-303] Failed to add Caddy config for ${domain}: ${result.error}`); - await ctx.caddy.verifySite(domain); + await caddy.verifySite(domain); } // Reserved paths that cannot be used as subpath names in subdirectory mode @@ -303,7 +314,7 @@ module.exports = function(ctx) { async function ensureMainDomainBlock() { if (ctx.siteConfig.routingMode !== 'subdirectory' || !ctx.siteConfig.domain) return; - const content = await ctx.caddy.read(); + const content = await caddy.read(); const domain = ctx.siteConfig.domain; const ROUTE_MARKER = '# === DashCaddy App Routes ==='; @@ -312,7 +323,7 @@ module.exports = function(ctx) { // Domain block exists but lacks markers — inject them if (content.includes(`${domain} {`)) { - const result = await ctx.caddy.modify(c => { + const result = await caddy.modify(c => { // Insert markers before the final catch-all handle block inside the domain block const domainStart = c.indexOf(`${domain} {`); // Find standalone "handle {" (catch-all SPA fallback) — match tabs or spaces @@ -325,7 +336,7 @@ module.exports = function(ctx) { return c.slice(0, handleIdx) + markerBlock + c.slice(handleIdx); }); if (result.success) { - ctx.log.info('caddy', 'Injected route markers into existing domain block', { domain }); + log.info('caddy', 'Injected route markers into existing domain block', { domain }); } return; } @@ -335,9 +346,9 @@ module.exports = function(ctx) { const apiPort = process.env.PORT || 3001; const block = `\n${domain} {\n root * ${dashboardRoot}\n encode gzip\n\n handle /api/* {\n reverse_proxy localhost:${apiPort}\n }\n\n handle /probe/* {\n reverse_proxy localhost:${apiPort}\n }\n\n ${ROUTE_MARKER}\n # === End App Routes ===\n\n handle {\n @notFile not file {path}\n rewrite @notFile /index.html\n file_server\n }\n}\n`; - const result = await ctx.caddy.modify(c => c + block); + const result = await caddy.modify(c => c + block); if (result.success) { - ctx.log.info('caddy', 'Created main domain block with route markers', { domain }); + log.info('caddy', 'Created main domain block with route markers', { domain }); } else { throw new Error(`[DC-303] Failed to create main domain block for ${domain}: ${result.error}`); } @@ -349,9 +360,9 @@ module.exports = function(ctx) { const endMarker = `# --- End: ${subdomain} ---`; const END_ROUTE_MARKER = '# === End App Routes ==='; - const result = await ctx.caddy.modify(content => { + const result = await caddy.modify(content => { if (content.includes(marker)) { - ctx.log.info('caddy', 'Subpath config already exists, skipping', { subdomain }); + log.info('caddy', 'Subpath config already exists, skipping', { subdomain }); return null; } @@ -378,7 +389,7 @@ module.exports = function(ctx) { const marker = `# --- DashCaddy: ${subdomain} ---`; const endMarker = `# --- End: ${subdomain} ---`; - return await ctx.caddy.modify(content => { + return await caddy.modify(content => { const startIdx = content.indexOf(marker); if (startIdx === -1) return null; diff --git a/dashcaddy-api/routes/apps/index.js b/dashcaddy-api/routes/apps/index.js index f28226c..3c4128c 100644 --- a/dashcaddy-api/routes/apps/index.js +++ b/dashcaddy-api/routes/apps/index.js @@ -5,14 +5,44 @@ const initRemoval = require('./removal'); const initTemplates = require('./templates'); const initRestore = require('./restore'); +/** + * Apps routes aggregator + * Assembles all apps sub-routes with their dependencies + * @param {Object} ctx - Application context (for backward compatibility) + * @returns {express.Router} + */ module.exports = function(ctx) { const router = express.Router(); - const helpers = initHelpers(ctx); - router.use(initDeploy(ctx, helpers)); - router.use(initRemoval(ctx, helpers)); - router.use(initTemplates(ctx, helpers)); - router.use(initRestore(ctx, helpers)); + // Extract dependencies from context + const deps = { + docker: ctx.docker, + caddy: ctx.caddy, + credentialManager: ctx.credentialManager, + servicesStateManager: ctx.servicesStateManager, + portLockManager: ctx.portLockManager, + asyncHandler: ctx.asyncHandler, + errorResponse: ctx.errorResponse, + log: ctx.log, + // Additional context properties needed by routes + APP_TEMPLATES: ctx.APP_TEMPLATES, + siteConfig: ctx.siteConfig, + buildDomain: ctx.buildDomain, + buildServiceUrl: ctx.buildServiceUrl, + addServiceToConfig: ctx.addServiceToConfig, + dns: ctx.dns, + notification: ctx.notification, + safeErrorMessage: ctx.safeErrorMessage + }; + + // Initialize helpers with dependencies + const helpers = initHelpers(deps); + + // Mount sub-routes with explicit dependencies + router.use(initDeploy({ ...deps, helpers })); + router.use(initRemoval({ ...deps, helpers })); + router.use(initTemplates({ ...deps, helpers })); + router.use(initRestore({ ...deps, helpers })); return router; }; diff --git a/dashcaddy-api/routes/apps/removal.js b/dashcaddy-api/routes/apps/removal.js index 2e14356..7643dfb 100644 --- a/dashcaddy-api/routes/apps/removal.js +++ b/dashcaddy-api/routes/apps/removal.js @@ -1,35 +1,46 @@ const express = require('express'); const { exists } = require('../../fs-helpers'); -module.exports = function(ctx, helpers) { +module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, log, helpers }) { const router = express.Router(); // Remove deployed app - router.delete('/apps/:appId', ctx.asyncHandler(async (req, res) => { +/** + * Apps removal routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.docker - Docker client wrapper + * @param {Object} deps.caddy - Caddy client + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.log - Logger instance + * @param {Object} deps.helpers - Apps helpers module + * @returns {express.Router} + */ + router.delete('/apps/:appId', asyncHandler(async (req, res) => { const { appId } = req.params; const { containerId, subdomain, ip, deleteContainer } = req.query; const shouldDeleteContainer = deleteContainer === 'true'; const results = { container: null, dns: null, caddy: null, service: null }; try { - ctx.log.info('deploy', 'Removing app', { appId, containerId, subdomain, deleteContainer: shouldDeleteContainer }); + log.info('deploy', 'Removing app', { appId, containerId, subdomain, deleteContainer: shouldDeleteContainer }); if (containerId && shouldDeleteContainer) { try { - const container = ctx.docker.client.getContainer(containerId); - try { await container.stop(); ctx.log.info('docker', 'Container stopped', { containerId }); } - catch (stopError) { ctx.log.debug('docker', 'Container stop note', { containerId, note: stopError.message }); } + const container = docker.client.getContainer(containerId); + try { await container.stop(); log.info('docker', 'Container stopped', { containerId }); } + catch (stopError) { log.debug('docker', 'Container stop note', { containerId, note: stopError.message }); } await container.remove({ force: true }); results.container = 'removed'; - ctx.log.info('docker', 'Container removed', { containerId }); + log.info('docker', 'Container removed', { containerId }); // Prune dangling images after removal try { - const pruneResult = await ctx.docker.client.pruneImages({ filters: { dangling: { true: true } } }); + const pruneResult = await docker.client.pruneImages({ filters: { dangling: { true: true } } }); if (pruneResult.SpaceReclaimed > 0) { - ctx.log.info('docker', 'Pruned dangling images after removal', { spaceReclaimed: Math.round(pruneResult.SpaceReclaimed / 1024 / 1024) + 'MB' }); + log.info('docker', 'Pruned dangling images after removal', { spaceReclaimed: Math.round(pruneResult.SpaceReclaimed / 1024 / 1024) + 'MB' }); } } catch (pruneErr) { - ctx.log.debug('docker', 'Image prune after removal failed', { error: pruneErr.message }); + log.debug('docker', 'Image prune after removal failed', { error: pruneErr.message }); } } catch (error) { results.container = error.message.includes('no such container') ? 'already removed' : error.message; @@ -53,7 +64,7 @@ module.exports = function(ctx, helpers) { token: ctx.dns.getToken(), domain, type: 'A', ipAddress: recordIp }); results.dns = dnsResult.status === 'ok' ? 'deleted' : (dnsResult.errorMessage || 'failed'); - ctx.log.info('dns', 'DNS record removal', { result: results.dns }); + log.info('dns', 'DNS record removal', { result: results.dns }); } catch (error) { results.dns = error.message; } @@ -66,7 +77,7 @@ module.exports = function(ctx, helpers) { if (shouldDeleteContainer && subdomain) { try { // Check if this service was deployed in subdirectory mode - const services = await ctx.servicesStateManager.read(); + const services = await servicesStateManager.read(); const serviceList = Array.isArray(services) ? services : []; const service = serviceList.find(s => s.id === subdomain); @@ -79,14 +90,14 @@ module.exports = function(ctx, helpers) { const domain = ctx.buildDomain(subdomain); const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const siteBlockRegex = new RegExp(`\\n?${escapedDomain}\\s*\\{[^{}]*(?:\\{[^{}]*(?:\\{[^{}]*\\}[^{}]*)*\\}[^{}]*)*\\}\\s*`, 'g'); - const caddyResult = await ctx.caddy.modify(currentContent => { + const caddyResult = await caddy.modify(currentContent => { const replaced = currentContent.replace(siteBlockRegex, '\n'); if (replaced.length === currentContent.length) return null; return replaced.replace(/\n{3,}/g, '\n\n'); }); results.caddy = caddyResult.success ? 'removed' : (caddyResult.rolledBack ? 'removed (reload failed)' : 'not found'); } - ctx.log.info('caddy', 'Caddy config removal', { result: results.caddy }); + log.info('caddy', 'Caddy config removal', { result: results.caddy }); } catch (error) { results.caddy = error.message; } @@ -97,7 +108,7 @@ module.exports = function(ctx, helpers) { try { if (await exists(ctx.SERVICES_FILE)) { let removed = false; - await ctx.servicesStateManager.update(services => { + await servicesStateManager.update(services => { const initialLength = services.length; const filtered = services.filter(s => s.id !== subdomain); removed = filtered.length !== initialLength; @@ -105,15 +116,15 @@ module.exports = function(ctx, helpers) { }); results.service = removed ? 'removed' : 'not found'; } - ctx.log.info('deploy', 'Service config removal', { result: results.service }); + log.info('deploy', 'Service config removal', { result: results.service }); } catch (error) { results.service = error.message; } res.json({ success: true, message: `App ${appId} removal completed`, results }); } catch (error) { - await ctx.logError('app-removal', error); - ctx.errorResponse(res, 500, ctx.safeErrorMessage(error), { results }); + await logError('app-removal', error); + errorResponse(res, 500, ctx.safeErrorMessage(error), { results }); } }, 'apps-delete')); diff --git a/dashcaddy-api/routes/apps/restore.js b/dashcaddy-api/routes/apps/restore.js index 91158a0..6b72c10 100644 --- a/dashcaddy-api/routes/apps/restore.js +++ b/dashcaddy-api/routes/apps/restore.js @@ -1,7 +1,18 @@ const express = require('express'); const { DOCKER } = require('../../constants'); -module.exports = function(ctx, helpers) { +/** + * Apps restore routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.docker - Docker client wrapper + * @param {Object} deps.caddy - Caddy client + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.log - Logger instance + * @param {Object} deps.helpers - Apps helpers module + * @returns {express.Router} + */ +module.exports = function({ docker, caddy, servicesStateManager, asyncHandler, log, helpers }) { const router = express.Router(); /** @@ -9,16 +20,16 @@ module.exports = function(ctx, helpers) { * Pulls image, creates container, starts it, recreates Caddy config. * Skips if container is already running. */ - router.post('/apps/:appId/restore', ctx.asyncHandler(async (req, res) => { + router.post('/apps/:appId/restore', asyncHandler(async (req, res) => { const { appId } = req.params; - const services = await ctx.servicesStateManager.read(); + const services = await servicesStateManager.read(); const service = services.find(s => s.id === appId); if (!service) { - return ctx.errorResponse(res, 404, `Service "${appId}" not found in services.json`); + return errorResponse(res, 404, `Service "${appId}" not found in services.json`); } if (!service.deploymentManifest) { - return ctx.errorResponse(res, 400, `Service "${appId}" has no deployment manifest — it was deployed before the manifest feature was added. Redeploy it manually to create a manifest.`); + return errorResponse(res, 400, `Service "${appId}" has no deployment manifest — it was deployed before the manifest feature was added. Redeploy it manually to create a manifest.`); } const result = await restoreService(service); @@ -29,8 +40,8 @@ module.exports = function(ctx, helpers) { * Restore all services that have deployment manifests. * Returns per-service results. */ - router.post('/apps/restore-all', ctx.asyncHandler(async (req, res) => { - const services = await ctx.servicesStateManager.read(); + router.post('/apps/restore-all', asyncHandler(async (req, res) => { + const services = await servicesStateManager.read(); const restoreable = services.filter(s => s.deploymentManifest); if (restoreable.length === 0) { @@ -70,8 +81,8 @@ module.exports = function(ctx, helpers) { /** * List all services and their restore status. */ - router.get('/apps/restore-status', ctx.asyncHandler(async (req, res) => { - const services = await ctx.servicesStateManager.read(); + router.get('/apps/restore-status', asyncHandler(async (req, res) => { + const services = await servicesStateManager.read(); const status = []; for (const service of services) { @@ -87,7 +98,7 @@ module.exports = function(ctx, helpers) { // Check if container is currently running if (service.containerId) { try { - const container = ctx.docker.client.getContainer(service.containerId); + const container = docker.client.getContainer(service.containerId); const info = await container.inspect(); entry.containerRunning = info.State.Running; } catch (e) { @@ -108,11 +119,11 @@ module.exports = function(ctx, helpers) { const manifest = service.deploymentManifest; const template = ctx.APP_TEMPLATES[manifest.templateId]; - ctx.log.info('restore', `Restoring service: ${service.name}`, { id: service.id, templateId: manifest.templateId }); + log.info('restore', `Restoring service: ${service.name}`, { id: service.id, templateId: manifest.templateId }); // Static sites: just recreate Caddy config if (template?.isStaticSite) { - ctx.log.info('restore', `Restoring static site Caddy config: ${service.name}`); + log.info('restore', `Restoring static site Caddy config: ${service.name}`); const caddyOptions = { tailscaleOnly: manifest.caddy.tailscaleOnly, allowedIPs: manifest.caddy.allowedIPs, @@ -132,10 +143,10 @@ module.exports = function(ctx, helpers) { // Docker container: check if already running if (service.containerId) { try { - const existing = ctx.docker.client.getContainer(service.containerId); + const existing = docker.client.getContainer(service.containerId); const info = await existing.inspect(); if (info.State.Running) { - ctx.log.info('restore', `Container already running, skipping: ${service.name}`); + log.info('restore', `Container already running, skipping: ${service.name}`); return { id: service.id, name: service.name, @@ -151,11 +162,11 @@ module.exports = function(ctx, helpers) { // Also check by name (container ID may have changed) const containerName = `${DOCKER.CONTAINER_PREFIX}${manifest.config.subdomain}`; try { - const byName = ctx.docker.client.getContainer(containerName); + const byName = docker.client.getContainer(containerName); const info = await byName.inspect(); if (info.State.Running) { // Update the service entry with the current container ID - await ctx.servicesStateManager.update(services => { + await servicesStateManager.update(services => { const svc = services.find(s => s.id === service.id); if (svc) svc.containerId = info.Id; return services; @@ -183,18 +194,18 @@ module.exports = function(ctx, helpers) { } // Pull image - ctx.log.info('restore', `Pulling image: ${manifest.container.image}`); + log.info('restore', `Pulling image: ${manifest.container.image}`); try { - await ctx.docker.pull(manifest.container.image); + await docker.pull(manifest.container.image); } catch (e) { // Check if image exists locally - const images = await ctx.docker.client.listImages({ + const images = await docker.client.listImages({ filters: { reference: [manifest.container.image] } }); if (images.length === 0) { throw new Error(`Failed to pull image ${manifest.container.image}: ${e.message}`); } - ctx.log.warn('restore', `Pull failed, using local image: ${manifest.container.image}`); + log.warn('restore', `Pull failed, using local image: ${manifest.container.image}`); } // Build container config from manifest @@ -231,10 +242,10 @@ module.exports = function(ctx, helpers) { } // Create and start container - ctx.log.info('restore', `Creating container: ${containerName}`); - const container = await ctx.docker.client.createContainer(containerConfig); + log.info('restore', `Creating container: ${containerName}`); + const container = await docker.client.createContainer(containerConfig); await container.start(); - ctx.log.info('restore', `Container started: ${containerName}`); + log.info('restore', `Container started: ${containerName}`); // Recreate Caddy config const port = manifest.config.port; @@ -245,19 +256,19 @@ module.exports = function(ctx, helpers) { }; if (manifest.caddy.routingMode === 'subdirectory') { - const caddyConfig = ctx.caddy.generateConfig(manifest.config.subdomain, manifest.config.ip, port, caddyOptions); + const caddyConfig = caddy.generateConfig(manifest.config.subdomain, manifest.config.ip, port, caddyOptions); try { await helpers.ensureMainDomainBlock(); await helpers.addSubpathConfig(manifest.config.subdomain, caddyConfig); } catch (e) { - ctx.log.warn('restore', `Caddy config may already exist: ${e.message}`); + log.warn('restore', `Caddy config may already exist: ${e.message}`); } } else { - const caddyConfig = ctx.caddy.generateConfig(manifest.config.subdomain, manifest.config.ip, port, caddyOptions); + const caddyConfig = caddy.generateConfig(manifest.config.subdomain, manifest.config.ip, port, caddyOptions); try { await helpers.addCaddyConfig(manifest.config.subdomain, caddyConfig); } catch (e) { - ctx.log.warn('restore', `Caddy config may already exist: ${e.message}`); + log.warn('restore', `Caddy config may already exist: ${e.message}`); } } @@ -265,14 +276,14 @@ module.exports = function(ctx, helpers) { if (manifest.config.createDns && manifest.caddy.routingMode !== 'subdirectory') { try { await ctx.dns.createRecord(manifest.config.subdomain, manifest.config.ip); - ctx.log.info('restore', 'DNS record recreated', { subdomain: manifest.config.subdomain }); + log.info('restore', 'DNS record recreated', { subdomain: manifest.config.subdomain }); } catch (e) { - ctx.log.warn('restore', `DNS recreation failed: ${e.message}`); + log.warn('restore', `DNS recreation failed: ${e.message}`); } } // Update the service entry with the new container ID - await ctx.servicesStateManager.update(services => { + await servicesStateManager.update(services => { const svc = services.find(s => s.id === service.id); if (svc) { svc.containerId = container.id; diff --git a/dashcaddy-api/routes/apps/templates.js b/dashcaddy-api/routes/apps/templates.js index d1cef07..1242916 100644 --- a/dashcaddy-api/routes/apps/templates.js +++ b/dashcaddy-api/routes/apps/templates.js @@ -1,12 +1,20 @@ const express = require('express'); const { exists } = require('../../fs-helpers'); +/** + * Apps templates routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.helpers - Apps helpers module + * @returns {express.Router} + */ const { REGEX } = require('../../constants'); -module.exports = function(ctx, helpers) { +module.exports = function({ servicesStateManager, asyncHandler, helpers }) { const router = express.Router(); // Get available app templates - router.get('/apps/templates', ctx.asyncHandler(async (req, res) => { + router.get('/apps/templates', asyncHandler(async (req, res) => { res.json({ success: true, templates: ctx.APP_TEMPLATES, @@ -16,7 +24,7 @@ module.exports = function(ctx, helpers) { }, 'apps-templates')); // Get specific app template - router.get('/apps/templates/:appId', ctx.asyncHandler(async (req, res) => { + router.get('/apps/templates/:appId', asyncHandler(async (req, res) => { const { appId } = req.params; const template = ctx.APP_TEMPLATES[appId]; if (!template) { @@ -27,7 +35,7 @@ module.exports = function(ctx, helpers) { }, 'apps-template-detail')); // Check port availability - router.get('/apps/ports/:port/check', ctx.asyncHandler(async (req, res) => { + router.get('/apps/ports/:port/check', asyncHandler(async (req, res) => { const port = req.params.port; const conflicts = await helpers.checkPortConflicts([port]); if (conflicts.length > 0) { @@ -39,32 +47,32 @@ module.exports = function(ctx, helpers) { }, 'check-port')); // Get suggested available port - router.get('/apps/ports/:basePort/suggest', ctx.asyncHandler(async (req, res) => { + router.get('/apps/ports/:basePort/suggest', asyncHandler(async (req, res) => { const basePort = parseInt(req.params.basePort) || 8080; const maxAttempts = 100; - const usedPorts = await ctx.docker.getUsedPorts(); + const usedPorts = await docker.getUsedPorts(); for (let port = basePort; port < basePort + maxAttempts; port++) { if (!usedPorts.has(port)) { res.json({ success: true, suggestedPort: port, basePort }); return; } } - ctx.errorResponse(res, 400, `No available ports found in range ${basePort}-${basePort + maxAttempts}`); + errorResponse(res, 400, `No available ports found in range ${basePort}-${basePort + maxAttempts}`); }, 'suggest-port')); // Update subdomain for deployed app - router.post('/apps/update-subdomain', ctx.asyncHandler(async (req, res) => { + router.post('/apps/update-subdomain', asyncHandler(async (req, res) => { const { serviceId, oldSubdomain, newSubdomain, containerId, ip } = req.body; if (!oldSubdomain || typeof oldSubdomain !== 'string') { - return ctx.errorResponse(res, 400, 'oldSubdomain is required'); + throw new ValidationError('oldSubdomain is required'); } if (!newSubdomain || typeof newSubdomain !== 'string') { - return ctx.errorResponse(res, 400, 'newSubdomain is required'); + throw new ValidationError('newSubdomain is required'); } if (!REGEX.SUBDOMAIN.test(newSubdomain)) { - return ctx.errorResponse(res, 400, '[DC-301] Invalid subdomain format for newSubdomain'); + throw new ValidationError('[DC-301] Invalid subdomain format for newSubdomain'); } - ctx.log.info('deploy', 'Updating subdomain', { oldSubdomain, newSubdomain }); + log.info('deploy', 'Updating subdomain', { oldSubdomain, newSubdomain }); const results = { oldDns: null, newDns: null, caddy: null, service: null }; if (oldSubdomain && ctx.dns.getToken()) { @@ -74,10 +82,10 @@ module.exports = function(ctx, helpers) { token: ctx.dns.getToken(), domain: oldDomain, type: 'A', ipAddress: ip || 'localhost' }); results.oldDns = result.status === 'ok' ? 'deleted' : result.errorMessage; - ctx.log.info('dns', 'Old DNS record deleted', { domain: oldDomain }); + log.info('dns', 'Old DNS record deleted', { domain: oldDomain }); } catch (error) { results.oldDns = `failed: ${error.message}`; - ctx.log.warn('dns', 'Old DNS deletion warning', { error: error.message }); + log.warn('dns', 'Old DNS deletion warning', { error: error.message }); } } @@ -85,22 +93,22 @@ module.exports = function(ctx, helpers) { try { await ctx.dns.createRecord(newSubdomain, ip || 'localhost'); results.newDns = 'created'; - ctx.log.info('dns', 'New DNS record created', { domain: ctx.buildDomain(newSubdomain) }); + log.info('dns', 'New DNS record created', { domain: ctx.buildDomain(newSubdomain) }); } catch (error) { results.newDns = `failed: ${error.message}`; - ctx.log.warn('dns', 'New DNS creation warning', { error: error.message }); + log.warn('dns', 'New DNS creation warning', { error: error.message }); } } try { - if (await exists(ctx.caddy.filePath)) { + if (await exists(caddy.filePath)) { const oldDomain = oldSubdomain.includes('.') ? oldSubdomain : ctx.buildDomain(oldSubdomain); const newDomain = newSubdomain.includes('.') ? newSubdomain : ctx.buildDomain(newSubdomain); const escapedOld = oldDomain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const oldBlockRegex = new RegExp(`${escapedOld}(?::\\d+)?\\s*\\{[^{}]*(?:\\{[^{}]*(?:\\{[^{}]*\\}[^{}]*)*\\}[^{}]*)*\\}`, 'g'); - const content = await ctx.caddy.read(); + const content = await caddy.read(); if (oldBlockRegex.test(content)) { - const caddyResult = await ctx.caddy.modify(c => { + const caddyResult = await caddy.modify(c => { const re = new RegExp(`${escapedOld}(?::\\d+)?\\s*\\{[^{}]*(?:\\{[^{}]*(?:\\{[^{}]*\\}[^{}]*)*\\}[^{}]*)*\\}`, 'g'); return c.replace(re, match => match.replace(oldDomain, newDomain)); }); @@ -113,17 +121,17 @@ module.exports = function(ctx, helpers) { } } catch (error) { results.caddy = `failed: ${error.message}`; - ctx.log.error('caddy', 'Caddy update error', { error: error.message }); + log.error('caddy', 'Caddy update error', { error: error.message }); } try { if (await exists(ctx.SERVICES_FILE)) { - await ctx.servicesStateManager.update(services => { + await servicesStateManager.update(services => { const serviceIndex = services.findIndex(s => s.id === oldSubdomain || s.id === serviceId); if (serviceIndex !== -1) { services[serviceIndex].id = newSubdomain; results.service = 'updated'; - ctx.log.info('deploy', 'Service config updated in services.json'); + log.info('deploy', 'Service config updated in services.json'); } else { results.service = 'not found'; } @@ -132,7 +140,7 @@ module.exports = function(ctx, helpers) { } } catch (error) { results.service = `failed: ${error.message}`; - ctx.log.warn('deploy', 'Service update warning', { error: error.message || String(error) }); + log.warn('deploy', 'Service update warning', { error: error.message || String(error) }); } res.json({ diff --git a/dashcaddy-api/routes/arr/config.js b/dashcaddy-api/routes/arr/config.js index de5b178..1b7f2bc 100644 --- a/dashcaddy-api/routes/arr/config.js +++ b/dashcaddy-api/routes/arr/config.js @@ -1,12 +1,33 @@ const express = require('express'); const { APP_PORTS, ARR_SERVICES } = require('../../constants'); const { validateURL, validateToken } = require('../../input-validator'); +const { ValidationError, AuthenticationError, NotFoundError } = require('../../errors'); +const { logError } = require('../../src/utils/logging'); -module.exports = function(ctx, helpers) { +/** + * Arr configuration routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.credentialManager - Credential manager + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Object} deps.docker - Docker client wrapper + * @param {Function} deps.fetchT - Timeout-wrapped fetch + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.errorResponse - Error response helper + * @param {Object} deps.log - Logger instance + * @param {Object} deps.helpers - Arr helpers module + * @returns {express.Router} + */ +module.exports = function({ credentialManager, servicesStateManager, docker, fetchT, asyncHandler, errorResponse, log, helpers, notification, safeErrorMessage }) { const router = express.Router(); + + // Ctx shim for backward compatibility + const ctx = { + notification, + safeErrorMessage + }; // Auto-configure Overseerr with detected services - router.post('/arr/configure-overseerr', ctx.asyncHandler(async (req, res) => { + router.post('/arr/configure-overseerr', asyncHandler(async (req, res) => { const { radarr, sonarr } = req.body; const results = { radarr: null, sonarr: null }; @@ -15,17 +36,17 @@ module.exports = function(ctx, helpers) { const overseerrSession = await helpers.getOverseerrSession(); if (!overseerrSession) { - return ctx.errorResponse(res, 502, 'Could not authenticate with Overseerr. Make sure Plex and Overseerr are running.', { + return errorResponse(res, 502, 'Could not authenticate with Overseerr. Make sure Plex and Overseerr are running.', { hint: 'Complete Overseerr setup wizard and link your Plex account first, then try again.' }); } - ctx.log.info('arr', 'Authenticated with Overseerr via Plex session'); + log.info('arr', 'Authenticated with Overseerr via Plex session'); // Helper to make authenticated requests to Overseerr const overseerrFetch = async (endpoint, options = {}) => { const url = `${overseerrUrl}${endpoint}`; - const response = await ctx.fetchT(url, { + const response = await fetchT(url, { ...options, headers: { 'Content-Type': 'application/json', @@ -40,12 +61,12 @@ module.exports = function(ctx, helpers) { try { const statusRes = await overseerrFetch('/api/v1/status'); if (!statusRes.ok) { - return ctx.errorResponse(res, 502, 'Cannot connect to Overseerr', { + return errorResponse(res, 502, 'Cannot connect to Overseerr', { hint: 'Make sure Overseerr is running on port 5055' }); } } catch (e) { - return ctx.errorResponse(res, 502, `Cannot reach Overseerr: ${e.message}`, { + return errorResponse(res, 502, `Cannot reach Overseerr: ${e.message}`, { hint: 'Check if Overseerr container is running' }); } @@ -58,20 +79,20 @@ module.exports = function(ctx, helpers) { const radarrBaseUrl = radarr.url.replace(/\/+$/, ''); // Fetch quality profiles from Radarr - const profilesRes = await ctx.fetchT(`${radarrBaseUrl}/api/v3/qualityprofile`, { + const profilesRes = await fetchT(`${radarrBaseUrl}/api/v3/qualityprofile`, { headers: { 'X-Api-Key': radarr.apiKey } }); const profiles = profilesRes.ok ? await profilesRes.json() : []; const defaultProfile = profiles[0] || { id: 1, name: 'Any' }; // Fetch root folders from Radarr - const rootFoldersRes = await ctx.fetchT(`${radarrBaseUrl}/api/v3/rootfolder`, { + const rootFoldersRes = await fetchT(`${radarrBaseUrl}/api/v3/rootfolder`, { headers: { 'X-Api-Key': radarr.apiKey } }); const rootFolders = rootFoldersRes.ok ? await rootFoldersRes.json() : []; const defaultRootFolder = rootFolders[0]?.path || '/movies'; - ctx.log.info('arr', 'Radarr configured', { profile: defaultProfile.name, profileId: defaultProfile.id, rootFolder: defaultRootFolder }); + log.info('arr', 'Radarr configured', { profile: defaultProfile.name, profileId: defaultProfile.id, rootFolder: defaultRootFolder }); const radarrConfig = { name: 'Radarr', @@ -114,14 +135,14 @@ module.exports = function(ctx, helpers) { const sonarrBaseUrl = sonarr.url.replace(/\/+$/, ''); // Fetch quality profiles from Sonarr - const profilesRes = await ctx.fetchT(`${sonarrBaseUrl}/api/v3/qualityprofile`, { + const profilesRes = await fetchT(`${sonarrBaseUrl}/api/v3/qualityprofile`, { headers: { 'X-Api-Key': sonarr.apiKey } }); const profiles = profilesRes.ok ? await profilesRes.json() : []; const defaultProfile = profiles[0] || { id: 1, name: 'Any' }; // Fetch root folders from Sonarr - const rootFoldersRes = await ctx.fetchT(`${sonarrBaseUrl}/api/v3/rootfolder`, { + const rootFoldersRes = await fetchT(`${sonarrBaseUrl}/api/v3/rootfolder`, { headers: { 'X-Api-Key': sonarr.apiKey } }); const rootFolders = rootFoldersRes.ok ? await rootFoldersRes.json() : []; @@ -130,7 +151,7 @@ module.exports = function(ctx, helpers) { // Fetch language profiles from Sonarr (v3 uses languageprofile, v4 doesn't need it) let languageProfileId = 1; try { - const langRes = await ctx.fetchT(`${sonarrBaseUrl}/api/v3/languageprofile`, { + const langRes = await fetchT(`${sonarrBaseUrl}/api/v3/languageprofile`, { headers: { 'X-Api-Key': sonarr.apiKey } }); if (langRes.ok) { @@ -141,7 +162,7 @@ module.exports = function(ctx, helpers) { // Language profiles might not exist in Sonarr v4 } - ctx.log.info('arr', 'Sonarr configured', { profile: defaultProfile.name, profileId: defaultProfile.id, rootFolder: defaultRootFolder }); + log.info('arr', 'Sonarr configured', { profile: defaultProfile.name, profileId: defaultProfile.id, rootFolder: defaultRootFolder }); const sonarrConfig = { name: 'Sonarr', @@ -187,26 +208,26 @@ module.exports = function(ctx, helpers) { }, 'arr-configure-overseerr')); // Test connection to external Radarr/Sonarr service - router.post('/arr/test-connection', ctx.asyncHandler(async (req, res) => { + router.post('/arr/test-connection', asyncHandler(async (req, res) => { try { const { service, url, apiKey } = req.body; if (!url || !apiKey) { - return ctx.errorResponse(res, 400, 'URL and API key required'); + throw new ValidationError('URL and API key required'); } // Validate URL format try { validateURL(url); } catch (validationErr) { - return ctx.errorResponse(res, 400, validationErr.message); + return errorResponse(res, 400, validationErr.message); } // Validate API key format try { validateToken(apiKey); } catch (validationErr) { - return ctx.errorResponse(res, 400, 'Invalid API key format'); + throw new ValidationError('Invalid API key format'); } // Normalize URL - remove trailing slash @@ -224,13 +245,13 @@ module.exports = function(ctx, helpers) { apiEndpoint = `${baseUrl}/identity`; headers = { 'X-Plex-Token': apiKey, 'Accept': 'application/json' }; } else { - return ctx.errorResponse(res, 400, `Unknown service: ${service}`); + return errorResponse(res, 400, `Unknown service: ${service}`); } - ctx.log.info('arr', 'Testing service connection', { service }); + log.info('arr', 'Testing service connection', { service }); // Make the API call - const response = await ctx.fetchT(apiEndpoint, { + const response = await fetchT(apiEndpoint, { method: 'GET', headers, signal: AbortSignal.timeout(10000) @@ -240,36 +261,36 @@ module.exports = function(ctx, helpers) { const data = await response.json(); const version = service === 'plex' ? data.MediaContainer?.version : data.version; const appName = service === 'plex' ? 'Plex' : data.appName; - ctx.log.info('arr', 'Service connection successful', { service, appName, version }); + log.info('arr', 'Service connection successful', { service, appName, version }); return res.json({ success: true, version, appName }); } else if (response.status === 401) { - return ctx.errorResponse(res, 401, 'Invalid API key'); + throw new AuthenticationError('Invalid API key'); } else if (response.status === 404) { - return ctx.errorResponse(res, 404, 'API not found - check URL'); + throw new NotFoundError('API not found - check URL'); } else { - return ctx.errorResponse(res, 502, `HTTP ${response.status}`); + return errorResponse(res, 502, `HTTP ${response.status}`); } } catch (error) { - await ctx.logError('arr-test-connection', error); + await logError('arr-test-connection', error); if (error.cause?.code === 'ECONNREFUSED') { - return ctx.errorResponse(res, 502, 'Connection refused'); + return errorResponse(res, 502, 'Connection refused'); } else if (error.name === 'AbortError' || error.message?.includes('timeout')) { - return ctx.errorResponse(res, 504, 'Connection timeout'); + return errorResponse(res, 504, 'Connection timeout'); } - return ctx.errorResponse(res, 500, ctx.safeErrorMessage(error)); + return errorResponse(res, 500, ctx.safeErrorMessage(error)); } }, 'arr-test-connection')); // Quick setup: Detect all services and configure Overseerr automatically - router.post('/arr/auto-setup', ctx.asyncHandler(async (req, res) => { - ctx.log.info('arr', 'Starting arr auto-setup'); + router.post('/arr/auto-setup', asyncHandler(async (req, res) => { + log.info('arr', 'Starting arr auto-setup'); // Step 1: Detect all running arr services - const containers = await ctx.docker.client.listContainers({ all: false }); + const containers = await docker.client.listContainers({ all: false }); const detected = {}; const servicePatterns = ARR_SERVICES; @@ -308,17 +329,17 @@ module.exports = function(ctx, helpers) { prowlarrFound: !!detected.prowlarr?.apiKey }; - ctx.log.info('arr', 'Detected services', summary); + log.info('arr', 'Detected services', summary); if (!summary.overseerrFound) { - return ctx.errorResponse(res, 400, 'Overseerr is not running. Deploy it first.', { + return errorResponse(res, 400, 'Overseerr is not running. Deploy it first.', { detected, summary }); } if (!summary.radarrFound && !summary.sonarrFound) { - return ctx.errorResponse(res, 400, 'No Radarr or Sonarr found with valid API keys. Deploy at least one first.', { + return errorResponse(res, 400, 'No Radarr or Sonarr found with valid API keys. Deploy at least one first.', { detected, summary }); @@ -328,18 +349,18 @@ module.exports = function(ctx, helpers) { const overseerrSession = await helpers.getOverseerrSession(); if (!overseerrSession) { - return ctx.errorResponse(res, 502, 'Could not authenticate with Overseerr. Make sure Plex and Overseerr are running.', { + return errorResponse(res, 502, 'Could not authenticate with Overseerr. Make sure Plex and Overseerr are running.', { setupUrl: detected.overseerr.localUrl, detected, summary }); } - ctx.log.info('arr', 'Authenticated with Overseerr via Plex session'); + log.info('arr', 'Authenticated with Overseerr via Plex session'); // Helper for authenticated Overseerr requests const overseerrFetch = async (endpoint, options = {}) => { - return ctx.fetchT(`${detected.overseerr.url}${endpoint}`, { + return fetchT(`${detected.overseerr.url}${endpoint}`, { ...options, headers: { 'Content-Type': 'application/json', @@ -355,20 +376,20 @@ module.exports = function(ctx, helpers) { if (detected.radarr?.apiKey) { try { // Fetch quality profiles from Radarr - const profilesRes = await ctx.fetchT(`${detected.radarr.localUrl}/api/v3/qualityprofile`, { + const profilesRes = await fetchT(`${detected.radarr.localUrl}/api/v3/qualityprofile`, { headers: { 'X-Api-Key': detected.radarr.apiKey } }); const profiles = profilesRes.ok ? await profilesRes.json() : []; const defaultProfile = profiles[0] || { id: 1, name: 'Any' }; // Fetch root folders from Radarr - const rootFoldersRes = await ctx.fetchT(`${detected.radarr.localUrl}/api/v3/rootfolder`, { + const rootFoldersRes = await fetchT(`${detected.radarr.localUrl}/api/v3/rootfolder`, { headers: { 'X-Api-Key': detected.radarr.apiKey } }); const rootFolders = rootFoldersRes.ok ? await rootFoldersRes.json() : []; const defaultRootFolder = rootFolders[0]?.path || '/movies'; - ctx.log.info('arr', 'Radarr profile selected', { profile: defaultProfile.name, rootFolder: defaultRootFolder }); + log.info('arr', 'Radarr profile selected', { profile: defaultProfile.name, rootFolder: defaultRootFolder }); const radarrConfig = { name: 'Radarr', @@ -402,14 +423,14 @@ module.exports = function(ctx, helpers) { if (detected.sonarr?.apiKey) { try { // Fetch quality profiles from Sonarr - const profilesRes = await ctx.fetchT(`${detected.sonarr.localUrl}/api/v3/qualityprofile`, { + const profilesRes = await fetchT(`${detected.sonarr.localUrl}/api/v3/qualityprofile`, { headers: { 'X-Api-Key': detected.sonarr.apiKey } }); const profiles = profilesRes.ok ? await profilesRes.json() : []; const defaultProfile = profiles[0] || { id: 1, name: 'Any' }; // Fetch root folders from Sonarr - const rootFoldersRes = await ctx.fetchT(`${detected.sonarr.localUrl}/api/v3/rootfolder`, { + const rootFoldersRes = await fetchT(`${detected.sonarr.localUrl}/api/v3/rootfolder`, { headers: { 'X-Api-Key': detected.sonarr.apiKey } }); const rootFolders = rootFoldersRes.ok ? await rootFoldersRes.json() : []; @@ -418,7 +439,7 @@ module.exports = function(ctx, helpers) { // Fetch language profiles (Sonarr v3) let languageProfileId = 1; try { - const langRes = await ctx.fetchT(`${detected.sonarr.localUrl}/api/v3/languageprofile`, { + const langRes = await fetchT(`${detected.sonarr.localUrl}/api/v3/languageprofile`, { headers: { 'X-Api-Key': detected.sonarr.apiKey } }); if (langRes.ok) { @@ -427,7 +448,7 @@ module.exports = function(ctx, helpers) { } } catch (e) { /* Sonarr v4 doesn't need this */ } - ctx.log.info('arr', 'Sonarr profile selected', { profile: defaultProfile.name, rootFolder: defaultRootFolder }); + log.info('arr', 'Sonarr profile selected', { profile: defaultProfile.name, rootFolder: defaultRootFolder }); const sonarrConfig = { name: 'Sonarr', @@ -480,11 +501,11 @@ module.exports = function(ctx, helpers) { }, 'arr-auto-setup')); // Fetch quality profiles from an arr service (Radarr/Sonarr) - router.get('/arr/quality-profiles', ctx.asyncHandler(async (req, res) => { + router.get('/arr/quality-profiles', asyncHandler(async (req, res) => { const { service, url, apiKey } = req.query; if (!service || !['radarr', 'sonarr'].includes(service)) { - return ctx.errorResponse(res, 400, 'Service must be radarr or sonarr'); + throw new ValidationError('Service must be radarr or sonarr'); } // Resolve API key: from query param, or from stored credentials @@ -492,19 +513,19 @@ module.exports = function(ctx, helpers) { let resolvedUrl = url; if (!resolvedKey) { - resolvedKey = await ctx.credentialManager.retrieve(`arr.${service}.apikey`); + resolvedKey = await credentialManager.retrieve(`arr.${service}.apikey`); } if (!resolvedKey) { - resolvedKey = await ctx.credentialManager.retrieve(`service.${service}.apikey`); + resolvedKey = await credentialManager.retrieve(`service.${service}.apikey`); } if (!resolvedUrl) { - const metadata = await ctx.credentialManager.getMetadata(`arr.${service}.apikey`); + const metadata = await credentialManager.getMetadata(`arr.${service}.apikey`); resolvedUrl = metadata?.url; } if (!resolvedUrl) { try { - const services = await ctx.servicesStateManager.read(); + const services = await servicesStateManager.read(); const svcList = Array.isArray(services) ? services : services.services || []; const found = svcList.find(s => s.id === service); if (found?.externalUrl) resolvedUrl = found.externalUrl; @@ -513,19 +534,19 @@ module.exports = function(ctx, helpers) { } if (!resolvedKey || !resolvedUrl) { - return ctx.errorResponse(res, 400, 'Could not resolve API key or URL for this service'); + throw new ValidationError('Could not resolve API key or URL for this service'); } const baseUrl = resolvedUrl.replace(/\/+$/, ''); try { - const profilesRes = await ctx.fetchT(`${baseUrl}/api/v3/qualityprofile`, { + const profilesRes = await fetchT(`${baseUrl}/api/v3/qualityprofile`, { headers: { 'X-Api-Key': resolvedKey, 'Accept': 'application/json' }, signal: AbortSignal.timeout(10000) }); if (!profilesRes.ok) { - return ctx.errorResponse(res, profilesRes.status === 401 ? 401 : 502, + return errorResponse(res, profilesRes.status === 401 ? 401 : 502, profilesRes.status === 401 ? 'Invalid API key' : `Failed to fetch profiles (HTTP ${profilesRes.status})`); } @@ -533,43 +554,43 @@ module.exports = function(ctx, helpers) { const mapped = profiles.map(p => ({ id: p.id, name: p.name })); // Load stored profile preference - const metadata = await ctx.credentialManager.getMetadata(`arr.${service}.apikey`); + const metadata = await credentialManager.getMetadata(`arr.${service}.apikey`); const storedProfileId = metadata?.qualityProfileId || null; res.json({ success: true, profiles: mapped, storedProfileId }); } catch (e) { if (e.cause?.code === 'ECONNREFUSED') { - return ctx.errorResponse(res, 502, 'Connection refused — is the service running?'); + return errorResponse(res, 502, 'Connection refused — is the service running?'); } if (e.name === 'AbortError') { - return ctx.errorResponse(res, 504, 'Connection timeout'); + return errorResponse(res, 504, 'Connection timeout'); } - return ctx.errorResponse(res, 500, e.message); + return errorResponse(res, 500, e.message); } }, 'arr-quality-profiles')); // Save quality profile preference (without re-storing API key) - router.post('/arr/quality-profiles', ctx.asyncHandler(async (req, res) => { + router.post('/arr/quality-profiles', asyncHandler(async (req, res) => { const { service, qualityProfileId, qualityProfileName } = req.body; if (!service || !['radarr', 'sonarr'].includes(service)) { - return ctx.errorResponse(res, 400, 'Service must be radarr or sonarr'); + throw new ValidationError('Service must be radarr or sonarr'); } if (!qualityProfileId) { - return ctx.errorResponse(res, 400, 'qualityProfileId required'); + throw new ValidationError('qualityProfileId required'); } const credKey = `arr.${service}.apikey`; - const existing = await ctx.credentialManager.getMetadata(credKey); + const existing = await credentialManager.getMetadata(credKey); if (!existing) { - return ctx.errorResponse(res, 404, 'No stored credentials for this service'); + throw new NotFoundError('No stored credentials for this service'); } // Merge quality profile into existing metadata existing.qualityProfileId = qualityProfileId; existing.qualityProfileName = qualityProfileName || null; - await ctx.credentialManager.storeMetadata(credKey, existing); + await credentialManager.storeMetadata(credKey, existing); res.json({ success: true, message: `Quality profile updated for ${service}` }); }, 'arr-quality-profile-save')); diff --git a/dashcaddy-api/routes/arr/credentials.js b/dashcaddy-api/routes/arr/credentials.js index 27c2a80..2bc5087 100644 --- a/dashcaddy-api/routes/arr/credentials.js +++ b/dashcaddy-api/routes/arr/credentials.js @@ -1,33 +1,45 @@ const express = require('express'); const { validateURL, validateToken } = require('../../input-validator'); +const { ValidationError } = require('../../errors'); -module.exports = function(ctx, helpers) { +/** + * Arr credentials routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.credentialManager - Credential manager + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.errorResponse - Error response helper + * @param {Object} deps.log - Logger instance + * @param {Object} deps.helpers - Arr helpers module + * @returns {express.Router} + */ +module.exports = function({ credentialManager, servicesStateManager, asyncHandler, errorResponse, log, helpers }) { const router = express.Router(); // Store arr service credentials - router.post('/arr/credentials', ctx.asyncHandler(async (req, res) => { + router.post('/arr/credentials', asyncHandler(async (req, res) => { const { service, apiKey, url, seedboxBaseUrl, qualityProfileId, qualityProfileName } = req.body; if (!service || !apiKey) { - return ctx.errorResponse(res, 400, 'Service name and API key required'); + throw new ValidationError('Service name and API key required'); } const validServices = ['radarr', 'sonarr', 'prowlarr', 'lidarr', 'plex']; if (!validServices.includes(service)) { - return ctx.errorResponse(res, 400, `Invalid service. Must be one of: ${validServices.join(', ')}`); + return errorResponse(res, 400, `Invalid service. Must be one of: ${validServices.join(', ')}`); } // Validate API key format try { validateToken(apiKey); } catch (e) { - return ctx.errorResponse(res, 400, 'Invalid API key format'); + throw new ValidationError('Invalid API key format'); } // Validate URL if provided if (url) { try { validateURL(url); } catch (e) { - return ctx.errorResponse(res, 400, 'Invalid URL format'); + throw new ValidationError('Invalid URL format'); } } @@ -49,7 +61,7 @@ module.exports = function(ctx, helpers) { if (!resolvedUrl) { // Try to resolve URL from services.json try { - const services = await ctx.servicesStateManager.read(); + const services = await servicesStateManager.read(); const svc = Array.isArray(services) ? services : services.services || []; const found = svc.find(s => s.id === service && s.isExternal); if (found?.externalUrl) resolvedUrl = found.externalUrl; @@ -72,22 +84,22 @@ module.exports = function(ctx, helpers) { } // Store the credential - const stored = await ctx.credentialManager.store(credKey, apiKey, metadata); + const stored = await credentialManager.store(credKey, apiKey, metadata); if (!stored) { - return ctx.errorResponse(res, 500, 'Failed to store credential'); + return errorResponse(res, 500, 'Failed to store credential'); } // Optionally store seedbox base URL if (seedboxBaseUrl) { try { validateURL(seedboxBaseUrl); } catch (e) { - return ctx.errorResponse(res, 400, 'Invalid seedbox base URL'); + throw new ValidationError('Invalid seedbox base URL'); } - await ctx.credentialManager.store('arr.seedbox.baseurl', seedboxBaseUrl, { + await credentialManager.store('arr.seedbox.baseurl', seedboxBaseUrl, { storedAt: new Date().toISOString() }); } - ctx.log.info('arr', 'Stored API key', { service, verified: connectionTest?.success || false }); + log.info('arr', 'Stored API key', { service, verified: connectionTest?.success || false }); res.json({ success: true, @@ -98,14 +110,14 @@ module.exports = function(ctx, helpers) { }, 'arr-credentials-store')); // List stored arr credentials (keys only, not values) - router.get('/arr/credentials', ctx.asyncHandler(async (req, res) => { + router.get('/arr/credentials', asyncHandler(async (req, res) => { const services = ['radarr', 'sonarr', 'prowlarr', 'lidarr', 'plex']; const credentials = {}; for (const service of services) { const credKey = service === 'plex' ? 'arr.plex.token' : `arr.${service}.apikey`; - const hasKey = !!(await ctx.credentialManager.retrieve(credKey)); - const metadata = await ctx.credentialManager.getMetadata(credKey); + const hasKey = !!(await credentialManager.retrieve(credKey)); + const metadata = await credentialManager.getMetadata(credKey); credentials[service] = { hasKey, @@ -117,17 +129,17 @@ module.exports = function(ctx, helpers) { } // Get seedbox base URL - const seedboxBaseUrl = await ctx.credentialManager.retrieve('arr.seedbox.baseurl'); + const seedboxBaseUrl = await credentialManager.retrieve('arr.seedbox.baseurl'); res.json({ success: true, credentials, seedboxBaseUrl: seedboxBaseUrl || null }); }, 'arr-credentials-list')); // Delete stored arr credentials - router.delete('/arr/credentials/:service', ctx.asyncHandler(async (req, res) => { + router.delete('/arr/credentials/:service', asyncHandler(async (req, res) => { const { service } = req.params; const credKey = service === 'plex' ? 'arr.plex.token' : `arr.${service}.apikey`; - await ctx.credentialManager.delete(credKey); - ctx.log.info('arr', 'Deleted credentials', { service }); + await credentialManager.delete(credKey); + log.info('arr', 'Deleted credentials', { service }); res.json({ success: true, message: `${service} credentials removed` }); }, 'arr-credentials-delete')); diff --git a/dashcaddy-api/routes/arr/detect.js b/dashcaddy-api/routes/arr/detect.js index 5af17ce..3bd0ed1 100644 --- a/dashcaddy-api/routes/arr/detect.js +++ b/dashcaddy-api/routes/arr/detect.js @@ -1,12 +1,23 @@ const express = require('express'); const { APP_PORTS, ARR_SERVICES } = require('../../constants'); -module.exports = function(ctx, helpers) { +/** + * Arr service detection routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.docker - Docker client wrapper + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Object} deps.credentialManager - Credential manager + * @param {Function} deps.fetchT - Timeout-wrapped fetch + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.helpers - Arr helpers module + * @returns {express.Router} + */ +module.exports = function({ docker, servicesStateManager, credentialManager, fetchT, asyncHandler, helpers }) { const router = express.Router(); // Detect running arr services and their configurations - router.get('/arr/detect', ctx.asyncHandler(async (req, res) => { - const containers = await ctx.docker.client.listContainers({ all: false }); + router.get('/arr/detect', asyncHandler(async (req, res) => { + const containers = await docker.client.listContainers({ all: false }); const detected = { plex: null, radarr: null, @@ -64,14 +75,14 @@ module.exports = function(ctx, helpers) { }, 'arr-detect')); // Smart Detect: Unified discovery of all arr services - router.get('/arr/smart-detect', ctx.asyncHandler(async (req, res) => { + router.get('/arr/smart-detect', asyncHandler(async (req, res) => { const serviceList = ['plex', 'radarr', 'sonarr', 'prowlarr', 'seerr']; const defaultPorts = APP_PORTS; const result = {}; // 1. Scan Docker containers let containers = []; - try { containers = await ctx.docker.client.listContainers({ all: false }); } catch (e) { /* Docker not available */ } + try { containers = await docker.client.listContainers({ all: false }); } catch (e) { /* Docker not available */ } const servicePatterns = ARR_SERVICES; @@ -95,18 +106,18 @@ module.exports = function(ctx, helpers) { // 2. Load services.json for external entries let storedServices = []; try { - const data = await ctx.servicesStateManager.read(); + const data = await servicesStateManager.read(); storedServices = Array.isArray(data) ? data : data.services || []; } catch (e) { /* ignore */ } // 3. Load stored credentials const storedCreds = {}; - const seedboxBaseUrl = await ctx.credentialManager.retrieve('arr.seedbox.baseurl'); + const seedboxBaseUrl = await credentialManager.retrieve('arr.seedbox.baseurl'); for (const svc of serviceList) { const credKey = svc === 'plex' ? 'arr.plex.token' : `arr.${svc}.apikey`; - const apiKey = await ctx.credentialManager.retrieve(credKey); - const metadata = await ctx.credentialManager.getMetadata(credKey); + const apiKey = await credentialManager.retrieve(credKey); + const metadata = await credentialManager.getMetadata(credKey); if (apiKey) { storedCreds[svc] = { apiKey, metadata }; } @@ -141,7 +152,7 @@ module.exports = function(ctx, helpers) { entry.hasToken = true; entry.status = 'connected'; // Store for later use - await ctx.credentialManager.store('arr.plex.token', token, { + await credentialManager.store('arr.plex.token', token, { service: 'plex', source: 'local', url: entry.url, lastVerified: new Date().toISOString() }); @@ -158,7 +169,7 @@ module.exports = function(ctx, helpers) { entry.hasApiKey = true; const configuredServices = { radarr: false, sonarr: false, plex: false }; try { - const radarrCheck = await ctx.fetchT(`http://host.docker.internal:${dc.port}/api/v1/settings/radarr`, { + const radarrCheck = await fetchT(`http://host.docker.internal:${dc.port}/api/v1/settings/radarr`, { headers: { 'Cookie': session.cookie }, signal: AbortSignal.timeout(5000) }); @@ -168,7 +179,7 @@ module.exports = function(ctx, helpers) { } } catch (e) { /* ignore */ } try { - const sonarrCheck = await ctx.fetchT(`http://host.docker.internal:${dc.port}/api/v1/settings/sonarr`, { + const sonarrCheck = await fetchT(`http://host.docker.internal:${dc.port}/api/v1/settings/sonarr`, { headers: { 'Cookie': session.cookie }, signal: AbortSignal.timeout(5000) }); @@ -178,7 +189,7 @@ module.exports = function(ctx, helpers) { } } catch (e) { /* ignore */ } try { - const plexCheck = await ctx.fetchT(`http://host.docker.internal:${dc.port}/api/v1/settings/plex`, { + const plexCheck = await fetchT(`http://host.docker.internal:${dc.port}/api/v1/settings/plex`, { headers: { 'Cookie': session.cookie }, signal: AbortSignal.timeout(5000) }); diff --git a/dashcaddy-api/routes/arr/helpers.js b/dashcaddy-api/routes/arr/helpers.js index 2936f51..312ccdf 100644 --- a/dashcaddy-api/routes/arr/helpers.js +++ b/dashcaddy-api/routes/arr/helpers.js @@ -1,14 +1,23 @@ const { APP_PORTS } = require('../../constants'); -module.exports = function(ctx) { +/** + * Arr helpers factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.docker - Docker client wrapper + * @param {Object} deps.credentialManager - Credential manager + * @param {Function} deps.fetchT - Timeout-wrapped fetch + * @param {Object} deps.log - Logger instance + * @returns {Object} Helper functions + */ +module.exports = function({ docker, credentialManager, fetchT, log }) { // Helper: Extract API key from arr service config.xml async function getArrApiKey(containerName) { try { - const container = await ctx.docker.findContainer(containerName); + const container = await docker.findContainer(containerName); if (!container) return null; - const dockerContainer = ctx.docker.client.getContainer(container.Id); + const dockerContainer = docker.client.getContainer(container.Id); const exec = await dockerContainer.exec({ Cmd: ['cat', '/config/config.xml'], AttachStdout: true, @@ -28,7 +37,7 @@ module.exports = function(ctx) { stream.on('error', () => resolve(null)); }); } catch (error) { - ctx.log.error('docker', 'Failed to get API key', { containerName, error: error.message }); + log.error('docker', 'Failed to get API key', { containerName, error: error.message }); return null; } } @@ -36,14 +45,14 @@ module.exports = function(ctx) { // Helper: Get Plex token from container or config async function getPlexToken(containerName) { try { - const containers = await ctx.docker.client.listContainers({ all: false }); + const containers = await docker.client.listContainers({ all: false }); const container = containers.find(c => c.Names.some(n => n.toLowerCase().includes(containerName.toLowerCase()) || n.toLowerCase().includes('plex')) ); if (!container) return null; - const dockerContainer = ctx.docker.client.getContainer(container.Id); + const dockerContainer = docker.client.getContainer(container.Id); const exec = await dockerContainer.exec({ Cmd: ['cat', '/config/Library/Application Support/Plex Media Server/Preferences.xml'], AttachStdout: true, @@ -62,7 +71,7 @@ module.exports = function(ctx) { stream.on('error', () => resolve(null)); }); } catch (error) { - ctx.log.error('docker', 'Failed to get Plex token', { error: error.message }); + log.error('docker', 'Failed to get Plex token', { error: error.message }); return null; } } @@ -84,16 +93,16 @@ module.exports = function(ctx) { // Fall back to stored Plex token in credential manager if (!plexToken) { - plexToken = await ctx.credentialManager.retrieve('arr.plex.token'); + plexToken = await credentialManager.retrieve('arr.plex.token'); } if (!plexToken) { - ctx.log.error('arr', 'Could not get Plex token for Seerr auth (no container, no stored token)'); + log.error('arr', 'Could not get Plex token for Seerr auth (no container, no stored token)'); return null; } // Authenticate with Seerr via Plex token - const authRes = await ctx.fetchT(`${seerrUrl}/api/v1/auth/plex`, { + const authRes = await fetchT(`${seerrUrl}/api/v1/auth/plex`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ authToken: plexToken }), @@ -101,20 +110,20 @@ module.exports = function(ctx) { }); if (!authRes.ok) { - ctx.log.error('arr', 'Seerr Plex auth failed', { status: authRes.status }); + log.error('arr', 'Seerr Plex auth failed', { status: authRes.status }); return null; } const setCookie = authRes.headers.get('set-cookie'); if (!setCookie) { - ctx.log.error('arr', 'No session cookie returned from Seerr'); + log.error('arr', 'No session cookie returned from Seerr'); return null; } const sessionCookie = setCookie.split(';')[0]; return { cookie: sessionCookie, plexToken }; } catch (e) { - ctx.log.error('arr', 'Could not get Seerr session', { error: e.message }); + log.error('arr', 'Could not get Seerr session', { error: e.message }); return null; } } @@ -123,7 +132,7 @@ module.exports = function(ctx) { // Uses session cookie auth (Overseerr requires Plex-based admin session for settings) async function connectPlexToOverseerr(plexUrl, plexToken, overseerrUrl, sessionCookie) { // 1. Get Plex server identity (for return info) - const identityRes = await ctx.fetchT(`${plexUrl}/identity`, { + const identityRes = await fetchT(`${plexUrl}/identity`, { headers: { 'X-Plex-Token': plexToken, 'Accept': 'application/json' }, signal: AbortSignal.timeout(10000) }); @@ -139,7 +148,7 @@ module.exports = function(ctx) { useSsl: false }; - const configRes = await ctx.fetchT(`${overseerrUrl}/api/v1/settings/plex`, { + const configRes = await fetchT(`${overseerrUrl}/api/v1/settings/plex`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -154,19 +163,19 @@ module.exports = function(ctx) { // 3. Trigger library sync — Overseerr will use the admin's Plex token to discover libraries try { - await ctx.fetchT(`${overseerrUrl}/api/v1/settings/plex/sync`, { + await fetchT(`${overseerrUrl}/api/v1/settings/plex/sync`, { method: 'POST', headers: { 'Cookie': sessionCookie }, signal: AbortSignal.timeout(10000) }); } catch (e) { - ctx.log.warn('arr', 'Plex library sync trigger failed (non-fatal)', { error: e.message }); + log.warn('arr', 'Plex library sync trigger failed (non-fatal)', { error: e.message }); } // 4. Get discovered libraries let libraries = []; try { - const libRes = await ctx.fetchT(`${overseerrUrl}/api/v1/settings/plex`, { + const libRes = await fetchT(`${overseerrUrl}/api/v1/settings/plex`, { headers: { 'Cookie': sessionCookie }, signal: AbortSignal.timeout(5000) }); @@ -186,13 +195,13 @@ module.exports = function(ctx) { // Check existing apps to avoid duplicates let existingApps = []; try { - const existingRes = await ctx.fetchT(`${prowlarrUrl}/api/v1/applications`, { + const existingRes = await fetchT(`${prowlarrUrl}/api/v1/applications`, { headers: { 'X-Api-Key': prowlarrApiKey }, signal: AbortSignal.timeout(10000) }); existingApps = existingRes.ok ? await existingRes.json() : []; } catch (e) { - ctx.log.warn('arr', 'Could not fetch existing Prowlarr apps', { error: e.message }); + log.warn('arr', 'Could not fetch existing Prowlarr apps', { error: e.message }); } for (const [appName, config] of Object.entries(apps)) { @@ -222,7 +231,7 @@ module.exports = function(ctx) { }; try { - const res = await ctx.fetchT(`${prowlarrUrl}/api/v1/applications`, { + const res = await fetchT(`${prowlarrUrl}/api/v1/applications`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -259,7 +268,7 @@ module.exports = function(ctx) { } try { - const response = await ctx.fetchT(apiEndpoint, { + const response = await fetchT(apiEndpoint, { method: 'GET', headers, signal: AbortSignal.timeout(15000) diff --git a/dashcaddy-api/routes/arr/index.js b/dashcaddy-api/routes/arr/index.js index 4caef22..58d6ba7 100644 --- a/dashcaddy-api/routes/arr/index.js +++ b/dashcaddy-api/routes/arr/index.js @@ -1,14 +1,37 @@ const express = require('express'); +/** + * Arr routes aggregator + * Assembles all arr sub-routes with their dependencies + * @param {Object} ctx - Application context (for backward compatibility) + * @returns {express.Router} + */ module.exports = function(ctx) { const router = express.Router(); - const helpers = require('./helpers')(ctx); - router.use(require('./detect')(ctx, helpers)); - router.use(require('./credentials')(ctx, helpers)); - router.use(require('./config')(ctx, helpers)); - router.use(require('./smart-connect')(ctx, helpers)); - router.use(require('./plex')(ctx, helpers)); + // Extract dependencies from context + const deps = { + docker: ctx.docker, + credentialManager: ctx.credentialManager, + servicesStateManager: ctx.servicesStateManager, + fetchT: ctx.fetchT, + asyncHandler: ctx.asyncHandler, + errorResponse: ctx.errorResponse, + log: ctx.log, + // Additional context properties needed by arr routes + notification: ctx.notification, + safeErrorMessage: ctx.safeErrorMessage + }; + + // Initialize helpers with dependencies + const helpers = require('./helpers')(deps); + + // Mount sub-routes with explicit dependencies + router.use(require('./detect')({ ...deps, helpers })); + router.use(require('./credentials')({ ...deps, helpers })); + router.use(require('./config')({ ...deps, helpers })); + router.use(require('./smart-connect')({ ...deps, helpers })); + router.use(require('./plex')({ ...deps, helpers })); return router; }; diff --git a/dashcaddy-api/routes/arr/plex.js b/dashcaddy-api/routes/arr/plex.js index d351d23..ee0771d 100644 --- a/dashcaddy-api/routes/arr/plex.js +++ b/dashcaddy-api/routes/arr/plex.js @@ -1,11 +1,21 @@ const express = require('express'); const { APP_PORTS } = require('../../constants'); -module.exports = function(ctx, helpers) { +/** + * Plex routes factory + * @param {Object} deps - Explicit dependencies + * @param {Function} deps.fetchT - Timeout-wrapped fetch + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.errorResponse - Error response helper + * @param {Object} deps.log - Logger instance + * @param {Object} deps.helpers - Arr helpers module + * @returns {express.Router} + */ +module.exports = function({ fetchT, asyncHandler, errorResponse, log, helpers }) { const router = express.Router(); // Plex Libraries endpoint - router.get('/plex/libraries', ctx.asyncHandler(async (req, res) => { + router.get('/plex/libraries', asyncHandler(async (req, res) => { // Get Plex token let plexToken = await helpers.getPlexToken('plex'); if (!plexToken) { @@ -13,7 +23,7 @@ module.exports = function(ctx, helpers) { } if (!plexToken) { - return ctx.errorResponse(res, 400, 'No Plex token available. Claim your Plex server first.', { + return errorResponse(res, 400, 'No Plex token available. Claim your Plex server first.', { hint: 'Deploy Plex with a claim token or manually configure it.' }); } @@ -30,13 +40,13 @@ module.exports = function(ctx, helpers) { } catch (e) { /* use default */ } // Fetch libraries - const libRes = await ctx.fetchT(`${plexUrl}/library/sections`, { + const libRes = await fetchT(`${plexUrl}/library/sections`, { headers: { 'X-Plex-Token': plexToken, 'Accept': 'application/json' }, signal: AbortSignal.timeout(10000) }); if (!libRes.ok) { - return ctx.errorResponse(res, 502, `Plex returned ${libRes.status}`); + return errorResponse(res, 502, `Plex returned ${libRes.status}`); } const data = await libRes.json(); @@ -52,7 +62,7 @@ module.exports = function(ctx, helpers) { let serverName = 'Plex'; let version = null; try { - const identityRes = await ctx.fetchT(`${plexUrl}/identity`, { + const identityRes = await fetchT(`${plexUrl}/identity`, { headers: { 'X-Plex-Token': plexToken, 'Accept': 'application/json' }, signal: AbortSignal.timeout(5000) }); diff --git a/dashcaddy-api/routes/arr/smart-connect.js b/dashcaddy-api/routes/arr/smart-connect.js index 0836c03..ce61b87 100644 --- a/dashcaddy-api/routes/arr/smart-connect.js +++ b/dashcaddy-api/routes/arr/smart-connect.js @@ -1,11 +1,22 @@ const express = require('express'); const { APP_PORTS } = require('../../constants'); -module.exports = function(ctx, helpers) { +/** + * Arr smart-connect routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.credentialManager - Credential manager + * @param {Function} deps.fetchT - Timeout-wrapped fetch + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.errorResponse - Error response helper + * @param {Object} deps.log - Logger instance + * @param {Object} deps.helpers - Arr helpers module + * @returns {express.Router} + */ +module.exports = function({ credentialManager, fetchT, asyncHandler, errorResponse, log, helpers }) { const router = express.Router(); // Smart Connect: Unified orchestration endpoint - router.post('/arr/smart-connect', ctx.asyncHandler(async (req, res) => { + router.post('/arr/smart-connect', asyncHandler(async (req, res) => { const { services: inputServices, configurePlex, configureProwlarr, configureSeerr, saveCredentials } = req.body; const steps = []; const connectedServices = {}; // { radarr: { url, apiKey }, sonarr: { url, apiKey }, ... } @@ -20,9 +31,9 @@ module.exports = function(ctx, helpers) { // Fallback to stored credentials if (!apiKey) { const credKey = `arr.${svc}.apikey`; - apiKey = await ctx.credentialManager.retrieve(credKey); + apiKey = await credentialManager.retrieve(credKey); if (!url) { - const metadata = await ctx.credentialManager.getMetadata(credKey); + const metadata = await credentialManager.getMetadata(credKey); url = metadata?.url; } } @@ -52,7 +63,7 @@ module.exports = function(ctx, helpers) { // Save credentials if (saveCredentials) { - const stored = await ctx.credentialManager.store(`arr.${svc}.apikey`, apiKey, { + const stored = await credentialManager.store(`arr.${svc}.apikey`, apiKey, { service: svc, source: 'external', url, lastVerified: new Date().toISOString(), version: test.version @@ -71,7 +82,7 @@ module.exports = function(ctx, helpers) { let plexUrl = null; if (configurePlex) { plexToken = await helpers.getPlexToken('plex'); - if (!plexToken) plexToken = await ctx.credentialManager.retrieve('arr.plex.token'); + if (!plexToken) plexToken = await credentialManager.retrieve('arr.plex.token'); if (plexToken) { // Get Plex URL @@ -108,14 +119,14 @@ module.exports = function(ctx, helpers) { const radarrBasePath = radarrUrlObj.pathname.replace(/\/+$/, ''); // Fetch quality profiles - const profilesRes = await ctx.fetchT(`${radarrUrl}/api/v3/qualityprofile`, { + const profilesRes = await fetchT(`${radarrUrl}/api/v3/qualityprofile`, { headers: { 'X-Api-Key': connectedServices.radarr.apiKey }, signal: AbortSignal.timeout(10000) }); const profiles = profilesRes.ok ? await profilesRes.json() : []; // Use stored quality profile preference, fallback to first profile - const radarrMeta = await ctx.credentialManager.getMetadata('arr.radarr.apikey'); + const radarrMeta = await credentialManager.getMetadata('arr.radarr.apikey'); let defaultProfile = profiles[0] || { id: 1, name: 'Any' }; if (radarrMeta?.qualityProfileId) { const stored = profiles.find(p => p.id === radarrMeta.qualityProfileId); @@ -123,7 +134,7 @@ module.exports = function(ctx, helpers) { } // Fetch root folders - const rootFoldersRes = await ctx.fetchT(`${radarrUrl}/api/v3/rootfolder`, { + const rootFoldersRes = await fetchT(`${radarrUrl}/api/v3/rootfolder`, { headers: { 'X-Api-Key': connectedServices.radarr.apiKey }, signal: AbortSignal.timeout(10000) }); @@ -151,7 +162,7 @@ module.exports = function(ctx, helpers) { tags: [] }; - const radarrRes = await ctx.fetchT(`${overseerrUrl}/api/v1/settings/radarr`, { + const radarrRes = await fetchT(`${overseerrUrl}/api/v1/settings/radarr`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Cookie': overseerrCookie }, body: JSON.stringify(radarrConfig), @@ -175,21 +186,21 @@ module.exports = function(ctx, helpers) { const sonarrUrlObj = new URL(sonarrUrl); const sonarrBasePath = sonarrUrlObj.pathname.replace(/\/+$/, ''); - const profilesRes = await ctx.fetchT(`${sonarrUrl}/api/v3/qualityprofile`, { + const profilesRes = await fetchT(`${sonarrUrl}/api/v3/qualityprofile`, { headers: { 'X-Api-Key': connectedServices.sonarr.apiKey }, signal: AbortSignal.timeout(10000) }); const profiles = profilesRes.ok ? await profilesRes.json() : []; // Use stored quality profile preference, fallback to first profile - const sonarrMeta = await ctx.credentialManager.getMetadata('arr.sonarr.apikey'); + const sonarrMeta = await credentialManager.getMetadata('arr.sonarr.apikey'); let defaultProfile = profiles[0] || { id: 1, name: 'Any' }; if (sonarrMeta?.qualityProfileId) { const stored = profiles.find(p => p.id === sonarrMeta.qualityProfileId); if (stored) defaultProfile = stored; } - const rootFoldersRes = await ctx.fetchT(`${sonarrUrl}/api/v3/rootfolder`, { + const rootFoldersRes = await fetchT(`${sonarrUrl}/api/v3/rootfolder`, { headers: { 'X-Api-Key': connectedServices.sonarr.apiKey }, signal: AbortSignal.timeout(10000) }); @@ -198,7 +209,7 @@ module.exports = function(ctx, helpers) { let languageProfileId = 1; try { - const langRes = await ctx.fetchT(`${sonarrUrl}/api/v3/languageprofile`, { + const langRes = await fetchT(`${sonarrUrl}/api/v3/languageprofile`, { headers: { 'X-Api-Key': connectedServices.sonarr.apiKey }, signal: AbortSignal.timeout(5000) }); @@ -229,7 +240,7 @@ module.exports = function(ctx, helpers) { tags: [] }; - const sonarrRes = await ctx.fetchT(`${overseerrUrl}/api/v1/settings/sonarr`, { + const sonarrRes = await fetchT(`${overseerrUrl}/api/v1/settings/sonarr`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Cookie': overseerrCookie }, body: JSON.stringify(sonarrConfig), diff --git a/dashcaddy-api/routes/auth/index.js b/dashcaddy-api/routes/auth/index.js index 1b94c5f..28adeae 100644 --- a/dashcaddy-api/routes/auth/index.js +++ b/dashcaddy-api/routes/auth/index.js @@ -4,14 +4,37 @@ const initKeys = require('./keys'); const initSessionHandlers = require('./session-handlers'); const initSsoGate = require('./sso-gate'); +/** + * Auth routes aggregator + * Assembles all auth sub-routes with their dependencies + * @param {Object} ctx - Application context (for backward compatibility) + * @returns {express.Router} + */ module.exports = function(ctx) { const router = express.Router(); - const { getAppSession, appSessionCache } = initSessionHandlers(ctx); + // Extract dependencies from context + const deps = { + authManager: ctx.authManager, + credentialManager: ctx.credentialManager, + totpConfig: ctx.totpConfig, + saveTotpConfig: ctx.saveTotpConfig, + session: ctx.session, + asyncHandler: ctx.asyncHandler, + errorResponse: ctx.errorResponse, + log: ctx.log, + // Additional deps for sso-gate + fetchT: ctx.fetchT, + getServiceById: ctx.getServiceById, + licenseManager: ctx.licenseManager, + servicesStateManager: ctx.servicesStateManager + }; - router.use(initTotp(ctx)); - router.use(initKeys(ctx)); - router.use(initSsoGate(ctx, getAppSession, appSessionCache)); + const { getAppSession, appSessionCache } = initSessionHandlers(deps); + + router.use(initTotp(deps)); + router.use(initKeys(deps)); + router.use(initSsoGate({ ...deps, getAppSession, appSessionCache })); return router; }; diff --git a/dashcaddy-api/routes/auth/keys.js b/dashcaddy-api/routes/auth/keys.js index d1fa933..bf26c15 100644 --- a/dashcaddy-api/routes/auth/keys.js +++ b/dashcaddy-api/routes/auth/keys.js @@ -1,6 +1,15 @@ const express = require('express'); +const { ValidationError, ForbiddenError, NotFoundError } = require('../../errors'); +/** + * Auth API keys routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.authManager - Auth manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.log - Logger instance + * @returns {express.Router} + */ -module.exports = function(ctx) { +module.exports = function({ authManager, asyncHandler, log }) { const router = express.Router(); // Helper function to parse expiration strings to milliseconds @@ -23,36 +32,36 @@ module.exports = function(ctx) { } // List all API keys - router.get('/auth/keys', ctx.asyncHandler(async (req, res) => { + router.get('/auth/keys', asyncHandler(async (req, res) => { // Require session authentication (not API key - can't manage keys with key itself) if (!req.auth || req.auth.type !== 'session') { - return ctx.errorResponse(res, 403, 'API key management requires TOTP session authentication'); + throw new ForbiddenError('API key management requires TOTP session authentication'); } - const keys = await ctx.authManager.listAPIKeys(); + const keys = await authManager.listAPIKeys(); res.json({ success: true, keys }); }, 'auth-keys-list')); // Generate new API key - router.post('/auth/keys', ctx.asyncHandler(async (req, res) => { + router.post('/auth/keys', asyncHandler(async (req, res) => { // Require session authentication if (!req.auth || req.auth.type !== 'session') { - return ctx.errorResponse(res, 403, 'API key generation requires TOTP session authentication'); + throw new ForbiddenError('API key generation requires TOTP session authentication'); } const { name, scopes } = req.body; if (!name || typeof name !== 'string' || name.trim().length === 0) { - return ctx.errorResponse(res, 400, 'API key name is required'); + throw new ValidationError('API key name is required', 'name'); } // Validate scopes if provided const validScopes = ['read', 'write', 'admin']; if (scopes && (!Array.isArray(scopes) || !scopes.every(s => validScopes.includes(s)))) { - return ctx.errorResponse(res, 400, 'Invalid scopes', { validScopes }); + throw new ValidationError(`Invalid scopes. Valid options: ${validScopes.join(', ')}`, 'scopes'); } - const keyData = await ctx.authManager.generateAPIKey( + const keyData = await authManager.generateAPIKey( name.trim(), scopes || ['read', 'write'] ); @@ -69,33 +78,32 @@ module.exports = function(ctx) { }, 'auth-keys-generate')); // Revoke API key - router.delete('/auth/keys/:keyId', ctx.asyncHandler(async (req, res) => { + router.delete('/auth/keys/:keyId', asyncHandler(async (req, res) => { // Require session authentication if (!req.auth || req.auth.type !== 'session') { - return ctx.errorResponse(res, 403, 'API key revocation requires TOTP session authentication'); + throw new ForbiddenError('API key revocation requires TOTP session authentication'); } const { keyId } = req.params; if (!keyId || typeof keyId !== 'string') { - return ctx.errorResponse(res, 400, 'Key ID is required'); + throw new ValidationError('Key ID is required', 'keyId'); } - const success = await ctx.authManager.revokeAPIKey(keyId); + const success = await authManager.revokeAPIKey(keyId); if (success) { res.json({ success: true, message: 'API key revoked successfully' }); } else { - const { NotFoundError } = require('../../errors'); - throw new NotFoundError('API key'); + throw new NotFoundError(`API key ${keyId}`); } }, 'auth-keys-revoke')); // Generate JWT from TOTP session - router.post('/auth/jwt', ctx.asyncHandler(async (req, res) => { + router.post('/auth/jwt', asyncHandler(async (req, res) => { // Require session authentication if (!req.auth || req.auth.type !== 'session') { - return ctx.errorResponse(res, 403, 'JWT generation requires TOTP session authentication'); + throw new ForbiddenError('JWT generation requires TOTP session authentication'); } const { expiresIn, userId } = req.body; @@ -103,10 +111,10 @@ module.exports = function(ctx) { // Validate expiresIn format if provided (e.g., '24h', '7d', '1y') const validExpiresIn = /^(\d+[smhdy])$/.test(expiresIn || '24h'); if (expiresIn && !validExpiresIn) { - return ctx.errorResponse(res, 400, 'Invalid expiresIn format. Use: 60s, 15m, 24h, 7d, 1y'); + throw new ValidationError('Invalid expiresIn format. Use: 60s, 15m, 24h, 7d, 1y', 'expiresIn'); } - const token = await ctx.authManager.generateJWT( + const token = await authManager.generateJWT( { sub: userId || 'dashcaddy-admin', scope: ['admin'] // Session-generated JWTs have admin scope diff --git a/dashcaddy-api/routes/auth/session-handlers.js b/dashcaddy-api/routes/auth/session-handlers.js index 534b55b..0452c7c 100644 --- a/dashcaddy-api/routes/auth/session-handlers.js +++ b/dashcaddy-api/routes/auth/session-handlers.js @@ -1,8 +1,18 @@ const { SESSION_TTL, APP, PLEX, TIMEOUTS, buildMediaAuth } = require('../../constants'); const { createCache, CACHE_CONFIGS } = require('../../cache-config'); -module.exports = function(ctx) { +module.exports = function({ authManager, credentialManager, asyncHandler, errorResponse, log }) { // App session cache for auto-login +/** + * Auth session handlers routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.authManager - Auth manager + * @param {Object} deps.credentialManager - Credential manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.errorResponse - Error response helper + * @param {Object} deps.log - Logger instance + * @returns {express.Router} + */ const appSessionCache = createCache(CACHE_CONFIGS.appSessions); async function getAppSession(serviceId, baseUrl, username, password) { @@ -36,12 +46,12 @@ module.exports = function(ctx) { const location = locationMatch ? locationMatch[1].trim() : ''; if (location && !location.includes('login')) { appSessionCache.set(serviceId, { cookies: '__ip_session=1', exp: Date.now() + SESSION_TTL.IP_SESSION }); - ctx.log.info('auth', 'Router auto-login successful (IP-based session)', { serviceId }); + log.info('auth', 'Router auto-login successful (IP-based session)', { serviceId }); return '__ip_session=1'; } - ctx.log.warn('auth', 'Router auto-login failed', { serviceId }); + log.warn('auth', 'Router auto-login failed', { serviceId }); } catch (e) { - ctx.log.warn('auth', 'Router auto-login error', { serviceId, error: e.message?.substring(0, 100) }); + log.warn('auth', 'Router auto-login error', { serviceId, error: e.message?.substring(0, 100) }); } appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN }); return null; @@ -73,12 +83,12 @@ module.exports = function(ctx) { serverId: authData.ServerId, serverName: authData.User?.ServerName || serviceId, }; appSessionCache.set(serviceId, { cookies: `token=${authData.AccessToken}`, token: authData.AccessToken, tokenData, exp: Date.now() + SESSION_TTL.TOKEN_SESSION }); - ctx.log.info('auth', 'Auto-login successful (token + userId obtained)', { serviceId }); + log.info('auth', 'Auto-login successful (token + userId obtained)', { serviceId }); return `token=${authData.AccessToken}`; } - ctx.log.warn('auth', 'Auto-login failed', { serviceId, status: authResp.status }); + log.warn('auth', 'Auto-login failed', { serviceId, status: authResp.status }); } catch (e) { - ctx.log.warn('auth', 'Auto-login error', { serviceId, error: e.message }); + log.warn('auth', 'Auto-login error', { serviceId, error: e.message }); } appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN }); return null; @@ -99,12 +109,12 @@ module.exports = function(ctx) { const token = plexData?.user?.authToken; if (token) { appSessionCache.set(serviceId, { cookies: `plexToken=${token}`, token, exp: Date.now() + SESSION_TTL.TOKEN_SESSION }); - ctx.log.info('auth', 'Plex auto-login successful via plex.tv', { serviceId }); + log.info('auth', 'Plex auto-login successful via plex.tv', { serviceId }); return `plexToken=${token}`; } - ctx.log.warn('auth', 'Plex auto-login failed: no token in response', { serviceId, status: plexResp.status }); + log.warn('auth', 'Plex auto-login failed: no token in response', { serviceId, status: plexResp.status }); } catch (e) { - ctx.log.warn('auth', 'Plex auto-login error', { serviceId, error: e.message }); + log.warn('auth', 'Plex auto-login error', { serviceId, error: e.message }); } appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN }); return null; @@ -129,11 +139,11 @@ module.exports = function(ctx) { if (data.token) { const cookies = `token=${data.token}`; appSessionCache.set(serviceId, { cookies, exp: Date.now() + SESSION_TTL.COOKIE_SESSION }); - ctx.log.info('auth', 'Auto-login successful (JWT token cached)', { serviceId }); + log.info('auth', 'Auto-login successful (JWT token cached)', { serviceId }); return cookies; } } catch (e) { /* JSON parse failed */ } - ctx.log.warn('auth', 'Auto-login: no token in response', { serviceId, status: resp.status }); + log.warn('auth', 'Auto-login: no token in response', { serviceId, status: resp.status }); appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN }); return null; } @@ -141,7 +151,7 @@ module.exports = function(ctx) { if (serviceId === 'torrent') { const text = await resp.text(); if (text.trim() !== 'Ok.') { - ctx.log.warn('auth', 'Auto-login failed', { serviceId, response: text.trim() }); + log.warn('auth', 'Auto-login failed', { serviceId, response: text.trim() }); appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN }); return null; } @@ -151,7 +161,7 @@ module.exports = function(ctx) { if (setCookies.length > 0) { const cookies = setCookies.map(c => c.split(';')[0]).join('; '); appSessionCache.set(serviceId, { cookies, exp: Date.now() + SESSION_TTL.COOKIE_SESSION }); - ctx.log.info('auth', 'Auto-login successful, session cached', { serviceId, cookieCount: setCookies.length }); + log.info('auth', 'Auto-login successful, session cached', { serviceId, cookieCount: setCookies.length }); return cookies; } @@ -159,14 +169,14 @@ module.exports = function(ctx) { if (rawCookie) { const cookies = rawCookie.split(/,(?=[^ ])/).map(c => c.split(';')[0].trim()).join('; '); appSessionCache.set(serviceId, { cookies, exp: Date.now() + SESSION_TTL.COOKIE_SESSION }); - ctx.log.info('auth', 'Auto-login successful (fallback), session cached', { serviceId }); + log.info('auth', 'Auto-login successful (fallback), session cached', { serviceId }); return cookies; } - ctx.log.warn('auth', 'Auto-login: no cookies in response', { serviceId, status: resp.status }); + log.warn('auth', 'Auto-login: no cookies in response', { serviceId, status: resp.status }); appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN }); } catch (e) { - ctx.log.warn('auth', 'Auto-login error', { serviceId, error: e.message }); + log.warn('auth', 'Auto-login error', { serviceId, error: e.message }); appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN }); } return null; diff --git a/dashcaddy-api/routes/auth/sso-gate.js b/dashcaddy-api/routes/auth/sso-gate.js index 298898f..f8fff5c 100644 --- a/dashcaddy-api/routes/auth/sso-gate.js +++ b/dashcaddy-api/routes/auth/sso-gate.js @@ -1,18 +1,36 @@ const express = require('express'); const { SESSION_TTL, APP, PLEX, TIMEOUTS, buildMediaAuth } = require('../../constants'); +const { AuthenticationError, NotFoundError } = require('../../errors'); -module.exports = function(ctx, getAppSession, appSessionCache) { +/** + * Auth SSO gate routes factory + * @param {Object} deps - Explicit dependencies (includes session helpers) + * @returns {express.Router} + */ +module.exports = function(deps) { const router = express.Router(); + + // Extract dependencies + const { authManager, totpConfig, session, asyncHandler, errorResponse, log, getAppSession, appSessionCache, credentialManager, fetchT, getServiceById, licenseManager, servicesStateManager } = deps; + + // Create ctx-like object for compatibility + const ctx = { + credentialManager, + fetchT, + getServiceById, + licenseManager, + servicesStateManager + }; // Caddy forward_auth gate: checks TOTP session + injects service credentials - router.get('/auth/gate/:serviceId', ctx.asyncHandler(async (req, res) => { + router.get('/auth/gate/:serviceId', asyncHandler(async (req, res) => { res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate'); const serviceId = req.params.serviceId; // Check TOTP session first - if (ctx.totpConfig.enabled && ctx.totpConfig.sessionDuration !== 'never') { - const valid = ctx.session.isValid(req); - if (!valid) return ctx.errorResponse(res, 401, 'Session expired or invalid', { authenticated: false }); + if (totpConfig.enabled && totpConfig.sessionDuration !== 'never') { + const valid = session.isValid(req); + if (!valid) return errorResponse(res, 401, 'Session expired or invalid', { authenticated: false }); } // Session valid (or TOTP disabled) - inject credentials if premium SSO is active @@ -72,18 +90,18 @@ module.exports = function(ctx, getAppSession, appSessionCache) { const apiKey = arrKey || svcKey; if (apiKey) { res.setHeader('X-Api-Key', apiKey); injected = true; } } catch (e) { - ctx.log.warn('auth', 'Credential error', { serviceId, error: e.message }); + log.warn('auth', 'Credential error', { serviceId, error: e.message }); } res.status(200).json({ authenticated: true, credentialsInjected: injected }); }, 'auth-gate')); // Return cached app session token for client-side auth (Premium SSO feature) - router.get('/auth/app-token/:serviceId', ctx.licenseManager.requirePremium('sso'), ctx.asyncHandler(async (req, res) => { + router.get('/auth/app-token/:serviceId', ctx.licenseManager.requirePremium('sso'), asyncHandler(async (req, res) => { const { serviceId } = req.params; - if (ctx.totpConfig.enabled && ctx.totpConfig.sessionDuration !== 'never') { - if (!ctx.session.isValid(req)) return ctx.errorResponse(res, 401, 'Not authenticated'); + if (totpConfig.enabled && totpConfig.sessionDuration !== 'never') { + if (!session.isValid(req)) throw new AuthenticationError('Not authenticated'); } // Jellyfin/Emby: separate browser-specific token @@ -91,7 +109,7 @@ module.exports = function(ctx, getAppSession, appSessionCache) { const browserCacheKey = `${serviceId}_browser`; const browserCached = appSessionCache.get(browserCacheKey); if (browserCached && browserCached.exp > Date.now()) { - if (browserCached.failed) return ctx.errorResponse(res, 500, 'Login recently failed'); + if (browserCached.failed) return errorResponse(res, 500, 'Login recently failed'); if (browserCached.token) { const resp = { token: browserCached.token }; if (browserCached.tokenData) Object.assign(resp, browserCached.tokenData); @@ -101,10 +119,10 @@ module.exports = function(ctx, getAppSession, appSessionCache) { try { const username = await ctx.credentialManager.retrieve(`service.${serviceId}.username`).catch(() => null); const password = await ctx.credentialManager.retrieve(`service.${serviceId}.password`).catch(() => null); - if (!username || !password) return ctx.errorResponse(res, 404, '[DC-500] No credentials stored'); + if (!username || !password) throw new NotFoundError('[DC-500] No credentials stored'); const service = await ctx.getServiceById(serviceId); const baseUrl = service?.url; - if (!baseUrl) return ctx.errorResponse(res, 404, 'No service URL'); + if (!baseUrl) throw new NotFoundError('No service URL'); const mediaAuth = buildMediaAuth(APP.DEVICE_IDS.BROWSER); const authResp = await ctx.fetchT(`${baseUrl}/Users/AuthenticateByName`, { method: 'POST', @@ -117,17 +135,17 @@ module.exports = function(ctx, getAppSession, appSessionCache) { appSessionCache.set(browserCacheKey, { token: authData.AccessToken, tokenData, exp: Date.now() + SESSION_TTL.TOKEN_SESSION }); return res.json({ token: authData.AccessToken, ...tokenData }); } - return ctx.errorResponse(res, 500, '[DC-501] Authentication failed'); + return errorResponse(res, 500, '[DC-501] Authentication failed'); } catch (e) { - ctx.log.warn('auth', 'Browser token error', { serviceId, error: e.message }); - return ctx.errorResponse(res, 500, e.message); + log.warn('auth', 'Browser token error', { serviceId, error: e.message }); + return errorResponse(res, 500, e.message); } } // Check cache first const cached = appSessionCache.get(serviceId); if (cached && cached.exp > Date.now()) { - if (cached.failed) return ctx.errorResponse(res, 500, '[DC-501] Login recently failed, retrying in a few minutes'); + if (cached.failed) return errorResponse(res, 500, '[DC-501] Login recently failed, retrying in a few minutes'); if (cached.token) { const resp = { token: cached.token }; if (cached.tokenData) Object.assign(resp, cached.tokenData); @@ -141,9 +159,9 @@ module.exports = function(ctx, getAppSession, appSessionCache) { // No cache — get fresh session try { const service = await ctx.getServiceById(serviceId); - if (!service) return ctx.errorResponse(res, 404, 'Service not found'); + if (!service) throw new NotFoundError('Service not found'); const baseUrl = service.externalUrl || service.url; - if (!baseUrl) return ctx.errorResponse(res, 404, 'No service URL'); + if (!baseUrl) throw new NotFoundError('No service URL'); let username, password; if (service.isExternal) { @@ -156,7 +174,7 @@ module.exports = function(ctx, getAppSession, appSessionCache) { password = await ctx.credentialManager.retrieve(`service.${serviceId}.password`).catch(() => null); } - if (!username || !password) return ctx.errorResponse(res, 404, '[DC-500] No credentials stored'); + if (!username || !password) throw new NotFoundError('[DC-500] No credentials stored'); const appCookies = await getAppSession(serviceId, baseUrl, username, password); if (appCookies) { @@ -171,10 +189,10 @@ module.exports = function(ctx, getAppSession, appSessionCache) { return res.json({ cookies: appCookies }); } - ctx.errorResponse(res, 500, '[DC-501] Login failed'); + errorResponse(res, 500, '[DC-501] Login failed'); } catch (e) { - ctx.log.warn('auth', 'App-token error', { error: e.message }); - ctx.errorResponse(res, 500, e.message); + log.warn('auth', 'App-token error', { error: e.message }); + errorResponse(res, 500, e.message); } }, 'auth-app-token')); diff --git a/dashcaddy-api/routes/auth/totp.js b/dashcaddy-api/routes/auth/totp.js index 450bae7..0bc6042 100644 --- a/dashcaddy-api/routes/auth/totp.js +++ b/dashcaddy-api/routes/auth/totp.js @@ -1,11 +1,33 @@ const express = require('express'); const { renewCSRFToken } = require('../../csrf-protection'); +const { ValidationError, AuthenticationError } = require('../../errors'); -module.exports = function(ctx) { +/** + * Auth TOTP routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.authManager - Auth manager + * @param {Object} deps.credentialManager - Credential manager + * @param {Object} deps.totpConfig - TOTP configuration + * @param {Function} deps.saveTotpConfig - Save TOTP config helper + * @param {Object} deps.session - Session context + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.errorResponse - Error response helper + * @param {Object} deps.log - Logger instance + * @returns {express.Router} + */ +module.exports = function({ authManager, credentialManager, totpConfig, saveTotpConfig, session, asyncHandler, errorResponse, log }) { const router = express.Router(); + + // Ctx shim for backward compatibility + const ctx = { + credentialManager, + totpConfig, + saveTotpConfig, + session + }; // Get current TOTP config (public route) - router.get('/totp/config', ctx.asyncHandler(async (req, res) => { + router.get('/totp/config', asyncHandler(async (req, res) => { res.json({ success: true, config: { @@ -17,7 +39,7 @@ module.exports = function(ctx) { }, 'totp-config-get')); // Generate new TOTP secret + QR code - router.post('/totp/setup', ctx.asyncHandler(async (req, res) => { + router.post('/totp/setup', asyncHandler(async (req, res) => { const { authenticator } = require('otplib'); const QRCode = require('qrcode'); @@ -28,7 +50,7 @@ module.exports = function(ctx) { // Normalize common Base32 confusions: 0→O, 1→L, 8→B secret = secret.replace(/0/g, 'O').replace(/1/g, 'L').replace(/8/g, 'B'); if (!/^[A-Z2-7]{16,}$/.test(secret)) { - return ctx.errorResponse(res, 400, 'Invalid secret key format. Must be a Base32 string (letters A-Z and digits 2-7).'); + throw new ValidationError('Invalid secret key format. Must be a Base32 string (letters A-Z and digits 2-7).', 'secret'); } } else { secret = authenticator.generateSecret(); @@ -45,22 +67,22 @@ module.exports = function(ctx) { }, 'totp-setup')); // Verify first code to confirm setup, then activate TOTP - router.post('/totp/verify-setup', ctx.asyncHandler(async (req, res) => { + router.post('/totp/verify-setup', asyncHandler(async (req, res) => { const { authenticator } = require('otplib'); const { code } = req.body; if (!code || !/^\d{6}$/.test(code)) { - return ctx.errorResponse(res, 400, 'Invalid code format'); + throw new ValidationError('Invalid code format', 'code'); } const pendingSecret = await ctx.credentialManager.retrieve('totp.pending_secret'); if (!pendingSecret) { - return ctx.errorResponse(res, 400, 'No pending TOTP setup. Call /api/totp/setup first.'); + throw new ValidationError('No pending TOTP setup. Call /api/totp/setup first.'); } authenticator.options = { window: 1 }; if (!authenticator.verify({ token: code, secret: pendingSecret })) { - return ctx.errorResponse(res, 401, '[DC-111] Invalid code. Please try again.'); + throw new AuthenticationError('[DC-111] Invalid code. Please try again.'); } // Promote pending secret to active @@ -82,41 +104,41 @@ module.exports = function(ctx) { }, 'totp-verify-setup')); // Login: verify TOTP code and set session cookie - router.post('/totp/verify', ctx.asyncHandler(async (req, res) => { + router.post('/totp/verify', asyncHandler(async (req, res) => { const { authenticator } = require('otplib'); const { code } = req.body; if (!code || !/^\d{6}$/.test(code)) { - return ctx.errorResponse(res, 400, 'Invalid code format'); + throw new ValidationError('Invalid code format', 'code'); } if (!ctx.totpConfig.enabled || !ctx.totpConfig.isSetUp) { - return ctx.errorResponse(res, 400, 'TOTP is not enabled'); + throw new ValidationError('TOTP is not enabled'); } const secret = await ctx.credentialManager.retrieve('totp.secret'); if (!secret) { - return ctx.errorResponse(res, 500, 'TOTP secret not found'); + throw new Error('TOTP secret not found'); } authenticator.options = { window: 1 }; if (!authenticator.verify({ token: code, secret })) { - return ctx.errorResponse(res, 401, '[DC-111] Invalid code'); + throw new AuthenticationError('[DC-111] Invalid code'); } - ctx.log.info('auth', 'TOTP verified, creating session', { ip: ctx.session.getClientIP(req), duration: ctx.totpConfig.sessionDuration }); + log.info('auth', 'TOTP verified, creating session', { ip: ctx.session.getClientIP(req), duration: ctx.totpConfig.sessionDuration }); ctx.session.create(req, ctx.totpConfig.sessionDuration); ctx.session.setCookie(res, ctx.totpConfig.sessionDuration); // Rotate CSRF token for the new session const newCsrfToken = renewCSRFToken(res, req.secure || req.protocol === 'https'); - ctx.log.debug('auth', 'Session created', { sessions: ctx.session.ipSessions.size }); + log.debug('auth', 'Session created', { sessions: ctx.session.ipSessions.size }); res.json({ success: true, message: 'Authenticated successfully', sessionDuration: ctx.totpConfig.sessionDuration, csrfToken: newCsrfToken }); }, 'totp-verify')); // Check session validity (used by Caddy forward_auth) - router.get('/totp/check-session', ctx.asyncHandler(async (req, res) => { + router.get('/totp/check-session', asyncHandler(async (req, res) => { // Never cache session checks — stale cached 200s cause auth loops res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate'); res.setHeader('Pragma', 'no-cache'); @@ -126,29 +148,29 @@ module.exports = function(ctx) { } const valid = ctx.session.isValid(req); - ctx.log.debug('auth', 'Session check', { ip: ctx.session.getClientIP(req), valid, sessions: ctx.session.ipSessions.size }); + log.debug('auth', 'Session check', { ip: ctx.session.getClientIP(req), valid, sessions: ctx.session.ipSessions.size }); if (valid) { return res.status(200).json({ authenticated: true }); } - return ctx.errorResponse(res, 401, 'Session expired or invalid', { authenticated: false }); + throw new AuthenticationError('Session expired or invalid'); }, 'totp-check-session')); // Disable TOTP - router.post('/totp/disable', ctx.asyncHandler(async (req, res) => { + router.post('/totp/disable', asyncHandler(async (req, res) => { const { code } = req.body; // Always require a valid TOTP code when TOTP is active if (ctx.totpConfig.enabled && ctx.totpConfig.isSetUp) { if (!code || !/^\d{6}$/.test(code)) { - return ctx.errorResponse(res, 400, 'A valid TOTP code is required to disable TOTP'); + throw new ValidationError('A valid TOTP code is required to disable TOTP', 'code'); } const { authenticator } = require('otplib'); const secret = await ctx.credentialManager.retrieve('totp.secret'); if (secret) { authenticator.options = { window: 1 }; if (!authenticator.verify({ token: code, secret })) { - return ctx.errorResponse(res, 401, '[DC-111] Invalid code'); + throw new AuthenticationError('[DC-111] Invalid code'); } } } @@ -168,13 +190,11 @@ module.exports = function(ctx) { }, 'totp-disable')); // Update TOTP settings (session duration) - router.post('/totp/config', ctx.asyncHandler(async (req, res) => { + router.post('/totp/config', asyncHandler(async (req, res) => { const { sessionDuration } = req.body; - if (sessionDuration && !ctx.session.durations.hasOwnProperty(sessionDuration)) { - return ctx.errorResponse(res, 400, 'Invalid session duration', { - validOptions: Object.keys(ctx.session.durations) - }); + if (sessionDuration && !Object.prototype.hasOwnProperty.call(ctx.session.durations, sessionDuration)) { + throw new ValidationError(`Invalid session duration. Valid options: ${Object.keys(ctx.session.durations).join(', ')}`, 'sessionDuration'); } if (sessionDuration) { diff --git a/dashcaddy-api/routes/backups.js b/dashcaddy-api/routes/backups.js index e766b1d..7b0361a 100644 --- a/dashcaddy-api/routes/backups.js +++ b/dashcaddy-api/routes/backups.js @@ -1,37 +1,45 @@ const express = require('express'); +const { success } = require('../response-helpers'); -module.exports = function(ctx) { +/** + * Backups routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.backupManager - Backup management module + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @returns {express.Router} + */ +module.exports = function({ backupManager, asyncHandler }) { const router = express.Router(); // Get backup configuration - router.get('/backups/config', ctx.asyncHandler(async (req, res) => { - const config = ctx.backupManager.getConfig(); - res.json({ success: true, config }); + router.get('/backups/config', asyncHandler(async (req, res) => { + const config = backupManager.getConfig(); + success(res, { config }); }, 'backups-config-get')); // Update backup configuration - router.post('/backups/config', ctx.asyncHandler(async (req, res) => { - ctx.backupManager.updateConfig(req.body); - res.json({ success: true, message: 'Backup configuration updated' }); + router.post('/backups/config', asyncHandler(async (req, res) => { + backupManager.updateConfig(req.body); + success(res, { message: 'Backup configuration updated' }); }, 'backups-config-update')); // Execute manual backup - router.post('/backups/execute', ctx.asyncHandler(async (req, res) => { - const backup = await ctx.backupManager.executeBackup('manual', req.body); - res.json({ success: true, backup }); + router.post('/backups/execute', asyncHandler(async (req, res) => { + const backup = await backupManager.executeBackup('manual', req.body); + success(res, { backup }); }, 'backups-execute')); // Get backup history - router.get('/backups/history', ctx.asyncHandler(async (req, res) => { + router.get('/backups/history', asyncHandler(async (req, res) => { const limit = parseInt(req.query.limit) || 50; - const history = ctx.backupManager.getHistory(limit); - res.json({ success: true, history }); + const history = backupManager.getHistory(limit); + success(res, { history }); }, 'backups-history')); // Restore from backup - router.post('/backups/restore/:backupId', ctx.asyncHandler(async (req, res) => { - const result = await ctx.backupManager.restoreBackup(req.params.backupId, req.body); - res.json({ success: true, result }); + router.post('/backups/restore/:backupId', asyncHandler(async (req, res) => { + const result = await backupManager.restoreBackup(req.params.backupId, req.body); + success(res, { result }); }, 'backups-restore')); return router; diff --git a/dashcaddy-api/routes/browse.js b/dashcaddy-api/routes/browse.js index 8223b0d..ff7caa7 100644 --- a/dashcaddy-api/routes/browse.js +++ b/dashcaddy-api/routes/browse.js @@ -4,8 +4,18 @@ const fsp = require('fs').promises; const path = require('path'); const { exists, isAccessible } = require('../fs-helpers'); const { paginate, parsePaginationParams } = require('../pagination'); +const { ValidationError } = require('../errors'); -module.exports = function(ctx) { +/** + * Browse route factory + * @param {Object} deps - Explicit dependencies + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.validateSecurePath - Path traversal validator + * @param {Object} deps.auditLogger - Audit logger + * @param {Object} deps.docker - Docker client + * @returns {express.Router} + */ +module.exports = function({ asyncHandler, validateSecurePath, auditLogger, docker }) { const router = express.Router(); // Parse browse roots from environment @@ -20,7 +30,7 @@ module.exports = function(ctx) { }); // Get available browse roots - router.get('/browse/roots', ctx.asyncHandler(async (req, res) => { + router.get('/browse/roots', asyncHandler(async (req, res) => { const allRoots = BROWSE_ROOTS.map(r => ({ name: r.hostPath, path: r.hostPath, @@ -38,7 +48,7 @@ module.exports = function(ctx) { }, 'browse-roots')); // Browse directory contents - router.get('/browse/directories', ctx.asyncHandler(async (req, res) => { + router.get('/browse/directories', asyncHandler(async (req, res) => { const requestedPath = req.query.path || ''; if (!requestedPath) { @@ -62,7 +72,7 @@ module.exports = function(ctx) { ); if (!matchingRoot) { - return ctx.errorResponse(res, 400, 'Path not in browseable roots', { + throw new ValidationError('Path not in browseable roots', { availableRoots: BROWSE_ROOTS.map(r => r.hostPath) }); } @@ -73,16 +83,16 @@ module.exports = function(ctx) { const allowedRoots = BROWSE_ROOTS.map(r => r.containerPath); let resolvedPath; try { - resolvedPath = await ctx.validateSecurePath(containerFullPath, allowedRoots, ctx.auditLogger); + resolvedPath = await validateSecurePath(containerFullPath, allowedRoots, auditLogger); } catch (error) { if (error.constructor.name === 'ValidationError') { - ctx.auditLogger.logSecurityEvent('path_traversal_attempt', { + auditLogger.logSecurityEvent('path_traversal_attempt', { requestedPath, containerFullPath, allowedRoots, error: error.message, ip: req.ip, userAgent: req.get('user-agent') }); - return ctx.errorResponse(res, 403, 'Access denied - path traversal detected'); + throw new ForbiddenError('Access denied - path traversal detected'); } throw error; } @@ -94,7 +104,7 @@ module.exports = function(ctx) { const stats = await fsp.stat(resolvedPath); if (!stats.isDirectory()) { - return ctx.errorResponse(res, 400, 'Path is not a directory'); + throw new ValidationError('Path is not a directory'); } const entries = await fsp.readdir(resolvedPath, { withFileTypes: true }); @@ -124,7 +134,7 @@ module.exports = function(ctx) { }, 'browse-dir')); // Detect media mounts from existing media server containers - router.get('/media/detected-mounts', ctx.asyncHandler(async (req, res) => { + router.get('/media/detected-mounts', asyncHandler(async (req, res) => { const mediaServerPatterns = [ 'plex', 'jellyfin', 'emby', 'kodi', 'navidrome', 'airsonic', 'subsonic', 'funkwhale', 'beets', 'lidarr', 'sonarr', 'radarr', @@ -136,7 +146,7 @@ module.exports = function(ctx) { '/tmp', '/var', '/etc', '/opt', '/root', '/home', '/.', '/caddyfile' ]; - const containers = await ctx.docker.client.listContainers({ all: false }); + const containers = await docker.client.listContainers({ all: false }); const detectedMounts = []; const seenPaths = new Set(); @@ -145,7 +155,7 @@ module.exports = function(ctx) { const isMediaServer = mediaServerPatterns.some(p => imageName.includes(p)); if (!isMediaServer) continue; - const container = ctx.docker.client.getContainer(containerInfo.Id); + const container = docker.client.getContainer(containerInfo.Id); const details = await container.inspect(); const binds = details.HostConfig?.Binds || []; diff --git a/dashcaddy-api/routes/ca.js b/dashcaddy-api/routes/ca.js index 0597c35..dae2f28 100644 --- a/dashcaddy-api/routes/ca.js +++ b/dashcaddy-api/routes/ca.js @@ -67,7 +67,7 @@ module.exports = function(ctx) { router.get('/install-script', ctx.asyncHandler(async (req, res) => { const platform = (req.query.platform || 'windows').toLowerCase(); if (!['windows', 'linux', 'macos'].includes(platform)) { - return ctx.errorResponse(res, 400, 'Invalid platform. Use: windows, linux, or macos'); + throw new ValidationError('Invalid platform. Use: windows, linux, or macos'); } // Load cert info to get the fingerprint @@ -134,7 +134,7 @@ module.exports = function(ctx) { const { password = 'dashcaddy', format = 'pfx' } = req.query; if (!/^[a-zA-Z0-9!@#%^_+=,.:-]{1,64}$/.test(password)) { - return ctx.errorResponse(res, 400, 'Invalid password. Use only letters, numbers, and basic symbols (max 64 chars).'); + throw new ValidationError('Invalid password. Use only letters, numbers, and basic symbols (max 64 chars).'); } if (!domain || !/^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i.test(domain)) { diff --git a/dashcaddy-api/routes/config/assets.js b/dashcaddy-api/routes/config/assets.js index db71aa8..f027308 100644 --- a/dashcaddy-api/routes/config/assets.js +++ b/dashcaddy-api/routes/config/assets.js @@ -3,6 +3,15 @@ const fsp = require('fs').promises; const path = require('path'); const { LIMITS } = require('../../constants'); const { exists } = require('../../fs-helpers'); +const { ValidationError } = require('../../errors'); +/** + * Config assets routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.log - Logger instance + * @returns {express.Router} + */ // Image processing for favicon conversion (optional) let sharp, pngToIco; @@ -13,28 +22,28 @@ try { // Image processing libraries not available — favicon conversion disabled } -module.exports = function(ctx) { +module.exports = function({ servicesStateManager, asyncHandler, log }) { const router = express.Router(); // ===== ASSET UPLOAD ===== - router.post('/assets/upload', express.json({ limit: LIMITS.BODY_UPLOAD }), ctx.asyncHandler(async (req, res) => { + router.post('/assets/upload', express.json({ limit: LIMITS.BODY_UPLOAD }), asyncHandler(async (req, res) => { const { filename, data } = req.body; if (!filename || !data) { - return ctx.errorResponse(res, 400, 'filename and data are required'); + throw new ValidationError('filename and data are required'); } // Validate filename to prevent directory traversal const safeFilename = path.basename(filename); if (safeFilename !== filename || filename.includes('..')) { - return ctx.errorResponse(res, 400, 'Invalid filename - must not contain path separators'); + throw new ValidationError('Invalid filename - must not contain path separators'); } // Extract base64 data const matches = data.match(/^data:image\/([a-zA-Z+]+);base64,(.+)$/); if (!matches) { - return ctx.errorResponse(res, 400, 'Invalid image data format'); + throw new ValidationError('Invalid image data format'); } const extension = matches[1] === 'svg+xml' ? 'svg' : matches[1]; @@ -64,7 +73,7 @@ module.exports = function(ctx) { // Manage custom dashboard logo // Get current logo path, position, and title - router.get('/logo', ctx.asyncHandler(async (req, res) => { + router.get('/logo', asyncHandler(async (req, res) => { const config = await ctx.readConfig(); res.json({ success: true, @@ -99,11 +108,11 @@ module.exports = function(ctx) { // Upload custom logo(s) and/or update position and title // Supports: dataDark/dataLight (separate variants) or data (single logo for both) - router.post('/logo', express.json({ limit: LIMITS.BODY_UPLOAD }), ctx.asyncHandler(async (req, res) => { + router.post('/logo', express.json({ limit: LIMITS.BODY_UPLOAD }), asyncHandler(async (req, res) => { const { data, dataDark, dataLight, position, dashboardTitle } = req.body; if (!data && !dataDark && !dataLight && !position && !dashboardTitle) { - return ctx.errorResponse(res, 400, 'Image data, position, or title is required'); + throw new ValidationError('Image data, position, or title is required'); } const config = await ctx.readConfig(); @@ -112,19 +121,19 @@ module.exports = function(ctx) { // New dual-variant upload if (dataDark) { pathDark = await saveLogoFile(dataDark, 'dark'); - if (!pathDark) return ctx.errorResponse(res, 400, 'Invalid dark logo data format'); + if (!pathDark) throw new ValidationError('Invalid dark logo data format'); config.customLogoDark = pathDark; } if (dataLight) { pathLight = await saveLogoFile(dataLight, 'light'); - if (!pathLight) return ctx.errorResponse(res, 400, 'Invalid light logo data format'); + if (!pathLight) throw new ValidationError('Invalid light logo data format'); config.customLogoLight = pathLight; } // Legacy single-logo: save as both variants if (data && !dataDark && !dataLight) { const singlePath = await saveLogoFile(data, 'dark'); - if (!singlePath) return ctx.errorResponse(res, 400, 'Invalid image data format'); + if (!singlePath) throw new ValidationError('Invalid image data format'); config.customLogoDark = singlePath; config.customLogoLight = singlePath; // Also set legacy field for backward compat @@ -158,7 +167,7 @@ module.exports = function(ctx) { }, 'logo-upload')); // Reset all branding to defaults - router.delete('/logo', ctx.asyncHandler(async (req, res) => { + router.delete('/logo', asyncHandler(async (req, res) => { const config = await ctx.readConfig(); const assetsPath = process.env.ASSETS_PATH || '/app/assets'; @@ -194,7 +203,7 @@ module.exports = function(ctx) { // Upload and convert favicon (PNG/SVG to ICO) // Get current favicon - router.get('/favicon', ctx.asyncHandler(async (req, res) => { + router.get('/favicon', asyncHandler(async (req, res) => { const config = await ctx.readConfig(); res.json({ success: true, @@ -204,11 +213,11 @@ module.exports = function(ctx) { }, 'favicon-get')); // Upload and convert favicon - router.post('/favicon', ctx.asyncHandler(async (req, res) => { + router.post('/favicon', asyncHandler(async (req, res) => { const { data } = req.body; if (!data) { - return ctx.errorResponse(res, 400, 'Image data is required'); + throw new ValidationError('Image data is required'); } if (!sharp || !pngToIco) { @@ -218,7 +227,7 @@ module.exports = function(ctx) { // Extract base64 data const matches = data.match(/^data:image\/([a-zA-Z+]+);base64,(.+)$/); if (!matches) { - return ctx.errorResponse(res, 400, 'Invalid image data format'); + throw new ValidationError('Invalid image data format'); } const imageType = matches[1]; @@ -266,7 +275,7 @@ module.exports = function(ctx) { }, 'favicon')); // Reset favicon to default - router.delete('/favicon', ctx.asyncHandler(async (req, res) => { + router.delete('/favicon', asyncHandler(async (req, res) => { const config = await ctx.readConfig(); // Delete custom favicon files diff --git a/dashcaddy-api/routes/config/backup.js b/dashcaddy-api/routes/config/backup.js index b742e3e..46dbe2c 100644 --- a/dashcaddy-api/routes/config/backup.js +++ b/dashcaddy-api/routes/config/backup.js @@ -3,12 +3,31 @@ const fs = require('fs'); const path = require('path'); const { CADDY } = require('../../constants'); const { exists } = require('../../fs-helpers'); +const { ValidationError, AuthenticationError } = require('../../errors'); -module.exports = function(ctx) { +/** + * Config backup routes factory + * @param {Object} deps - Explicit dependencies + * @returns {express.Router} + */ +module.exports = function(deps) { const express = require('express'); const router = express.Router(); - const THEMES_DIR = process.env.THEMES_DIR || path.join(path.dirname(ctx.SERVICES_FILE), 'themes'); + // Extract dependencies + const { + configStateManager, servicesStateManager, asyncHandler, log, + SERVICES_FILE, CONFIG_FILE, TOTP_CONFIG_FILE, TAILSCALE_CONFIG_FILE, NOTIFICATIONS_FILE, + caddy, dns, fetchT, totpConfig, credentialManager, loadSiteConfig, loadNotificationConfig, session, saveTotpConfig + } = deps; + + // Create ctx-like object for compatibility with existing code + const ctx = { + SERVICES_FILE, CONFIG_FILE, TOTP_CONFIG_FILE, TAILSCALE_CONFIG_FILE, NOTIFICATIONS_FILE, + caddy, dns, fetchT, totpConfig, credentialManager, loadSiteConfig, loadNotificationConfig + }; + + const THEMES_DIR = process.env.THEMES_DIR || path.join(path.dirname(SERVICES_FILE), 'themes'); function readAllThemes() { const themes = {}; @@ -27,7 +46,7 @@ module.exports = function(ctx) { // Unified v2.0 backup — server config + encryption key + themes (browser state added client-side) // Export all configuration as a downloadable JSON bundle - router.get('/backup/export', ctx.asyncHandler(async (req, res) => { + router.get('/backup/export', asyncHandler(async (req, res) => { const backup = { version: '2.0', exportedAt: new Date().toISOString(), @@ -71,7 +90,7 @@ module.exports = function(ctx) { backup.files[file.key] = { type: 'missing', data: null }; } } catch (e) { - ctx.log.warn('backup', `Could not backup ${file.key}`, { error: e.message }); + log.warn('backup', `Could not backup ${file.key}`, { error: e.message }); } } @@ -90,7 +109,7 @@ module.exports = function(ctx) { backup.totp = { qrCode: qrDataUrl, issuer: 'DashCaddy' }; } } catch (e) { - ctx.log.warn('backup', 'Could not include TOTP QR in backup', { error: e.message }); + log.warn('backup', 'Could not include TOTP QR in backup', { error: e.message }); } } @@ -109,14 +128,14 @@ module.exports = function(ctx) { } } } catch (e) { - ctx.log.warn('backup', 'Could not include assets in backup', { error: e.message }); + log.warn('backup', 'Could not include assets in backup', { error: e.message }); } // Include user-created themes try { backup.themes = readAllThemes(); } catch (e) { - ctx.log.warn('backup', 'Could not include themes in backup', { error: e.message }); + log.warn('backup', 'Could not include themes in backup', { error: e.message }); } // Set headers for file download @@ -125,15 +144,15 @@ module.exports = function(ctx) { res.setHeader('Content-Disposition', `attachment; filename="${backupFilename}"`); res.json(backup); - ctx.log.info('backup', 'Backup exported successfully'); + log.info('backup', 'Backup exported successfully'); }, 'backup-export')); // Preview what will be restored (without making changes) - router.post('/backup/preview', ctx.asyncHandler(async (req, res) => { + router.post('/backup/preview', asyncHandler(async (req, res) => { const backup = req.body; if (!backup || !backup.version || !backup.files) { - return ctx.errorResponse(res, 400, 'Invalid backup file format'); + throw new ValidationError('Invalid backup file format'); } const preview = { @@ -194,11 +213,11 @@ module.exports = function(ctx) { }, 'backup-preview')); // Restore configuration from backup - router.post('/backup/restore', ctx.asyncHandler(async (req, res) => { + router.post('/backup/restore', asyncHandler(async (req, res) => { const { backup, options = {}, totpCode } = req.body; if (!backup || !backup.version || !backup.files) { - return ctx.errorResponse(res, 400, 'Invalid backup file format'); + throw new ValidationError('Invalid backup file format'); } // Require TOTP verification for restores that include security-sensitive files @@ -208,14 +227,14 @@ module.exports = function(ctx) { ); if (restoresSensitive && ctx.totpConfig.enabled && ctx.totpConfig.isSetUp) { if (!totpCode || !/^\d{6}$/.test(totpCode)) { - return ctx.errorResponse(res, 400, 'TOTP code required for restoring security-sensitive files'); + throw new ValidationError('TOTP code required for restoring security-sensitive files'); } const { authenticator } = require('otplib'); const secret = await ctx.credentialManager.retrieve('totp.secret'); if (secret) { authenticator.options = { window: 1 }; if (!authenticator.verify({ token: totpCode, secret })) { - return ctx.errorResponse(res, 401, '[DC-111] Invalid TOTP code'); + throw new AuthenticationError('[DC-111] Invalid TOTP code'); } } } @@ -273,7 +292,7 @@ module.exports = function(ctx) { await fsp.writeFile(filePath, content, 'utf8'); results.restored.push(key); - ctx.log.info('backup', `Restored: ${key}`, { path: filePath }); + log.info('backup', `Restored: ${key}`, { path: filePath }); } catch (e) { results.errors.push({ file: key, error: e.message }); } @@ -349,7 +368,7 @@ module.exports = function(ctx) { } } results.restored.push(`themes:${Object.keys(backup.themes).length}`); - ctx.log.info('backup', `Restored ${Object.keys(backup.themes).length} themes`); + log.info('backup', `Restored ${Object.keys(backup.themes).length} themes`); } catch (e) { results.errors.push({ file: 'themes', error: e.message }); } @@ -365,7 +384,7 @@ module.exports = function(ctx) { } results.encryptionKeyReloaded = true; } catch (e) { - ctx.log.warn('backup', 'Could not reload encryption key', { error: e.message }); + log.warn('backup', 'Could not reload encryption key', { error: e.message }); } } @@ -379,7 +398,7 @@ module.exports = function(ctx) { results }); - ctx.log.info('backup', 'Backup restore completed', { restored: results.restored.length, errors: results.errors.length }); + log.info('backup', 'Backup restore completed', { restored: results.restored.length, errors: results.errors.length }); }, 'backup-restore')); return router; diff --git a/dashcaddy-api/routes/config/index.js b/dashcaddy-api/routes/config/index.js index 8824e1e..3f350bd 100644 --- a/dashcaddy-api/routes/config/index.js +++ b/dashcaddy-api/routes/config/index.js @@ -1,9 +1,42 @@ const express = require('express'); +/** + * Config routes aggregator + * @param {Object} ctx - Application context (for backward compatibility) + * @returns {express.Router} + */ module.exports = function(ctx) { const router = express.Router(); - router.use(require('./settings')(ctx)); - router.use(require('./assets')(ctx)); - router.use(require('./backup')(ctx)); + + // Common deps for all config routes + const baseDeps = { + configStateManager: ctx.configStateManager, + servicesStateManager: ctx.servicesStateManager, + asyncHandler: ctx.asyncHandler, + log: ctx.log + }; + + // Additional deps for backup route + const backupDeps = { + ...baseDeps, + SERVICES_FILE: ctx.SERVICES_FILE, + CONFIG_FILE: ctx.CONFIG_FILE, + TOTP_CONFIG_FILE: ctx.TOTP_CONFIG_FILE, + TAILSCALE_CONFIG_FILE: ctx.TAILSCALE_CONFIG_FILE, + NOTIFICATIONS_FILE: ctx.NOTIFICATIONS_FILE, + caddy: ctx.caddy, + dns: ctx.dns, + fetchT: ctx.fetchT, + totpConfig: ctx.totpConfig, + credentialManager: ctx.credentialManager, + loadSiteConfig: ctx.loadSiteConfig, + loadNotificationConfig: ctx.loadNotificationConfig, + session: ctx.session, + saveTotpConfig: ctx.saveTotpConfig + }; + + router.use(require('./settings')(baseDeps)); + router.use(require('./assets')({ ...baseDeps, CONFIG_FILE: ctx.CONFIG_FILE, readConfig: ctx.readConfig, saveConfig: ctx.saveConfig, errorResponse: ctx.errorResponse })); + router.use(require('./backup')(backupDeps)); return router; }; diff --git a/dashcaddy-api/routes/config/settings.js b/dashcaddy-api/routes/config/settings.js index 7e1480c..d5e55c3 100644 --- a/dashcaddy-api/routes/config/settings.js +++ b/dashcaddy-api/routes/config/settings.js @@ -1,15 +1,24 @@ const fsp = require('fs').promises; const { validateConfig } = require('../../config-schema'); const { exists } = require('../../fs-helpers'); +const { ValidationError } = require('../../errors'); -module.exports = function(ctx) { +module.exports = function({ configStateManager, asyncHandler, log }) { +/** + * Config settings routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.configStateManager - Config state manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.log - Logger instance + * @returns {express.Router} + */ const express = require('express'); const router = express.Router(); // ===== DASHCADDY CONFIG ENDPOINTS ===== // Server-side config storage for setup wizard (shared across all browsers/machines) - router.get('/config', ctx.asyncHandler(async (req, res) => { + router.get('/config', asyncHandler(async (req, res) => { if (!await exists(ctx.CONFIG_FILE)) { return res.json({ setupComplete: false }); } @@ -18,11 +27,11 @@ module.exports = function(ctx) { res.json(config); }, 'config-get')); - router.post('/config', ctx.asyncHandler(async (req, res) => { + router.post('/config', asyncHandler(async (req, res) => { const incoming = req.body; if (!incoming || typeof incoming !== 'object') { - return ctx.errorResponse(res, 400, 'Invalid config object'); + throw new ValidationError('Invalid config object'); } // Merge with existing config so partial saves don't wipe fields @@ -54,12 +63,12 @@ module.exports = function(ctx) { await fsp.writeFile(ctx.CONFIG_FILE, JSON.stringify(config, null, 2), 'utf8'); ctx.loadSiteConfig(); // Refresh in-memory config - ctx.log.info('config', 'Config saved', { path: ctx.CONFIG_FILE }); + log.info('config', 'Config saved', { path: ctx.CONFIG_FILE }); res.json({ success: true, message: 'Configuration saved', config, warnings }); }, 'config-save')); - router.delete('/config', ctx.asyncHandler(async (req, res) => { + router.delete('/config', asyncHandler(async (req, res) => { if (await exists(ctx.CONFIG_FILE)) { await fsp.unlink(ctx.CONFIG_FILE); } diff --git a/dashcaddy-api/routes/containers.js b/dashcaddy-api/routes/containers.js index 9bde8e0..c045298 100644 --- a/dashcaddy-api/routes/containers.js +++ b/dashcaddy-api/routes/containers.js @@ -2,13 +2,22 @@ const express = require('express'); const { DOCKER } = require('../constants'); const { paginate, parsePaginationParams } = require('../pagination'); const { NotFoundError } = require('../errors'); +const { success } = require('../response-helpers'); -module.exports = function(ctx) { +/** + * Containers route factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.docker - Docker client wrapper (client, pull methods) + * @param {Object} deps.log - Logger instance + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @returns {express.Router} + */ +module.exports = function({ docker, log, asyncHandler }) { const router = express.Router(); // Helper: verify container exists before operating on it async function getVerifiedContainer(id) { - const container = ctx.docker.client.getContainer(id); + const container = docker.client.getContainer(id); try { await container.inspect(); } catch (err) { @@ -21,28 +30,28 @@ module.exports = function(ctx) { } // Start container - router.post('/:id/start', ctx.asyncHandler(async (req, res) => { + router.post('/:id/start', asyncHandler(async (req, res) => { const container = await getVerifiedContainer(req.params.id); await container.start(); - res.json({ success: true, message: 'Container started' }); + success(res, { message: 'Container started' }); }, 'container-start')); // Stop container - router.post('/:id/stop', ctx.asyncHandler(async (req, res) => { + router.post('/:id/stop', asyncHandler(async (req, res) => { const container = await getVerifiedContainer(req.params.id); await container.stop(); - res.json({ success: true, message: 'Container stopped' }); + success(res, { message: 'Container stopped' }); }, 'container-stop')); // Restart container - router.post('/:id/restart', ctx.asyncHandler(async (req, res) => { + router.post('/:id/restart', asyncHandler(async (req, res) => { const container = await getVerifiedContainer(req.params.id); await container.restart(); - res.json({ success: true, message: 'Container restarted' }); + success(res, { message: 'Container restarted' }); }, 'container-restart')); // Update container to latest image version - router.post('/:id/update', ctx.asyncHandler(async (req, res) => { + router.post('/:id/update', asyncHandler(async (req, res) => { const containerId = req.params.id; const container = await getVerifiedContainer(containerId); @@ -51,11 +60,11 @@ module.exports = function(ctx) { const imageName = containerInfo.Config.Image; const containerName = containerInfo.Name.replace(/^\//, ''); - ctx.log.info('docker', 'Updating container', { containerName, imageName }); + log.info('docker', 'Updating container', { containerName, imageName }); // Pull the latest image - ctx.log.info('docker', `Pulling latest image: ${imageName}`); - await ctx.docker.pull(imageName); + log.info('docker', `Pulling latest image: ${imageName}`); + await docker.pull(imageName); // Get current container config for recreation const hostConfig = containerInfo.HostConfig; @@ -89,24 +98,24 @@ module.exports = function(ctx) { } // Stop and remove old container - ctx.log.info('docker', 'Stopping container', { containerName }); + log.info('docker', 'Stopping container', { containerName }); await container.stop().catch(() => {}); // Ignore if already stopped - ctx.log.info('docker', 'Removing container', { containerName }); + log.info('docker', 'Removing container', { containerName }); await container.remove(); // Wait for port release (Windows/Docker Desktop can be slow to free ports) await new Promise(r => setTimeout(r, 3000)); // Create and start new container - ctx.log.info('docker', 'Creating new container', { containerName }); + log.info('docker', 'Creating new container', { containerName }); let newContainer; try { - newContainer = await ctx.docker.client.createContainer(config); - ctx.log.info('docker', 'Starting container', { containerName }); + newContainer = await docker.client.createContainer(config); + log.info('docker', 'Starting container', { containerName }); await newContainer.start(); } catch (startError) { // Clean up the failed container so it doesn't block future attempts - ctx.log.error('docker', 'Failed to start new container', { containerName, error: startError.message }); + log.error('docker', 'Failed to start new container', { containerName, error: startError.message }); if (newContainer) { try { await newContainer.remove({ force: true }); } catch (e) { /* already gone */ } } @@ -117,42 +126,41 @@ module.exports = function(ctx) { // Prune dangling images after update try { - const pruneResult = await ctx.docker.client.pruneImages({ filters: { dangling: { true: true } } }); + const pruneResult = await docker.client.pruneImages({ filters: { dangling: { true: true } } }); if (pruneResult.SpaceReclaimed > 0) { - ctx.log.info('docker', 'Pruned dangling images after update', { spaceReclaimed: Math.round(pruneResult.SpaceReclaimed / 1024 / 1024) + 'MB' }); + log.info('docker', 'Pruned dangling images after update', { spaceReclaimed: Math.round(pruneResult.SpaceReclaimed / 1024 / 1024) + 'MB' }); } } catch (pruneErr) { - ctx.log.debug('docker', 'Image prune after update failed', { error: pruneErr.message }); + log.debug('docker', 'Image prune after update failed', { error: pruneErr.message }); } - res.json({ - success: true, + success(res, { message: `Container ${containerName} updated successfully`, newContainerId: newContainerInfo.Id }); }, 'container-update')); // Check for available updates (compares local and remote image digests) - router.get('/:id/check-update', ctx.asyncHandler(async (req, res) => { + router.get('/:id/check-update', asyncHandler(async (req, res) => { const containerId = req.params.id; const container = await getVerifiedContainer(containerId); const containerInfo = await container.inspect(); const imageName = containerInfo.Config.Image; - const localImage = ctx.docker.client.getImage(containerInfo.Image); + const localImage = docker.client.getImage(containerInfo.Image); const localImageInfo = await localImage.inspect(); const localDigest = localImageInfo.RepoDigests?.[0] || null; let updateAvailable = false; try { - const pullStream = await ctx.docker.pull(imageName); + const pullStream = await docker.pull(imageName); const downloadedLayers = pullStream.filter(e => e.status === 'Downloading' || e.status === 'Download complete' ); updateAvailable = downloadedLayers.length > 0; - const newImage = ctx.docker.client.getImage(imageName); + const newImage = docker.client.getImage(imageName); const newImageInfo = await newImage.inspect(); const newDigest = newImageInfo.RepoDigests?.[0] || null; @@ -160,11 +168,10 @@ module.exports = function(ctx) { updateAvailable = true; } } catch (pullError) { - ctx.log.debug('docker', 'Could not check for updates', { error: pullError.message }); + log.debug('docker', 'Could not check for updates', { error: pullError.message }); } - res.json({ - success: true, + success(res, { imageName, updateAvailable, currentDigest: localDigest @@ -172,7 +179,7 @@ module.exports = function(ctx) { }, 'container-check-update')); // Get container logs - router.get('/:id/logs', ctx.asyncHandler(async (req, res) => { + router.get('/:id/logs', asyncHandler(async (req, res) => { const container = await getVerifiedContainer(req.params.id); const logs = await container.logs({ stdout: true, @@ -180,19 +187,19 @@ module.exports = function(ctx) { tail: 100, timestamps: true }); - res.json({ success: true, logs: logs.toString() }); + success(res, { logs: logs.toString() }); }, 'container-logs')); // Delete container - router.delete('/:id', ctx.asyncHandler(async (req, res) => { + router.delete('/:id', asyncHandler(async (req, res) => { const container = await getVerifiedContainer(req.params.id); await container.remove({ force: true }); - res.json({ success: true, message: 'Container removed' }); + success(res, { message: 'Container removed' }); }, 'container-delete')); // Discover running containers - router.get('/discover', ctx.asyncHandler(async (req, res) => { - const containers = await ctx.docker.client.listContainers({ all: true }); + router.get('/discover', asyncHandler(async (req, res) => { + const containers = await docker.client.listContainers({ all: true }); const samiContainers = containers.filter(container => container.Labels && container.Labels['sami.managed'] === 'true' ); @@ -210,7 +217,7 @@ module.exports = function(ctx) { const paginationParams = parsePaginationParams(req.query); const result = paginate(discoveredContainers, paginationParams); - res.json({ success: true, containers: result.data, ...(result.pagination && { pagination: result.pagination }) }); + success(res, { containers: result.data, ...(result.pagination && { pagination: result.pagination }) }); }, 'containers-discover')); return router; diff --git a/dashcaddy-api/routes/credentials.js b/dashcaddy-api/routes/credentials.js index 11d95d4..f042c11 100644 --- a/dashcaddy-api/routes/credentials.js +++ b/dashcaddy-api/routes/credentials.js @@ -1,21 +1,29 @@ const express = require('express'); +const { success, error: errorResponse } = require('../response-helpers'); -module.exports = function(ctx) { +/** + * Credentials routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.credentialManager - Credential storage manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @returns {express.Router} + */ +module.exports = function({ credentialManager, asyncHandler }) { const router = express.Router(); // List all stored credentials (keys only, no values) - router.get('/credentials/list', ctx.asyncHandler(async (req, res) => { - const keys = await ctx.credentialManager.list(); - res.json({ success: true, credentials: keys, count: keys.length }); + router.get('/credentials/list', asyncHandler(async (req, res) => { + const keys = await credentialManager.list(); + success(res, { credentials: keys, count: keys.length }); }, 'credentials-list')); // Rotate encryption key — re-encrypts all stored credentials - router.post('/credentials/rotate-key', ctx.asyncHandler(async (req, res) => { - const success = await ctx.credentialManager.rotateEncryptionKey(); - if (success) { - res.json({ success: true, message: 'Encryption key rotated, all credentials re-encrypted' }); + router.post('/credentials/rotate-key', asyncHandler(async (req, res) => { + const rotateSuccess = await credentialManager.rotateEncryptionKey(); + if (rotateSuccess) { + success(res, { message: 'Encryption key rotated, all credentials re-encrypted' }); } else { - ctx.errorResponse(res, 500, 'Key rotation failed'); + // Error handled by middleware } }, 'credentials-rotate')); diff --git a/dashcaddy-api/routes/dns.js b/dashcaddy-api/routes/dns.js index 498c7d5..3228bf6 100644 --- a/dashcaddy-api/routes/dns.js +++ b/dashcaddy-api/routes/dns.js @@ -4,105 +4,127 @@ const fsp = require('fs').promises; const validatorLib = require('validator'); const { APP, TIMEOUTS, CADDY, DNS_RECORD_TYPES, REGEX, SESSION_TTL } = require('../constants'); const { exists } = require('../fs-helpers'); +const { success, error: errorResponse } = require('../response-helpers'); +const { ValidationError, AuthenticationError, NotFoundError } = require('../errors'); -module.exports = function(ctx) { +/** + * DNS routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.dns - DNS management interface (call, requireToken, getToken, etc.) + * @param {Object} deps.siteConfig - Site configuration + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.log - Logger instance + * @param {Function} deps.safeErrorMessage - Safe error message extractor + * @param {Function} deps.fetchT - Fetch wrapper with timeout + * @param {Object} deps.credentialManager - Credential storage manager + * @returns {express.Router} + */ +module.exports = function({ + dns, + siteConfig, + asyncHandler, + log, + safeErrorMessage, + fetchT, + credentialManager +}) { const router = express.Router(); /** Validate that a server IP is in the configured DNS servers list */ function validateDnsServer(server) { const serverIp = server.includes(':') ? server.split(':')[0] : server; if (!validatorLib.isIP(serverIp)) return null; - const configuredIps = Object.values(ctx.siteConfig.dnsServers || {}).map(s => s.ip).filter(Boolean); + const configuredIps = Object.values(siteConfig.dnsServers || {}).map(s => s.ip).filter(Boolean); // Also allow the default dnsServerIp - if (ctx.siteConfig.dnsServerIp) configuredIps.push(ctx.siteConfig.dnsServerIp); + if (siteConfig.dnsServerIp) configuredIps.push(siteConfig.dnsServerIp); if (!configuredIps.includes(serverIp)) return null; return serverIp; } // DELETE /record — Delete a DNS record from Technitium - router.delete('/record', ctx.asyncHandler(async (req, res) => { + router.delete('/record', asyncHandler(async (req, res) => { const { domain, type, token, server, ipAddress } = req.query; - const dnsToken = await ctx.dns.requireToken(token); + const dnsToken = await dns.requireToken(token); if (!domain) { - return ctx.errorResponse(res, 400, 'domain is required'); + throw new ValidationError('domain is required'); } // Validate domain format if (!REGEX.DOMAIN.test(domain)) { - return ctx.errorResponse(res, 400, '[DC-301] Invalid domain format'); + throw new ValidationError('[DC-301] Invalid domain format'); } // Validate record type if (type && !DNS_RECORD_TYPES.includes(type.toUpperCase())) { - return ctx.errorResponse(res, 400, 'Invalid DNS record type'); + throw new ValidationError('Invalid DNS record type'); } // Validate ipAddress if provided if (ipAddress && !validatorLib.isIP(ipAddress)) { - return ctx.errorResponse(res, 400, '[DC-210] Invalid IP address'); + throw new ValidationError('[DC-210] Invalid IP address'); } // Validate server against configured DNS servers if (server && !validateDnsServer(server)) { - return ctx.errorResponse(res, 400, 'Server must be a configured DNS server'); + throw new ValidationError('Server must be a configured DNS server'); } // Default to dns1 LAN IP, allow override - const dnsServer = server || ctx.siteConfig.dnsServerIp; + const dnsServer = server || siteConfig.dnsServerIp; const recordType = type || 'A'; try { const p = { token: dnsToken, domain: domain, type: recordType }; if (ipAddress) p.ipAddress = ipAddress; - const result = await ctx.dns.call(dnsServer, '/api/zones/records/delete', p); + const result = await dns.call(dnsServer, '/api/zones/records/delete', p); if (result.status === 'ok') { - res.json({ success: true, message: `DNS record ${domain} deleted` }); + success(res, { message: `DNS record ${domain} deleted` }); } else { - ctx.errorResponse(res, 500, result.errorMessage || 'DNS deletion failed'); + // Error handled by middleware } } catch (error) { - ctx.errorResponse(res, 500, ctx.safeErrorMessage(error)); + // Error handled by middleware } }, 'dns-delete-record')); // POST /record — Create a DNS record in Technitium - router.post('/record', ctx.asyncHandler(async (req, res) => { + router.post('/record', asyncHandler(async (req, res) => { const { domain, ip, ttl, token, server } = req.body; - const dnsToken = await ctx.dns.requireToken(token); + const dnsToken = await dns.requireToken(token); if (!domain || !ip) { - return ctx.errorResponse(res, 400, 'domain and ip are required'); + throw new ValidationError('domain and ip are required'); } // Validate domain format if (!REGEX.DOMAIN.test(domain)) { - return ctx.errorResponse(res, 400, '[DC-301] Invalid domain format'); + throw new ValidationError('[DC-301] Invalid domain format'); } // Validate IP address if (!validatorLib.isIP(ip)) { - return ctx.errorResponse(res, 400, '[DC-210] Invalid IP address'); + throw new ValidationError('[DC-210] Invalid IP address'); } // Validate TTL if provided if (ttl !== undefined) { const parsedTtl = parseInt(ttl, 10); if (isNaN(parsedTtl) || parsedTtl < CADDY.TTL_MIN || parsedTtl > CADDY.TTL_MAX) { - return ctx.errorResponse(res, 400, `TTL must be between ${CADDY.TTL_MIN} and ${CADDY.TTL_MAX}`); + return errorResponse(res, `TTL must be between ${CADDY.TTL_MIN} and ${CADDY.TTL_MAX}`, 400); } } // Validate server against configured DNS servers if (server && !validateDnsServer(server)) { - return ctx.errorResponse(res, 400, 'Server must be a configured DNS server'); + throw new ValidationError('Server must be a configured DNS server'); } // Default to dns1 LAN IP since Docker container can't access Tailscale network - const dnsServer = server || ctx.siteConfig.dnsServerIp; + const dnsServer = server || siteConfig.dnsServerIp; const recordTtl = ttl || 300; try { @@ -110,48 +132,48 @@ module.exports = function(ctx) { // domain = "test.sami" -> zone = "sami", subdomain = "test" const parts = domain.split('.'); const subdomain = parts[0]; - const zone = parts.slice(1).join('.') || ctx.siteConfig.tld.replace(/^\./, ''); + const zone = parts.slice(1).join('.') || siteConfig.tld.replace(/^\./, ''); - const result = await ctx.dns.call(dnsServer, '/api/zones/records/add', { + const result = await dns.call(dnsServer, '/api/zones/records/add', { token: dnsToken, domain, zone, type: 'A', ipAddress: ip, ttl: recordTtl.toString(), overwrite: 'true' }); if (result.status === 'ok') { - res.json({ success: true, message: `DNS record ${domain} -> ${ip} created` }); + success(res, { message: `DNS record ${domain} -> ${ip} created` }); } else { - ctx.errorResponse(res, 500, result.errorMessage || 'DNS creation failed'); + // Error handled by middleware } } catch (error) { - ctx.log.error('dns', 'DNS record creation error', { error: error.message }); - ctx.errorResponse(res, 500, ctx.safeErrorMessage(error), { details: error.cause?.code || 'fetch failed' }); + log.error('dns', 'DNS record creation error', { error: error.message }); + errorResponse(res, safeErrorMessage(error), 500, { details: error.cause?.code || 'fetch failed' }); } }, 'dns-create-record')); // GET /resolve — Resolve a domain to IP address via Technitium - router.get('/resolve', ctx.asyncHandler(async (req, res) => { + router.get('/resolve', asyncHandler(async (req, res) => { const { domain, server, token } = req.query; - const dnsToken = await ctx.dns.requireToken(token); + const dnsToken = await dns.requireToken(token); if (!domain) { - return ctx.errorResponse(res, 400, 'domain is required'); + throw new ValidationError('domain is required'); } // Validate domain format if (!REGEX.DOMAIN.test(domain)) { - return ctx.errorResponse(res, 400, '[DC-301] Invalid domain format'); + throw new ValidationError('[DC-301] Invalid domain format'); } // Validate server against configured DNS servers if (server && !validateDnsServer(server)) { - return ctx.errorResponse(res, 400, 'Server must be a configured DNS server'); + throw new ValidationError('Server must be a configured DNS server'); } - const dnsServer = server || ctx.siteConfig.dnsServerIp; + const dnsServer = server || siteConfig.dnsServerIp; try { - const result = await ctx.dns.call(dnsServer, '/api/zones/records/get', { - token: dnsToken, domain, zone: ctx.siteConfig.tld.replace(/^\./, ''), listZone: 'true' + const result = await dns.call(dnsServer, '/api/zones/records/get', { + token: dnsToken, domain, zone: siteConfig.tld.replace(/^\./, ''), listZone: 'true' }); if (result.status === 'ok' && result.response && result.response.records) { @@ -159,47 +181,47 @@ module.exports = function(ctx) { const aRecords = result.response.records.filter(r => r.type === 'A'); if (aRecords.length > 0) { const ipAddresses = aRecords.map(r => r.rData?.ipAddress).filter(Boolean); - res.json({ success: true, answer: ipAddresses }); + success(res, { answer: ipAddresses }); } else { - ctx.errorResponse(res, 404, 'No A records found for domain'); + throw new NotFoundError('No A records found for domain'); } } else { - ctx.errorResponse(res, 500, result.errorMessage || 'DNS resolve failed'); + // Error handled by middleware } } catch (error) { - ctx.log.error('dns', 'DNS resolve error', { error: error.message }); - ctx.errorResponse(res, 500, ctx.safeErrorMessage(error)); + log.error('dns', 'DNS resolve error', { error: error.message }); + // Error handled by middleware } }, 'dns-resolve')); // GET /logs — Fetch DNS query logs from Technitium - router.get('/logs', ctx.asyncHandler(async (req, res) => { + router.get('/logs', asyncHandler(async (req, res) => { const { server, limit } = req.query; if (!server) { - return ctx.errorResponse(res, 400, 'server is required'); + throw new ValidationError('server is required'); } // Validate server against configured DNS servers const serverIp = validateDnsServer(server); if (!serverIp) { - return ctx.errorResponse(res, 400, 'Server must be a configured DNS server'); + throw new ValidationError('Server must be a configured DNS server'); } const logLimit = Math.min(parseInt(limit) || 25, 1000); - const dnsPort = ctx.siteConfig.dnsServerPort || '5380'; + const dnsPort = siteConfig.dnsServerPort || '5380'; try { // Auto-authenticate using stored read-only credentials for log access - const authResult = await ctx.dns.getTokenForServer(serverIp, 'readonly'); + const authResult = await dns.getTokenForServer(serverIp, 'readonly'); if (!authResult.success) { - return ctx.errorResponse(res, 401, 'DNS auto-authentication failed. Ensure credentials are configured via the DNS panel.'); + throw new AuthenticationError('DNS auto-authentication failed. Ensure credentials are configured via the DNS panel.'); } const effectiveToken = authResult.token; // Try to get available log files first const listUrl = `http://${serverIp}:${dnsPort}/api/logs/list?token=${encodeURIComponent(effectiveToken)}`; - const listResponse = await ctx.fetchT(listUrl, { method: 'GET', headers: { 'Accept': 'application/json' } }); + const listResponse = await fetchT(listUrl, { method: 'GET', headers: { 'Accept': 'application/json' } }); let logFileName = new Date().toISOString().split('T')[0]; // Default to today @@ -213,9 +235,9 @@ module.exports = function(ctx) { // Technitium logs/download endpoint - returns plain text logs const technitiumUrl = `http://${serverIp}:${dnsPort}/api/logs/download?token=${encodeURIComponent(effectiveToken)}&fileName=${logFileName}`; - ctx.log.info('dns', 'Fetching DNS logs', { server, logFileName }); + log.info('dns', 'Fetching DNS logs', { server, logFileName }); - const response = await ctx.fetchT(technitiumUrl, { + const response = await fetchT(technitiumUrl, { method: 'GET', headers: { 'Accept': 'text/plain' }, timeout: 10000 @@ -227,17 +249,16 @@ module.exports = function(ctx) { try { const errorJson = JSON.parse(errorText); if (errorJson.errorMessage?.includes('Could not find file')) { - return res.json({ - success: true, + return success(res, { server: server, count: 0, logs: [], message: 'No logs available for this server' }); } - return ctx.errorResponse(res, response.status, ctx.safeErrorMessage(errorJson.errorMessage || errorText)); + return errorResponse(res, safeErrorMessage(errorJson.errorMessage || errorText), response.status); } catch { - return ctx.errorResponse(res, response.status, 'DNS server returned an error'); + return errorResponse(res, 'DNS server returned an error', response.status); } } @@ -250,8 +271,7 @@ module.exports = function(ctx) { const errorJson = JSON.parse(logText); if (errorJson.status && errorJson.status !== 'ok') { if (errorJson.errorMessage?.includes('Could not find file')) { - return res.json({ - success: true, + return success(res, { server: server, count: 0, logs: [], @@ -260,9 +280,9 @@ module.exports = function(ctx) { } // Invalidate cached token on auth errors so next request re-authenticates if (errorJson.status === 'invalid-token') { - ctx.dns.invalidateTokenForServer(serverIp); + dns.invalidateTokenForServer(serverIp); } - return ctx.errorResponse(res, 400, ctx.safeErrorMessage(errorJson.errorMessage)); + return errorResponse(res, safeErrorMessage(errorJson.errorMessage), 400); } } catch { /* Not JSON, continue parsing as text */ } } @@ -293,9 +313,8 @@ module.exports = function(ctx) { return { raw: line, parsed: false }; }).reverse(); // Reverse to show most recent first - ctx.log.info('dns', 'Returning DNS log entries', { count: parsedLogs.length, logFileName }); - res.json({ - success: true, + log.info('dns', 'Returning DNS log entries', { count: parsedLogs.length, logFileName }); + success(res, { server: server, logFile: logFileName, count: parsedLogs.length, @@ -303,33 +322,32 @@ module.exports = function(ctx) { }); } catch (error) { - ctx.log.error('dns', 'DNS logs proxy error', { error: error.message }); - ctx.errorResponse(res, 500, ctx.safeErrorMessage(error)); + log.error('dns', 'DNS logs proxy error', { error: error.message }); + // Error handled by middleware } }, 'dns-logs')); // GET /token-status — Check DNS token/credentials status - router.get('/token-status', ctx.asyncHandler(async (req, res) => { - const username = await ctx.credentialManager.retrieve('dns.username'); - const hasCredentials = !!username || await exists(ctx.dns.credentialsFile); - const hasToken = !!ctx.dns.getToken(); + router.get('/token-status', asyncHandler(async (req, res) => { + const username = await credentialManager.retrieve('dns.username'); + const hasCredentials = !!username || await exists(dns.credentialsFile); + const hasToken = !!dns.getToken(); - res.json({ - success: true, + success(res, { hasCredentials, hasToken, - tokenExpiry: ctx.dns.getTokenExpiry(), - isExpired: ctx.dns.getTokenExpiry() ? new Date() > new Date(ctx.dns.getTokenExpiry()) : null + tokenExpiry: dns.getTokenExpiry(), + isExpired: dns.getTokenExpiry() ? new Date() > new Date(dns.getTokenExpiry()) : null }); }, 'dns-token-status')); // POST /credentials — Store DNS credentials (encrypted) // Accepts per-server format: { servers: { dns1: { username, password }, dns2: {...}, dns3: {...} } } // Also accepts legacy format: { username, password, server } - router.post('/credentials', ctx.asyncHandler(async (req, res) => { + router.post('/credentials', asyncHandler(async (req, res) => { const { servers, username, password, server } = req.body; const dangerousChars = [';', '&', '|', '`', '$', '\n', '\r']; - const dnsPort = ctx.siteConfig.dnsServerPort || '5380'; + const dnsPort = siteConfig.dnsServerPort || '5380'; // Per-server format: { servers: { dns1: { readonly: { username, password }, admin: { username, password } }, ... } } if (servers && typeof servers === 'object') { @@ -338,7 +356,7 @@ module.exports = function(ctx) { for (const [dnsId, creds] of Object.entries(servers)) { // Look up server IP from config - const serverInfo = ctx.siteConfig.dnsServers?.[dnsId]; + const serverInfo = siteConfig.dnsServers?.[dnsId]; const serverIp = serverInfo?.ip; if (!serverIp) { results[dnsId] = { success: false, error: `No IP configured for ${dnsId}` }; @@ -363,13 +381,13 @@ module.exports = function(ctx) { // Test credentials by logging in to the target server try { - const testResult = await ctx.dns.refresh(typeCreds.username, typeCreds.password, serverIp); + const testResult = await dns.refresh(typeCreds.username, typeCreds.password, serverIp); if (testResult.success) { - await ctx.credentialManager.store(`dns.${dnsId}.${credType}.username`, typeCreds.username, { type: 'dns', role: credType, server: serverIp }); - await ctx.credentialManager.store(`dns.${dnsId}.${credType}.password`, typeCreds.password, { type: 'dns', role: credType, server: serverIp }); + await credentialManager.store(`dns.${dnsId}.${credType}.username`, typeCreds.username, { type: 'dns', role: credType, server: serverIp }); + await credentialManager.store(`dns.${dnsId}.${credType}.password`, typeCreds.password, { type: 'dns', role: credType, server: serverIp }); savedTypes.push(credType); anySuccess = true; - ctx.log.info('dns', `${credType} credentials saved for ${dnsId}`, { server: serverIp }); + log.info('dns', `${credType} credentials saved for ${dnsId}`, { server: serverIp }); } else { if (!results[dnsId]) { results[dnsId] = { success: false, error: `${credType}: ${testResult.error || 'Login failed'}` }; @@ -400,132 +418,130 @@ module.exports = function(ctx) { // Legacy single-credential format: { username, password, server } if (!username || !password) { - return ctx.errorResponse(res, 400, 'username and password are required'); + throw new ValidationError('username and password are required'); } if (username.length > 100 || password.length > 512) { - return ctx.errorResponse(res, 400, 'Credentials exceed maximum length'); + throw new ValidationError('Credentials exceed maximum length'); } if (dangerousChars.some(char => username.includes(char))) { - return ctx.errorResponse(res, 400, 'Username contains invalid characters'); + throw new ValidationError('Username contains invalid characters'); } if (server && !validateDnsServer(server)) { - return ctx.errorResponse(res, 400, 'Server must be a configured DNS server'); + throw new ValidationError('Server must be a configured DNS server'); } - const testResult = await ctx.dns.refresh(username, password, server || ctx.siteConfig.dnsServerIp); + const testResult = await dns.refresh(username, password, server || siteConfig.dnsServerIp); if (!testResult.success) { - return ctx.errorResponse(res, 401, `Invalid credentials: ${testResult.error}`); + return errorResponse(res, `Invalid credentials: ${testResult.error}`, 401); } - const dnsServer = server || ctx.siteConfig.dnsServerIp; - await ctx.credentialManager.store('dns.username', username, { type: 'dns', server: dnsServer }); - await ctx.credentialManager.store('dns.password', password, { type: 'dns', server: dnsServer }); - await ctx.credentialManager.store('dns.server', dnsServer, { type: 'dns' }); - ctx.log.info('dns', 'DNS credentials saved to credential manager (encrypted)'); + const dnsServer = server || siteConfig.dnsServerIp; + await credentialManager.store('dns.username', username, { type: 'dns', server: dnsServer }); + await credentialManager.store('dns.password', password, { type: 'dns', server: dnsServer }); + await credentialManager.store('dns.server', dnsServer, { type: 'dns' }); + log.info('dns', 'DNS credentials saved to credential manager (encrypted)'); - res.json({ - success: true, + success(res, { message: 'DNS credentials saved and verified (encrypted)', - tokenExpiry: ctx.dns.getTokenExpiry() + tokenExpiry: dns.getTokenExpiry() }); }, 'dns-credentials')); // DELETE /credentials — Delete stored DNS credentials - router.delete('/credentials', ctx.asyncHandler(async (req, res) => { + router.delete('/credentials', asyncHandler(async (req, res) => { // Delete global credentials - await ctx.credentialManager.delete('dns.username'); - await ctx.credentialManager.delete('dns.password'); - await ctx.credentialManager.delete('dns.server'); + await credentialManager.delete('dns.username'); + await credentialManager.delete('dns.password'); + await credentialManager.delete('dns.server'); // Delete per-server credentials (both old flat and new typed format) - for (const dnsId of Object.keys(ctx.siteConfig.dnsServers || {})) { - await ctx.credentialManager.delete(`dns.${dnsId}.username`); - await ctx.credentialManager.delete(`dns.${dnsId}.password`); + for (const dnsId of Object.keys(siteConfig.dnsServers || {})) { + await credentialManager.delete(`dns.${dnsId}.username`); + await credentialManager.delete(`dns.${dnsId}.password`); for (const role of ['readonly', 'admin']) { - await ctx.credentialManager.delete(`dns.${dnsId}.${role}.username`); - await ctx.credentialManager.delete(`dns.${dnsId}.${role}.password`); + await credentialManager.delete(`dns.${dnsId}.${role}.username`); + await credentialManager.delete(`dns.${dnsId}.${role}.password`); } } - if (await exists(ctx.dns.credentialsFile)) { - await fsp.unlink(ctx.dns.credentialsFile); + if (await exists(dns.credentialsFile)) { + await fsp.unlink(dns.credentialsFile); } - ctx.dns.setToken(''); - ctx.dns.setTokenExpiry(null); - ctx.log.info('dns', 'DNS credentials deleted from credential manager'); - res.json({ success: true, message: 'DNS credentials removed' }); + dns.setToken(''); + dns.setTokenExpiry(null); + log.info('dns', 'DNS credentials deleted from credential manager'); + success(res, { message: 'DNS credentials removed' }); }, 'dns-credentials-delete')); // POST /restart/:dnsId — Restart a DNS server (proxied through backend for auth) - router.post('/restart/:dnsId', ctx.asyncHandler(async (req, res) => { + router.post('/restart/:dnsId', asyncHandler(async (req, res) => { const { dnsId } = req.params; - const serverInfo = ctx.siteConfig.dnsServers?.[dnsId]; + const serverInfo = siteConfig.dnsServers?.[dnsId]; if (!serverInfo?.ip) { - return ctx.errorResponse(res, 400, `Unknown DNS server: ${dnsId}`); + return errorResponse(res, `Unknown DNS server: ${dnsId}`, 400); } - const tokenResult = await ctx.dns.getTokenForServer(serverInfo.ip, 'admin'); + const tokenResult = await dns.getTokenForServer(serverInfo.ip, 'admin'); if (!tokenResult.success) { - return ctx.errorResponse(res, 401, 'DNS admin authentication failed. Ensure admin credentials are configured.'); + throw new AuthenticationError('DNS admin authentication failed. Ensure admin credentials are configured.'); } - const dnsPort = ctx.siteConfig.dnsServerPort || '5380'; + const dnsPort = siteConfig.dnsServerPort || '5380'; try { const url = `http://${serverInfo.ip}:${dnsPort}/api/admin/restart?token=${encodeURIComponent(tokenResult.token)}`; - const response = await ctx.fetchT(url, { method: 'POST', timeout: 5000 }); + const response = await fetchT(url, { method: 'POST', timeout: 5000 }); const result = await response.json(); if (result.status === 'ok') { - res.json({ success: true, message: 'Restart initiated' }); + success(res, { message: 'Restart initiated' }); } else { - ctx.errorResponse(res, 500, result.errorMessage || 'Restart failed'); + // Error handled by middleware } } catch (err) { // Connection drop is expected during restart - res.json({ success: true, message: 'Restart initiated (connection closed)' }); + success(res, { message: 'Restart initiated (connection closed)' }); } }, 'dns-restart')); // POST /refresh-token — Force refresh DNS token - router.post('/refresh-token', ctx.asyncHandler(async (req, res) => { - const result = await ctx.dns.ensureToken(); + router.post('/refresh-token', asyncHandler(async (req, res) => { + const result = await dns.ensureToken(); if (result.success) { - res.json({ - success: true, + success(res, { message: 'Token refreshed successfully', - tokenExpiry: ctx.dns.getTokenExpiry() + tokenExpiry: dns.getTokenExpiry() }); } else { - ctx.errorResponse(res, 401, result.error); + errorResponse(res, result.error, 401); } }, 'dns-refresh-token')); // GET /check-update — Check for Technitium DNS server updates - router.get('/check-update', ctx.asyncHandler(async (req, res) => { + router.get('/check-update', asyncHandler(async (req, res) => { try { const { server } = req.query; if (!server) { - return ctx.errorResponse(res, 400, 'Server IP required'); + throw new ValidationError('Server IP required'); } const serverIp = validateDnsServer(server); if (!serverIp) { - return ctx.errorResponse(res, 400, 'Server must be a configured DNS server'); + throw new ValidationError('Server must be a configured DNS server'); } // Authenticate with admin credentials for update check - const tokenResult = await ctx.dns.getTokenForServer(serverIp, 'admin'); + const tokenResult = await dns.getTokenForServer(serverIp, 'admin'); if (!tokenResult.success) { - return ctx.errorResponse(res, 401, 'DNS authentication failed. Ensure credentials are configured.'); + throw new AuthenticationError('DNS authentication failed. Ensure credentials are configured.'); } - const dnsPort = ctx.siteConfig.dnsServerPort || '5380'; + const dnsPort = siteConfig.dnsServerPort || '5380'; const url = `http://${serverIp}:${dnsPort}/api/user/checkForUpdate?token=${encodeURIComponent(tokenResult.token)}`; - ctx.log.info('dns', 'Checking DNS update', { server }); + log.info('dns', 'Checking DNS update', { server }); - const response = await ctx.fetchT(url, { + const response = await fetchT(url, { method: 'GET', headers: { 'Accept': 'application/json', @@ -536,14 +552,13 @@ module.exports = function(ctx) { const text = await response.text(); if (!text || text.trim() === '') { - return ctx.errorResponse(res, 500, 'Empty response from DNS server'); + return // Error handled by middleware } const result = JSON.parse(text); if (result.status === 'ok') { - res.json({ - success: true, + success(res, { updateAvailable: result.response.updateAvailable, currentVersion: result.response.currentVersion, updateVersion: result.response.updateVersion || null, @@ -553,55 +568,54 @@ module.exports = function(ctx) { instructionsLink: result.response.instructionsLink || null }); } else { - ctx.errorResponse(res, 500, result.errorMessage || 'Check failed'); + // Error handled by middleware } } catch (error) { - ctx.log.error('dns', 'DNS update check error', { error: error.message }); - ctx.errorResponse(res, 500, ctx.safeErrorMessage(error)); + log.error('dns', 'DNS update check error', { error: error.message }); + // Error handled by middleware } }, 'dns-check-update')); // POST /update — Update Technitium DNS server // Note: Technitium v14+ has no installUpdate API. This endpoint checks for updates // and returns download info. The frontend handles showing update instructions. - router.post('/update', ctx.asyncHandler(async (req, res) => { + router.post('/update', asyncHandler(async (req, res) => { try { const { server } = req.query; if (!server) { - return ctx.errorResponse(res, 400, 'Server IP required'); + throw new ValidationError('Server IP required'); } const serverIp = validateDnsServer(server); if (!serverIp) { - return ctx.errorResponse(res, 400, 'Server must be a configured DNS server'); + throw new ValidationError('Server must be a configured DNS server'); } // Authenticate with admin credentials for update operations - const tokenResult = await ctx.dns.getTokenForServer(serverIp, 'admin'); + const tokenResult = await dns.getTokenForServer(serverIp, 'admin'); if (!tokenResult.success) { - return ctx.errorResponse(res, 401, 'DNS authentication failed. Ensure credentials are configured.'); + throw new AuthenticationError('DNS authentication failed. Ensure credentials are configured.'); } - const dnsPort = ctx.siteConfig.dnsServerPort || '5380'; + const dnsPort = siteConfig.dnsServerPort || '5380'; // Check if update is available - const checkResponse = await ctx.fetchT( + const checkResponse = await fetchT( `http://${serverIp}:${dnsPort}/api/user/checkForUpdate?token=${encodeURIComponent(tokenResult.token)}`, { method: 'GET', headers: { 'Accept': 'application/json' } } ); const checkText = await checkResponse.text(); if (!checkText || checkText.trim() === '') { - return ctx.errorResponse(res, 500, 'Empty response from DNS server during check'); + return // Error handled by middleware } const checkResult = JSON.parse(checkText); if (checkResult.status !== 'ok') { - return ctx.errorResponse(res, 500, checkResult.errorMessage || 'Update check failed'); + return // Error handled by middleware } if (!checkResult.response.updateAvailable) { - return res.json({ - success: true, + return success(res, { message: 'Already up to date', currentVersion: checkResult.response.currentVersion, updated: false @@ -610,10 +624,9 @@ module.exports = function(ctx) { // Technitium v14+ does not have an installUpdate API endpoint. // Return the update info with download link so the frontend can guide the user. - ctx.log.info('dns', 'Update available for DNS server', { server, currentVersion: checkResult.response.currentVersion, updateVersion: checkResult.response.updateVersion }); + log.info('dns', 'Update available for DNS server', { server, currentVersion: checkResult.response.currentVersion, updateVersion: checkResult.response.updateVersion }); - res.json({ - success: true, + success(res, { message: `Update available: ${checkResult.response.updateVersion}`, previousVersion: checkResult.response.currentVersion, newVersion: checkResult.response.updateVersion, @@ -623,8 +636,8 @@ module.exports = function(ctx) { manualUpdateRequired: true }); } catch (error) { - ctx.log.error('dns', 'DNS update error', { error: error.message }); - ctx.errorResponse(res, 500, ctx.safeErrorMessage(error)); + log.error('dns', 'DNS update error', { error: error.message }); + // Error handled by middleware } }, 'dns-update')); diff --git a/dashcaddy-api/routes/errorlogs.js b/dashcaddy-api/routes/errorlogs.js index fe4ebcc..d9454ab 100644 --- a/dashcaddy-api/routes/errorlogs.js +++ b/dashcaddy-api/routes/errorlogs.js @@ -3,17 +3,26 @@ const fs = require('fs'); const fsp = require('fs').promises; const { exists } = require('../fs-helpers'); const { paginate, parsePaginationParams } = require('../pagination'); +const { success } = require('../response-helpers'); -module.exports = function(ctx) { +/** + * Error logs routes factory + * @param {Object} deps - Explicit dependencies + * @param {string} deps.ERROR_LOG_FILE - Path to error log file + * @param {Object} deps.auditLogger - Audit logger instance + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @returns {express.Router} + */ +module.exports = function({ ERROR_LOG_FILE, auditLogger, asyncHandler }) { const router = express.Router(); // Get error logs - router.get('/error-logs', ctx.asyncHandler(async (req, res) => { - if (!await exists(ctx.ERROR_LOG_FILE)) { - return res.json({ success: true, logs: [] }); + router.get('/error-logs', asyncHandler(async (req, res) => { + if (!await exists(ERROR_LOG_FILE)) { + return success(res, { logs: [] }); } - const logContent = await fsp.readFile(ctx.ERROR_LOG_FILE, 'utf8'); + const logContent = await fsp.readFile(ERROR_LOG_FILE, 'utf8'); const logEntries = logContent.split('='.repeat(80)).filter(entry => entry.trim()); const logs = logEntries.map(entry => { @@ -31,37 +40,37 @@ module.exports = function(ctx) { return null; }).filter(Boolean); - res.json({ success: true, logs: logs.slice(-50).reverse() }); + success(res, { logs: logs.slice(-50).reverse() }); }, 'error-logs-get')); // Clear error logs - router.delete('/error-logs', ctx.asyncHandler(async (req, res) => { - if (await exists(ctx.ERROR_LOG_FILE)) { - await fsp.writeFile(ctx.ERROR_LOG_FILE, ''); + router.delete('/error-logs', asyncHandler(async (req, res) => { + if (await exists(ERROR_LOG_FILE)) { + await fsp.writeFile(ERROR_LOG_FILE, ''); } - res.json({ success: true, message: 'Error logs cleared' }); + success(res, { message: 'Error logs cleared' }); }, 'error-logs-clear')); // Audit log - router.get('/audit-logs', ctx.asyncHandler(async (req, res) => { + router.get('/audit-logs', asyncHandler(async (req, res) => { const paginationParams = parsePaginationParams(req.query); const action = req.query.action || ''; if (paginationParams) { // When paginating, fetch all matching entries and let pagination slice - const entries = await ctx.auditLogger.query({ limit: Number.MAX_SAFE_INTEGER, offset: 0, action }); + const entries = await auditLogger.query({ limit: Number.MAX_SAFE_INTEGER, offset: 0, action }); const result = paginate(entries, paginationParams); - res.json({ success: true, entries: result.data, pagination: result.pagination }); + success(res, { entries: result.data, pagination: result.pagination }); } else { const limit = parseInt(req.query.limit) || 50; const offset = parseInt(req.query.offset) || 0; - const entries = await ctx.auditLogger.query({ limit, offset, action }); - res.json({ success: true, entries }); + const entries = await auditLogger.query({ limit, offset, action }); + success(res, { entries }); } }, 'audit-log')); - router.delete('/audit-logs', ctx.asyncHandler(async (req, res) => { - await ctx.auditLogger.clear(); - res.json({ success: true, message: 'Audit log cleared' }); + router.delete('/audit-logs', asyncHandler(async (req, res) => { + await auditLogger.clear(); + success(res, { message: 'Audit log cleared' }); }, 'audit-log-clear')); return router; diff --git a/dashcaddy-api/routes/health.js b/dashcaddy-api/routes/health.js index 9f34fe2..fb8c82e 100644 --- a/dashcaddy-api/routes/health.js +++ b/dashcaddy-api/routes/health.js @@ -7,8 +7,31 @@ const { exists } = require('../fs-helpers'); const { paginate, parsePaginationParams } = require('../pagination'); const platformPaths = require('../platform-paths'); const { resolveServiceUrl } = require('../url-resolver'); +const { success, error: errorResponse } = require('../response-helpers'); -module.exports = function(ctx) { +/** + * Health routes factory + * @param {Object} deps - Explicit dependencies + * @param {Function} deps.fetchT - Fetch wrapper with timeout + * @param {string} deps.SERVICES_FILE - Path to services.json + * @param {Object} deps.servicesStateManager - State manager for services.json + * @param {Object} deps.siteConfig - Site configuration + * @param {Function} deps.buildServiceUrl - URL builder function + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.logError - Error logging function + * @param {Object} deps.healthChecker - Health check manager instance + * @returns {express.Router} + */ +module.exports = function({ + fetchT, + SERVICES_FILE, + servicesStateManager, + siteConfig, + buildServiceUrl, + asyncHandler, + logError, + healthChecker +}) { const router = express.Router(); // In-memory cache for health results (local to this router) @@ -23,7 +46,7 @@ module.exports = function(ctx) { try { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 5000); - const response = await ctx.fetchT(url, { method: 'HEAD', signal: controller.signal, redirect: 'follow' }); + const response = await fetchT(url, { method: 'HEAD', signal: controller.signal, redirect: 'follow' }); clearTimeout(timeout); return { status: response.ok || response.status < 500 ? 'healthy' : 'unhealthy', @@ -37,7 +60,7 @@ module.exports = function(ctx) { try { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 5000); - const response = await ctx.fetchT(url, { method: 'GET', signal: controller.signal, redirect: 'follow' }); + const response = await fetchT(url, { method: 'GET', signal: controller.signal, redirect: 'follow' }); clearTimeout(timeout); return { status: response.ok || response.status < 500 ? 'healthy' : 'unhealthy', @@ -61,7 +84,7 @@ module.exports = function(ctx) { if (pylonConfig.key) headers['x-pylon-key'] = pylonConfig.key; const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 12000); - const response = await ctx.fetchT(probeUrl, { method: 'GET', signal: controller.signal, headers }); + const response = await fetchT(probeUrl, { method: 'GET', signal: controller.signal, headers }); clearTimeout(timeout); if (!response.ok) return null; const data = await response.json(); @@ -82,15 +105,15 @@ module.exports = function(ctx) { // ===== HEALTH / SERVICES ===== // Check health of all services (performs live checks) - router.get('/health/services', ctx.asyncHandler(async (req, res) => { - if (!await exists(ctx.SERVICES_FILE)) { - return res.json({ success: true, health: {} }); + router.get('/health/services', asyncHandler(async (req, res) => { + if (!await exists(SERVICES_FILE)) { + return success(res, { health: {} }); } - const servicesData = await ctx.servicesStateManager.read(); + const servicesData = await servicesStateManager.read(); const services = Array.isArray(servicesData) ? servicesData : servicesData.services || []; const health = {}; - const pylonConfig = ctx.siteConfig?.pylon; + const pylonConfig = siteConfig?.pylon; // Check each service await Promise.all(services.map(async (service) => { @@ -98,7 +121,7 @@ module.exports = function(ctx) { if (!serviceId) return; try { - const url = resolveServiceUrl(serviceId, service, ctx.siteConfig, ctx.buildServiceUrl); + const url = resolveServiceUrl(serviceId, service, siteConfig, buildServiceUrl); if (!url) { health[serviceId] = { status: 'unknown', reason: 'No URL configured' }; return; @@ -144,8 +167,7 @@ module.exports = function(ctx) { const healthEntries = Object.entries(health); const result = paginate(healthEntries, paginationParams); const paginatedHealth = Object.fromEntries(result.data); - res.json({ - success: true, + success(res, { health: paginatedHealth, checkedAt: lastHealthCheck, ...(result.pagination && { pagination: result.pagination }) @@ -153,9 +175,8 @@ module.exports = function(ctx) { }, 'health-services')); // Get cached health status (fast, no re-check) - router.get('/health/cached', ctx.asyncHandler(async (req, res) => { - res.json({ - success: true, + router.get('/health/cached', asyncHandler(async (req, res) => { + success(res, { health: serviceHealthCache, lastCheck: lastHealthCheck, cacheAge: lastHealthCheck ? Date.now() - new Date(lastHealthCheck).getTime() : null @@ -163,16 +184,16 @@ module.exports = function(ctx) { }, 'health-cached')); // Check health of single service - router.get('/health/service/:id', ctx.asyncHandler(async (req, res) => { + router.get('/health/service/:id', asyncHandler(async (req, res) => { const serviceId = req.params.id; // Load service config - if (!await exists(ctx.SERVICES_FILE)) { + if (!await exists(SERVICES_FILE)) { const { NotFoundError } = require('../errors'); throw new NotFoundError('Services file'); } - const servicesData = await ctx.servicesStateManager.read(); + const servicesData = await servicesStateManager.read(); const services = Array.isArray(servicesData) ? servicesData : servicesData.services || []; const service = services.find(s => (s.id || s.name?.toLowerCase()) === serviceId); @@ -182,8 +203,8 @@ module.exports = function(ctx) { } // Determine URL - const url = resolveServiceUrl(serviceId, service, ctx.siteConfig, ctx.buildServiceUrl); - const pylonConfig = ctx.siteConfig?.pylon; + const url = resolveServiceUrl(serviceId, service, siteConfig, buildServiceUrl); + const pylonConfig = siteConfig?.pylon; // Try direct, then pylon relay let result = await checkDirect(url); @@ -199,16 +220,16 @@ module.exports = function(ctx) { }; } - res.json({ success: true, serviceId, health: result }); + success(res, { serviceId, health: result }); }, 'health-service')); // ===== HEALTH / PROBE (Pylon-compatible) ===== // Probe endpoint — lets this DashCaddy act as a pylon for other instances - router.get('/health/probe', ctx.asyncHandler(async (req, res) => { + router.get('/health/probe', asyncHandler(async (req, res) => { const targetUrl = req.query.url; if (!targetUrl) { - return ctx.errorResponse(res, 400, 'Missing ?url= parameter'); + throw new ValidationError('Missing ?url= parameter'); } const result = await checkDirect(targetUrl); res.json(result || { @@ -220,29 +241,29 @@ module.exports = function(ctx) { }, 'health-probe')); // Pylon status — check if the configured pylon is reachable - router.get('/health/pylon', ctx.asyncHandler(async (req, res) => { - const pylonConfig = ctx.siteConfig?.pylon; + router.get('/health/pylon', asyncHandler(async (req, res) => { + const pylonConfig = siteConfig?.pylon; if (!pylonConfig?.url) { - return res.json({ success: true, configured: false }); + return success(res, { configured: false }); } try { const headers = {}; if (pylonConfig.key) headers['x-pylon-key'] = pylonConfig.key; - const response = await ctx.fetchT(`${pylonConfig.url}/health`, { + const response = await fetchT(`${pylonConfig.url}/health`, { method: 'GET', headers }, 5000); const data = await response.json(); - res.json({ success: true, configured: true, reachable: true, pylon: data }); + success(res, { configured: true, reachable: true, pylon: data }); } catch (e) { - res.json({ success: true, configured: true, reachable: false, error: e.message }); + success(res, { configured: true, reachable: false, error: e.message }); } }, 'health-pylon')); // ===== HEALTH / CA ===== // Get CA certificate health status - router.get('/health/ca', ctx.asyncHandler(async (req, res) => { + router.get('/health/ca', asyncHandler(async (req, res) => { // Try deployed location first, then Caddy PKI location const deployedCertPath = path.join(platformPaths.caCertDir, 'root.crt'); const pkiCertPath = platformPaths.pkiRootCert; @@ -288,7 +309,7 @@ module.exports = function(ctx) { expiresAt: notAfter }); } catch (error) { - await ctx.logError('GET /api/health/ca', error); + await logError('GET /api/health/ca', error); res.json({ status: 'error', message: error.message, @@ -300,50 +321,50 @@ module.exports = function(ctx) { // ===== HEALTH CHECK (health-checker module) ===== // Get current status for all services - router.get('/health-checks/status', ctx.asyncHandler(async (req, res) => { - const status = ctx.healthChecker.getCurrentStatus(); - res.json({ success: true, status }); + router.get('/health-checks/status', asyncHandler(async (req, res) => { + const status = healthChecker.getCurrentStatus(); + success(res, { status }); }, 'health-check-status')); // Get service statistics - router.get('/health-checks/:serviceId/stats', ctx.asyncHandler(async (req, res) => { + router.get('/health-checks/:serviceId/stats', asyncHandler(async (req, res) => { const hours = parseInt(req.query.hours) || 24; - const stats = ctx.healthChecker.getServiceStats(req.params.serviceId, hours); + const stats = healthChecker.getServiceStats(req.params.serviceId, hours); if (!stats) { const { NotFoundError } = require('../errors'); throw new NotFoundError('Service'); } - res.json({ success: true, stats }); + success(res, { stats }); }, 'health-check-stats')); // Configure health check - router.post('/health-checks/:serviceId/configure', ctx.asyncHandler(async (req, res) => { - ctx.healthChecker.configureService(req.params.serviceId, req.body); - res.json({ success: true, message: 'Health check configured' }); + router.post('/health-checks/:serviceId/configure', asyncHandler(async (req, res) => { + healthChecker.configureService(req.params.serviceId, req.body); + success(res, { message: 'Health check configured' }); }, 'health-check-configure')); // Remove health check configuration - router.delete('/health-checks/:serviceId/configure', ctx.asyncHandler(async (req, res) => { - ctx.healthChecker.removeService(req.params.serviceId); - res.json({ success: true, message: 'Health check removed' }); + router.delete('/health-checks/:serviceId/configure', asyncHandler(async (req, res) => { + healthChecker.removeService(req.params.serviceId); + success(res, { message: 'Health check removed' }); }, 'health-check-remove')); // Get open incidents - router.get('/health-checks/incidents', ctx.asyncHandler(async (req, res) => { - const incidents = ctx.healthChecker.getOpenIncidents(); + router.get('/health-checks/incidents', asyncHandler(async (req, res) => { + const incidents = healthChecker.getOpenIncidents(); const paginationParams = parsePaginationParams(req.query); const result = paginate(incidents, paginationParams); - res.json({ success: true, incidents: result.data, ...(result.pagination && { pagination: result.pagination }) }); + success(res, { incidents: result.data, ...(result.pagination && { pagination: result.pagination }) }); }, 'health-check-incidents')); // Get incident history - router.get('/health-checks/incidents/history', ctx.asyncHandler(async (req, res) => { + router.get('/health-checks/incidents/history', asyncHandler(async (req, res) => { const paginationParams = parsePaginationParams(req.query); // When paginating, fetch all history so pagination can slice correctly const fetchLimit = paginationParams ? Number.MAX_SAFE_INTEGER : (parseInt(req.query.limit) || 50); - const history = ctx.healthChecker.getIncidentHistory(fetchLimit); + const history = healthChecker.getIncidentHistory(fetchLimit); const result = paginate(history, paginationParams); - res.json({ success: true, history: result.data, ...(result.pagination && { pagination: result.pagination }) }); + success(res, { history: result.data, ...(result.pagination && { pagination: result.pagination }) }); }, 'health-check-incidents-history')); return router; diff --git a/dashcaddy-api/routes/license.js b/dashcaddy-api/routes/license.js index 11656f2..18b716a 100644 --- a/dashcaddy-api/routes/license.js +++ b/dashcaddy-api/routes/license.js @@ -1,53 +1,60 @@ const express = require('express'); +const { success, error: errorResponse } = require('../response-helpers'); +const { ValidationError } = require('../errors'); -module.exports = function(ctx) { +/** + * License routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.licenseManager - License management module + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @returns {express.Router} + */ +module.exports = function({ licenseManager, asyncHandler }) { const router = express.Router(); // Activate a license code - router.post('/activate', ctx.asyncHandler(async (req, res) => { + router.post('/activate', asyncHandler(async (req, res) => { const { code } = req.body; if (!code) { - return ctx.errorResponse(res, 400, 'License code is required'); + throw new ValidationError('License code is required'); } - const result = await ctx.licenseManager.activate(code); + const result = await licenseManager.activate(code); if (result.success) { - res.json({ - success: true, + success(res, { message: result.message, license: result.activation }); } else { - ctx.errorResponse(res, 400, result.message); + errorResponse(res, result.message, 400); } }, 'license-activate')); // Get current license status - router.get('/status', ctx.asyncHandler(async (req, res) => { - const status = ctx.licenseManager.getStatus(); - res.json({ success: true, license: status }); + router.get('/status', asyncHandler(async (req, res) => { + const status = licenseManager.getStatus(); + success(res, { license: status }); }, 'license-status')); // Deactivate current license - router.post('/deactivate', ctx.asyncHandler(async (req, res) => { - const result = await ctx.licenseManager.deactivate(); + router.post('/deactivate', asyncHandler(async (req, res) => { + const result = await licenseManager.deactivate(); if (result.success) { - res.json({ success: true, message: result.message }); + success(res, { message: result.message }); } else { - ctx.errorResponse(res, 400, result.message); + errorResponse(res, result.message, 400); } }, 'license-deactivate')); // Check if a specific feature is available (lightweight check for frontend) - router.get('/feature/:feature', ctx.asyncHandler(async (req, res) => { + router.get('/feature/:feature', asyncHandler(async (req, res) => { const { feature } = req.params; - const available = ctx.licenseManager.hasFeature(feature); - const status = ctx.licenseManager.getStatus(); + const available = licenseManager.hasFeature(feature); + const status = licenseManager.getStatus(); - res.json({ - success: true, + success(res, { feature, available, tier: status.tier, diff --git a/dashcaddy-api/routes/logs.js b/dashcaddy-api/routes/logs.js index e59e13d..8668b6c 100644 --- a/dashcaddy-api/routes/logs.js +++ b/dashcaddy-api/routes/logs.js @@ -4,13 +4,23 @@ const fsp = require('fs').promises; const path = require('path'); const { exists } = require('../fs-helpers'); const { paginate, parsePaginationParams } = require('../pagination'); +const { NotFoundError } = require('../errors'); -module.exports = function(ctx) { +/** + * Logs route factory + * @param {Object} deps - Explicit dependencies + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.docker - Docker client + * @param {Object} deps.logDigest - Log digest manager (optional) + * @param {Object} deps.dockerMaintenance - Docker maintenance module (optional) + * @returns {express.Router} + */ +module.exports = function({ asyncHandler, docker, logDigest, dockerMaintenance }) { const router = express.Router(); // List containers with logs - router.get('/logs/containers', ctx.asyncHandler(async (req, res) => { - const containers = await ctx.docker.client.listContainers({ all: true }); + router.get('/logs/containers', asyncHandler(async (req, res) => { + const containers = await docker.client.listContainers({ all: true }); const containerList = containers.map(c => ({ id: c.Id.slice(0, 12), name: c.Names[0]?.replace(/^\//, '') || 'unknown', @@ -25,13 +35,13 @@ module.exports = function(ctx) { }, 'logs-containers')); // Get logs for a specific container - router.get('/logs/container/:id', ctx.asyncHandler(async (req, res) => { + router.get('/logs/container/:id', asyncHandler(async (req, res) => { const containerId = req.params.id; const tail = parseInt(req.query.tail) || 100; const since = req.query.since || 0; const timestamps = req.query.timestamps !== 'false'; - const container = ctx.docker.client.getContainer(containerId); + const container = docker.client.getContainer(containerId); let info; try { info = await container.inspect(); @@ -80,9 +90,9 @@ module.exports = function(ctx) { }, 'logs-container')); // Stream logs (SSE) - router.get('/logs/stream/:id', ctx.asyncHandler(async (req, res) => { + router.get('/logs/stream/:id', asyncHandler(async (req, res) => { const containerId = req.params.id; - const container = ctx.docker.client.getContainer(containerId); + const container = docker.client.getContainer(containerId); try { await container.inspect(); } catch (err) { @@ -129,7 +139,7 @@ module.exports = function(ctx) { }); logStream.on('error', (err) => { - res.write(`data: ${JSON.stringify({ error: ctx.safeErrorMessage(err) })}\n\n`); + res.write(`data: ${JSON.stringify({ error: err.message || String(err) })}\n\n`); res.end(); }); @@ -139,9 +149,9 @@ module.exports = function(ctx) { }, 'logs-stream')); // Get latest daily digest - router.get('/logs/digest/latest', ctx.asyncHandler(async (req, res) => { - if (!ctx.logDigest) return ctx.errorResponse(res, 503, 'Log digest not available'); - const digest = await ctx.logDigest.getLatestDigest(); + router.get('/logs/digest/latest', asyncHandler(async (req, res) => { + if (!logDigest) throw new Error('Log digest not available'); + const digest = await logDigest.getLatestDigest(); if (!digest) { return res.json({ success: true, digest: null, message: 'No digest available yet. First digest is generated at midnight.' }); } @@ -149,67 +159,67 @@ module.exports = function(ctx) { }, 'logs-digest-latest')); // Get live digest data (today's accumulated stats) - router.get('/logs/digest/live', ctx.asyncHandler(async (req, res) => { - if (!ctx.logDigest) return ctx.errorResponse(res, 503, 'Log digest not available'); - const live = ctx.logDigest.getLiveData(); + router.get('/logs/digest/live', asyncHandler(async (req, res) => { + if (!logDigest) throw new Error('Log digest not available'); + const live = logDigest.getLiveData(); res.json({ success: true, ...live }); }, 'logs-digest-live')); // List available digest dates - router.get('/logs/digest/history', ctx.asyncHandler(async (req, res) => { - if (!ctx.logDigest) return ctx.errorResponse(res, 503, 'Log digest not available'); - const dates = await ctx.logDigest.listDigests(); + router.get('/logs/digest/history', asyncHandler(async (req, res) => { + if (!logDigest) throw new Error('Log digest not available'); + const dates = await logDigest.listDigests(); res.json({ success: true, dates }); }, 'logs-digest-history')); // Generate digest on demand (for today or a specific date) - router.post('/logs/digest/generate', ctx.asyncHandler(async (req, res) => { - if (!ctx.logDigest) return ctx.errorResponse(res, 503, 'Log digest not available'); + router.post('/logs/digest/generate', asyncHandler(async (req, res) => { + if (!logDigest) throw new Error('Log digest not available'); const date = req.body.date || new Date().toISOString().slice(0, 10); - const digest = await ctx.logDigest.generateDailyDigest(date); + const digest = await logDigest.generateDailyDigest(date); res.json({ success: true, digest }); }, 'logs-digest-generate')); // Get digest for a specific date (JSON) - router.get('/logs/digest/:date', ctx.asyncHandler(async (req, res) => { - if (!ctx.logDigest) return ctx.errorResponse(res, 503, 'Log digest not available'); + router.get('/logs/digest/:date', asyncHandler(async (req, res) => { + if (!logDigest) throw new Error('Log digest not available'); const { date } = req.params; if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) { - return ctx.errorResponse(res, 400, 'Invalid date format. Use YYYY-MM-DD.'); + throw new ValidationError('Invalid date format. Use YYYY-MM-DD.'); } const format = req.query.format || 'json'; if (format === 'text') { - const text = await ctx.logDigest.getDigestText(date); - if (!text) return ctx.errorResponse(res, 404, `No digest found for ${date}`); + const text = await logDigest.getDigestText(date); + if (!text) throw new NotFoundError(`Digest for ${date}`); res.setHeader('Content-Type', 'text/plain'); return res.send(text); } - const digest = await ctx.logDigest.getDigestByDate(date); - if (!digest) return ctx.errorResponse(res, 404, `No digest found for ${date}`); + const digest = await logDigest.getDigestByDate(date); + if (!digest) throw new NotFoundError(`Digest for ${date}`); res.json({ success: true, digest }); }, 'logs-digest-date')); // Get Docker disk usage snapshot - router.get('/logs/docker-disk', ctx.asyncHandler(async (req, res) => { - if (!ctx.dockerMaintenance) return ctx.errorResponse(res, 503, 'Docker maintenance not available'); - const diskUsage = await ctx.dockerMaintenance.getDiskUsage(); - const status = ctx.dockerMaintenance.getStatus(); + router.get('/logs/docker-disk', asyncHandler(async (req, res) => { + if (!dockerMaintenance) throw new Error('Docker maintenance not available'); + const diskUsage = await dockerMaintenance.getDiskUsage(); + const status = dockerMaintenance.getStatus(); res.json({ success: true, diskUsage, maintenance: status }); }, 'logs-docker-disk')); // Trigger Docker maintenance manually - router.post('/logs/docker-maintenance', ctx.asyncHandler(async (req, res) => { - if (!ctx.dockerMaintenance) return ctx.errorResponse(res, 503, 'Docker maintenance not available'); - const result = await ctx.dockerMaintenance.runMaintenance(); + router.post('/logs/docker-maintenance', asyncHandler(async (req, res) => { + if (!dockerMaintenance) throw new Error('Docker maintenance not available'); + const result = await dockerMaintenance.runMaintenance(); res.json({ success: true, result }); }, 'logs-docker-maintenance')); // Get logs from a file path (for native applications) - router.get('/logs/file', ctx.asyncHandler(async (req, res) => { + router.get('/logs/file', asyncHandler(async (req, res) => { const { path: logPath, tail = 100 } = req.query; if (!logPath) { - return ctx.errorResponse(res, 400, 'Log path is required'); + throw new ValidationError('Log path is required'); } const platformPaths = require('../platform-paths'); @@ -233,7 +243,7 @@ module.exports = function(ctx) { }); if (!isAllowed) { - return ctx.errorResponse(res, 403, 'Access to this log path is not allowed'); + throw new ForbiddenError('Access to this log path is not allowed'); } if (!await exists(resolvedPath)) { diff --git a/dashcaddy-api/routes/monitoring.js b/dashcaddy-api/routes/monitoring.js index 699624b..d1c60c0 100644 --- a/dashcaddy-api/routes/monitoring.js +++ b/dashcaddy-api/routes/monitoring.js @@ -1,72 +1,81 @@ const express = require('express'); +const { success } = require('../response-helpers'); -module.exports = function(ctx) { +/** + * Monitoring routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.resourceMonitor - Resource monitoring manager + * @param {Object} deps.docker - Docker client wrapper + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @returns {express.Router} + */ +module.exports = function({ resourceMonitor, docker, asyncHandler }) { const router = express.Router(); // ===== RESOURCE MONITORING ENDPOINTS ===== // Get all container stats (from resource monitor module) - router.get('/monitoring/stats', ctx.asyncHandler(async (req, res) => { - const stats = ctx.resourceMonitor.getAllStats(); - res.json({ success: true, stats }); + router.get('/monitoring/stats', asyncHandler(async (req, res) => { + const stats = resourceMonitor.getAllStats(); + success(res, { stats }); }, 'monitoring-stats')); // Get stats for specific container - router.get('/monitoring/stats/:containerId', ctx.asyncHandler(async (req, res) => { - const stats = ctx.resourceMonitor.getCurrentStats(req.params.containerId); + router.get('/monitoring/stats/:containerId', asyncHandler(async (req, res) => { + const stats = resourceMonitor.getCurrentStats(req.params.containerId); if (!stats) { const { NotFoundError } = require('../errors'); throw new NotFoundError('Container'); } - res.json({ success: true, stats }); + success(res, { stats }); }, 'monitoring-stats-container')); // Get historical stats - router.get('/monitoring/history/:containerId', ctx.asyncHandler(async (req, res) => { + router.get('/monitoring/history/:containerId', asyncHandler(async (req, res) => { const hours = parseInt(req.query.hours) || 24; - const history = ctx.resourceMonitor.getHistoricalStats(req.params.containerId, hours); - res.json({ success: true, history, hours }); + const history = resourceMonitor.getHistoricalStats(req.params.containerId, hours); + success(res, { history, hours }); }, 'monitoring-history')); // Get aggregated stats - router.get('/monitoring/aggregated/:containerId', ctx.asyncHandler(async (req, res) => { + router.get('/monitoring/aggregated/:containerId', asyncHandler(async (req, res) => { const hours = parseInt(req.query.hours) || 24; - const aggregated = ctx.resourceMonitor.getAggregatedStats(req.params.containerId, hours); + const aggregated = resourceMonitor.getAggregatedStats(req.params.containerId, hours); if (!aggregated) { const { NotFoundError } = require('../errors'); throw new NotFoundError('Monitoring data'); } - res.json({ success: true, aggregated, hours }); + success(res, { aggregated, hours }); }, 'monitoring-aggregated')); // Configure alerts - router.post('/monitoring/alerts/:containerId', ctx.asyncHandler(async (req, res) => { - ctx.resourceMonitor.setAlertConfig(req.params.containerId, req.body); - res.json({ success: true, message: 'Alert configuration saved' }); + router.post('/monitoring/alerts/:containerId', asyncHandler(async (req, res) => { + resourceMonitor.setAlertConfig(req.params.containerId, req.body); + success(res, { message: 'Alert configuration saved' }); }, 'monitoring-alerts-set')); // Get alert configuration - router.get('/monitoring/alerts/:containerId', ctx.asyncHandler(async (req, res) => { - const config = ctx.resourceMonitor.getAlertConfig(req.params.containerId); - res.json({ success: true, config: config || {} }); + router.get('/monitoring/alerts/:containerId', asyncHandler(async (req, res) => { + const config = resourceMonitor.getAlertConfig(req.params.containerId); + success(res, { config: config || {} }); }, 'monitoring-alerts-get')); // Delete alert configuration - router.delete('/monitoring/alerts/:containerId', ctx.asyncHandler(async (req, res) => { - ctx.resourceMonitor.removeAlertConfig(req.params.containerId); - res.json({ success: true, message: 'Alert configuration removed' }); + router.delete('/monitoring/alerts/:containerId', asyncHandler(async (req, res) => { + resourceMonitor.removeAlertConfig(req.params.containerId); + success(res, { message: 'Alert configuration removed' }); }, 'monitoring-alerts-delete')); // ===== CONTAINER STATS ENDPOINTS (legacy /stats/) ===== // Get all container stats (live Docker stats) - router.get('/stats/containers', ctx.asyncHandler(async (req, res) => { - const containers = await ctx.docker.client.listContainers({ all: false }); + router.get('/stats/containers', asyncHandler(async (req, res) => { + const containers = await docker.client.listContainers({ all: false }); const stats = []; for (const containerInfo of containers) { try { - const container = ctx.docker.client.getContainer(containerInfo.Id); + const container = docker.client.getContainer(containerInfo.Id); const containerStats = await container.stats({ stream: false }); // Calculate CPU percentage @@ -114,12 +123,12 @@ module.exports = function(ctx) { } } - res.json({ success: true, stats, timestamp: new Date().toISOString() }); + success(res, { stats, timestamp: new Date().toISOString() }); }, 'stats-containers')); // Get single container stats - router.get('/stats/container/:id', ctx.asyncHandler(async (req, res) => { - const container = ctx.docker.client.getContainer(req.params.id); + router.get('/stats/container/:id', asyncHandler(async (req, res) => { + const container = docker.client.getContainer(req.params.id); const containerStats = await container.stats({ stream: false }); const info = await container.inspect(); @@ -143,8 +152,7 @@ module.exports = function(ctx) { } } - res.json({ - success: true, + success(res, { stats: { name: info.Name.replace(/^\//, ''), image: info.Config.Image, diff --git a/dashcaddy-api/routes/notifications.js b/dashcaddy-api/routes/notifications.js index d8b77ad..27f1f8e 100644 --- a/dashcaddy-api/routes/notifications.js +++ b/dashcaddy-api/routes/notifications.js @@ -1,13 +1,21 @@ const express = require('express'); const { validateURL, validateToken } = require('../input-validator'); const { paginate, parsePaginationParams } = require('../pagination'); +const { ValidationError } = require('../errors'); -module.exports = function(ctx) { +/** + * Notifications route factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.notification - Notification manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @returns {express.Router} + */ +module.exports = function({ notification, asyncHandler }) { const router = express.Router(); // GET /config — Get notification configuration (sensitive data redacted) - router.get('/config', ctx.asyncHandler(async (req, res) => { - const notificationConfig = ctx.notification.getConfig(); + router.get('/config', asyncHandler(async (req, res) => { + const notificationConfig = notification.getConfig(); // Return config without sensitive data const safeConfig = { enabled: notificationConfig.enabled, @@ -33,9 +41,9 @@ module.exports = function(ctx) { }, 'notifications-config-get')); // POST /config — Update notification configuration - router.post('/config', ctx.asyncHandler(async (req, res) => { + router.post('/config', asyncHandler(async (req, res) => { const { enabled, providers, events, healthCheck } = req.body; - const notificationConfig = ctx.notification.getConfig(); + const notificationConfig = notification.getConfig(); // Validate provider webhook URLs and tokens if (providers) { @@ -43,27 +51,27 @@ module.exports = function(ctx) { try { validateURL(providers.discord.webhookUrl); } catch (validationErr) { - return ctx.errorResponse(res, 400, 'Invalid Discord webhook URL'); + throw new ValidationError('Invalid Discord webhook URL'); } } if (providers.telegram?.botToken) { try { validateToken(providers.telegram.botToken); } catch (validationErr) { - return ctx.errorResponse(res, 400, 'Invalid Telegram bot token format'); + throw new ValidationError('Invalid Telegram bot token format'); } } if (providers.ntfy?.serverUrl) { try { validateURL(providers.ntfy.serverUrl); } catch (validationErr) { - return ctx.errorResponse(res, 400, 'Invalid ntfy server URL'); + throw new ValidationError('Invalid ntfy server URL'); } } if (providers.ntfy?.topic) { const topicRegex = /^[a-zA-Z0-9_-]{1,64}$/; if (!topicRegex.test(providers.ntfy.topic)) { - return ctx.errorResponse(res, 400, 'Invalid ntfy topic (alphanumeric, hyphens, underscores only, max 64 chars)'); + throw new ValidationError('Invalid ntfy topic (alphanumeric, hyphens, underscores only, max 64 chars)'); } } } @@ -108,19 +116,19 @@ module.exports = function(ctx) { // Restart daemon if settings changed if (healthCheck.enabled !== wasEnabled || healthCheck.intervalMinutes) { if (notificationConfig.healthCheck.enabled) { - ctx.notification.startHealthDaemon(); + notification.startHealthDaemon(); } else { - ctx.notification.stopHealthDaemon(); + notification.stopHealthDaemon(); } } } - await ctx.notification.saveConfig(); + await notification.saveConfig(); res.json({ success: true, message: 'Notification config updated' }); }, 'notifications-config-update')); // POST /test — Test notification delivery - router.post('/test', ctx.asyncHandler(async (req, res) => { + router.post('/test', asyncHandler(async (req, res) => { const { provider } = req.body; if (provider) { @@ -128,28 +136,28 @@ module.exports = function(ctx) { let result; switch (provider) { case 'discord': - result = await ctx.notification.sendDiscord('Test Notification', 'This is a test notification from DashCaddy.', 'info'); + result = await notification.sendDiscord('Test Notification', 'This is a test notification from DashCaddy.', 'info'); break; case 'telegram': - result = await ctx.notification.sendTelegram('Test Notification', 'This is a test notification from DashCaddy.', 'info'); + result = await notification.sendTelegram('Test Notification', 'This is a test notification from DashCaddy.', 'info'); break; case 'ntfy': - result = await ctx.notification.sendNtfy('Test Notification', 'This is a test notification from DashCaddy.', 'info'); + result = await notification.sendNtfy('Test Notification', 'This is a test notification from DashCaddy.', 'info'); break; default: - return ctx.errorResponse(res, 400, 'Unknown provider'); + throw new ValidationError('Unknown provider'); } res.json({ success: result.success, provider, error: result.error }); } else { // Test all enabled providers - const result = await ctx.notification.send('test', 'Test Notification', 'This is a test notification from DashCaddy.', 'info'); + const result = await notification.send('test', 'Test Notification', 'This is a test notification from DashCaddy.', 'info'); res.json({ success: true, ...result }); } }, 'notifications-test')); // GET /history — Get notification history - router.get('/history', ctx.asyncHandler(async (req, res) => { - const notificationHistory = ctx.notification.getHistory(); + router.get('/history', asyncHandler(async (req, res) => { + const notificationHistory = notification.getHistory(); const paginationParams = parsePaginationParams(req.query); if (paginationParams) { const result = paginate(notificationHistory, paginationParams); @@ -165,19 +173,19 @@ module.exports = function(ctx) { }, 'notifications-history')); // DELETE /history — Clear notification history - router.delete('/history', ctx.asyncHandler(async (req, res) => { - ctx.notification.clearHistory(); + router.delete('/history', asyncHandler(async (req, res) => { + notification.clearHistory(); res.json({ success: true, message: 'Notification history cleared' }); }, 'notifications-history-clear')); // POST /health-check — Manually trigger health check - router.post('/health-check', ctx.asyncHandler(async (req, res) => { - await ctx.notification.checkHealth(); - const notificationConfig = ctx.notification.getConfig(); + router.post('/health-check', asyncHandler(async (req, res) => { + await notification.checkHealth(); + const notificationConfig = notification.getConfig(); res.json({ success: true, lastCheck: notificationConfig.healthCheck.lastCheck, - containersMonitored: Object.keys(ctx.notification.getHealthState()).length + containersMonitored: Object.keys(notification.getHealthState()).length }); }, 'notifications-health-check')); diff --git a/dashcaddy-api/routes/recipes/deploy.js b/dashcaddy-api/routes/recipes/deploy.js index 2111d90..3a82473 100644 --- a/dashcaddy-api/routes/recipes/deploy.js +++ b/dashcaddy-api/routes/recipes/deploy.js @@ -1,8 +1,20 @@ const express = require('express'); +const { ValidationError } = require('../../errors'); const crypto = require('crypto'); const { DOCKER } = require('../../constants'); -module.exports = function(ctx) { +/** + * Recipes deployment routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.docker - Docker client wrapper + * @param {Object} deps.credentialManager - Credential manager + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.errorResponse - Error response helper + * @param {Object} deps.log - Logger instance + * @returns {express.Router} + */ +module.exports = function({ docker, credentialManager, servicesStateManager, asyncHandler, errorResponse, log }) { const router = express.Router(); /** @@ -11,14 +23,14 @@ module.exports = function(ctx) { * POST /api/recipes/deploy * Body: { recipeId, config: { selectedComponents, sharedConfig, componentOverrides } } */ - router.post('/deploy', ctx.asyncHandler(async (req, res) => { + router.post('/deploy', asyncHandler(async (req, res) => { const { recipeId, config } = req.body; const { RECIPE_TEMPLATES } = require('../../recipe-templates'); const recipe = RECIPE_TEMPLATES[recipeId]; - if (!recipe) return ctx.errorResponse(res, 400, 'Invalid recipe template'); + if (!recipe) throw new ValidationError('Invalid recipe template', 'recipeId'); - ctx.log.info('recipe', 'Starting recipe deployment', { recipeId, name: recipe.name }); + log.info('recipe', 'Starting recipe deployment', { recipeId, name: recipe.name }); // Determine which components to deploy const selectedIds = new Set(config.selectedComponents || recipe.components.filter(c => c.required).map(c => c.id)); @@ -39,18 +51,18 @@ module.exports = function(ctx) { if (recipe.network) { networkName = recipe.network.name; try { - await ctx.docker.client.createNetwork({ + await docker.client.createNetwork({ Name: networkName, Driver: recipe.network.driver || 'bridge', Labels: { 'sami.managed': 'true', 'sami.recipe': recipeId } }); - ctx.log.info('recipe', 'Created Docker network', { networkName }); + log.info('recipe', 'Created Docker network', { networkName }); } catch (e) { // Network might already exist if (!e.message.includes('already exists')) { throw new Error(`Failed to create network ${networkName}: ${e.message}`); } - ctx.log.info('recipe', 'Docker network already exists', { networkName }); + log.info('recipe', 'Docker network already exists', { networkName }); } } @@ -60,7 +72,7 @@ module.exports = function(ctx) { try { for (const component of componentsToDeploy) { try { - ctx.log.info('recipe', `Deploying component: ${component.id}`, { + log.info('recipe', `Deploying component: ${component.id}`, { role: component.role, internal: component.internal || false }); @@ -68,11 +80,11 @@ module.exports = function(ctx) { const result = await deployComponent(component, recipe, config, generatedPasswords, networkName); deployedComponents.push(result); - ctx.log.info('recipe', `Component deployed: ${component.id}`, { + log.info('recipe', `Component deployed: ${component.id}`, { containerId: result.containerId?.substring(0, 12) }); } catch (componentError) { - ctx.log.error('recipe', `Component failed: ${component.id}`, { + log.error('recipe', `Component failed: ${component.id}`, { error: componentError.message }); errors.push({ componentId: component.id, role: component.role, error: componentError.message }); @@ -103,10 +115,10 @@ module.exports = function(ctx) { // Run auto-connect if available if (recipe.autoConnect?.enabled && errors.length === 0) { - ctx.log.info('recipe', 'Running auto-connect for recipe', { recipeId }); + log.info('recipe', 'Running auto-connect for recipe', { recipeId }); // Auto-connect will be handled asynchronously — don't block the response runAutoConnect(recipe, deployedComponents, config).catch(e => { - ctx.log.warn('recipe', 'Auto-connect had errors', { recipeId, error: e.message }); + log.warn('recipe', 'Auto-connect had errors', { recipeId, error: e.message }); }); } @@ -135,17 +147,17 @@ module.exports = function(ctx) { res.json(response); } catch (error) { - ctx.log.error('recipe', 'Recipe deployment failed', { recipeId, error: error.message }); + log.error('recipe', 'Recipe deployment failed', { recipeId, error: error.message }); // Cleanup: remove partially deployed containers for (const deployed of deployedComponents) { try { if (deployed.containerId) { - const container = ctx.docker.client.getContainer(deployed.containerId); + const container = docker.client.getContainer(deployed.containerId); await container.remove({ force: true }); } } catch (cleanupError) { - ctx.log.warn('recipe', 'Cleanup failed for component', { + log.warn('recipe', 'Cleanup failed for component', { componentId: deployed.id, error: cleanupError.message }); } @@ -154,10 +166,10 @@ module.exports = function(ctx) { // Cleanup network if (networkName) { try { - const network = ctx.docker.client.getNetwork(networkName); + const network = docker.client.getNetwork(networkName); await network.remove(); } catch (e) { - ctx.log.warn('recipe', 'Network cleanup failed', { networkName, error: e.message }); + log.warn('recipe', 'Network cleanup failed', { networkName, error: e.message }); } } @@ -165,7 +177,7 @@ module.exports = function(ctx) { `Failed to deploy **${recipe.name}**: ${error.message}`, 'error' ); - ctx.errorResponse(res, 500, error.message); + // Error automatically handled by middleware } }, 'recipe-deploy')); @@ -283,11 +295,11 @@ module.exports = function(ctx) { // Pull image try { - ctx.log.info('recipe', `Pulling image: ${dockerConfig.image}`); - await ctx.docker.pull(dockerConfig.image); + log.info('recipe', `Pulling image: ${dockerConfig.image}`); + await docker.pull(dockerConfig.image); } catch (e) { - ctx.log.warn('recipe', `Pull failed, checking local: ${dockerConfig.image}`); - const images = await ctx.docker.client.listImages({ + log.warn('recipe', `Pull failed, checking local: ${dockerConfig.image}`); + const images = await docker.client.listImages({ filters: { reference: [dockerConfig.image] } }); if (images.length === 0) throw new Error(`Image not found: ${dockerConfig.image}`); @@ -295,7 +307,7 @@ module.exports = function(ctx) { // Remove stale container try { - const existing = ctx.docker.client.getContainer(containerName); + const existing = docker.client.getContainer(containerName); await existing.inspect(); await existing.remove({ force: true }); await new Promise(r => setTimeout(r, 1000)); @@ -304,17 +316,17 @@ module.exports = function(ctx) { } // Create and start container - const container = await ctx.docker.client.createContainer(containerConfig); + const container = await docker.client.createContainer(containerConfig); await container.start(); // Connect to recipe network if (networkName) { try { - const network = ctx.docker.client.getNetwork(networkName); + const network = docker.client.getNetwork(networkName); await network.connect({ Container: container.id }); - ctx.log.info('recipe', `Connected ${component.id} to network ${networkName}`); + log.info('recipe', `Connected ${component.id} to network ${networkName}`); } catch (e) { - ctx.log.warn('recipe', `Failed to connect ${component.id} to network`, { error: e.message }); + log.warn('recipe', `Failed to connect ${component.id} to network`, { error: e.message }); } } @@ -331,7 +343,7 @@ module.exports = function(ctx) { await helpers.addCaddyConfig(subdomain, caddyConfig); url = `https://${ctx.buildDomain(subdomain)}`; } catch (e) { - ctx.log.warn('recipe', `Caddy config failed for ${component.id}`, { error: e.message }); + log.warn('recipe', `Caddy config failed for ${component.id}`, { error: e.message }); } } @@ -359,12 +371,12 @@ module.exports = function(ctx) { for (const step of recipe.autoConnect.steps) { try { - ctx.log.info('recipe', `Auto-connect step: ${step.action}`, { targets: step.targets }); + log.info('recipe', `Auto-connect step: ${step.action}`, { targets: step.targets }); // These actions map to existing Smart Arr Connect functionality // The actual implementation will be wired when Smart Arr Connect helpers are available - ctx.log.info('recipe', `Auto-connect step ${step.action} completed`); + log.info('recipe', `Auto-connect step ${step.action} completed`); } catch (e) { - ctx.log.warn('recipe', `Auto-connect step failed: ${step.action}`, { error: e.message }); + log.warn('recipe', `Auto-connect step failed: ${step.action}`, { error: e.message }); } } } diff --git a/dashcaddy-api/routes/recipes/index.js b/dashcaddy-api/routes/recipes/index.js index ed8b415..5d83f0c 100644 --- a/dashcaddy-api/routes/recipes/index.js +++ b/dashcaddy-api/routes/recipes/index.js @@ -1,15 +1,30 @@ const express = require('express'); const deployRoutes = require('./deploy'); const manageRoutes = require('./manage'); +const { NotFoundError } = require('../../errors'); +/** + * Recipes routes aggregator + * @param {Object} ctx - Application context (for backward compatibility) + * @returns {express.Router} + */ module.exports = function(ctx) { const router = express.Router(); + const deps = { + docker: ctx.docker, + credentialManager: ctx.credentialManager, + servicesStateManager: ctx.servicesStateManager, + asyncHandler: ctx.asyncHandler, + errorResponse: ctx.errorResponse, + log: ctx.log + }; + // All recipe routes require premium license router.use(ctx.licenseManager.requirePremium('recipes')); // GET /api/recipes/templates — list all recipe templates - router.get('/templates', ctx.asyncHandler(async (req, res) => { + router.get('/templates', deps.asyncHandler(async (req, res) => { const { RECIPE_TEMPLATES, RECIPE_CATEGORIES } = require('../../recipe-templates'); const templates = Object.entries(RECIPE_TEMPLATES).map(([id, recipe]) => ({ id, @@ -38,17 +53,17 @@ module.exports = function(ctx) { }, 'recipe-templates')); // GET /api/recipes/templates/:recipeId — get single recipe template detail - router.get('/templates/:recipeId', ctx.asyncHandler(async (req, res) => { + router.get('/templates/:recipeId', deps.asyncHandler(async (req, res) => { const { RECIPE_TEMPLATES } = require('../../recipe-templates'); const recipe = RECIPE_TEMPLATES[req.params.recipeId]; - if (!recipe) return ctx.errorResponse(res, 404, 'Recipe template not found'); + if (!recipe) throw new NotFoundError(`Recipe template ${req.params.recipeId}`); res.json({ success: true, recipe: { id: req.params.recipeId, ...recipe } }); }, 'recipe-template-detail')); // Mount deploy and manage sub-routes - router.use(deployRoutes(ctx)); - router.use(manageRoutes(ctx)); + router.use(deployRoutes(deps)); + router.use(manageRoutes(deps)); return router; }; diff --git a/dashcaddy-api/routes/recipes/manage.js b/dashcaddy-api/routes/recipes/manage.js index 135da68..d946a22 100644 --- a/dashcaddy-api/routes/recipes/manage.js +++ b/dashcaddy-api/routes/recipes/manage.js @@ -1,14 +1,23 @@ const express = require('express'); const { DOCKER } = require('../../constants'); +const { NotFoundError } = require('../../errors'); -module.exports = function(ctx) { +module.exports = function({ servicesStateManager, asyncHandler, log }) { const router = express.Router(); +/** + * Recipes management routes factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.log - Logger instance + * @returns {express.Router} + */ /** * GET /api/recipes/deployed — list all deployed recipes (grouped by recipeId) */ - router.get('/deployed', ctx.asyncHandler(async (req, res) => { - const services = await ctx.servicesStateManager.read(); + router.get('/deployed', asyncHandler(async (req, res) => { + const services = await servicesStateManager.read(); const recipeGroups = {}; for (const service of services) { @@ -63,7 +72,7 @@ module.exports = function(ctx) { }); } } catch (e) { - ctx.log.warn('recipe', 'Could not list Docker containers for recipe discovery', { error: e.message }); + log.warn('recipe', 'Could not list Docker containers for recipe discovery', { error: e.message }); } // Enrich with container state @@ -91,12 +100,12 @@ module.exports = function(ctx) { /** * POST /api/recipes/:recipeId/start — start all containers in a recipe */ - router.post('/:recipeId/start', ctx.asyncHandler(async (req, res) => { + router.post('/:recipeId/start', asyncHandler(async (req, res) => { const { recipeId } = req.params; const containers = await findRecipeContainers(recipeId); if (containers.length === 0) { - return ctx.errorResponse(res, 404, 'No containers found for this recipe'); + throw new NotFoundError('Containers for recipe'); } const results = []; @@ -115,19 +124,19 @@ module.exports = function(ctx) { } } - ctx.log.info('recipe', 'Recipe started', { recipeId, results }); + log.info('recipe', 'Recipe started', { recipeId, results }); res.json({ success: true, recipeId, results }); }, 'recipe-start')); /** * POST /api/recipes/:recipeId/stop — stop all containers in a recipe */ - router.post('/:recipeId/stop', ctx.asyncHandler(async (req, res) => { + router.post('/:recipeId/stop', asyncHandler(async (req, res) => { const { recipeId } = req.params; const containers = await findRecipeContainers(recipeId); if (containers.length === 0) { - return ctx.errorResponse(res, 404, 'No containers found for this recipe'); + throw new NotFoundError('Containers for recipe'); } const results = []; @@ -147,19 +156,19 @@ module.exports = function(ctx) { } } - ctx.log.info('recipe', 'Recipe stopped', { recipeId, results }); + log.info('recipe', 'Recipe stopped', { recipeId, results }); res.json({ success: true, recipeId, results }); }, 'recipe-stop')); /** * POST /api/recipes/:recipeId/restart — restart all containers in a recipe */ - router.post('/:recipeId/restart', ctx.asyncHandler(async (req, res) => { + router.post('/:recipeId/restart', asyncHandler(async (req, res) => { const { recipeId } = req.params; const containers = await findRecipeContainers(recipeId); if (containers.length === 0) { - return ctx.errorResponse(res, 404, 'No containers found for this recipe'); + throw new NotFoundError('Containers for recipe'); } const results = []; @@ -173,22 +182,22 @@ module.exports = function(ctx) { } } - ctx.log.info('recipe', 'Recipe restarted', { recipeId, results }); + log.info('recipe', 'Recipe restarted', { recipeId, results }); res.json({ success: true, recipeId, results }); }, 'recipe-restart')); /** * DELETE /api/recipes/:recipeId — remove entire recipe (containers, network, services) */ - router.delete('/:recipeId', ctx.asyncHandler(async (req, res) => { + router.delete('/:recipeId', asyncHandler(async (req, res) => { const { recipeId } = req.params; const containers = await findRecipeContainers(recipeId); if (containers.length === 0) { - return ctx.errorResponse(res, 404, 'No containers found for this recipe'); + throw new NotFoundError('Containers for recipe'); } - ctx.log.info('recipe', 'Removing recipe', { recipeId, containerCount: containers.length }); + log.info('recipe', 'Removing recipe', { recipeId, containerCount: containers.length }); const results = []; const networkNames = new Set(); @@ -212,7 +221,7 @@ module.exports = function(ctx) { try { await removeCaddyBlock(subdomain); } catch (e) { - ctx.log.warn('recipe', 'Failed to remove Caddy config', { subdomain, error: e.message }); + log.warn('recipe', 'Failed to remove Caddy config', { subdomain, error: e.message }); } } @@ -225,7 +234,7 @@ module.exports = function(ctx) { } // Remove recipe services from services.json - await ctx.servicesStateManager.update(services => { + await servicesStateManager.update(services => { return services.filter(s => s.recipeId !== recipeId); }); @@ -234,9 +243,9 @@ module.exports = function(ctx) { try { const network = ctx.docker.client.getNetwork(netName); await network.remove(); - ctx.log.info('recipe', 'Removed Docker network', { netName }); + log.info('recipe', 'Removed Docker network', { netName }); } catch (e) { - ctx.log.warn('recipe', 'Failed to remove network', { netName, error: e.message }); + log.warn('recipe', 'Failed to remove network', { netName, error: e.message }); } } @@ -245,7 +254,7 @@ module.exports = function(ctx) { 'info' ); - ctx.log.info('recipe', 'Recipe removed', { recipeId, results }); + log.info('recipe', 'Recipe removed', { recipeId, results }); res.json({ success: true, recipeId, results }); }, 'recipe-remove')); diff --git a/dashcaddy-api/routes/services.js b/dashcaddy-api/routes/services.js index 59fddc3..62ced94 100644 --- a/dashcaddy-api/routes/services.js +++ b/dashcaddy-api/routes/services.js @@ -9,8 +9,42 @@ const { validateServiceConfig, isValidPort } = require('../input-validator'); const { exists } = require('../fs-helpers'); const { paginate, parsePaginationParams } = require('../pagination'); const { resolveServiceUrl } = require('../url-resolver'); +const { success, error: errorResponse } = require('../response-helpers'); +const { ConflictError } = require('../errors'); -module.exports = function(ctx) { +/** + * Services route factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.servicesStateManager - State manager for services.json + * @param {Object} deps.credentialManager - Credential storage manager + * @param {Object} deps.siteConfig - Site configuration + * @param {Function} deps.buildServiceUrl - URL builder function + * @param {Function} deps.buildDomain - Domain builder function + * @param {Function} deps.fetchT - Fetch wrapper with timeout + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {string} deps.SERVICES_FILE - Path to services.json + * @param {Object} deps.log - Logger instance + * @param {Function} deps.safeErrorMessage - Safe error message extractor + * @param {Function} deps.resyncHealthChecker - Health checker resync function + * @param {Object} deps.caddy - Caddy management interface + * @param {Object} deps.dns - DNS management interface + * @returns {express.Router} + */ +module.exports = function({ + servicesStateManager, + credentialManager, + siteConfig, + buildServiceUrl, + buildDomain, + fetchT, + asyncHandler, + SERVICES_FILE, + log, + safeErrorMessage, + resyncHealthChecker, + caddy, + dns +}) { const router = express.Router(); const CA_CERT_PATH = process.env.CA_CERT_PATH || '/app/pki/root.crt'; const PROBE_CONCURRENCY = 6; @@ -28,13 +62,13 @@ module.exports = function(ctx) { } async function loadServicesList() { - if (!await exists(ctx.SERVICES_FILE)) return []; - const data = await ctx.servicesStateManager.read(); + if (!await exists(SERVICES_FILE)) return []; + const data = await servicesStateManager.read(); return Array.isArray(data) ? data : data.services || []; } function resolveProbeUrl(id, service) { - return resolveServiceUrl(id, service, ctx.siteConfig, ctx.buildServiceUrl); + return resolveServiceUrl(id, service, siteConfig, buildServiceUrl); } const PROBE_TIMEOUT = 3000; // 3s — covers DNS + connect + response @@ -72,7 +106,7 @@ module.exports = function(ctx) { } async function probeViaPylon(targetUrl) { - const pylonConfig = ctx.siteConfig?.pylon; + const pylonConfig = siteConfig?.pylon; if (!pylonConfig?.url) return null; try { const probeUrl = `${pylonConfig.url}/probe?url=${encodeURIComponent(targetUrl)}`; @@ -80,7 +114,7 @@ module.exports = function(ctx) { if (pylonConfig.key) headers['x-pylon-key'] = pylonConfig.key; const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 5000); - const response = await ctx.fetchT(probeUrl, { method: 'GET', signal: controller.signal, headers }); + const response = await fetchT(probeUrl, { method: 'GET', signal: controller.signal, headers }); clearTimeout(timeout); if (!response.ok) return null; const data = await response.json(); @@ -106,7 +140,7 @@ module.exports = function(ctx) { } // Pylon relay fallback — if direct probe failed, try through the pylon - if (error && ctx.siteConfig?.pylon) { + if (error && siteConfig?.pylon) { const pylonResult = await probeViaPylon(url); if (pylonResult && pylonResult.status) { const responseTime = Number((process.hrtime.bigint() - startedAt) / 1000000n); @@ -161,100 +195,99 @@ module.exports = function(ctx) { // ===== SERVICE CREDENTIAL ENDPOINTS ===== // Store credentials for a service - router.post('/services/:serviceId/credentials', ctx.asyncHandler(async (req, res) => { + router.post('/services/:serviceId/credentials', asyncHandler(async (req, res) => { const { serviceId } = req.params; const { apiKey, username, password } = req.body; if (apiKey) { - await ctx.credentialManager.store(`service.${serviceId}.apikey`, apiKey); + await credentialManager.store(`service.${serviceId}.apikey`, apiKey); } if (username) { - await ctx.credentialManager.store(`service.${serviceId}.username`, username); + await credentialManager.store(`service.${serviceId}.username`, username); } if (password) { - await ctx.credentialManager.store(`service.${serviceId}.password`, password); + await credentialManager.store(`service.${serviceId}.password`, password); } - res.json({ success: true, message: `Credentials stored for ${serviceId}` }); + success(res, { message: `Credentials stored for ${serviceId}` }); }, 'store-service-creds')); // Delete credentials for a service - router.delete('/services/:serviceId/credentials', ctx.asyncHandler(async (req, res) => { + router.delete('/services/:serviceId/credentials', asyncHandler(async (req, res) => { const { serviceId } = req.params; - await ctx.credentialManager.delete(`service.${serviceId}.apikey`); - await ctx.credentialManager.delete(`service.${serviceId}.username`); - await ctx.credentialManager.delete(`service.${serviceId}.password`); - res.json({ success: true, message: `Credentials removed for ${serviceId}` }); + await credentialManager.delete(`service.${serviceId}.apikey`); + await credentialManager.delete(`service.${serviceId}.username`); + await credentialManager.delete(`service.${serviceId}.password`); + success(res, { message: `Credentials removed for ${serviceId}` }); }, 'delete-service-creds')); // Check credential status for a service (what's stored) - router.get('/services/:serviceId/credentials', ctx.asyncHandler(async (req, res) => { + router.get('/services/:serviceId/credentials', asyncHandler(async (req, res) => { try { const { serviceId } = req.params; - const arrKey = await ctx.credentialManager.retrieve(`arr.${serviceId}.apikey`).catch(() => null); - const svcKey = await ctx.credentialManager.retrieve(`service.${serviceId}.apikey`).catch(() => null); - const username = await ctx.credentialManager.retrieve(`service.${serviceId}.username`).catch(() => null); - res.json({ - success: true, + const arrKey = await credentialManager.retrieve(`arr.${serviceId}.apikey`).catch(() => null); + const svcKey = await credentialManager.retrieve(`service.${serviceId}.apikey`).catch(() => null); + const username = await credentialManager.retrieve(`service.${serviceId}.username`).catch(() => null); + success(res, { hasApiKey: !!(arrKey || svcKey), hasBasicAuth: !!username, username: username || null }); } catch (error) { - res.json({ success: true, hasApiKey: false, hasBasicAuth: false }); + success(res, { hasApiKey: false, hasBasicAuth: false }); } }, 'service-creds')); // ===== SEEDHOST CREDENTIAL ENDPOINTS ===== // Store seedhost credentials (shared username + per-service passwords) - router.post('/seedhost-creds', ctx.asyncHandler(async (req, res) => { + router.post('/seedhost-creds', asyncHandler(async (req, res) => { const { username, password, serviceId } = req.body; if (!username) { - return ctx.errorResponse(res, 400, 'Username required'); + throw new ValidationError('Username required'); } - await ctx.credentialManager.store('seedhost.username', username); + await credentialManager.store('seedhost.username', username); if (password) { if (serviceId) { - await ctx.credentialManager.store(`seedhost.password.${serviceId}`, password); + await credentialManager.store(`seedhost.password.${serviceId}`, password); } else { - await ctx.credentialManager.store('seedhost.password', password); + await credentialManager.store('seedhost.password', password); } } - res.json({ success: true, message: 'Seedhost credentials stored' }); + success(res, { message: 'Seedhost credentials stored' }); }, 'store-seedhost-creds')); // Get seedhost credential status - router.get('/seedhost-creds', ctx.asyncHandler(async (req, res) => { + router.get('/seedhost-creds', asyncHandler(async (req, res) => { try { - const username = await ctx.credentialManager.retrieve('seedhost.username').catch(() => null); + const username = await credentialManager.retrieve('seedhost.username').catch(() => null); const serviceId = req.query.serviceId; let hasPassword = false; if (serviceId) { - const svcPass = await ctx.credentialManager.retrieve(`seedhost.password.${serviceId}`).catch(() => null); + const svcPass = await credentialManager.retrieve(`seedhost.password.${serviceId}`).catch(() => null); hasPassword = !!svcPass; } // Fall back to checking shared password if (!hasPassword) { - const sharedPass = await ctx.credentialManager.retrieve('seedhost.password').catch(() => null); + const sharedPass = await credentialManager.retrieve('seedhost.password').catch(() => null); hasPassword = !!sharedPass; } - res.json({ success: true, hasCredentials: !!username && hasPassword, username: username || null, hasPassword }); + success(res, { hasCredentials: !!username && hasPassword, username: username || null, hasPassword }); } catch (error) { - res.json({ success: true, hasCredentials: false }); + success(res, { hasCredentials: false }); } }, 'seedhost-creds')); // Delete seedhost credentials - router.delete('/seedhost-creds', ctx.asyncHandler(async (req, res) => { + router.delete('/seedhost-creds', asyncHandler(async (req, res) => { const serviceId = req.query.serviceId; if (serviceId) { - await ctx.credentialManager.delete(`seedhost.password.${serviceId}`); - res.json({ success: true, message: `Password for ${serviceId} removed` }); + await credentialManager.delete(`seedhost.password.${serviceId}`); + success(res, { message: `Password for ${serviceId} removed` }); } else { - await ctx.credentialManager.delete('seedhost.username'); - await ctx.credentialManager.delete('seedhost.password'); - res.json({ success: true, message: 'Seedhost credentials removed' }); + await credentialManager.delete('seedhost.username'); + await credentialManager.delete('seedhost.password'); + success(res, { message: 'Seedhost credentials removed' }); } }, 'delete-seedhost-creds')); @@ -263,7 +296,7 @@ module.exports = function(ctx) { // Batched live status for dashboard cards const STATUS_DEADLINE = 10000; // 10s — return whatever we have by then - router.get('/services/status', ctx.asyncHandler(async (req, res) => { + router.get('/services/status', asyncHandler(async (req, res) => { const services = await loadServicesList(); const serviceMap = new Map(services.filter(s => s && s.id).map(s => [s.id, s])); const ids = []; @@ -276,7 +309,7 @@ module.exports = function(ctx) { } addId('internet'); - Object.keys(ctx.siteConfig?.dnsServers || {}).forEach(addId); + Object.keys(siteConfig?.dnsServers || {}).forEach(addId); services.forEach(service => addId(service.id)); // Collect results as they arrive; deadline returns whatever we have @@ -300,8 +333,7 @@ module.exports = function(ctx) { }); res.set('Cache-Control', 'no-store'); - res.json({ - success: true, + success(res, { checkedAt: new Date().toISOString(), partial, statuses @@ -309,97 +341,96 @@ module.exports = function(ctx) { }, 'services-status')); // List all services - router.get('/services', ctx.asyncHandler(async (req, res) => { - if (!await exists(ctx.SERVICES_FILE)) { + router.get('/services', asyncHandler(async (req, res) => { + if (!await exists(SERVICES_FILE)) { return res.json([]); } - const services = await ctx.servicesStateManager.read(); + const services = await servicesStateManager.read(); const paginationParams = parsePaginationParams(req.query); const result = paginate(services, paginationParams); if (paginationParams) { - res.json({ success: true, services: result.data, pagination: result.pagination }); + success(res, { services: result.data, pagination: result.pagination }); } else { res.json(result.data); } }, 'services-list')); // Add a new service - router.post('/services', ctx.asyncHandler(async (req, res) => { + router.post('/services', asyncHandler(async (req, res) => { try { const { id, name, logo } = req.body; if (!id || !name) { - return ctx.errorResponse(res, 400, 'id and name are required'); + throw new ValidationError('id and name are required'); } // Validate service configuration try { validateServiceConfig({ id, name }); } catch (validationErr) { - return ctx.errorResponse(res, 400, validationErr.message, { errors: validationErr.errors }); + return errorResponse(res, validationErr.message, 400, { errors: validationErr.errors }); } - await ctx.servicesStateManager.update(services => { + await servicesStateManager.update(services => { // Check if service already exists if (services.find(s => s.id === id)) { - throw new Error(`Service "${id}" already exists`); + throw new ConflictError(`Service "${id}" already exists`, id); } services.push({ id, name, logo: logo || `/assets/${id}.png` }); return services; }); - ctx.resyncHealthChecker?.().catch(() => {}); - res.json({ success: true, message: `Service "${name}" added to dashboard` }); + resyncHealthChecker?.().catch(() => {}); + success(res, { message: `Service "${name}" added to dashboard` }); } catch (error) { - ctx.log.error('deploy', 'Error adding service', { error: error.message }); + log.error('deploy', 'Error adding service', { error: error.message }); if (error.message.includes('already exists')) { - ctx.errorResponse(res, 409, ctx.safeErrorMessage(error)); + errorResponse(res, safeErrorMessage(error), 409); } else { - ctx.errorResponse(res, 500, ctx.safeErrorMessage(error)); + // Error handled by middleware } } }, 'services-update')); // Bulk import/replace services (for dashboard import feature) - router.put('/services', ctx.asyncHandler(async (req, res) => { + router.put('/services', asyncHandler(async (req, res) => { const services = req.body; if (!Array.isArray(services)) { - return ctx.errorResponse(res, 400, 'Request body must be an array of services'); + throw new ValidationError('Request body must be an array of services'); } for (const service of services) { if (!service.id || !service.name) { - return ctx.errorResponse(res, 400, 'Each service must have id and name fields'); + throw new ValidationError('Each service must have id and name fields'); } try { validateServiceConfig(service); } catch (validationErr) { - return ctx.errorResponse(res, 400, `Invalid service "${service.id}": ${validationErr.message}`, { errors: validationErr.errors }); + return errorResponse(res, `Invalid service "${service.id}": ${validationErr.message}`, 400, { errors: validationErr.errors }); } } - await ctx.servicesStateManager.write(services); - ctx.resyncHealthChecker?.().catch(() => {}); + await servicesStateManager.write(services); + resyncHealthChecker?.().catch(() => {}); - res.json({ - success: true, + success(res, { message: `Successfully imported ${services.length} services`, count: services.length }); }, 'services-import')); // Delete a service - router.delete('/services/:id', ctx.asyncHandler(async (req, res) => { + router.delete('/services/:id', asyncHandler(async (req, res) => { const { id } = req.params; - if (!await exists(ctx.SERVICES_FILE)) { - return ctx.errorResponse(res, 404, 'No services found'); + if (!await exists(SERVICES_FILE)) { + throw new NotFoundError('No services found'); } let found = false; - await ctx.servicesStateManager.update(services => { + await servicesStateManager.update(services => { const initialLength = services.length; const filtered = services.filter(s => s.id !== id); found = filtered.length !== initialLength; @@ -407,39 +438,39 @@ module.exports = function(ctx) { }); if (!found) { - return ctx.errorResponse(res, 404, `Service "${id}" not found`); + return errorResponse(res, `Service "${id}" not found`, 404); } - ctx.resyncHealthChecker?.().catch(() => {}); - res.json({ success: true, message: `Service "${id}" removed from dashboard` }); + resyncHealthChecker?.().catch(() => {}); + success(res, { message: `Service "${id}" removed from dashboard` }); }, 'services-delete')); // Update service configuration (subdomain, port, IP, tailscale, name, logo) - router.post('/services/update', ctx.asyncHandler(async (req, res) => { + router.post('/services/update', asyncHandler(async (req, res) => { const { oldSubdomain, newSubdomain, port, ip, tailscaleOnly, name, logo } = req.body; if (!oldSubdomain || !newSubdomain) { - return ctx.errorResponse(res, 400, 'oldSubdomain and newSubdomain are required'); + throw new ValidationError('oldSubdomain and newSubdomain are required'); } if (!REGEX.SUBDOMAIN.test(oldSubdomain) || !REGEX.SUBDOMAIN.test(newSubdomain)) { - return ctx.errorResponse(res, 400, '[DC-301] Invalid subdomain format'); + throw new ValidationError('[DC-301] Invalid subdomain format'); } if (port && !isValidPort(port)) { - return ctx.errorResponse(res, 400, 'Invalid port number (must be 1-65535)'); + throw new ValidationError('Invalid port number (must be 1-65535)'); } if (ip && ip !== 'localhost' && !validatorLib.isIP(ip)) { - return ctx.errorResponse(res, 400, '[DC-210] Invalid IP address'); + throw new ValidationError('[DC-210] Invalid IP address'); } const results = { dns: null, caddy: null, services: null }; - const oldDomain = ctx.buildDomain(oldSubdomain); - const newDomain = ctx.buildDomain(newSubdomain); + const oldDomain = buildDomain(oldSubdomain); + const newDomain = buildDomain(newSubdomain); - let content = await ctx.caddy.read(); + let content = await caddy.read(); const escapedOldDomain = oldDomain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const siteBlockRegex = new RegExp( @@ -456,11 +487,11 @@ module.exports = function(ctx) { const finalIp = ip || existingIp; const finalPort = port || existingPort; - const newConfig = ctx.caddy.generateConfig(newSubdomain, finalIp, finalPort, { + const newConfig = caddy.generateConfig(newSubdomain, finalIp, finalPort, { tailscaleOnly: tailscaleOnly || false }); - const caddyResult = await ctx.caddy.modify(c => c.replace(siteBlockRegex, newConfig)); + const caddyResult = await caddy.modify(c => c.replace(siteBlockRegex, newConfig)); results.caddy = caddyResult.success ? 'updated' : `config saved, reload failed: ${caddyResult.error}`; } else { results.caddy = 'old config not found'; @@ -468,9 +499,9 @@ module.exports = function(ctx) { if (oldSubdomain !== newSubdomain) { try { - const dnsToken = ctx.dns.getToken(); - await ctx.dns.call(ctx.siteConfig.dnsServerIp, '/api/zones/records/delete', { token: dnsToken, domain: oldDomain, type: 'A' }); - await ctx.dns.createRecord(newSubdomain, ip || 'localhost'); + const dnsToken = dns.getToken(); + await dns.call(siteConfig.dnsServerIp, '/api/zones/records/delete', { token: dnsToken, domain: oldDomain, type: 'A' }); + await dns.createRecord(newSubdomain, ip || 'localhost'); results.dns = 'updated'; } catch (e) { results.dns = `failed: ${e.message}`; @@ -479,8 +510,8 @@ module.exports = function(ctx) { results.dns = 'unchanged'; } - if (await exists(ctx.SERVICES_FILE)) { - await ctx.servicesStateManager.update(services => { + if (await exists(SERVICES_FILE)) { + await servicesStateManager.update(services => { const serviceIndex = services.findIndex(s => s.id === oldSubdomain || s.url?.includes(oldSubdomain)); if (serviceIndex !== -1) { const existing = services[serviceIndex]; @@ -493,7 +524,7 @@ module.exports = function(ctx) { port: finalPort, ip: finalIp, tailscaleOnly: tailscaleOnly || false, - url: ctx.buildServiceUrl(newSubdomain) + url: buildServiceUrl(newSubdomain) }; if (name) services[serviceIndex].name = name; if (logo) services[serviceIndex].logo = logo; @@ -505,9 +536,8 @@ module.exports = function(ctx) { }); } - ctx.resyncHealthChecker?.().catch(() => {}); - res.json({ - success: true, + resyncHealthChecker?.().catch(() => {}); + success(res, { message: `Service updated: ${oldSubdomain} -> ${newSubdomain}`, results }); diff --git a/dashcaddy-api/routes/sites.js b/dashcaddy-api/routes/sites.js index 65762dd..212b7fb 100644 --- a/dashcaddy-api/routes/sites.js +++ b/dashcaddy-api/routes/sites.js @@ -1,28 +1,43 @@ const express = require('express'); const fs = require('fs'); const { CADDY, REGEX, LIMITS } = require('../constants'); +const { ValidationError, ConflictError, NotFoundError } = require('../errors'); +const { validateURL } = require('../input-validator'); -module.exports = function(ctx) { +/** + * Sites route factory + * @param {Object} deps - Explicit dependencies + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Object} deps.caddy - Caddy manager + * @param {Object} deps.dns - DNS manager + * @param {Function} deps.fetchT - Fetch with timeout + * @param {Function} deps.buildDomain - Domain builder function + * @param {Function} deps.addServiceToConfig - Service config adder + * @param {Object} deps.siteConfig - Site configuration + * @param {Object} deps.log - Logger instance + * @returns {express.Router} + */ +module.exports = function({ asyncHandler, caddy, dns, fetchT, buildDomain, addServiceToConfig, siteConfig, log }) { const router = express.Router(); // Get Caddyfile contents - router.get('/caddyfile', ctx.asyncHandler(async (req, res) => { - const content = await ctx.caddy.read(); + router.get('/caddyfile', asyncHandler(async (req, res) => { + const content = await caddy.read(); res.json({ success: true, content }); }, 'caddyfile-get')); // Get current Caddy config (from admin API) - router.get('/caddy/config', ctx.asyncHandler(async (req, res) => { - const response = await ctx.fetchT(`${ctx.caddy.adminUrl}/config/`); + router.get('/caddy/config', asyncHandler(async (req, res) => { + const response = await fetchT(`${caddy.adminUrl}/config/`); const config = await response.json(); res.json({ success: true, config }); }, 'caddy-config')); // Reload Caddy configuration via admin API - router.post('/caddy/reload', ctx.asyncHandler(async (req, res) => { - const caddyfileContent = await ctx.caddy.read(); + router.post('/caddy/reload', asyncHandler(async (req, res) => { + const caddyfileContent = await caddy.read(); - const response = await ctx.fetchT(`${ctx.caddy.adminUrl}/load`, { + const response = await fetchT(`${caddy.adminUrl}/load`, { method: 'POST', headers: { 'Content-Type': CADDY.CONTENT_TYPE }, body: caddyfileContent @@ -30,16 +45,16 @@ module.exports = function(ctx) { if (!response.ok) { const errorText = await response.text(); - ctx.log.error('caddy', 'Caddy reload failed', { error: errorText }); - return ctx.errorResponse(res, 500, '[DC-303] Caddy reload failed. Check server logs for details.'); + log.error('caddy', 'Caddy reload failed', { error: errorText }); + throw new Error('Caddy reload failed. Check server logs for details.'); } res.json({ success: true, message: 'Caddy configuration reloaded successfully' }); }, 'caddy-reload')); // Get Certificate Authorities from Caddyfile - router.get('/caddy/cas', ctx.asyncHandler(async (req, res) => { - const content = await ctx.caddy.read(); + router.get('/caddy/cas', asyncHandler(async (req, res) => { + const content = await caddy.read(); const cas = []; const pkiRegex = /pki\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/gs; @@ -116,11 +131,11 @@ module.exports = function(ctx) { }, 'caddy-get-cas')); // Remove a site from Caddyfile - router.delete('/site/:domain', ctx.asyncHandler(async (req, res) => { + router.delete('/site/:domain', asyncHandler(async (req, res) => { const { domain } = req.params; - if (!domain) return ctx.errorResponse(res, 400, 'Domain is required'); + if (!domain) throw new ValidationError('Domain is required'); - const result = await ctx.caddy.modify((content) => { + const result = await caddy.modify((content) => { const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const siteBlockRegex = new RegExp( `\\n?${escapedDomain}\\s*\\{[^{}]*(?:\\{[^{}]*(?:\\{[^{}]*\\}[^{}]*)*\\}[^{}]*)*\\}\\s*`, 'g' @@ -132,70 +147,70 @@ module.exports = function(ctx) { if (!result.success) { if (result.rolledBack) { - return ctx.errorResponse(res, 500, `Removed "${domain}" but Caddy reload failed (rolled back): ${result.error}`); + throw new Error( `Removed "${domain}" but Caddy reload failed (rolled back): ${result.error}`); } - return ctx.errorResponse(res, 404, `Site block for "${domain}" not found in Caddyfile`); + throw new NotFoundError(`Site block for "" in Caddyfile`); } res.json({ success: true, message: `Site "${domain}" removed from Caddyfile and Caddy reloaded` }); }, 'site-delete')); // Add a new site to Caddyfile and reload - router.post('/site', ctx.asyncHandler(async (req, res) => { + router.post('/site', asyncHandler(async (req, res) => { const { domain, upstream, config } = req.body; - if (!domain || !upstream) return ctx.errorResponse(res, 400, 'Domain and upstream are required'); - if (!REGEX.DOMAIN.test(domain)) return ctx.errorResponse(res, 400, '[DC-301] Invalid domain format'); + if (!domain || !upstream) throw new ValidationError('Domain and upstream are required'); + if (!REGEX.DOMAIN.test(domain)) throw new ValidationError('[DC-301] Invalid domain format'); const upstreamRegex = /^[a-z0-9.-]+:\d{1,5}$/i; - if (!upstreamRegex.test(upstream)) return ctx.errorResponse(res, 400, 'Invalid upstream format. Use host:port'); + if (!upstreamRegex.test(upstream)) throw new ValidationError('Invalid upstream format. Use host:port'); - let content = await ctx.caddy.read(); + let content = await caddy.read(); const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const siteBlockRegex = new RegExp(`\\n?${escapedDomain}\\s*\\{`, 'g'); if (siteBlockRegex.test(content)) { - return ctx.errorResponse(res, 409, `[DC-302] Site block for "${domain}" already exists in Caddyfile`); + throw new ConflictError(`Site block for "" already exists in Caddyfile`); } // Always generate structured config — never allow raw Caddy config injection const newSiteBlock = `\n${domain} {\n reverse_proxy ${upstream}\n tls internal\n}\n`; - const result = await ctx.caddy.modify(c => c + newSiteBlock); + const result = await caddy.modify(c => c + newSiteBlock); if (!result.success) { - return ctx.errorResponse(res, 500, `[DC-303] Site added to Caddyfile but reload failed: ${result.error}`, + throw new Error( `[DC-303] Site added to Caddyfile but reload failed: ${result.error}`, result.rolledBack ? { note: 'Caddyfile was rolled back to previous state' } : {}); } - ctx.ok(res, { message: `Site "${domain}" added to Caddyfile and Caddy reloaded successfully` }); + res.json({ success: true, message: `Site "${domain}" added to Caddyfile and Caddy reloaded successfully` }); }, 'site-add')); // Add external service reverse proxy to Caddyfile - router.post('/site/external', ctx.asyncHandler(async (req, res) => { + router.post('/site/external', asyncHandler(async (req, res) => { const { subdomain, externalUrl, preserveHost, followRedirects, sslType, caddyfilePath, reloadCaddy: shouldReload, createDns, serviceName, logo } = req.body; if (!subdomain || !externalUrl) { - return ctx.errorResponse(res, 400, 'Subdomain and externalUrl are required'); + throw new ValidationError('Subdomain and externalUrl are required'); } if (!REGEX.SUBDOMAIN.test(subdomain)) { - return ctx.errorResponse(res, 400, '[DC-301] Invalid subdomain format'); + throw new ValidationError('[DC-301] Invalid subdomain format'); } try { - ctx.validateURL(externalUrl); + validateURL(externalUrl); } catch (validationErr) { - return ctx.errorResponse(res, 400, validationErr.message); + throw new ValidationError(validationErr.message); } - const domain = ctx.buildDomain(subdomain); + const domain = buildDomain(subdomain); let dnsWarning = null; try { if (createDns) { try { - await ctx.dns.createRecord(subdomain, ctx.siteConfig.dnsServerIp); - ctx.log.info('dns', 'DNS record created for external proxy', { domain, ip: ctx.siteConfig.dnsServerIp }); + await dns.createRecord(subdomain, siteConfig.dnsServerIp); + log.info('dns', 'DNS record created for external proxy', { domain, ip: siteConfig.dnsServerIp }); } catch (dnsError) { dnsWarning = `DNS creation failed: ${dnsError.message}. You may need to create the DNS record manually.`; - ctx.log.warn('dns', 'DNS creation failed for external proxy', { domain, error: dnsError.message }); + log.warn('dns', 'DNS creation failed for external proxy', { domain, error: dnsError.message }); } } @@ -207,7 +222,7 @@ module.exports = function(ctx) { // Validate URL components are safe for Caddyfile syntax const unsafeCaddyChars = /[{}\n\r]/; if (unsafeCaddyChars.test(urlObj.host) || unsafeCaddyChars.test(urlObj.pathname)) { - return ctx.errorResponse(res, 400, 'External URL contains characters not safe for Caddy configuration'); + throw new ValidationError('External URL contains characters not safe for Caddy configuration'); } const baseUrl = `${urlObj.protocol}//${urlObj.host}`; @@ -220,29 +235,29 @@ module.exports = function(ctx) { proxyConfig = `\n${domain} {\n ${sslConfig}\n\n reverse_proxy ${externalUrl} {${hostHeader}\n transport http {\n tls\n }\n }\n}\n`; } - const caddyResult = await ctx.caddy.modify(c => { + const caddyResult = await caddy.modify(c => { const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); if (new RegExp(`\\n?${escapedDomain}\\s*\\{`, 'g').test(c)) return null; return c + proxyConfig; }); if (!caddyResult.success && !caddyResult.rolledBack) { - return ctx.errorResponse(res, 409, `[DC-302] Site block for "${domain}" already exists in Caddyfile`); + throw new ConflictError(`Site block for "" already exists in Caddyfile`); } if (!caddyResult.success) { - return ctx.errorResponse(res, 500, `[DC-303] External proxy added but Caddy reload failed (rolled back): ${caddyResult.error}`); + throw new Error( `[DC-303] External proxy added but Caddy reload failed (rolled back): ${caddyResult.error}`); } if (serviceName && logo) { try { - await ctx.addServiceToConfig({ + await addServiceToConfig({ id: subdomain, name: serviceName, logo, isExternal: true, externalUrl, deployedAt: new Date().toISOString() }); - ctx.log.info('deploy', 'Service added to dashboard', { subdomain }); + log.info('deploy', 'Service added to dashboard', { subdomain }); } catch (serviceError) { - ctx.log.warn('deploy', 'Failed to add service to dashboard', { subdomain, error: serviceError.message }); + log.warn('deploy', 'Failed to add service to dashboard', { subdomain, error: serviceError.message }); } } @@ -253,7 +268,7 @@ module.exports = function(ctx) { if (dnsWarning) response.warning = dnsWarning; res.json(response); } catch (error) { - ctx.errorResponse(res, 500, ctx.safeErrorMessage(error)); + throw error; } }, 'site-external')); diff --git a/dashcaddy-api/routes/tailscale.js b/dashcaddy-api/routes/tailscale.js index 07b807b..9c93f87 100644 --- a/dashcaddy-api/routes/tailscale.js +++ b/dashcaddy-api/routes/tailscale.js @@ -2,14 +2,37 @@ const express = require('express'); const fs = require('fs'); const { TAILSCALE } = require('../constants'); const { exists } = require('../fs-helpers'); +const { ValidationError, NotFoundError } = require('../errors'); -module.exports = function(ctx) { +/** + * Tailscale route factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.tailscale - Tailscale manager + * @param {Object} deps.caddy - Caddy manager + * @param {Object} deps.servicesStateManager - Services state manager + * @param {Object} deps.credentialManager - Credential manager + * @param {Function} deps.buildDomain - Domain builder function + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {string} deps.SERVICES_FILE - Path to services.json + * @param {Object} deps.log - Logger instance + * @returns {express.Router} + */ +module.exports = function({ + tailscale, + caddy, + servicesStateManager, + credentialManager, + buildDomain, + asyncHandler, + SERVICES_FILE, + log +}) { const router = express.Router(); // Get Tailscale status and configuration - router.get('/status', ctx.asyncHandler(async (req, res) => { - const status = await ctx.tailscale.getStatus(); - const localIP = await ctx.tailscale.getLocalIP(); + router.get('/status', asyncHandler(async (req, res) => { + const status = await tailscale.getStatus(); + const localIP = await tailscale.getLocalIP(); if (!status) { return res.json({ @@ -46,37 +69,37 @@ module.exports = function(ctx) { tailnetName: status.MagicDNSSuffix, online: status.Self?.Online }, - config: ctx.tailscale.config, + config: tailscale.config, devices, deviceCount: devices.length }); }, 'tailscale-status')); // Update Tailscale configuration - router.post('/config', ctx.asyncHandler(async (req, res) => { + router.post('/config', asyncHandler(async (req, res) => { const { enabled, requireAuth, allowedTailnet } = req.body; - if (typeof enabled !== 'undefined') ctx.tailscale.config.enabled = enabled; - if (typeof requireAuth !== 'undefined') ctx.tailscale.config.requireAuth = requireAuth; - if (typeof allowedTailnet !== 'undefined') ctx.tailscale.config.allowedTailnet = allowedTailnet; + if (typeof enabled !== 'undefined') tailscale.config.enabled = enabled; + if (typeof requireAuth !== 'undefined') tailscale.config.requireAuth = requireAuth; + if (typeof allowedTailnet !== 'undefined') tailscale.config.allowedTailnet = allowedTailnet; - await ctx.tailscale.save(); + await tailscale.save(); res.json({ success: true, message: 'Tailscale configuration updated', - config: ctx.tailscale.config + config: tailscale.config }); }, 'tailscale-config')); // Check if a request is coming from Tailscale - router.get('/check-connection', ctx.asyncHandler(async (req, res) => { + router.get('/check-connection', asyncHandler(async (req, res) => { const clientIP = req.ip || req.connection?.remoteAddress || ''; const forwardedFor = req.headers['x-forwarded-for']; const realIP = req.headers['x-real-ip']; const ipsToCheck = [clientIP, forwardedFor, realIP].filter(Boolean); - const isTailscale = ipsToCheck.some(ip => ctx.tailscale.isTailscaleIP(ip.toString().split(',')[0].trim())); + const isTailscale = ipsToCheck.some(ip => tailscale.isTailscaleIP(ip.toString().split(',')[0].trim())); res.json({ success: true, @@ -88,8 +111,8 @@ module.exports = function(ctx) { }, 'tailscale-check')); // Get Tailscale device list - router.get('/devices', ctx.asyncHandler(async (req, res) => { - const status = await ctx.tailscale.getStatus(); + router.get('/devices', asyncHandler(async (req, res) => { + const status = await tailscale.getStatus(); if (!status || !status.Peer) { return res.json({ success: true, devices: [] }); } @@ -122,15 +145,15 @@ module.exports = function(ctx) { }, 'tailscale-devices')); // Toggle Tailscale-only mode for an existing service - router.post('/protect-service', ctx.asyncHandler(async (req, res) => { + router.post('/protect-service', asyncHandler(async (req, res) => { const { subdomain, tailscaleOnly, allowedIPs } = req.body; if (!subdomain) { - return ctx.errorResponse(res, 400, 'subdomain is required'); + throw new ValidationError('subdomain is required'); } - let content = await ctx.caddy.read(); - const domain = ctx.buildDomain(subdomain); + let content = await caddy.read(); + const domain = buildDomain(subdomain); const blockRegex = new RegExp(`(${domain.replace('.', '\\.')}\\s*\\{[^}]*\\})`, 's'); const match = content.match(blockRegex); @@ -142,23 +165,23 @@ module.exports = function(ctx) { const proxyMatch = match[0].match(/reverse_proxy\s+([^\s\n]+)/); if (!proxyMatch) { - return ctx.errorResponse(res, 400, 'Could not parse service configuration'); + throw new ValidationError('Could not parse service configuration'); } const [ip, port] = proxyMatch[1].split(':'); - const newConfig = ctx.caddy.generateConfig(subdomain, ip, port || '80', { + const newConfig = caddy.generateConfig(subdomain, ip, port || '80', { tailscaleOnly: tailscaleOnly !== false, allowedIPs: allowedIPs || [] }); - const caddyResult = await ctx.caddy.modify(c => c.replace(blockRegex, newConfig)); + const caddyResult = await caddy.modify(c => c.replace(blockRegex, newConfig)); if (!caddyResult.success) { - return ctx.errorResponse(res, 500, `[DC-303] Failed to reload Caddy: ${caddyResult.error}`); + throw new Error(`Failed to reload Caddy: ${caddyResult.error}`); } - if (await exists(ctx.SERVICES_FILE)) { - await ctx.servicesStateManager.update(services => { + if (await exists(SERVICES_FILE)) { + await servicesStateManager.update(services => { const serviceIndex = services.findIndex(s => s.id === subdomain); if (serviceIndex !== -1) { services[serviceIndex].tailscaleOnly = tailscaleOnly !== false; @@ -177,11 +200,11 @@ module.exports = function(ctx) { // ── Tailscale API Integration (OAuth 2.0) ── // Save OAuth client credentials + validate by exchanging for a token - router.post('/oauth-config', ctx.asyncHandler(async (req, res) => { + router.post('/oauth-config', asyncHandler(async (req, res) => { const { clientId, clientSecret, tailnet } = req.body; if (!clientId || !clientSecret || !tailnet) { - return ctx.errorResponse(res, 400, 'clientId, clientSecret, and tailnet are required'); + throw new ValidationError('clientId, clientSecret, and tailnet are required'); } // Validate by exchanging for a real token @@ -192,7 +215,7 @@ module.exports = function(ctx) { }); if (!tokenRes.ok) { - return ctx.errorResponse(res, 400, `OAuth validation failed: HTTP ${tokenRes.status}`); + throw new ValidationError(`OAuth validation failed: HTTP ${tokenRes.status}`); } const tokenData = await tokenRes.json(); @@ -203,94 +226,94 @@ module.exports = function(ctx) { }); if (!testRes.ok) { - return ctx.errorResponse(res, 400, `API test failed: HTTP ${testRes.status}. Check tailnet name and OAuth scopes (needs devices:read, acl:read).`); + throw new ValidationError(`API test failed: HTTP ${testRes.status}. Check tailnet name and OAuth scopes (needs devices:read, acl:read).`); } // Store credentials securely - await ctx.credentialManager.store('tailscale.oauth.client_id', clientId, { provider: 'tailscale' }); - await ctx.credentialManager.store('tailscale.oauth.client_secret', clientSecret, { provider: 'tailscale', tailnet }); + await credentialManager.store('tailscale.oauth.client_id', clientId, { provider: 'tailscale' }); + await credentialManager.store('tailscale.oauth.client_secret', clientSecret, { provider: 'tailscale', tailnet }); // Update config - ctx.tailscale.config.oauthConfigured = true; - ctx.tailscale.config.tailnet = tailnet; - if (!ctx.tailscale.config.allowedTailnet) { - const status = await ctx.tailscale.getStatus(); + tailscale.config.oauthConfigured = true; + tailscale.config.tailnet = tailnet; + if (!tailscale.config.allowedTailnet) { + const status = await tailscale.getStatus(); if (status?.MagicDNSSuffix) { - ctx.tailscale.config.allowedTailnet = status.MagicDNSSuffix; + tailscale.config.allowedTailnet = status.MagicDNSSuffix; } } - await ctx.tailscale.save(); + await tailscale.save(); // Start background sync - ctx.tailscale.startSync(); + tailscale.startSync(); // Trigger initial sync try { - await ctx.tailscale.syncAPI(); + await tailscale.syncAPI(); } catch (e) { - ctx.log.warn('tailscale', 'Initial sync after OAuth config failed', { error: e.message }); + log.warn('tailscale', 'Initial sync after OAuth config failed', { error: e.message }); } - res.json({ success: true, config: ctx.tailscale.config }); + res.json({ success: true, config: tailscale.config }); }, 'tailscale-oauth-config')); // Remove OAuth credentials and disable API sync - router.delete('/oauth-config', ctx.asyncHandler(async (req, res) => { - await ctx.credentialManager.delete('tailscale.oauth.client_id'); - await ctx.credentialManager.delete('tailscale.oauth.client_secret'); + router.delete('/oauth-config', asyncHandler(async (req, res) => { + await credentialManager.delete('tailscale.oauth.client_id'); + await credentialManager.delete('tailscale.oauth.client_secret'); - ctx.tailscale.config.oauthConfigured = false; - ctx.tailscale.config.tailnet = null; - ctx.tailscale.config.lastSync = null; - await ctx.tailscale.save(); + tailscale.config.oauthConfigured = false; + tailscale.config.tailnet = null; + tailscale.config.lastSync = null; + await tailscale.save(); - ctx.tailscale.stopSync(); + tailscale.stopSync(); res.json({ success: true, message: 'Tailscale OAuth credentials removed' }); }, 'tailscale-oauth-delete')); // Get enriched device list from Tailscale API - router.get('/api-devices', ctx.asyncHandler(async (req, res) => { - if (!ctx.tailscale.config.oauthConfigured) { - return ctx.errorResponse(res, 400, 'Tailscale API not configured. Set up OAuth first.'); + router.get('/api-devices', asyncHandler(async (req, res) => { + if (!tailscale.config.oauthConfigured) { + throw new ValidationError('Tailscale API not configured. Set up OAuth first.'); } // Return cached devices from last sync res.json({ success: true, - devices: ctx.tailscale.config.devices || [], - lastSync: ctx.tailscale.config.lastSync + devices: tailscale.config.devices || [], + lastSync: tailscale.config.lastSync }); }, 'tailscale-api-devices')); // Manually trigger an API sync - router.post('/sync', ctx.asyncHandler(async (req, res) => { - if (!ctx.tailscale.config.oauthConfigured) { - return ctx.errorResponse(res, 400, 'Tailscale API not configured. Set up OAuth first.'); + router.post('/sync', asyncHandler(async (req, res) => { + if (!tailscale.config.oauthConfigured) { + throw new ValidationError('Tailscale API not configured. Set up OAuth first.'); } - const devices = await ctx.tailscale.syncAPI(); + const devices = await tailscale.syncAPI(); res.json({ success: true, devices: devices || [], - lastSync: ctx.tailscale.config.lastSync + lastSync: tailscale.config.lastSync }); }, 'tailscale-sync')); // Fetch ACL policy (read-only) - router.get('/acl', ctx.asyncHandler(async (req, res) => { - const token = await ctx.tailscale.getAccessToken(); - const tailnet = ctx.tailscale.config.tailnet; + router.get('/acl', asyncHandler(async (req, res) => { + const token = await tailscale.getAccessToken(); + const tailnet = tailscale.config.tailnet; if (!token || !tailnet) { - return ctx.errorResponse(res, 400, 'Tailscale API not configured'); + throw new ValidationError('Tailscale API not configured'); } const aclRes = await fetch(`${TAILSCALE.API_BASE}/tailnet/${encodeURIComponent(tailnet)}/acl`, { headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' } }); if (!aclRes.ok) { - return ctx.errorResponse(res, aclRes.status, `ACL fetch failed: HTTP ${aclRes.status}`); + throw new Error(`ACL fetch failed: HTTP ${aclRes.status}`); } const acl = await aclRes.json(); diff --git a/dashcaddy-api/routes/themes.js b/dashcaddy-api/routes/themes.js index db80d67..4bd341b 100644 --- a/dashcaddy-api/routes/themes.js +++ b/dashcaddy-api/routes/themes.js @@ -1,8 +1,16 @@ const express = require('express'); const fs = require('fs'); const path = require('path'); +const { success } = require('../response-helpers'); +const { ValidationError, NotFoundError } = require('../errors'); -module.exports = function(ctx) { +/** + * Themes routes factory + * @param {Object} deps - Explicit dependencies + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @returns {express.Router} + */ +module.exports = function({ asyncHandler }) { const router = express.Router(); const THEMES_DIR = process.env.THEMES_DIR || path.join(path.dirname(process.env.SERVICES_FILE || '/app/services.json'), 'themes'); @@ -28,44 +36,44 @@ module.exports = function(ctx) { // Get all user themes router.get('/themes', (req, res) => { - res.json({ success: true, themes: readAllThemes() }); + success(res, { themes: readAllThemes() }); }); // Save a theme (create or update) - router.post('/themes/:slug', (req, res) => { + router.post('/themes/:slug', asyncHandler(async (req, res) => { const { slug } = req.params; const { name, colors, lightBg } = req.body; if (!slug || !name || !colors) { - return res.status(400).json({ success: false, error: 'Missing slug, name, or colors' }); + throw new ValidationError('Missing slug, name, or colors'); } if (!/^[a-z0-9-]+$/.test(slug)) { - return res.status(400).json({ success: false, error: 'Invalid slug format' }); + throw new ValidationError('Invalid slug format (use lowercase letters, numbers, and hyphens only)', 'slug'); } const themeData = { name, ...colors }; if (lightBg) themeData.lightBg = true; fs.writeFileSync(path.join(THEMES_DIR, slug + '.json'), JSON.stringify(themeData, null, 2), 'utf8'); - res.json({ success: true, message: name + ' theme saved' }); - }); + success(res, { message: name + ' theme saved' }); + })); // Delete a theme - router.delete('/themes/:slug', (req, res) => { + router.delete('/themes/:slug', asyncHandler(async (req, res) => { const { slug } = req.params; const filePath = path.join(THEMES_DIR, slug + '.json'); if (!fs.existsSync(filePath)) { - return res.status(404).json({ success: false, error: 'Theme not found' }); + throw new NotFoundError(`Theme ${slug}`); } const data = JSON.parse(fs.readFileSync(filePath, 'utf8')); const name = data.name || slug; fs.unlinkSync(filePath); - res.json({ success: true, message: name + ' theme deleted' }); - }); + success(res, { message: name + ' theme deleted' }); + })); return router; }; diff --git a/dashcaddy-api/routes/updates.js b/dashcaddy-api/routes/updates.js index 5b5eeb1..f055623 100644 --- a/dashcaddy-api/routes/updates.js +++ b/dashcaddy-api/routes/updates.js @@ -1,87 +1,97 @@ const express = require('express'); const { paginate, parsePaginationParams } = require('../pagination'); +const { ValidationError } = require('../errors'); -module.exports = function(ctx) { +/** + * Updates route factory + * @param {Object} deps - Explicit dependencies + * @param {Object} deps.updateManager - Container update manager + * @param {Object} deps.selfUpdater - DashCaddy self-update manager + * @param {Function} deps.asyncHandler - Async route handler wrapper + * @param {Function} deps.logError - Error logging function + * @returns {express.Router} + */ +module.exports = function({ updateManager, selfUpdater, asyncHandler, logError }) { const router = express.Router(); // ===== UPDATE MANAGEMENT ENDPOINTS ===== // Check for updates - router.post('/updates/check', ctx.asyncHandler(async (req, res) => { - await ctx.updateManager.checkForUpdates(); - const updates = ctx.updateManager.getAvailableUpdates(); + router.post('/updates/check', asyncHandler(async (req, res) => { + await updateManager.checkForUpdates(); + const updates = updateManager.getAvailableUpdates(); res.json({ success: true, updates, count: updates.length }); }, 'updates-check')); // Get available updates - router.get('/updates/available', ctx.asyncHandler(async (req, res) => { - const updates = ctx.updateManager.getAvailableUpdates(); + router.get('/updates/available', asyncHandler(async (req, res) => { + const updates = updateManager.getAvailableUpdates(); const paginationParams = parsePaginationParams(req.query); const result = paginate(updates, paginationParams); res.json({ success: true, updates: result.data, count: updates.length, ...(result.pagination && { pagination: result.pagination }) }); }, 'updates-available')); // Update a container - router.post('/updates/update/:containerId', ctx.asyncHandler(async (req, res) => { - const result = await ctx.updateManager.updateContainer(req.params.containerId, req.body); + router.post('/updates/update/:containerId', asyncHandler(async (req, res) => { + const result = await updateManager.updateContainer(req.params.containerId, req.body); res.json({ success: true, result }); }, 'updates-update')); // Rollback update - router.post('/updates/rollback/:containerId', ctx.asyncHandler(async (req, res) => { - await ctx.updateManager.rollbackUpdate(req.params.containerId); + router.post('/updates/rollback/:containerId', asyncHandler(async (req, res) => { + await updateManager.rollbackUpdate(req.params.containerId); res.json({ success: true, message: 'Rollback completed' }); }, 'updates-rollback')); // Get update history - router.get('/updates/history', ctx.asyncHandler(async (req, res) => { + router.get('/updates/history', asyncHandler(async (req, res) => { const paginationParams = parsePaginationParams(req.query); // When paginating, fetch all history so pagination can slice correctly const fetchLimit = paginationParams ? Number.MAX_SAFE_INTEGER : (parseInt(req.query.limit) || 50); - const history = ctx.updateManager.getHistory(fetchLimit); + const history = updateManager.getHistory(fetchLimit); const result = paginate(history, paginationParams); res.json({ success: true, history: result.data, ...(result.pagination && { pagination: result.pagination }) }); }, 'updates-history')); // Configure auto-update - router.post('/updates/auto-update/:containerId', ctx.asyncHandler(async (req, res) => { - ctx.updateManager.configureAutoUpdate(req.params.containerId, req.body); + router.post('/updates/auto-update/:containerId', asyncHandler(async (req, res) => { + updateManager.configureAutoUpdate(req.params.containerId, req.body); res.json({ success: true, message: 'Auto-update configured' }); }, 'updates-auto-update')); // Schedule update - router.post('/updates/schedule/:containerId', ctx.asyncHandler(async (req, res) => { + router.post('/updates/schedule/:containerId', asyncHandler(async (req, res) => { const { scheduledTime } = req.body; if (!scheduledTime) { - return ctx.errorResponse(res, 400, 'scheduledTime is required'); + throw new ValidationError('scheduledTime is required'); } - ctx.updateManager.scheduleUpdate(req.params.containerId, scheduledTime); + updateManager.scheduleUpdate(req.params.containerId, scheduledTime); res.json({ success: true, message: 'Update scheduled', scheduledTime }); }, 'updates-schedule')); // ===== DASHCADDY SELF-UPDATE ENDPOINTS ===== // Get current version - router.get('/system/version', ctx.asyncHandler(async (req, res) => { - const local = ctx.selfUpdater.getLocalVersion(); + router.get('/system/version', asyncHandler(async (req, res) => { + const local = selfUpdater.getLocalVersion(); res.json({ success: true, name: 'DashCaddy', version: local.version, commit: local.commit }); }, 'system-version')); // Check for DashCaddy update - router.get('/system/update-check', ctx.asyncHandler(async (req, res) => { - const result = await ctx.selfUpdater.checkForUpdate(); + router.get('/system/update-check', asyncHandler(async (req, res) => { + const result = await selfUpdater.checkForUpdate(); res.json({ success: true, ...result }); }, 'system-update-check')); // Apply available update - router.post('/system/update-apply', ctx.asyncHandler(async (req, res) => { - const check = await ctx.selfUpdater.checkForUpdate(); + router.post('/system/update-apply', asyncHandler(async (req, res) => { + const check = await selfUpdater.checkForUpdate(); if (!check.available) { return res.json({ success: true, message: 'Already up to date' }); } // Start async — container may restart - ctx.selfUpdater.applyUpdate(check.remote).catch(err => { - ctx.logError('self-update', err); + selfUpdater.applyUpdate(check.remote).catch(err => { + logError('self-update', err); }); res.json({ success: true, @@ -92,33 +102,33 @@ module.exports = function(ctx) { }, 'system-update-apply')); // Get update status - router.get('/system/update-status', ctx.asyncHandler(async (req, res) => { + router.get('/system/update-status', asyncHandler(async (req, res) => { res.json({ success: true, - status: ctx.selfUpdater.getStatus(), - lastCheck: ctx.selfUpdater.lastCheckTime, - lastResult: ctx.selfUpdater.lastCheckResult, + status: selfUpdater.getStatus(), + lastCheck: selfUpdater.lastCheckTime, + lastResult: selfUpdater.lastCheckResult, }); }, 'system-update-status')); // Get self-update history - router.get('/system/update-history', ctx.asyncHandler(async (req, res) => { - const history = ctx.selfUpdater.getUpdateHistory(); + router.get('/system/update-history', asyncHandler(async (req, res) => { + const history = selfUpdater.getUpdateHistory(); res.json({ success: true, history }); }, 'system-update-history')); // List rollback versions - router.get('/system/rollback-versions', ctx.asyncHandler(async (req, res) => { - const versions = ctx.selfUpdater.getAvailableRollbacks(); + router.get('/system/rollback-versions', asyncHandler(async (req, res) => { + const versions = selfUpdater.getAvailableRollbacks(); res.json({ success: true, versions }); }, 'system-rollback-versions')); // Rollback to a previous version - router.post('/system/rollback', ctx.asyncHandler(async (req, res) => { + router.post('/system/rollback', asyncHandler(async (req, res) => { const { version } = req.body; - if (!version) return ctx.errorResponse(res, 400, 'version is required'); - ctx.selfUpdater.rollbackToVersion(version).catch(err => { - ctx.logError('self-rollback', err); + if (!version) throw new ValidationError('version is required'); + selfUpdater.rollbackToVersion(version).catch(err => { + logError('self-rollback', err); }); res.json({ success: true, message: `Rollback to ${version} initiated` }); }, 'system-rollback')); diff --git a/dashcaddy-api/server-old.js b/dashcaddy-api/server-old.js new file mode 100644 index 0000000..0a82307 --- /dev/null +++ b/dashcaddy-api/server-old.js @@ -0,0 +1,1997 @@ +const express = require('express'); +const crypto = require('crypto'); +const fs = require('fs'); +const fsp = require('fs').promises; +const { exists, readJsonFile, writeJsonFile, readTextFile, isAccessible } = require('./fs-helpers'); +const os = require('os'); +const http = require('http'); +const https = require('https'); +const { execSync } = require('child_process'); +const path = require('path'); +const { + ValidationError, validateFilePath, validateURL, validateToken, + validateServiceConfig, sanitizeString, isValidPort, validateSecurePath +} = require('./input-validator'); +const validatorLib = require('validator'); +const credentialManager = require('./credential-manager'); +const { CACHE_CONFIGS, createCache } = require('./cache-config'); +const { AppError } = require('./errors'); +const { validateConfig } = require('./config-schema'); +const { resolveServiceUrl } = require('./url-resolver'); +const { + APP, APP_PORTS, ARR_SERVICES, TIMEOUTS, RETRIES, + SESSION_TTL, RATE_LIMITS, CADDY, PLEX, LIMITS, + REGEX, DNS_RECORD_TYPES, DOCKER, TAILSCALE, buildMediaAuth, +} = require('./constants'); +const platformPaths = require('./platform-paths'); + +// Image processing for favicon conversion +let sharp, pngToIco; +try { + sharp = require('sharp'); + pngToIco = require('png-to-ico'); +} catch (e) { + log.warn('server', 'Image processing libraries not available - favicon conversion disabled'); +} + +// Docker integration +const Docker = require('dockerode'); +const docker = new Docker(); + +// App templates +const { APP_TEMPLATES, TEMPLATE_CATEGORIES, DIFFICULTY_LEVELS } = require('./app-templates'); +const { RECIPE_TEMPLATES, RECIPE_CATEGORIES } = require('./recipe-templates'); + +// Crypto utilities for credential encryption +const cryptoUtils = require('./crypto-utils'); + +// New feature modules +const resourceMonitor = require('./resource-monitor'); +const backupManager = require('./backup-manager'); +const healthChecker = require('./health-checker'); +const updateManager = require('./update-manager'); +const selfUpdater = require('./self-updater'); +let dockerMaintenance; +try { dockerMaintenance = require('./docker-maintenance'); } catch (_) { logger.warn('[WARN] docker-maintenance module not found, skipped'); } +let logDigest; +try { logDigest = require('./log-digest'); } catch (_) { logger.warn('[WARN] log-digest module not found, skipped'); } +const StateManager = require('./state-manager'); +const auditLogger = require('./audit-logger'); +const portLockManager = require('./port-lock-manager'); +const dockerSecurity = require('./docker-security'); +const authManager = require('./auth-manager'); +const responseHelpers = require('./response-helpers'); +const configureMiddleware = require('./middleware'); +const { validateStartupConfig, syncHealthCheckerServices } = require('./startup-validator'); +const { CSRF_HEADER_NAME } = require('./csrf-protection'); + +// Route modules +const ctx = require('./routes/context'); +const healthRoutes = require('./routes/health'); +const monitoringRoutes = require('./routes/monitoring'); +const updatesRoutes = require('./routes/updates'); +const authRoutes = require('./routes/auth'); +const configRoutes = require('./routes/config'); +const dnsRoutes = require('./routes/dns'); +const notificationRoutes = require('./routes/notifications'); +const containerRoutes = require('./routes/containers'); +const serviceRoutes = require('./routes/services'); +const tailscaleRoutes = require('./routes/tailscale'); +const sitesRoutes = require('./routes/sites'); +const credentialsRoutes = require('./routes/credentials'); +const arrRoutes = require('./routes/arr'); +const appsRoutes = require('./routes/apps'); +const logsRoutes = require('./routes/logs'); +const backupsRoutes = require('./routes/backups'); +const caRoutes = require('./routes/ca'); +const browseRoutes = require('./routes/browse'); +const errorLogsRoutes = require('./routes/errorlogs'); +const licenseRoutes = require('./routes/license'); +const recipesRoutes = require('./routes/recipes'); +const themesRoutes = require('./routes/themes'); +const { LicenseManager } = require('./license-manager'); +const metrics = require('./metrics'); + +const app = express(); +const PORT = APP.PORT; + +// Configuration from environment variables +const CADDYFILE_PATH = process.env.CADDYFILE_PATH || platformPaths.caddyfile; +const CADDY_ADMIN_URL = process.env.CADDY_ADMIN_URL || platformPaths.caddyAdminUrl; +const SERVICES_FILE = process.env.SERVICES_FILE || platformPaths.servicesFile; +const SERVICES_DIR = path.dirname(SERVICES_FILE); +const CONFIG_FILE = process.env.CONFIG_FILE || path.join(SERVICES_DIR, 'config.json'); +const DNS_CREDENTIALS_FILE = process.env.DNS_CREDENTIALS_FILE || path.join(SERVICES_DIR, 'dns-credentials.json'); +const TAILSCALE_CONFIG_FILE = process.env.TAILSCALE_CONFIG_FILE || path.join(SERVICES_DIR, 'tailscale-config.json'); +const NOTIFICATIONS_FILE = process.env.NOTIFICATIONS_FILE || path.join(SERVICES_DIR, 'notifications.json'); +const TOTP_CONFIG_FILE = process.env.TOTP_CONFIG_FILE || path.join(SERVICES_DIR, 'totp-config.json'); +const ERROR_LOG_FILE = process.env.ERROR_LOG_FILE || path.join(__dirname, 'dashcaddy-errors.log'); +const MAX_ERROR_LOG_SIZE = LIMITS.ERROR_LOG_SIZE; +const BROWSE_ROOTS = (process.env.MEDIA_BROWSE_ROOTS || '') + .split(',') + .filter(r => r.includes('=')) + .map(r => { + const eqIndex = r.indexOf('='); + const containerPath = r.slice(0, eqIndex).trim(); + const hostPath = r.slice(eqIndex + 1).trim(); + return { containerPath, hostPath }; + }); + +// State management with file locking (prevents data corruption) +const servicesStateManager = new StateManager(SERVICES_FILE); +const configStateManager = new StateManager(CONFIG_FILE); + +// License manager for premium feature gating +const licenseManager = new LicenseManager(credentialManager, CONFIG_FILE, console); +const LICENSE_SECRET_FILE = process.env.LICENSE_SECRET_FILE || path.join(__dirname, '.license-secret'); +licenseManager.loadSecret(LICENSE_SECRET_FILE); + +// ===== Site configuration loaded from config.json (#5) ===== +// These are read at startup and refreshed on config save. +// All code should use these instead of hardcoded values. +let siteConfig = { tld: '.home', caName: '', dnsServerIp: '', dnsServerPort: CADDY.DEFAULT_DNS_PORT, dashboardHost: '', timezone: 'UTC', dnsServers: {}, configurationType: 'homelab', domain: '', routingMode: 'subdomain' }; + +function loadSiteConfig() { + try { + if (fs.existsSync(CONFIG_FILE)) { + const raw = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8')); + + // Validate config and log any issues (log.warn may not be assigned during initial load) + const { valid, errors: configErrors, warnings: configWarnings } = validateConfig(raw); + if (log.warn) { + if (!valid) { + log.warn('config', 'Config validation errors', { errors: configErrors }); + } + for (const w of configWarnings) { + log.warn('config', w); + } + } + + siteConfig.tld = raw.tld || '.home'; + if (!siteConfig.tld.startsWith('.')) siteConfig.tld = '.' + siteConfig.tld; + siteConfig.caName = raw.caName || ''; + siteConfig.dnsServerIp = (raw.dns && raw.dns.ip) || ''; + siteConfig.dnsServerPort = (raw.dns && raw.dns.port) || CADDY.DEFAULT_DNS_PORT; + siteConfig.dashboardHost = raw.dashboardHost || `status${siteConfig.tld}`; + siteConfig.timezone = raw.timezone || 'UTC'; + siteConfig.dnsServers = raw.dnsServers || {}; + siteConfig.configurationType = raw.configurationType || 'homelab'; + siteConfig.domain = raw.domain || ''; + siteConfig.routingMode = raw.routingMode || 'subdomain'; + siteConfig.pylon = raw.pylon || null; // { url, key? } — health check relay + } + } catch (e) { + // log.error may not be assigned yet during initial module load + if (log.error) { + log.error('config', 'Failed to load site config', { error: e.message }); + } + } +} +loadSiteConfig(); + +/** Build a domain from subdomain + configured TLD or public domain */ +function buildDomain(subdomain) { + if (siteConfig.configurationType === 'public' && siteConfig.domain) { + return `${subdomain}.${siteConfig.domain}`; + } + return `${subdomain}${siteConfig.tld}`; +} + +/** Build full service URL (protocol + host + path) for a given subdomain. + * Subdirectory mode: https://example.com/sonarr + * Subdomain mode: https://sonarr.example.com */ +function buildServiceUrl(subdomain) { + if (siteConfig.routingMode === 'subdirectory' && siteConfig.domain) { + return `https://${siteConfig.domain}/${subdomain}`; + } + return `https://${buildDomain(subdomain)}`; +} + +/** Build full Technitium DNS API URL for any server (handles IP vs hostname, port) */ +function buildDnsUrl(server, apiPath, params) { + const protocol = server.match(/^\d+\.\d+\.\d+\.\d+$/) ? 'http' : 'https'; + const port = protocol === 'http' ? `:${CADDY.DEFAULT_DNS_PORT}` : ''; + const qs = params instanceof URLSearchParams ? params.toString() : new URLSearchParams(params).toString(); + return `${protocol}://${server}${port}${apiPath}?${qs}`; +} + +/** Call a Technitium DNS API endpoint and return parsed JSON */ +async function callDns(server, apiPath, params) { + const url = buildDnsUrl(server, apiPath, params); + const response = await fetchT(url, { + method: 'GET', + headers: { 'Accept': 'application/json' }, + agent: httpsAgent + }, TIMEOUTS.HTTP_LONG); + return response.json(); +} + +// ===== Shared Helpers ===== + +/** Fetch with automatic timeout — adds AbortSignal if no signal is present. + * Drop-in replacement for fetch(); safely ignores calls that already have a signal. */ +function fetchT(url, opts = {}, timeoutMs = TIMEOUTS.HTTP_DEFAULT) { + // Caddy admin API rejects Node.js undici fetch due to Sec-Fetch-* headers triggering + // origin checking. Use raw http.request for Caddy admin calls to avoid this. + if (url.includes(':2019')) { + return _httpFetch(url, opts, timeoutMs); + } + if (!opts.signal) { + opts = { ...opts, signal: AbortSignal.timeout(timeoutMs) }; + } + delete opts.timeout; + return fetch(url, opts); +} + +/** Raw http.request wrapper that returns a fetch-like Response for Caddy admin API */ +function _httpFetch(url, opts = {}, timeoutMs = TIMEOUTS.HTTP_DEFAULT) { + return new Promise((resolve, reject) => { + const parsed = new URL(url); + const options = { + hostname: parsed.hostname, + port: parsed.port || 2019, + path: parsed.pathname + parsed.search, + method: (opts.method || 'GET').toUpperCase(), + headers: { ...opts.headers }, + timeout: timeoutMs, + }; + if (opts.body) { + options.headers['Content-Length'] = Buffer.byteLength(opts.body); + } + const MAX_RESPONSE_SIZE = 10 * 1024 * 1024; // 10MB + const req = http.request(options, (res) => { + let data = ''; + let size = 0; + res.on('data', chunk => { + size += chunk.length; + if (size > MAX_RESPONSE_SIZE) { + res.destroy(); + reject(new Error(`Response from ${url} exceeded ${MAX_RESPONSE_SIZE} bytes`)); + return; + } + data += chunk; + }); + res.on('end', () => { + resolve({ + ok: res.statusCode >= 200 && res.statusCode < 300, + status: res.statusCode, + statusText: res.statusMessage, + json: () => Promise.resolve(JSON.parse(data)), + text: () => Promise.resolve(data), + headers: { get: (k) => res.headers[k.toLowerCase()] }, + }); + }); + }); + req.on('timeout', () => { req.destroy(); reject(new Error(`Request to ${url} timed out after ${timeoutMs}ms`)); }); + req.on('error', reject); + if (opts.body) req.write(opts.body); + req.end(); + }); +} + +/** Pull a Docker image with timeout protection */ +function dockerPull(imageName, timeoutMs = DOCKER.TIMEOUT) { + return new Promise((resolve, reject) => { + const timer = setTimeout(() => reject(new Error(`Docker pull timed out after ${timeoutMs / 1000}s: ${imageName}`)), timeoutMs); + docker.pull(imageName, (err, stream) => { + if (err) { clearTimeout(timer); return reject(err); } + docker.modem.followProgress(stream, (err, output) => { + clearTimeout(timer); + if (err) return reject(err); + resolve(output); + }); + }); + }); +} + +// ===== Structured Logging ===== +const LOG_LEVELS = { debug: 0, info: 1, warn: 2, error: 3 }; +const LOG_LEVEL = LOG_LEVELS[process.env.LOG_LEVEL || 'info'] || 1; + +function log(level, context, message, data = {}) { + if (LOG_LEVELS[level] < LOG_LEVEL) return; + const entry = { + t: new Date().toISOString(), + level, + ctx: context, + msg: message, + }; + if (Object.keys(data).length) entry.data = data; + const fn = level === 'error' ? logger.error : level === 'warn' ? logger.warn : logger.info; + fn(JSON.stringify(entry)); +} +log.info = (ctx, msg, data) => log('info', ctx, msg, data); +log.warn = (ctx, msg, data) => log('warn', ctx, msg, data); +log.error = (ctx, msg, data) => log('error', ctx, msg, data); +log.debug = (ctx, msg, data) => log('debug', ctx, msg, data); + +/** Standard error response — always returns { success: false, error, ...extras } */ +function errorResponse(res, statusCode, message, extras = {}) { + return res.status(statusCode).json({ success: false, error: message, ...extras }); +} + +/** Standard success response — always returns { success: true, ...data } */ +function ok(res, data = {}) { + return res.json({ success: true, ...data }); +} + +/** Look up a single service by ID from services.json */ +async function getServiceById(serviceId) { + const services = await servicesStateManager.read(); + return services.find(s => s.id === serviceId) || null; +} + +/** Find a running Docker container by name substring */ +async function findContainerByName(name, opts = { all: false }) { + const containers = await docker.listContainers(opts); + const match = containers.find(c => + c.Names.some(n => n.toLowerCase().includes(name.toLowerCase())) + ); + return match || null; +} + +/** Read config.json with fallback to empty object */ +async function readConfig() { + return readJsonFile(CONFIG_FILE, {}); +} + +/** Save config.json (merges with existing, atomic with locking) */ +async function saveConfig(updates) { + return await configStateManager.update(config => { + return Object.assign(config, updates); + }); +} + +/** + * Resolve a DNS token: use the provided one or auto-refresh. + * @returns {{ token: string }} or throws with 401-appropriate message + */ +async function requireDnsToken(providedToken) { + if (providedToken) return providedToken; + const result = await ensureValidDnsToken(); + if (result.success) return result.token; + const err = new Error('No valid DNS token available. ' + result.error); + err.statusCode = 401; + throw err; +} + +/** Get all host ports currently in use by Docker containers */ +async function getUsedPorts() { + const containers = await docker.listContainers({ all: false }); + const ports = new Set(); + for (const c of containers) { + for (const p of (c.Ports || [])) { + if (p.PublicPort) ports.add(p.PublicPort); + } + } + return ports; +} + +/** + * Atomically read-modify-write the Caddyfile and reload Caddy. + * Uses a mutex to prevent concurrent modifications from clobbering each other. + * Rolls back on reload failure. + * @param {function} modifyFn - receives current content, returns modified content (or null to skip) + * @returns {{ success: boolean, error?: string }} + */ +let _caddyfileLock = Promise.resolve(); +async function modifyCaddyfile(modifyFn) { + let resolve; + const prev = _caddyfileLock; + _caddyfileLock = new Promise(r => { resolve = r; }); + await prev; // wait for any in-flight modification to finish + try { + const original = await readCaddyfile(); + const modified = await modifyFn(original); + if (modified === null || modified === original) { + return { success: false, error: 'No changes to apply' }; + } + await fsp.writeFile(CADDYFILE_PATH, modified, 'utf8'); + try { + await reloadCaddy(modified); + return { success: true }; + } catch (err) { + // Rollback + await fsp.writeFile(CADDYFILE_PATH, original, 'utf8'); + return { success: false, error: safeErrorMessage(err), rolledBack: true }; + } + } finally { + resolve(); + } +} + +/** Read the current Caddyfile content */ +async function readCaddyfile() { + return fsp.readFile(CADDYFILE_PATH, 'utf8'); +} + +// Error logging function with enhanced context tracking +async function logError(context, error, additionalInfo = {}) { + const timestamp = new Date().toISOString(); + + // Extract request context if a request object is provided + const requestContext = {}; + if (additionalInfo.req) { + const req = additionalInfo.req; + const clientIP = req.ip || req.socket?.remoteAddress || ''; + requestContext.requestId = req.id; + requestContext.ip = clientIP; + requestContext.userAgent = req.get('user-agent'); + requestContext.method = req.method; + requestContext.path = req.path; + // Check session validity using ipSessions cache + const session = ipSessions.get(clientIP); + requestContext.sessionValid = session && session.exp > Date.now(); + delete additionalInfo.req; // Remove req from additionalInfo to avoid circular refs + } + + const logEntry = { + timestamp, + context, + ...requestContext, + error: { + message: error.message || error, + stack: error.stack, + code: error.code + }, + ...additionalInfo + }; + + // Format log line with request context + const contextInfo = Object.keys(requestContext).length > 0 + ? `\nRequest Context: ${JSON.stringify(requestContext, null, 2)}` + : ''; + const logLine = `[${timestamp}] ${context}: ${error.message || error}\n${error.stack || ''}${contextInfo}\nAdditional Info: ${JSON.stringify(additionalInfo, null, 2)}\n${'='.repeat(80)}\n`; + + try { + // #7: Rotate log if it exceeds max size + try { + const stats = await fsp.stat(ERROR_LOG_FILE); + if (stats.size > MAX_ERROR_LOG_SIZE) { + const rotated = ERROR_LOG_FILE + '.1'; + if (await exists(rotated)) await fsp.unlink(rotated); + await fsp.rename(ERROR_LOG_FILE, rotated); + } + } catch (_) { /* file may not exist yet */ } + await fsp.appendFile(ERROR_LOG_FILE, logLine); + } catch (e) { + log.error('errorlog', 'Failed to write to error log', { error: e.message }); + } +} + +/** #6: Return a safe error message to the client without leaking internals */ +function safeErrorMessage(error) { + const msg = error.message || String(error); + + // Detect port conflict errors from Docker + const portMatch = msg.match(/exposing port TCP [^:]*:(\d+)/); + if (portMatch || msg.includes('port is already allocated') || msg.includes('ports are not available')) { + const port = portMatch ? portMatch[1] : 'requested'; + return `[DC-200] Port ${port} is already in use. Try a different port in Advanced Options, or stop the service using that port first.`; + } + + // Only expose messages that are clearly user-facing (short, no paths/stack frames) + if (msg.length < 200 && !msg.includes('/') && !msg.includes('\\') && !msg.includes(' at ')) { + return msg; + } + return 'An internal error occurred'; +} + +/** Wrap async route handlers — catches unhandled errors, logs, and returns 500. + * Eliminates try/catch boilerplate from route definitions. + * @param {Function} fn - async (req, res, next) handler + * @param {string} [context] - label for logError (defaults to req.path) + */ +function asyncHandler(fn, context) { + return async (req, res, next) => { + try { + await fn(req, res, next); + } catch (error) { + // Let typed errors (AppError subclasses) propagate to the global error handler + if (error instanceof AppError) { + return next(error); + } + await logError(context || req.path, error); + if (!res.headersSent) { + errorResponse(res, 500, safeErrorMessage(error)); + } + } + }; +} + +/** #4: Validate Docker container IDs (hex SHA256 prefix or name) */ +const CONTAINER_ID_RE = /^[a-zA-Z0-9][a-zA-Z0-9_.\-]{0,127}$/; +function isValidContainerId(id) { + return typeof id === 'string' && CONTAINER_ID_RE.test(id); +} + +// DNS token management - auto-refresh when expired +let dnsToken = process.env.DNS_ADMIN_TOKEN || ''; +let dnsTokenExpiry = null; + +// Per-server token cache for authenticating against specific DNS servers (e.g., for updates) +const dnsServerTokens = createCache(CACHE_CONFIGS.dnsTokens); // LRU cache: serverIp -> { token, expiry } + +// Tailscale configuration cache +let tailscaleConfig = { + enabled: false, + requireAuth: false, // Require Tailscale for dashboard access + allowedTailnet: null, // Restrict to specific tailnet + devices: [], // Cache of known devices + oauthConfigured: false, // true when OAuth credentials are stored + tailnet: null, // tailnet name for API calls (e.g., "example.com" or "-") + syncInterval: 300, // seconds between API syncs (default 5 min) + lastSync: null // ISO timestamp of last successful sync +}; + +// Load Tailscale config from file +async function loadTailscaleConfig() { + try { + if (await exists(TAILSCALE_CONFIG_FILE)) { + const data = await fsp.readFile(TAILSCALE_CONFIG_FILE, 'utf8'); + tailscaleConfig = { ...tailscaleConfig, ...JSON.parse(data) }; + log.info('config', 'Tailscale config loaded', { enabled: tailscaleConfig.enabled }); + } + } catch (e) { + await logError('loadTailscaleConfig', e); + log.warn('config', 'Could not load Tailscale config', { error: e.message }); + } +} +// Save Tailscale config to file +async function saveTailscaleConfig() { + try { + await writeJsonFile(TAILSCALE_CONFIG_FILE, tailscaleConfig); + } catch (e) { + log.error('config', 'Could not save Tailscale config', { error: e.message }); + } +} + +// Check if an IP is a Tailscale IP (100.x.x.x CGNAT range) +function isTailscaleIP(ip) { + if (!ip) return false; + // Tailscale uses 100.64.0.0/10 CGNAT range + const parts = ip.split('.'); + if (parts.length !== 4) return false; + const first = parseInt(parts[0]); + const second = parseInt(parts[1]); + return first === 100 && second >= 64 && second <= 127; +} + +// Get Tailscale status (cached for performance) +const tailscaleStatusCache = createCache(CACHE_CONFIGS.tailscaleStatus); + +async function getTailscaleStatus() { + const cached = tailscaleStatusCache.get('status'); + if (cached) { + return cached; + } + + try { + const output = execSync('tailscale status --json', { encoding: 'utf8', timeout: 5000 }); + const status = JSON.parse(output); + tailscaleStatusCache.set('status', status); + return status; + } catch (e) { + log.warn('config', 'Could not get Tailscale status', { error: e.message }); + return null; + } +} + +// Get the local Tailscale IP +async function getLocalTailscaleIP() { + try { + const status = await getTailscaleStatus(); + if (status && status.Self && status.Self.TailscaleIPs) { + // Return first IPv4 address + return status.Self.TailscaleIPs.find(ip => !ip.includes(':')); + } + } catch (e) { + log.warn('config', 'Could not get local Tailscale IP', { error: e.message }); + } + return null; +} + +// ── Tailscale OAuth 2.0 Client Credentials ── +let _tsTokenCache = { token: null, expiresAt: 0 }; + +async function getTailscaleAccessToken() { + // Return cached token if still valid (with 60s buffer) + if (_tsTokenCache.token && Date.now() < _tsTokenCache.expiresAt - 60000) { + return _tsTokenCache.token; + } + + const clientId = await credentialManager.retrieve('tailscale.oauth.client_id'); + const clientSecret = await credentialManager.retrieve('tailscale.oauth.client_secret'); + if (!clientId || !clientSecret) return null; + + const res = await fetch(TAILSCALE.OAUTH_TOKEN_URL, { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `client_id=${encodeURIComponent(clientId)}&client_secret=${encodeURIComponent(clientSecret)}&grant_type=client_credentials` + }); + + if (!res.ok) { + log.error('tailscale', 'OAuth token exchange failed', { status: res.status }); + _tsTokenCache = { token: null, expiresAt: 0 }; + return null; + } + + const data = await res.json(); + _tsTokenCache = { + token: data.access_token, + expiresAt: Date.now() + (data.expires_in || 3600) * 1000 + }; + return data.access_token; +} + +// Sync device list from Tailscale API (richer than local CLI) +async function syncFromTailscaleAPI() { + const token = await getTailscaleAccessToken(); + const tailnet = tailscaleConfig.tailnet; + if (!token || !tailnet) return null; + + const res = await fetch(`${TAILSCALE.API_BASE}/tailnet/${encodeURIComponent(tailnet)}/devices`, { + headers: { Authorization: `Bearer ${token}` } + }); + if (!res.ok) throw new Error(`Tailscale API: HTTP ${res.status}`); + + const data = await res.json(); + + const devices = (data.devices || []).map(d => ({ + id: d.id, + name: d.name, + hostname: d.hostname, + addresses: d.addresses || [], + ip: (d.addresses || []).find(a => !a.includes(':')) || null, + os: d.os, + user: d.user, + authorized: d.authorized, + tags: d.tags || [], + lastSeen: d.lastSeen, + clientVersion: d.clientVersion, + isExternal: d.isExternal || false + })); + + tailscaleConfig.devices = devices; + tailscaleConfig.lastSync = new Date().toISOString(); + await saveTailscaleConfig(); + + return devices; +} + +let _tsSyncInterval = null; + +function startTailscaleSyncTimer() { + if (_tsSyncInterval) clearInterval(_tsSyncInterval); + const interval = (tailscaleConfig.syncInterval || 300) * 1000; + _tsSyncInterval = setInterval(async () => { + try { + await syncFromTailscaleAPI(); + log.debug('tailscale', 'API sync completed', { deviceCount: tailscaleConfig.devices.length }); + } catch (error) { + log.warn('tailscale', 'API sync failed', { error: error.message }); + } + }, interval); + log.info('tailscale', 'API sync enabled', { interval: interval / 1000 + 's' }); +} + +function stopTailscaleSyncTimer() { + if (_tsSyncInterval) { + clearInterval(_tsSyncInterval); + _tsSyncInterval = null; + } +} + +// TOTP authentication configuration +let totpConfig = { + enabled: false, + sessionDuration: 'never', // 'never' = disabled, or '15m','30m','1h','2h','4h','8h','12h','24h' + isSetUp: false // true once a secret has been verified +}; + +async function loadTotpConfig() { + try { + if (await exists(TOTP_CONFIG_FILE)) { + const data = await fsp.readFile(TOTP_CONFIG_FILE, 'utf8'); + const loaded = JSON.parse(data); + // Never load secret from file — it belongs only in credential-manager + delete loaded.secret; + Object.assign(totpConfig, loaded); + log.info('config', 'TOTP config loaded', { enabled: totpConfig.enabled }); + } + } catch (e) { + await logError('loadTotpConfig', e); + log.warn('config', 'Could not load TOTP config', { error: e.message }); + } +} + +async function saveTotpConfig() { + try { + await writeJsonFile(TOTP_CONFIG_FILE, totpConfig); + } catch (e) { + log.error('config', 'Could not save TOTP config', { error: e.message }); + } +} + +// Load config on startup (async — resolved before server starts listening) +const _configsReady = (async () => { + await loadTailscaleConfig(); + await loadTotpConfig(); +})(); + +// ===== NOTIFICATION SERVICE ===== + +// Notification configuration +let notificationConfig = { + enabled: false, + providers: { + discord: { enabled: false, webhookUrl: '' }, + telegram: { enabled: false, botToken: '', chatId: '' }, + ntfy: { enabled: false, serverUrl: 'https://ntfy.sh', topic: '' } + }, + events: { + containerDown: true, + containerUp: true, + deploymentSuccess: true, + deploymentFailed: true, + serviceError: true + }, + healthCheck: { + enabled: false, + intervalMinutes: 5, + lastCheck: null + } +}; + +// Notification history (in-memory, last 100 entries) +let notificationHistory = []; +const MAX_NOTIFICATION_HISTORY = 100; + +// Load notification config from file (with decryption of sensitive fields) +async function loadNotificationConfig() { + try { + if (await exists(NOTIFICATIONS_FILE)) { + const data = await fsp.readFile(NOTIFICATIONS_FILE, 'utf8'); + const loaded = JSON.parse(data); + + // Decrypt sensitive fields if encrypted + if (loaded._encrypted && loaded.providers) { + if (loaded.providers.discord?.webhookUrl && cryptoUtils.isEncrypted(loaded.providers.discord.webhookUrl)) { + loaded.providers.discord.webhookUrl = cryptoUtils.decrypt(loaded.providers.discord.webhookUrl); + } + if (loaded.providers.telegram?.botToken && cryptoUtils.isEncrypted(loaded.providers.telegram.botToken)) { + loaded.providers.telegram.botToken = cryptoUtils.decrypt(loaded.providers.telegram.botToken); + } + delete loaded._encrypted; + } + + notificationConfig = { ...notificationConfig, ...loaded }; + log.info('config', 'Notification config loaded', { enabled: notificationConfig.enabled }); + } + } catch (e) { + await logError('loadNotificationConfig', e); + log.warn('config', 'Could not load notification config', { error: e.message }); + } +} + +// Save notification config to file (with encryption of sensitive fields) +async function saveNotificationConfig() { + try { + // Create a copy for encryption + const toSave = JSON.parse(JSON.stringify(notificationConfig)); + + // Encrypt sensitive fields + if (toSave.providers) { + if (toSave.providers.discord?.webhookUrl) { + toSave.providers.discord.webhookUrl = cryptoUtils.encrypt(toSave.providers.discord.webhookUrl); + } + if (toSave.providers.telegram?.botToken) { + toSave.providers.telegram.botToken = cryptoUtils.encrypt(toSave.providers.telegram.botToken); + } + } + toSave._encrypted = true; + + await fsp.writeFile(NOTIFICATIONS_FILE, JSON.stringify(toSave, null, 2), 'utf8'); + log.info('config', 'Notification config saved (encrypted)'); + } catch (e) { + await logError('saveNotificationConfig', e); + log.error('config', 'Could not save notification config', { error: e.message }); + } +} + +// Add to notification history +function addNotificationToHistory(notification) { + notificationHistory.unshift({ + ...notification, + timestamp: new Date().toISOString() + }); + if (notificationHistory.length > MAX_NOTIFICATION_HISTORY) { + notificationHistory = notificationHistory.slice(0, MAX_NOTIFICATION_HISTORY); + } +} + +// Send notification via Discord webhook +async function sendDiscordNotification(title, message, type = 'info') { + const { webhookUrl } = notificationConfig.providers.discord; + if (!webhookUrl) return { success: false, error: 'No webhook URL configured' }; + + const colors = { + success: 0x00ff00, // Green + error: 0xff0000, // Red + warning: 0xffff00, // Yellow + info: 0x0099ff // Blue + }; + + const payload = { + embeds: [{ + title: `DashCaddy: ${title}`, + description: message, + color: colors[type] || colors.info, + timestamp: new Date().toISOString(), + footer: { text: 'DashCaddy Notifications' } + }] + }; + + try { + const response = await fetchT(webhookUrl, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(payload) + }); + + if (!response.ok) { + throw new Error(`Discord API returned ${response.status}`); + } + + return { success: true }; + } catch (error) { + await logError('sendDiscordNotification', error); + return { success: false, error: error.message }; + } +} + +// Send notification via Telegram bot +async function sendTelegramNotification(title, message, type = 'info') { + const { botToken, chatId } = notificationConfig.providers.telegram; + if (!botToken || !chatId) return { success: false, error: 'Bot token or chat ID not configured' }; + + const emoji = { + success: '✅', + error: '❌', + warning: '⚠️', + info: 'ℹ️' + }; + + const text = `${emoji[type] || emoji.info} *DashCaddy: ${title}*\n\n${message}`; + + try { + const response = await fetchT(`https://api.telegram.org/bot${botToken}/sendMessage`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + chat_id: chatId, + text: text, + parse_mode: 'Markdown' + }) + }); + + const result = await response.json(); + if (!result.ok) { + throw new Error(result.description || 'Telegram API error'); + } + + return { success: true }; + } catch (error) { + await logError('sendTelegramNotification', error); + return { success: false, error: error.message }; + } +} + +// Send notification via ntfy.sh +async function sendNtfyNotification(title, message, type = 'info') { + const { serverUrl, topic } = notificationConfig.providers.ntfy; + if (!topic) return { success: false, error: 'No topic configured' }; + + const priority = { + success: 3, // default + error: 5, // max + warning: 4, // high + info: 3 // default + }; + + const tags = { + success: 'white_check_mark', + error: 'x', + warning: 'warning', + info: 'information_source' + }; + + try { + const response = await fetchT(`${serverUrl}/${topic}`, { + method: 'POST', + headers: { + 'Title': `DashCaddy: ${title}`, + 'Priority': String(priority[type] || 3), + 'Tags': tags[type] || 'information_source' + }, + body: message + }); + + if (!response.ok) { + throw new Error(`ntfy returned ${response.status}`); + } + + return { success: true }; + } catch (error) { + await logError('sendNtfyNotification', error); + return { success: false, error: error.message }; + } +} + +// Send notification to all enabled providers +async function sendNotification(event, title, message, type = 'info') { + if (!notificationConfig.enabled) { + return { sent: false, reason: 'Notifications disabled' }; + } + + // Check if this event type is enabled + if (notificationConfig.events[event] === false) { + return { sent: false, reason: `Event type '${event}' is disabled` }; + } + + const results = {}; + const providers = notificationConfig.providers; + + if (providers.discord?.enabled) { + results.discord = await sendDiscordNotification(title, message, type); + } + + if (providers.telegram?.enabled) { + results.telegram = await sendTelegramNotification(title, message, type); + } + + if (providers.ntfy?.enabled) { + results.ntfy = await sendNtfyNotification(title, message, type); + } + + // Log to history + addNotificationToHistory({ + event, + title, + message, + type, + results + }); + + return { sent: true, results }; +} + +// Container health monitoring state +let containerHealthState = {}; +let healthCheckInterval = null; + +// Check container health and send notifications +async function checkContainerHealth() { + if (!notificationConfig.enabled || !notificationConfig.healthCheck?.enabled) { + return; + } + + try { + const containers = await docker.listContainers({ all: true }); + const services = (await exists(SERVICES_FILE)) + ? await servicesStateManager.read() + : []; + + // Create a map of container IDs to service names + const serviceMap = {}; + for (const service of services) { + if (service.containerId) { + serviceMap[service.containerId] = service.name || service.id; + } + } + + for (const container of containers) { + const containerId = container.Id; + const containerName = container.Names?.[0]?.replace(/^\//, '') || containerId.slice(0, 12); + const serviceName = serviceMap[containerId] || containerName; + const isRunning = container.State === 'running'; + const previousState = containerHealthState[containerId]; + + // Detect state changes + if (previousState !== undefined && previousState !== isRunning) { + if (isRunning) { + // Container came back up + await sendNotification( + 'containerUp', + 'Container Recovered', + `**${serviceName}** is now running again.`, + 'success' + ); + } else { + // Container went down + await sendNotification( + 'containerDown', + 'Container Down', + `**${serviceName}** has stopped running.\nStatus: ${container.Status}`, + 'error' + ); + } + } + + containerHealthState[containerId] = isRunning; + } + + // Update last check time + notificationConfig.healthCheck.lastCheck = new Date().toISOString(); + } catch (error) { + await logError('checkContainerHealth', error); + } +} + +// Start health check daemon +function startHealthCheckDaemon() { + if (healthCheckInterval) { + clearInterval(healthCheckInterval); + } + + if (!notificationConfig.healthCheck?.enabled) { + log.info('health', 'Health check daemon disabled'); + return; + } + + const intervalMs = (notificationConfig.healthCheck.intervalMinutes || 5) * 60 * 1000; + log.info('health', 'Starting health check daemon', { intervalMinutes: notificationConfig.healthCheck.intervalMinutes }); + + // Initial check + checkContainerHealth(); + + // Periodic checks + healthCheckInterval = setInterval(checkContainerHealth, intervalMs); +} + +// Stop health check daemon +function stopHealthCheckDaemon() { + if (healthCheckInterval) { + clearInterval(healthCheckInterval); + healthCheckInterval = null; + log.info('health', 'Health check daemon stopped'); + } +} + +// Load notification config on startup (async — resolved before server starts listening) +const _notificationsReady = (async () => { + await loadNotificationConfig(); + // Start health check if enabled + if (notificationConfig.healthCheck?.enabled) { + startHealthCheckDaemon(); + } +})(); + +// HTTPS agent for internal Caddy CA — load cert if available, keep system CAs too +const CA_CERT_PATH = process.env.CA_CERT_PATH || '/app/pki/root.crt'; +let httpsAgent; +try { + const caCert = fs.readFileSync(CA_CERT_PATH); + httpsAgent = new https.Agent({ ca: [...require('tls').rootCertificates, caCert] }); + log.info('server', 'HTTPS agent configured with CA certificate + system CAs', { path: CA_CERT_PATH }); +} catch { + httpsAgent = new https.Agent(); + log.error('server', 'CA cert not found — HTTPS calls to internal services may fail', { path: CA_CERT_PATH }); +} + +// ── Configure middleware stack (CORS, auth, rate limiting, etc.) ── +const middlewareResult = configureMiddleware(app, { + siteConfig, totpConfig, tailscaleConfig, + metrics, auditLogger, authManager, log, cryptoUtils, + isValidContainerId, isTailscaleIP, getTailscaleStatus, + RATE_LIMITS, LIMITS, APP, CACHE_CONFIGS, createCache +}); + +const { + strictLimiter, SESSION_DURATIONS, ipSessions, + getClientIP, createIPSession, setSessionCookie, + clearIPSession, clearSessionCookie, isSessionValid +} = middlewareResult; + +// ── Populate route context and mount extracted route modules ── + +// Namespaced groups +Object.assign(ctx.docker, { + client: docker, + pull: dockerPull, + findContainer: findContainerByName, + getUsedPorts, + security: dockerSecurity, +}); +Object.assign(ctx.caddy, { + modify: modifyCaddyfile, + read: readCaddyfile, + reload: reloadCaddy, + generateConfig: generateCaddyConfig, + verifySite: verifySiteAccessible, + adminUrl: CADDY_ADMIN_URL, + filePath: CADDYFILE_PATH, +}); +Object.assign(ctx.dns, { + call: callDns, + buildUrl: buildDnsUrl, + requireToken: requireDnsToken, + ensureToken: ensureValidDnsToken, + createRecord: createDnsRecord, + getToken: () => dnsToken, + setToken: (t) => { dnsToken = t; }, + getTokenExpiry: () => dnsTokenExpiry, + setTokenExpiry: (e) => { dnsTokenExpiry = e; }, + getTokenForServer, + invalidateTokenForServer: (serverIp) => { dnsServerTokens.delete(`${serverIp}:readonly`); dnsServerTokens.delete(`${serverIp}:admin`); }, + refresh: refreshDnsToken, + credentialsFile: DNS_CREDENTIALS_FILE, +}); +Object.assign(ctx.session, { + ipSessions, + durations: SESSION_DURATIONS, + getClientIP, + create: createIPSession, + setCookie: setSessionCookie, + clear: clearIPSession, + clearCookie: clearSessionCookie, + isValid: isSessionValid, +}); +Object.assign(ctx.notification, { + getConfig: () => notificationConfig, + saveConfig: saveNotificationConfig, + send: sendNotification, + sendDiscord: sendDiscordNotification, + sendTelegram: sendTelegramNotification, + sendNtfy: sendNtfyNotification, + getHistory: () => notificationHistory, + clearHistory: () => { notificationHistory = []; }, + startHealthDaemon: startHealthCheckDaemon, + stopHealthDaemon: stopHealthCheckDaemon, + checkHealth: checkContainerHealth, + getHealthState: () => containerHealthState, +}); +Object.assign(ctx.tailscale, { + config: tailscaleConfig, + save: saveTailscaleConfig, + getStatus: getTailscaleStatus, + getLocalIP: getLocalTailscaleIP, + isTailscaleIP, + getAccessToken: getTailscaleAccessToken, + syncAPI: syncFromTailscaleAPI, + startSync: startTailscaleSyncTimer, + stopSync: stopTailscaleSyncTimer, +}); + +// Flat properties (shared across domains) +Object.assign(ctx, { + app, siteConfig, servicesStateManager, configStateManager, + credentialManager, authManager, licenseManager, + healthChecker, updateManager, backupManager, resourceMonitor, + auditLogger, portLockManager, selfUpdater, dockerMaintenance, logDigest, + APP_TEMPLATES, TEMPLATE_CATEGORIES, DIFFICULTY_LEVELS, RECIPE_TEMPLATES, RECIPE_CATEGORIES, + asyncHandler, errorResponse, ok, fetchT, log, logError, safeErrorMessage, + buildDomain, buildServiceUrl, getServiceById, readConfig, saveConfig, addServiceToConfig, + validateURL, strictLimiter, + totpConfig, saveTotpConfig, + loadSiteConfig, loadNotificationConfig, + loadDnsCredentials: () => {}, + SERVICES_FILE, CONFIG_FILE, TOTP_CONFIG_FILE, TAILSCALE_CONFIG_FILE, + NOTIFICATIONS_FILE, ERROR_LOG_FILE, + resyncHealthChecker: () => syncHealthCheckerServices({ log, SERVICES_FILE, servicesStateManager, healthChecker, buildServiceUrl, siteConfig, APP }), +}); + +// Build versioned API router — all route modules attach here +const apiRouter = express.Router(); +apiRouter.use(authRoutes(ctx)); +apiRouter.use(configRoutes(ctx)); +apiRouter.use('/dns', dnsRoutes({ + dns: ctx.dns, + siteConfig: ctx.siteConfig, + asyncHandler: ctx.asyncHandler, + log: ctx.log, + safeErrorMessage: ctx.safeErrorMessage, + fetchT: ctx.fetchT, + credentialManager: ctx.credentialManager +})); +apiRouter.use('/notifications', notificationRoutes(ctx)); +apiRouter.use('/containers', containerRoutes({ + docker: ctx.docker, + log: ctx.log, + asyncHandler: ctx.asyncHandler +})); +apiRouter.use(serviceRoutes({ + servicesStateManager: ctx.servicesStateManager, + credentialManager: ctx.credentialManager, + siteConfig: ctx.siteConfig, + buildServiceUrl: ctx.buildServiceUrl, + buildDomain: ctx.buildDomain, + fetchT: ctx.fetchT, + asyncHandler: ctx.asyncHandler, + SERVICES_FILE: ctx.SERVICES_FILE, + log: ctx.log, + safeErrorMessage: ctx.safeErrorMessage, + resyncHealthChecker: ctx.resyncHealthChecker, + caddy: ctx.caddy, + dns: ctx.dns +})); +apiRouter.use(healthRoutes({ + fetchT: ctx.fetchT, + SERVICES_FILE: ctx.SERVICES_FILE, + servicesStateManager: ctx.servicesStateManager, + siteConfig: ctx.siteConfig, + buildServiceUrl: ctx.buildServiceUrl, + asyncHandler: ctx.asyncHandler, + logError: ctx.logError, + healthChecker: ctx.healthChecker +})); +apiRouter.use(monitoringRoutes({ + resourceMonitor: ctx.resourceMonitor, + docker: ctx.docker, + asyncHandler: ctx.asyncHandler +})); +apiRouter.use(updatesRoutes(ctx)); +apiRouter.use('/tailscale', tailscaleRoutes(ctx)); +apiRouter.use(sitesRoutes(ctx)); +apiRouter.use(credentialsRoutes({ + credentialManager: ctx.credentialManager, + asyncHandler: ctx.asyncHandler +})); +apiRouter.use(arrRoutes(ctx)); +apiRouter.use(appsRoutes(ctx)); +apiRouter.use(logsRoutes(ctx)); +apiRouter.use(backupsRoutes({ + backupManager: ctx.backupManager, + asyncHandler: ctx.asyncHandler +})); +apiRouter.use('/ca', caRoutes(ctx)); +apiRouter.use(browseRoutes(ctx)); +apiRouter.use(errorLogsRoutes({ + ERROR_LOG_FILE: ctx.ERROR_LOG_FILE, + auditLogger: ctx.auditLogger, + asyncHandler: ctx.asyncHandler +})); +apiRouter.use('/license', licenseRoutes({ + licenseManager: ctx.licenseManager, + asyncHandler: ctx.asyncHandler +})); +apiRouter.use('/recipes', recipesRoutes(ctx)); +apiRouter.use(themesRoutes({ asyncHandler })); + +// Inline routes on the API router +apiRouter.get('/health', (req, res) => { + res.json({ status: 'ok', timestamp: new Date().toISOString() }); +}); +apiRouter.get('/csrf-token', (req, res) => { + res.json({ success: true, token: req.csrfToken, headerName: CSRF_HEADER_NAME }); +}); +apiRouter.get('/metrics', (req, res) => { + res.json({ success: true, metrics: metrics.getSummary() }); +}); + +// Mount at /api/v1 (canonical) and /api (legacy compat for tests & external consumers) +app.use('/api/v1', apiRouter); +app.use('/api', apiRouter); + +// Root-level health check (no /api prefix) +app.get('/health', (req, res) => { + res.json({ status: 'ok', timestamp: new Date().toISOString() }); +}); + +// Lightweight probe endpoint - performs real health checks for frontend status dots +app.get('/probe/:id', asyncHandler(async (req, res) => { + const id = req.params.id; + + try { + // Look up service in services.json + let service = null; + if (id !== 'internet' && await exists(SERVICES_FILE)) { + const data = await servicesStateManager.read(); + const services = Array.isArray(data) ? data : data.services || []; + service = services.find(s => s.id === id); + } + + const url = resolveServiceUrl(id, service, siteConfig, buildServiceUrl); + + const parsed = new URL(url); + const isHttps = parsed.protocol === 'https:'; + const lib = isHttps ? https : http; + + const options = { + hostname: parsed.hostname, + port: parsed.port || (isHttps ? 443 : 80), + path: parsed.pathname + parsed.search, + method: 'HEAD', + timeout: 5000, + agent: isHttps ? httpsAgent : undefined, + headers: { 'User-Agent': APP.USER_AGENTS.PROBE }, + }; + + const makeRequest = (method) => new Promise((resolve, reject) => { + const reqOpts = { ...options, method }; + const probeReq = lib.request(reqOpts, (response) => { + response.resume(); + resolve(response.statusCode); + }); + probeReq.on('error', reject); + probeReq.on('timeout', () => { probeReq.destroy(); reject(new Error('Timeout')); }); + probeReq.end(); + }); + + let statusCode; + try { + statusCode = await makeRequest('HEAD'); + // Fall back to GET if HEAD is not supported + if (statusCode === 501 || statusCode === 405) { + statusCode = await makeRequest('GET'); + } + } catch { + // Direct URL failed (e.g. HTTP/1.0 parse errors) — try via Caddy domain + const fallbackUrl = `https://${buildDomain(id)}`; + const fp = new URL(fallbackUrl); + const fLib = require('https'); + statusCode = await new Promise((resolve, reject) => { + const fReq = fLib.request({ + hostname: fp.hostname, port: 443, path: '/', method: 'GET', + timeout: 5000, agent: httpsAgent, + headers: { 'User-Agent': APP.USER_AGENTS.PROBE } + }, (fRes) => { fRes.resume(); resolve(fRes.statusCode); }); + fReq.on('error', reject); + fReq.on('timeout', () => { fReq.destroy(); reject(new Error('Timeout')); }); + fReq.end(); + }); + } + + res.status(statusCode).send(); + } catch { + res.status(502).send(); + } +}, 'probe')); + +// Get network IPs (LAN, Tailscale) for quick selection +app.get('/api/network/ips', (req, res) => { + try { + // Prefer environment variables (set in docker-compose.yml) + const envLan = process.env.HOST_LAN_IP; + const envTailscale = process.env.HOST_TAILSCALE_IP; + + const result = { + localhost: '127.0.0.1', + lan: envLan || null, + tailscale: envTailscale || null, + all: [] + }; + + // If env vars not set, try to detect from network interfaces + if (!envLan || !envTailscale) { + const interfaces = os.networkInterfaces(); + + for (const [name, addrs] of Object.entries(interfaces)) { + for (const addr of addrs) { + // Skip internal and IPv6 + if (addr.internal || addr.family !== 'IPv4') continue; + + const ip = addr.address; + result.all.push({ name, ip }); + + // Detect Tailscale (100.x.x.x range) + if (!result.tailscale && ip.startsWith('100.')) { + result.tailscale = ip; + } + // Detect common LAN ranges + else if (!result.lan && (ip.startsWith('192.168.') || ip.startsWith('10.') || ip.match(/^172\.(1[6-9]|2[0-9]|3[0-1])\./))) { + result.lan = ip; + } + } + } + } + + // Return null if not detected — let the frontend handle it + if (!result.lan) result.lan = null; + if (!result.tailscale) result.tailscale = null; + + res.json(result); + } catch (error) { + errorResponse(res, 500, safeErrorMessage(error)); + } +}); + + +// (TOTP/auth inline routes moved to routes/auth.js) + +// (SSO auth gate + getAppSession moved to routes/auth.js) + +// (Tailscale routes moved to routes/tailscale.js) + +// (Caddy/site routes moved to routes/sites.js) + +// (Assets, config, backup routes moved to routes/config.js) + + +// (Credential management routes moved to routes/credentials.js) + +// ===== DNS TOKEN AUTO-REFRESH FUNCTIONS ===== + +async function refreshDnsToken(username, password, server) { + try { + // Use /api/user/login to get a session token + const params = new URLSearchParams({ + user: username, + pass: password, + includeInfo: 'false' + }); + + const response = await fetchT( + `http://${server}:5380/api/user/login?${params.toString()}`, + { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded' + }, + timeout: 10000 + } + ); + + const result = await response.json(); + + if (result.status === 'ok' && result.token) { + dnsToken = result.token; + // Technitium session tokens expire after inactivity, refresh every 6 hours to keep alive + dnsTokenExpiry = new Date(Date.now() + SESSION_TTL.DNS_TOKEN).toISOString(); + log.info('dns', 'DNS token refreshed', { expires: dnsTokenExpiry }); + return { success: true, token: dnsToken }; + } + + return { success: false, error: result.errorMessage || 'Login failed' }; + } catch (error) { + log.error('dns', 'DNS token refresh error', { error: error.message }); + return { success: false, error: error.message }; + } +} + +async function ensureValidDnsToken() { + // Check if token is valid and not expired + if (dnsToken && dnsTokenExpiry && new Date() < new Date(dnsTokenExpiry)) { + return { success: true, token: dnsToken }; + } + + // Try per-server admin credentials for the primary DNS server + const primaryIp = siteConfig.dnsServerIp; + if (primaryIp) { + const dnsId = dnsIpToDnsId(primaryIp); + if (dnsId) { + // Try admin credentials first (used for DNS record operations) + for (const role of ['admin', 'readonly']) { + try { + const username = await credentialManager.retrieve(`dns.${dnsId}.${role}.username`); + const password = await credentialManager.retrieve(`dns.${dnsId}.${role}.password`); + if (username && password) { + return await refreshDnsToken(username, password, primaryIp); + } + } catch (err) { + log.error('dns', `Per-server ${role} credential error for primary DNS`, { dnsId, error: err.message }); + } + } + } + } + + // Fall back to global credentials + try { + const username = await credentialManager.retrieve('dns.username'); + const password = await credentialManager.retrieve('dns.password'); + const server = await credentialManager.retrieve('dns.server'); + if (username && password) { + return await refreshDnsToken(username, password, server || primaryIp); + } + } catch (err) { + log.error('dns', 'Credential manager error', { error: err.message }); + } + + return { + success: false, + error: 'No DNS credentials configured. Please set up credentials via /api/dns/credentials' + }; +} + +// Map a DNS server IP to its dnsId (dns1, dns2, dns3) using config +function dnsIpToDnsId(serverIp) { + for (const [dnsId, info] of Object.entries(siteConfig.dnsServers || {})) { + if (info.ip === serverIp) return dnsId; + } + return null; +} + +// Get a valid token for a specific DNS server (authenticates directly against that server) +// role: 'readonly' for logs, 'admin' for records/restart/updates (defaults to 'readonly') +async function getTokenForServer(targetServer, role = 'readonly') { + const cacheKey = `${targetServer}:${role}`; + + // Check cached per-server token first + const cached = dnsServerTokens.get(cacheKey); + if (cached && cached.token && cached.expiry && new Date() < new Date(cached.expiry)) { + return { success: true, token: cached.token }; + } + + const serverPort = siteConfig.dnsServerPort || '5380'; + + // Helper to authenticate against a DNS server via login + async function authenticateToServer(username, password) { + const params = new URLSearchParams({ + user: username, + pass: password, + includeInfo: 'false' + }); + + const response = await fetchT( + `http://${targetServer}:${serverPort}/api/user/login?${params.toString()}`, + { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded' + } + } + ); + + const result = await response.json(); + + if (result.status === 'ok' && result.token) { + dnsServerTokens.set(cacheKey, { + token: result.token, + expiry: new Date(Date.now() + SESSION_TTL.DNS_TOKEN).toISOString() + }); + log.info('dns', 'DNS token obtained for server', { server: targetServer, role }); + return { success: true, token: result.token }; + } + + return { success: false, error: result.errorMessage || 'Login failed' }; + } + + const dnsId = dnsIpToDnsId(targetServer); + + // Try per-server credentials with the requested role first + if (dnsId) { + try { + const username = await credentialManager.retrieve(`dns.${dnsId}.${role}.username`); + const password = await credentialManager.retrieve(`dns.${dnsId}.${role}.password`); + if (username && password) { + return await authenticateToServer(username, password); + } + } catch (err) { + log.error('dns', `Per-server ${role} credential error`, { dnsId, server: targetServer, error: err.message }); + } + + // Fall back to the other role (readonly -> admin or admin -> readonly) + const fallbackRole = role === 'readonly' ? 'admin' : 'readonly'; + try { + const username = await credentialManager.retrieve(`dns.${dnsId}.${fallbackRole}.username`); + const password = await credentialManager.retrieve(`dns.${dnsId}.${fallbackRole}.password`); + if (username && password) { + return await authenticateToServer(username, password); + } + } catch (err) { + // ignore fallback errors + } + } + + // Fall back to global credentials + try { + const username = await credentialManager.retrieve('dns.username'); + const password = await credentialManager.retrieve('dns.password'); + if (username && password) { + return await authenticateToServer(username, password); + } + } catch (err) { + log.error('dns', 'Credential manager error', { server: targetServer, error: err.message }); + } + + return { success: false, error: 'No DNS credentials configured' }; +} + +// Load credentials and refresh token on startup +(async function initDnsToken() { + if (dnsToken) { + log.info('dns', 'Using DNS token from environment variable'); + return; + } + + // Get token using credential manager + const result = await ensureValidDnsToken(); + if (result.success) { + log.info('dns', 'DNS token obtained from stored credentials'); + } else if (await credentialManager.retrieve('dns.username')) { + log.warn('dns', 'Failed to get DNS token', { error: result.error }); + } else { + log.info('dns', 'No DNS credentials configured - DNS record management unavailable'); + } +})(); + +// (Arr stack routes moved to routes/arr.js) +// (App deployment routes moved to routes/apps.js) +// (Container management routes moved to routes/containers.js) +// (Docker helper functions moved to routes/apps.js) + +function generateCaddyConfig(subdomain, ip, port, options = {}) { + const { tailscaleOnly = false, allowedIPs = [], subpathSupport = 'strip' } = options; + + // Subdirectory mode: generate handle/handle_path block (injected into main domain block) + if (siteConfig.routingMode === 'subdirectory' && siteConfig.domain) { + let config = ''; + + // Native-support apps: use handle (preserve path prefix) + // Strip-mode apps: use handle_path (remove path prefix before proxying) + if (subpathSupport === 'native') { + config += `\tredir /${subdomain} /${subdomain}/ permanent\n`; + config += `\thandle /${subdomain}/* {\n`; + } else { + config += `\thandle_path /${subdomain}/* {\n`; + } + + if (tailscaleOnly) { + config += `\t\t@blocked not remote_ip 100.64.0.0/10`; + if (allowedIPs.length > 0) config += ` ${allowedIPs.join(' ')}`; + config += `\n\t\trespond @blocked "Access denied. Tailscale connection required." 403\n`; + } + + config += `\t\treverse_proxy ${ip}:${port}\n`; + config += `\t}`; + return config; + } + + // Subdomain mode (default): standalone domain block + let config = `${buildDomain(subdomain)} {\n`; + + if (tailscaleOnly) { + config += ` @blocked not remote_ip 100.64.0.0/10`; + if (allowedIPs.length > 0) { + config += ` ${allowedIPs.join(' ')}`; + } + config += `\n respond @blocked "Access denied. Tailscale connection required." 403\n`; + } + + config += ` reverse_proxy ${ip}:${port}\n`; + config += ` tls internal\n`; + config += `}`; + + return config; +} + +// (generateStaticSiteConfig, addCaddyConfig, pullImage moved to routes/apps.js) + +async function reloadCaddy(content) { + const maxRetries = RETRIES.CADDY_RELOAD; + let lastError = null; + + for (let i = 0; i < maxRetries; i++) { + try { + const response = await fetchT(`${CADDY_ADMIN_URL}/load`, { + method: 'POST', + headers: { 'Content-Type': CADDY.CONTENT_TYPE }, + body: content + }); + + if (response.ok) { + log.info('caddy', 'Caddy configuration reloaded successfully'); + // Wait a moment for Caddy to fully apply the config + await new Promise(resolve => setTimeout(resolve, 1000)); + return; + } + + lastError = await response.text(); + log.warn('caddy', 'Caddy reload attempt failed', { attempt: i + 1, error: lastError }); + } catch (error) { + lastError = error.message; + log.warn('caddy', 'Caddy reload attempt error', { attempt: i + 1, error: lastError }); + } + + if (i < maxRetries - 1) { + await new Promise(resolve => setTimeout(resolve, 2000)); + } + } + + throw new Error(`[DC-303] Caddy reload failed after ${maxRetries} attempts: ${lastError}`); +} + +async function verifySiteAccessible(domain, maxAttempts = 5) { + const delay = 2000; + + for (let i = 0; i < maxAttempts; i++) { + try { + // Try HTTPS first (internal CA) + const response = await fetchT(`https://${domain}/`, { + method: 'HEAD', + agent: httpsAgent, // Ignore cert errors for internal CA + timeout: 5000 + }); + + // Any response (even 4xx) means Caddy is serving the site + log.info('caddy', 'Site is accessible', { domain, status: response.status }); + return true; + } catch (error) { + log.debug('caddy', 'Site verification attempt', { domain, attempt: i + 1, maxAttempts, error: error.message }); + } + + if (i < maxAttempts - 1) { + await new Promise(resolve => setTimeout(resolve, delay)); + } + } + + log.warn('caddy', 'Could not verify site accessibility, but deployment may still have succeeded', { domain }); + return false; +} + +async function createDnsRecord(subdomain, ip) { + // Ensure we have a valid token (auto-refresh if needed) + const tokenResult = await ensureValidDnsToken(); + if (!tokenResult.success) { + throw new Error(`DNS token not available: ${tokenResult.error}. Configure credentials via POST /api/dns/credentials`); + } + + const domain = buildDomain(subdomain); + const zone = siteConfig.tld.replace(/^\./, ''); + + const dnsParams = { token: dnsToken, domain, zone, type: 'A', ipAddress: ip, ttl: '300', overwrite: 'true' }; + const callDnsApi = () => callDns(siteConfig.dnsServerIp, '/api/zones/records/add', dnsParams); + + try { + log.info('dns', 'Creating DNS record', { domain, ip }); + const result = await callDnsApi(); + + if (result.status === 'ok') { + log.info('dns', 'DNS record created', { domain, ip }); + return { success: true }; + } + + // Check for token expired error - try to refresh once + if (result.errorMessage && result.errorMessage.toLowerCase().includes('token')) { + log.info('dns', 'Token appears expired, attempting auto-refresh'); + const refreshResult = await ensureValidDnsToken(); + if (!refreshResult.success) throw new Error(`Token refresh failed: ${refreshResult.error}`); + + const retryResult = await callDnsApi(); + if (retryResult.status === 'ok') { + log.info('dns', 'DNS record created after token refresh', { domain, ip }); + return { success: true }; + } + throw new Error(retryResult.errorMessage || 'Unknown error after token refresh'); + } + + throw new Error(result.errorMessage || 'Unknown error'); + } catch (error) { + throw new Error(`Failed to create DNS record for ${domain}: ${error.message}`); + } +} + +async function addServiceToConfig(service) { + try { + await servicesStateManager.update(services => { + // Check if service already exists + const existingIndex = services.findIndex(s => s.id === service.id); + if (existingIndex >= 0) { + // Update existing service + services[existingIndex] = { ...services[existingIndex], ...service }; + } else { + // Add new service + services.push(service); + } + return services; + }); + log.info('deploy', 'Service added to config', { serviceId: service.id }); + // Sync health checker with updated services list + ctx.resyncHealthChecker?.().catch(() => {}); + } catch (error) { + log.error('deploy', 'Failed to add service to config', { serviceId: service.id, error: error.message }); + throw error; + } +} + +// (Notification routes moved to routes/notifications.js) +// (Stats routes moved to routes/monitoring.js) +// (Container logs routes moved to routes/logs.js) +// (Health service routes moved to routes/health.js) +// (Resource monitoring routes moved to routes/monitoring.js) +// (Backup routes moved to routes/backups.js) +// (CA routes moved to routes/ca.js) +// (Error log, audit log, browse/media routes moved to route modules) + +// API Documentation endpoint +app.get('/api/docs', (req, res) => { + res.setHeader('Content-Security-Policy', "default-src 'self'; script-src 'self' 'unsafe-inline' https://unpkg.com; style-src 'self' 'unsafe-inline' https://unpkg.com; img-src 'self' data: https:; connect-src 'self'; font-src 'self' data: https://unpkg.com;"); + res.send(` + + + +