feat: add Pylon health relay for remote service health checks
DashCaddy Pylon is a lightweight probe agent that runs on remote
networks to relay health checks for services the main DashCaddy
instance can't reach directly (e.g., .sami domains, LAN IPs).
- Standalone zero-dependency Node.js script (pylon/dashcaddy-pylon.js)
- Optional API key auth, HEAD→GET fallback, batch probe support
- Health routes now try direct check first, fall back to pylon relay
- New endpoints: /health/probe (act as pylon), /health/pylon (status)
- Config: add "pylon": { "url": "...", "key": "..." } to config.json
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,70 @@ module.exports = function(ctx) {
|
||||
let serviceHealthCache = {};
|
||||
let lastHealthCheck = null;
|
||||
|
||||
/**
|
||||
* Check a URL directly via fetch. Returns health object or null on failure.
|
||||
*/
|
||||
async function checkDirect(url) {
|
||||
// Try HEAD first
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||
const response = await ctx.fetchT(url, { method: 'HEAD', signal: controller.signal, redirect: 'follow' });
|
||||
clearTimeout(timeout);
|
||||
return {
|
||||
status: response.ok || response.status < 500 ? 'healthy' : 'unhealthy',
|
||||
statusCode: response.status,
|
||||
url,
|
||||
checkedAt: new Date().toISOString()
|
||||
};
|
||||
} catch (_) {}
|
||||
|
||||
// Fallback to GET
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||
const response = await ctx.fetchT(url, { method: 'GET', signal: controller.signal, redirect: 'follow' });
|
||||
clearTimeout(timeout);
|
||||
return {
|
||||
status: response.ok || response.status < 500 ? 'healthy' : 'unhealthy',
|
||||
statusCode: response.status,
|
||||
url,
|
||||
checkedAt: new Date().toISOString()
|
||||
};
|
||||
} catch (e) {
|
||||
return null; // Direct check completely failed
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check a URL through a Pylon relay. Returns health object or null on failure.
|
||||
*/
|
||||
async function checkViaPylon(pylonConfig, url) {
|
||||
if (!pylonConfig?.url) return null;
|
||||
try {
|
||||
const probeUrl = `${pylonConfig.url}/probe?url=${encodeURIComponent(url)}`;
|
||||
const headers = {};
|
||||
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 });
|
||||
clearTimeout(timeout);
|
||||
if (!response.ok) return null;
|
||||
const data = await response.json();
|
||||
return {
|
||||
status: data.status || 'unhealthy',
|
||||
statusCode: data.statusCode,
|
||||
responseTime: data.responseTime,
|
||||
reason: data.reason,
|
||||
url,
|
||||
via: 'pylon',
|
||||
checkedAt: data.checkedAt || new Date().toISOString()
|
||||
};
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== HEALTH / SERVICES =====
|
||||
|
||||
// Check health of all services (performs live checks)
|
||||
@@ -26,6 +90,7 @@ module.exports = function(ctx) {
|
||||
const servicesData = await ctx.servicesStateManager.read();
|
||||
const services = Array.isArray(servicesData) ? servicesData : servicesData.services || [];
|
||||
const health = {};
|
||||
const pylonConfig = ctx.siteConfig?.pylon;
|
||||
|
||||
// Check each service
|
||||
await Promise.all(services.map(async (service) => {
|
||||
@@ -33,65 +98,35 @@ module.exports = function(ctx) {
|
||||
if (!serviceId) return;
|
||||
|
||||
try {
|
||||
let url = null;
|
||||
let checkType = 'http';
|
||||
|
||||
// Determine URL to check
|
||||
url = resolveServiceUrl(serviceId, service, ctx.siteConfig, ctx.buildServiceUrl);
|
||||
|
||||
const url = resolveServiceUrl(serviceId, service, ctx.siteConfig, ctx.buildServiceUrl);
|
||||
if (!url) {
|
||||
health[serviceId] = { status: 'unknown', reason: 'No URL configured' };
|
||||
return;
|
||||
}
|
||||
|
||||
// Perform health check with timeout
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||
// 1. Try direct check
|
||||
let result = await checkDirect(url);
|
||||
if (result) {
|
||||
health[serviceId] = result;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await ctx.fetchT(url, {
|
||||
method: 'HEAD',
|
||||
signal: controller.signal,
|
||||
redirect: 'follow'
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
|
||||
health[serviceId] = {
|
||||
status: response.ok || response.status < 500 ? 'healthy' : 'unhealthy',
|
||||
statusCode: response.status,
|
||||
url,
|
||||
checkedAt: new Date().toISOString()
|
||||
};
|
||||
} catch (fetchError) {
|
||||
clearTimeout(timeout);
|
||||
|
||||
// Try GET if HEAD fails
|
||||
try {
|
||||
const getController = new AbortController();
|
||||
const getTimeout = setTimeout(() => getController.abort(), 5000);
|
||||
|
||||
const getResponse = await ctx.fetchT(url, {
|
||||
method: 'GET',
|
||||
signal: getController.signal,
|
||||
redirect: 'follow'
|
||||
});
|
||||
clearTimeout(getTimeout);
|
||||
|
||||
health[serviceId] = {
|
||||
status: getResponse.ok || getResponse.status < 500 ? 'healthy' : 'unhealthy',
|
||||
statusCode: getResponse.status,
|
||||
url,
|
||||
checkedAt: new Date().toISOString()
|
||||
};
|
||||
} catch (e) {
|
||||
health[serviceId] = {
|
||||
status: 'unhealthy',
|
||||
reason: e.name === 'AbortError' ? 'Timeout' : e.message,
|
||||
url,
|
||||
checkedAt: new Date().toISOString()
|
||||
};
|
||||
// 2. Direct failed — try through pylon relay
|
||||
if (pylonConfig) {
|
||||
result = await checkViaPylon(pylonConfig, url);
|
||||
if (result) {
|
||||
health[serviceId] = result;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Both failed
|
||||
health[serviceId] = {
|
||||
status: 'unhealthy',
|
||||
reason: pylonConfig ? 'Unreachable (direct + pylon)' : 'fetch failed',
|
||||
url,
|
||||
checkedAt: new Date().toISOString()
|
||||
};
|
||||
} catch (e) {
|
||||
health[serviceId] = {
|
||||
status: 'error',
|
||||
@@ -148,44 +183,62 @@ module.exports = function(ctx) {
|
||||
|
||||
// Determine URL
|
||||
const url = resolveServiceUrl(serviceId, service, ctx.siteConfig, ctx.buildServiceUrl);
|
||||
const pylonConfig = ctx.siteConfig?.pylon;
|
||||
|
||||
// Check health
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||
|
||||
try {
|
||||
const response = await ctx.fetchT(url, {
|
||||
method: 'GET',
|
||||
signal: controller.signal,
|
||||
redirect: 'follow'
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
serviceId,
|
||||
health: {
|
||||
status: response.ok || response.status < 500 ? 'healthy' : 'unhealthy',
|
||||
statusCode: response.status,
|
||||
url,
|
||||
checkedAt: new Date().toISOString()
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
clearTimeout(timeout);
|
||||
res.json({
|
||||
success: true,
|
||||
serviceId,
|
||||
health: {
|
||||
status: 'unhealthy',
|
||||
reason: e.name === 'AbortError' ? 'Timeout' : e.message,
|
||||
url,
|
||||
checkedAt: new Date().toISOString()
|
||||
}
|
||||
});
|
||||
// Try direct, then pylon relay
|
||||
let result = await checkDirect(url);
|
||||
if (!result && pylonConfig) {
|
||||
result = await checkViaPylon(pylonConfig, url);
|
||||
}
|
||||
if (!result) {
|
||||
result = {
|
||||
status: 'unhealthy',
|
||||
reason: pylonConfig ? 'Unreachable (direct + pylon)' : 'fetch failed',
|
||||
url,
|
||||
checkedAt: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
|
||||
res.json({ success: true, 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) => {
|
||||
const targetUrl = req.query.url;
|
||||
if (!targetUrl) {
|
||||
return ctx.errorResponse(res, 400, 'Missing ?url= parameter');
|
||||
}
|
||||
const result = await checkDirect(targetUrl);
|
||||
res.json(result || {
|
||||
status: 'unhealthy',
|
||||
reason: 'fetch failed',
|
||||
url: targetUrl,
|
||||
checkedAt: new Date().toISOString()
|
||||
});
|
||||
}, '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;
|
||||
if (!pylonConfig?.url) {
|
||||
return res.json({ success: true, configured: false });
|
||||
}
|
||||
try {
|
||||
const headers = {};
|
||||
if (pylonConfig.key) headers['x-pylon-key'] = pylonConfig.key;
|
||||
const response = await ctx.fetchT(`${pylonConfig.url}/health`, {
|
||||
method: 'GET',
|
||||
headers
|
||||
}, 5000);
|
||||
const data = await response.json();
|
||||
res.json({ success: true, configured: true, reachable: true, pylon: data });
|
||||
} catch (e) {
|
||||
res.json({ success: true, configured: true, reachable: false, error: e.message });
|
||||
}
|
||||
}, 'health-pylon'));
|
||||
|
||||
// ===== HEALTH / CA =====
|
||||
|
||||
// Get CA certificate health status
|
||||
|
||||
Reference in New Issue
Block a user