Files
adam-acs/app/middleware/cwmp_logger_middleware.ts
T
Kevin L'Hereec 0c83a4ccb2 feat: TR-069 Exchange Viewer - structured MongoDB storage with SSE streaming and sequence diagram UI
- MongoDB storage: cwmp_exchanges collection with indexes (deviceId+timestamp, deviceId+sequenceNumber unique, TTL 24h)
- ExchangeStoreService: CWMP method extraction, per-device queue for ordering, retry logic, message cap (5000/device)
- Exchange API: GET/DELETE/SSE stream endpoints with Last-Event-ID reconnection
- CwmpLoggerMiddleware: dual-write (MongoDB + legacy files) with fire-and-forget
- Frontend: SequenceViewer (CPE↔ACS lifelines, arrows, auto-scroll, virtual scroll) + InspectorPanel (formatted XML, copy, syntax highlighting)
- Tr069Trace page: integrated new viewer with col-6/col-6 layout
- DeviceDetail page: collapsible exchange section when device is traced
- 14 property-based tests, 40+ unit tests, 5 integration tests
- Health check includes index verification
- Config: feature flags, SSE timeouts, retry policy
2026-06-03 15:52:44 +02:00

100 lines
3.4 KiB
TypeScript

import type { HttpContext } from '@adonisjs/core/http'
import type { NextFn } from '@adonisjs/core/types/http'
// @ts-ignore - xml-formatter types conflict with ESM interop
import formatXml from 'xml-formatter'
import { Tr069TraceService } from '#services/tr069_trace_service'
import { ExchangeStoreService } from '#services/exchange_store_service'
import logger from '@adonisjs/core/services/logger'
/**
* CWMP Logger Middleware
*
* Buffers SOAP request/response. After next():
* - Always logs a brief summary (INFO) to the app logger.
* - For traced devices:
* 1. Persists raw XML to ExchangeStoreService (new structured MongoDB storage)
* 2. Writes pretty-printed XML to file-based logs (legacy, kept for Phase 1 dual-write safety)
*
* The ExchangeStoreService calls are fire-and-forget: they never block CWMP processing.
*/
export default class CwmpLoggerMiddleware {
private prettyXml(xml: string): string {
try {
return (formatXml as any)(xml, {
indentation: ' ',
collapseContent: true,
lineSeparator: '\n',
})
} catch {
return xml
}
}
async handle(ctx: HttpContext, next: NextFn) {
const { request, response } = ctx
const rawBody = request.raw() || ''
const socket = request.request?.socket
const remoteIp = request.ip()
const remotePort = socket?.remotePort || 0
const localAddress = socket?.localAddress || 'unknown'
const localPort = socket?.localPort || 0
const sessionId = `${remoteIp}:${remotePort}->${localAddress}:${localPort}`
let capturedResponseXml: string | null = null
const originalSend = response.send.bind(response)
response.send = (body: any) => {
if (typeof body === 'string' && body.includes('soap-env:Envelope')) {
capturedResponseXml = body
}
return originalSend(body)
}
await next()
const deviceId = ctx.cwmpDeviceId
const traceService = Tr069TraceService.getInstance()
const isTraced = deviceId ? traceService.isTraced(deviceId) : false
if (isTraced && deviceId) {
// New system: persist raw XML to MongoDB (fire-and-forget, never blocks CWMP)
// Ordering is guaranteed by the per-device queue in ExchangeStoreService
const exchangeStore = ExchangeStoreService.getInstance()
// Persist CPE→ACS (including empty POST)
exchangeStore.persistExchange({
deviceId,
direction: 'CPE→ACS',
sessionId,
rawXml: rawBody || '',
}).catch(() => {})
// Persist ACS→CPE response (will queue after the CPE→ACS above)
if (capturedResponseXml) {
exchangeStore.persistExchange({
deviceId,
direction: 'ACS→CPE',
sessionId,
rawXml: capturedResponseXml,
}).catch(() => {})
}
// Old system: file-based writes (kept for Phase 1 dual-write rollback safety)
if (rawBody && rawBody.trim() !== '') {
traceService.writeExchange(deviceId, 'CPE→ACS', sessionId, this.prettyXml(rawBody))
}
if (capturedResponseXml) {
traceService.writeExchange(
deviceId,
'ACS→CPE',
sessionId,
this.prettyXml(capturedResponseXml)
)
}
} else if (rawBody && rawBody.trim() !== '') {
// Brief app-level log only (no SOAP body)
logger.debug({ sessionId, deviceId: deviceId ?? 'unknown' }, 'TR-069 exchange (not traced)')
}
}
}