|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const express = require('express'); |
| 4 | +const { PORT } = require('../config/env'); |
| 5 | + |
| 6 | +function resolveServerUrl(req) { |
| 7 | + const proto = req.headers['x-forwarded-proto'] || req.protocol || 'http'; |
| 8 | + const host = req.headers['x-forwarded-host'] || req.headers.host || `localhost:${PORT}`; |
| 9 | + return `${proto}://${host}`.replace(/\/$/, ''); |
| 10 | +} |
| 11 | + |
| 12 | +function parseTimeQuery(value, fallback) { |
| 13 | + if (value === undefined || value === null || value === '') return fallback; |
| 14 | + const numeric = Number(value); |
| 15 | + if (Number.isFinite(numeric)) return numeric; |
| 16 | + return new Date(String(value)).getTime(); |
| 17 | +} |
| 18 | + |
| 19 | +function createProbeAgentPublicRouter({ probeAgentService }) { |
| 20 | + const router = express.Router(); |
| 21 | + |
| 22 | + router.post('/agent/probe/register', (req, res, next) => { |
| 23 | + try { |
| 24 | + const result = probeAgentService.registerAgent(req.body || {}); |
| 25 | + res.json({ ok: true, ...result }); |
| 26 | + } catch (error) { |
| 27 | + next(error); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + router.post('/agent/probe/report', (req, res, next) => { |
| 32 | + try { |
| 33 | + const agent = probeAgentService.authenticateAgent(req.headers.authorization); |
| 34 | + if (!agent) return res.status(401).json({ ok: false, error: 'agent token 无效' }); |
| 35 | + const result = probeAgentService.saveReport(agent, req.body || {}); |
| 36 | + return res.json({ ok: true, ...result }); |
| 37 | + } catch (error) { |
| 38 | + next(error); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + router.get('/agent/probe/commands/next', async (req, res, next) => { |
| 43 | + try { |
| 44 | + const agent = probeAgentService.authenticateAgent(req.headers.authorization); |
| 45 | + if (!agent) return res.status(401).json({ ok: false, error: 'agent token 无效' }); |
| 46 | + const command = await probeAgentService.waitForNextCommand(agent); |
| 47 | + if (!command) return res.status(204).end(); |
| 48 | + return res.json({ ok: true, command }); |
| 49 | + } catch (error) { |
| 50 | + next(error); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + router.post('/agent/probe/commands/:commandId/result', (req, res, next) => { |
| 55 | + try { |
| 56 | + const agent = probeAgentService.authenticateAgent(req.headers.authorization); |
| 57 | + if (!agent) return res.status(401).json({ ok: false, error: 'agent token 无效' }); |
| 58 | + const result = probeAgentService.completeCommand(agent, req.params.commandId, req.body || {}); |
| 59 | + return res.json(result); |
| 60 | + } catch (error) { |
| 61 | + next(error); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + return router; |
| 66 | +} |
| 67 | + |
| 68 | +function createProbeAgentAdminRouter({ probeAgentService, probeAgentInstallerService, probeAggregatorService }) { |
| 69 | + const router = express.Router(); |
| 70 | + |
| 71 | + router.post('/probe-agents/:hostId/install-token', (req, res, next) => { |
| 72 | + try { |
| 73 | + const result = probeAgentService.generateInstallToken(req.params.hostId); |
| 74 | + const serverUrl = String(req.body?.serverUrl || resolveServerUrl(req)).replace(/\/$/, ''); |
| 75 | + res.json({ |
| 76 | + ok: true, |
| 77 | + ...result, |
| 78 | + serverUrl, |
| 79 | + config: { |
| 80 | + hostId: result.hostId, |
| 81 | + installToken: result.installToken, |
| 82 | + serverUrl, |
| 83 | + registerUrl: `${serverUrl}/api/agent/probe/register`, |
| 84 | + reportUrl: `${serverUrl}/api/agent/probe/report`, |
| 85 | + }, |
| 86 | + }); |
| 87 | + } catch (error) { |
| 88 | + next(error); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + router.post('/probe-agents/:hostId/install', async (req, res, next) => { |
| 93 | + try { |
| 94 | + if (!probeAgentInstallerService) return res.status(501).json({ ok: false, error: 'Agent 安装器未启用' }); |
| 95 | + const serverUrl = String(req.body?.serverUrl || resolveServerUrl(req)).replace(/\/$/, ''); |
| 96 | + const intervalSec = typeof req.body?.intervalSec === 'number' ? req.body.intervalSec : undefined; |
| 97 | + const relayUpstreamId = typeof req.body?.relayUpstreamId === 'string' ? req.body.relayUpstreamId : undefined; |
| 98 | + const install = await probeAgentInstallerService.install(req.params.hostId, { |
| 99 | + serverUrl, |
| 100 | + intervalSec, |
| 101 | + relayUpstreamId, |
| 102 | + clientIp: req.ip, |
| 103 | + }); |
| 104 | + res.json({ |
| 105 | + ok: true, |
| 106 | + hostId: install.hostId, |
| 107 | + expiresAt: install.expiresAt, |
| 108 | + serverUrl: install.serverUrl || serverUrl, |
| 109 | + relayUpstreamId: install.relayUpstreamId, |
| 110 | + result: install.result, |
| 111 | + }); |
| 112 | + } catch (error) { |
| 113 | + next(error); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + router.post('/probe-agents/:hostId/uninstall', async (req, res, next) => { |
| 118 | + try { |
| 119 | + if (!probeAgentInstallerService) return res.status(501).json({ ok: false, error: 'Agent 安装器未启用' }); |
| 120 | + const result = await probeAgentInstallerService.uninstall(req.params.hostId, { clientIp: req.ip }); |
| 121 | + res.json({ ok: true, ...result }); |
| 122 | + } catch (error) { |
| 123 | + next(error); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + router.post('/probe-agents/:hostId/revoke', (req, res, next) => { |
| 128 | + try { |
| 129 | + res.json(probeAgentService.revokeAgent(req.params.hostId)); |
| 130 | + } catch (error) { |
| 131 | + next(error); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + router.post('/probe-agents/:hostId/restart', async (req, res, next) => { |
| 136 | + try { |
| 137 | + if (!probeAgentInstallerService) return res.status(501).json({ ok: false, error: 'Agent 安装器未启用' }); |
| 138 | + const result = await probeAgentInstallerService.restart(req.params.hostId, { clientIp: req.ip }); |
| 139 | + res.json({ ok: true, ...result }); |
| 140 | + } catch (error) { |
| 141 | + next(error); |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + router.get('/probe-agents/:hostId/logs', async (req, res, next) => { |
| 146 | + try { |
| 147 | + if (!probeAgentInstallerService) return res.status(501).json({ ok: false, error: 'Agent 安装器未启用' }); |
| 148 | + const lines = parseInt(req.query.lines, 10) || 200; |
| 149 | + const result = await probeAgentInstallerService.fetchLogs(req.params.hostId, { clientIp: req.ip, lines }); |
| 150 | + res.json({ ok: true, ...result }); |
| 151 | + } catch (error) { |
| 152 | + next(error); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + router.get('/probe-agents/:hostId/samples', (req, res, next) => { |
| 157 | + try { |
| 158 | + const minutes = parseInt(req.query.minutes, 10); |
| 159 | + const sinceMs = Number.isFinite(minutes) && minutes > 0 |
| 160 | + ? Math.min(minutes, 60 * 24) * 60 * 1000 |
| 161 | + : 60 * 60 * 1000; |
| 162 | + const samples = probeAgentService.getSampleHistory(req.params.hostId, { sinceMs }); |
| 163 | + res.json({ ok: true, hostId: req.params.hostId, sinceMs, samples }); |
| 164 | + } catch (error) { |
| 165 | + next(error); |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + router.post('/probe-agents/samples-bulk', (req, res, next) => { |
| 170 | + try { |
| 171 | + const minutes = Number(req.body?.minutes); |
| 172 | + const sinceMs = Number.isFinite(minutes) && minutes > 0 |
| 173 | + ? Math.min(minutes, 60 * 24) * 60 * 1000 |
| 174 | + : 60 * 60 * 1000; |
| 175 | + const hostIds = Array.isArray(req.body?.hostIds) ? req.body.hostIds.filter((id) => typeof id === 'string').slice(0, 200) : []; |
| 176 | + const result = {}; |
| 177 | + for (const hostId of hostIds) { |
| 178 | + result[hostId] = probeAgentService.getSampleHistory(hostId, { sinceMs }); |
| 179 | + } |
| 180 | + res.json({ ok: true, sinceMs, samples: result }); |
| 181 | + } catch (error) { |
| 182 | + next(error); |
| 183 | + } |
| 184 | + }); |
| 185 | + |
| 186 | + router.get('/probe-agents/:hostId/timeseries', (req, res, next) => { |
| 187 | + try { |
| 188 | + if (!probeAggregatorService) return res.status(501).json({ ok: false, error: '聚合服务未启用' }); |
| 189 | + const now = Date.now(); |
| 190 | + const to = parseTimeQuery(req.query.to, now); |
| 191 | + const from = parseTimeQuery(req.query.from, to - 24 * 60 * 60 * 1000); |
| 192 | + if (!Number.isFinite(from) || !Number.isFinite(to) || from >= to) { |
| 193 | + return res.status(400).json({ ok: false, error: 'from/to 参数非法' }); |
| 194 | + } |
| 195 | + const resolution = typeof req.query.resolution === 'string' ? req.query.resolution : 'auto'; |
| 196 | + const result = probeAggregatorService.listTimeseries(req.params.hostId, { |
| 197 | + fromMs: from, |
| 198 | + toMs: to, |
| 199 | + resolution, |
| 200 | + }); |
| 201 | + res.json({ |
| 202 | + ok: true, |
| 203 | + hostId: req.params.hostId, |
| 204 | + fromMs: from, |
| 205 | + toMs: to, |
| 206 | + resolution: result.resolution, |
| 207 | + points: result.points, |
| 208 | + }); |
| 209 | + } catch (error) { |
| 210 | + next(error); |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + return router; |
| 215 | +} |
| 216 | + |
| 217 | +module.exports = { |
| 218 | + createProbeAgentAdminRouter, |
| 219 | + createProbeAgentPublicRouter, |
| 220 | +}; |
0 commit comments