Compare commits

...
3 Commits
Author SHA1 Message Date
Denozordec d255694cda fix(mcp): исправить проверку зависимостей туннеля на больших таблицах маршрутов
Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Successful in 10m41s
Docker images / frontend-image (push) Successful in 2m13s
Docker images / updater-image (push) Successful in 1m17s
Docker images / netflow-agent-image (push) Successful in 33s
Docker images / backend-image (push) Successful in 1m58s
Docker images / publish-release (push) Successful in 11s
Docker images / notify-webhook (push) Successful in 2s
2026-11-22 17:51:05 +07:00
Denozordec e85e347cbe fix(traffic): исправить пики, историю и итоги трафика
Docker images / prepare-release (push) Successful in 12s
Docker images / backend-test (push) Successful in 10m29s
Docker images / frontend-image (push) Successful in 2m55s
Docker images / updater-image (push) Successful in 1m38s
Docker images / netflow-agent-image (push) Successful in 32s
Docker images / backend-image (push) Successful in 3m38s
Docker images / publish-release (push) Successful in 12s
Docker images / notify-webhook (push) Successful in 2s
2026-11-22 16:57:52 +07:00
Denozordec 165f5e1260 fix(ipsec): исправить сверку списка файлов при экспорте сертификатов
Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Successful in 10m23s
Docker images / backend-image (push) Successful in 2m35s
Docker images / frontend-image (push) Successful in 2m38s
Docker images / updater-image (push) Successful in 1m7s
Docker images / netflow-agent-image (push) Successful in 32s
Docker images / notify-webhook (push) Successful in 2s
Docker images / publish-release (push) Successful in 10s
2026-11-22 15:36:37 +07:00
45 changed files with 666 additions and 167 deletions

No files matched your search

+61 -30
View File
@@ -2,7 +2,7 @@
import { useState, useMemo, useEffect, type ReactNode } from "react"
import { Alert, AlertTitle, AlertDescription } from "@/components/reui/alert"
import { projectTrafficObservations, type TrafficObservationMeta } from "@/lib/traffic-observations"
import { peakOfCombinedTraffic, projectTrafficObservations, type TrafficObservationMeta } from "@/lib/traffic-observations"
import { projectTrafficBindingStatus } from "@/lib/traffic-binding-status"
import { TrafficObservationStatus } from "@/components/traffic/traffic-observation-status"
import { TrafficDataTime } from "@/components/traffic/traffic-data-time"
@@ -25,7 +25,7 @@ import { useDataSource } from "@/lib/data-source"
import { useTrafficLive } from "@/hooks/use-traffic-live"
import { useFlowLive } from "@/hooks/use-flow-live"
import { requestJson } from "@/shared/api/http-client"
import { getFlowAnalytics, getFlowClients, getFlowExporters, getFlowMonthly, getTrafficFlows } from "@/shared/api/traffic-flow"
import { getFlowAnalytics, getFlowClients, getFlowExporters, getFlowMonthly, getTrafficFlowSettings, getTrafficFlows } from "@/shared/api/traffic-flow"
import { listServers } from "@/shared/api/servers"
import { FlowOverlaySheet } from "@/components/traffic/flow-overlay-sheet"
import { FlowAnalyticsDetail, FlowEntityCardView } from "@/components/traffic/flow-analytics-panel"
@@ -56,32 +56,31 @@ function addSeries(a: number[], b: number[]): number[] {
return a.map((v, i) => v + (b[i] ?? 0))
}
function flowIngestLine(stats: FlowStatsDto | null): string | null {
type FlowCollectorStatus = Pick<FlowStatsDto, "listenerBound" | "listenerAddress" | "packetsReceived" | "lastDatagramAt" | "lastExporterIp" | "lastError" | "collectorCountersSource">
function flowIngestLine(stats: FlowCollectorStatus | null): string | null {
if (!stats) return null
const listener = stats.listenerBound
? (stats.listenerAddress ?? "слушает")
: "не слушает"
: "приём не подтверждён"
const last = stats.lastDatagramAt
? new Date(stats.lastDatagramAt).toLocaleString("ru-RU")
: "—"
const exporter = stats.lastExporterIp ? ` · ${stats.lastExporterIp}` : ""
const err = stats.lastError ? ` · ${stats.lastError}` : ""
return `Коллектор: ${listener} · пакеты ${stats.packetsReceived ?? 0} · последний ${last}${exporter}${err}`
const hint = stats.collectorCountersSource === "last-known" ? " · последние известные данные" : ""
return `Коллектор: ${listener} · пакеты ${stats.packetsReceived ?? "—"} · последний ${last}${exporter}${hint}${err}`
}
function flowEmptyHint(stats: FlowStatsDto | null, collectorAlive?: boolean): string | undefined {
function flowEmptyHint(stats: FlowCollectorStatus | null): string | undefined {
if (!stats) return undefined
if (stats.lastError) return stats.lastError
if (stats.collectorCountersSource === "last-known") return "Текущий приём IPFIX не подтверждён."
if (stats.listenerBound === false) return "Коллектор UDP не слушает."
if (stats.packetsReceived) {
return `IPFIX приходит (${stats.lastExporterIp ?? "экспортёр"}), но сессии ещё не записаны.`
return "В выбранном интервале нет сохранённых потоков."
}
if (stats.listenerBound === false && !collectorAlive) {
return "Коллектор UDP не слушает. Подключите JH ещё раз — ingest включится автоматически."
}
if (stats.listenerBound || collectorAlive) {
return "Коллектор жив, IPFIX ещё не доходит. На jump-host у target Src должен быть 0.0.0.0 (авто)."
}
return "IPFIX ещё не доходит до коллектора. На jump-host у target Src должен быть 0.0.0.0 (авто). На хосте MM проверьте bind 10.255.254.1:4739 после wg-flow."
return stats.listenerBound ? "Коллектор слушает, датаграммы IPFIX ещё не получены." : undefined
}
// ─── data model ───────────────────────────────────────────────────────────────
@@ -803,7 +802,7 @@ export default function TrafficPage() {
const [trafficRetry, setTrafficRetry] = useState(0)
const [liveRetry, setLiveRetry] = useState(0)
const trafficKey = JSON.stringify([isLive, backendUrl, range, groupMode, trafficRetry])
const [trafficState, setTrafficState] = useState<{ key: string; status: "ready" | "error"; receivedAt?: number; error?: string } | null>(null)
const [trafficState, setTrafficState] = useState<{ key: string; status: "ready" | "error"; receivedAt?: number; peaks?: { rx: number | null; tx: number | null }; error?: string } | null>(null)
const trafficReady = trafficState?.key === trafficKey && trafficState.status === "ready"
const trafficLoading = trafficState?.key !== trafficKey
const trafficError = trafficState?.key === trafficKey ? trafficState.error : undefined
@@ -828,6 +827,12 @@ export default function TrafficPage() {
const liveBusy = isLive && groupMode === "flows" && !currentFlows
const liveError = currentFlows?.error
const flowStats = currentFlows?.stats ?? null
const [flowCollector, setFlowCollector] = useState<{ url: string; status: FlowCollectorStatus | null; receivedAt?: number } | null>(null)
const collectorReading = flowCollector?.url === backendUrl ? flowCollector : { status: flowStats, receivedAt: currentFlows?.receivedAt }
const collectorStatus: FlowCollectorStatus | null = collectorReading.status && collectorReading.receivedAt !== undefined
&& trafficClock - collectorReading.receivedAt > 35_000
? { ...collectorReading.status, listenerBound: false, collectorCountersSource: "last-known" }
: collectorReading.status
const flowExporters = useMemo(() => currentFlows?.exporters ?? [], [currentFlows])
const flowClients = useMemo(() => currentFlows?.clients ?? [], [currentFlows])
const [flowMonth, setFlowMonth] = useState(() => new Date().toISOString().slice(0, 7))
@@ -882,7 +887,7 @@ export default function TrafficPage() {
try {
const q = encodeURIComponent(targetRange === "30d" ? "24h" : targetRange)
const [srvRes, usersRes, ifacesRes] = await Promise.all([
apiFetch<{ servers: LiveTrafficServer[] }>(`/api/traffic/servers?range=${q}`),
apiFetch<{ servers: LiveTrafficServer[]; peaks?: { rx: number | null; tx: number | null } }>(`/api/traffic/servers?range=${q}`),
apiFetch<{ users: UserTraffic[] }>(`/api/traffic/users?range=${q}`),
apiFetch<{ interfaces: BoundIfaceTraffic[] }>(`/api/traffic/bound-interfaces?range=${q}`),
])
@@ -897,7 +902,7 @@ export default function TrafficPage() {
setLiveServerOwner(JSON.stringify([isLive, backendUrl]))
setLiveUsers(mappedUsers)
setLiveBoundIfaces(liveIfaces)
setTrafficState({ key, status: "ready", receivedAt: Date.now() })
setTrafficState({ key, status: "ready", receivedAt: Date.now(), peaks: srvRes.peaks })
setSelectedId((prev) => {
if (groupMode === "users") return mappedUsers.some((x) => x.id === prev) ? prev : (mappedUsers[0]?.id ?? "")
if (groupMode === "ifaces") return liveIfaces.some((x) => x.id === prev) ? prev : (liveIfaces[0]?.id ?? "")
@@ -911,6 +916,28 @@ export default function TrafficPage() {
return () => { cancelled = true }
}, [apiFetch, isLive, groupMode, backendUrl, trafficRetry, range])
useEffect(() => {
if (!isLive || groupMode !== "flows" || range === "30d") return
const controller = new AbortController()
let timer: ReturnType<typeof setTimeout>
const refresh = async () => {
try {
const status = await getTrafficFlowSettings(backendUrl, { signal: controller.signal })
if (!controller.signal.aborted) setFlowCollector({ url: backendUrl, status, receivedAt: Date.now() })
} catch {
if (!controller.signal.aborted) setFlowCollector(current => ({
url: backendUrl,
status: current?.url === backendUrl && current.status
? { ...current.status, listenerBound: false, collectorCountersSource: "last-known" }
: null,
}))
}
if (!controller.signal.aborted) timer = setTimeout(refresh, 15_000)
}
timer = setTimeout(refresh, 15_000)
return () => { controller.abort(); clearTimeout(timer) }
}, [isLive, groupMode, range, backendUrl])
useEffect(() => {
if (!isLive || effectiveMode !== "flows") return
let cancelled = false
@@ -1116,14 +1143,14 @@ export default function TrafficPage() {
: effectiveMode === "ifaces"
? activeBoundIfaces
: activeServerTraffic
const totalRx = kpiSource.every(s => s.rxNow !== null) ? kpiSource.reduce((a, s) => a + (s.rxNow ?? 0), 0) : null
const totalTx = kpiSource.every(s => s.txNow !== null) ? kpiSource.reduce((a, s) => a + (s.txNow ?? 0), 0) : null
const peakRx = kpiSource.every(s => s.rxPeak !== null) ? kpiSource.reduce((a, s) => Math.max(a, s.rxPeak ?? 0), 0) : null
const peakTx = kpiSource.every(s => s.txPeak !== null) ? kpiSource.reduce((a, s) => Math.max(a, s.txPeak ?? 0), 0) : null
const totalRx = kpiSource.length && kpiSource.every(s => s.rxNow !== null) ? kpiSource.reduce((a, s) => a + (s.rxNow ?? 0), 0) : null
const totalTx = kpiSource.length && kpiSource.every(s => s.txNow !== null) ? kpiSource.reduce((a, s) => a + (s.txNow ?? 0), 0) : null
const exactPeaks = effectiveMode === "servers" && trafficReady ? trafficState?.peaks : undefined
const peakRx = exactPeaks ? exactPeaks.rx : peakOfCombinedTraffic(kpiSource.map(s => s.rxSeries))
const peakTx = exactPeaks ? exactPeaks.tx : peakOfCombinedTraffic(kpiSource.map(s => s.txSeries))
const visibleSortFields = SORT_FIELDS.filter(s => !s.modesOnly || s.modesOnly.includes(effectiveMode))
const ingestLine = flowIngestLine(flowStats)
const collectorAlive = Boolean(flowStats?.listenerBound || flowStats?.packetsReceived)
const ingestLine = flowIngestLine(collectorStatus)
const flowError = liveError
|| flowLiveError
|| (displayedFlow?.degraded ? "Коллектор перегружен: упрощённая аналитика" : null)
@@ -1139,7 +1166,7 @@ export default function TrafficPage() {
},
{
id: "bps",
label: "Скорость",
label: "Скорость IPFIX",
value: displayedFlow ? fmtRate(displayedFlow.bpsNow / 1_000_000) : "—",
hint: flowLiveHint,
icon: <ActivityIcon className="size-4" />,
@@ -1164,29 +1191,33 @@ export default function TrafficPage() {
const counterKpiItems = [
{
id: "rx",
label: "RX",
label: "Сумма RX",
value: fmtRate(totalRx),
hint: effectiveMode === "servers" ? "Интерфейсы и туннели" : undefined,
icon: <ArrowDownIcon className="size-4" />,
iconClassName: "text-success",
},
{
id: "tx",
label: "TX",
label: "Сумма TX",
value: fmtRate(totalTx),
hint: effectiveMode === "servers" ? "Интерфейсы и туннели" : undefined,
icon: <ArrowUpIcon className="size-4" />,
iconClassName: "text-info",
},
{
id: "peak-rx",
label: "Пик RX",
label: "Пик суммы RX",
value: fmtRate(peakRx),
hint: exactPeaks ? "По общим замерам" : "По интервалам графика",
icon: <TrendingUpIcon className="size-4" />,
iconClassName: "text-warning",
},
{
id: "peak-tx",
label: "Пик TX",
label: "Пик суммы TX",
value: fmtRate(peakTx),
hint: exactPeaks ? "По общим замерам" : "По интервалам графика",
icon: <TrendingUpIcon className="size-4" />,
iconClassName: "text-warning",
},
@@ -1330,7 +1361,7 @@ export default function TrafficPage() {
))}
{!liveBusy && !liveError && (range === "30d" ? visibleMonthlyServers.length : sortedFlowCards.length) === 0 ? (
<p className="text-xs text-muted-foreground">
{range === "30d" ? (q ? "Нет серверов по этому запросу" : "В каталоге нет серверов") : flowEmptyHint(flowStats, collectorAlive) ?? "Нет экспортёров IPFIX. Подключите jump-host."}
{range === "30d" ? (q ? "Нет серверов по этому запросу" : "В каталоге нет серверов") : flowEmptyHint(collectorStatus) ?? "Нет экспортёров IPFIX."}
</p>
) : null}
</div>
@@ -1359,7 +1390,7 @@ export default function TrafficPage() {
excludeOverlay={flowExcludeOverlay}
onExcludeOverlay={setFlowExcludeOverlay}
liveHint={flowLiveHint}
emptyHint={analyticsLoading ? "Загрузка аналитики…" : currentAnalytics?.error ? "Не удалось получить данные выбранного среза" : flowEmptyHint(flowStats, collectorAlive)}
emptyHint={analyticsLoading ? "Загрузка аналитики…" : currentAnalytics?.error ? "Не удалось получить данные выбранного среза" : flowEmptyHint(collectorStatus)}
/>
</FramePanel>
</Frame>
@@ -22,8 +22,8 @@ import { getFlowMonthly } from "../../services/traffic-flow-analytics.js"
import { readPublicTrafficSettings, updateTrafficSettings } from "../../services/traffic-collector.js"
import { readUptimeSettings, updateUptimeSettings, readProbeRows, replaceProbes } from "../../services/uptime-collector.js"
import { readSpeedProbeCatalog, replaceSpeedProbeCatalog } from "../../services/speed-probe-catalog.js"
import { getTrafficFlowSettingsRow, toTrafficFlowSettingsDto } from "../../services/traffic-flow-settings.js"
import { getFlowListenerState } from "../../services/traffic-flow-ingest.js"
import { getTrafficFlowSettingsRow } from "../../services/traffic-flow-settings.js"
import { readTrafficFlowSettingsStatus } from "../../services/traffic-flow-ingest.js"
import { withFlowConfigurationLock, getFlowHostStatus, reconcileFlowHost } from "../../services/traffic-flow-host-agent.js"
import { agentSocket } from "../../services/traffic-flow-agent-client.js"
import { getStatistics, getStatisticsPivot, pivotDimsConflict } from "../../services/statistics-aggregate.js"
@@ -185,7 +185,7 @@ export function registerApplicationActions(register: typeof add) {
change("traffic.settings.update", "mm_traffic_settings_update_plan", "План настроек сбора трафика", "mm:traffic:write", z.object({ settings: trafficSettings }).strict(), readTrafficConfiguration, async input => { const result = await updateTrafficSettings(trafficSettings.parse(input.settings)); await refreshScheduler(); return result })
query("uptime.settings.read", "mm_uptime_settings_get", "Настройки мониторинга и проб", "mm:uptime:read", async () => readUptimeSettings())
change("uptime.settings.update", "mm_uptime_settings_update_plan", "План расписания мониторинга и проб", "mm:uptime:write", z.object({ settings: uptimeSettings }).strict(), readUptimeConfiguration, async input => { const result = await updateUptimeSettings(uptimeSettings.parse(input.settings)); await refreshScheduler(); return result })
query("netflow.settings.read", "mm_netflow_settings_get", "Публичные настройки NetFlow", "mm:traffic:read", async () => toTrafficFlowSettingsDto(getFlowListenerState()))
query("netflow.settings.read", "mm_netflow_settings_get", "Публичные настройки NetFlow", "mm:traffic:read", readTrafficFlowSettingsStatus)
query("scheduler.runs", "mm_scheduler_runs_list", "История заданий", "mm:settings:admin", async input => listSchedulerRuns({ limit: Number(input.limit), offset: Number(input.offset), jobKey: input.jobKey as string | undefined }), z.object({ ...paging, jobKey: z.enum(JOB_KEYS).optional() }).strict())
query("servers.api-ping.settings.read", "mm_servers_api_ping_settings_get", "Настройки API-ping серверов", "mm:servers:read", async () => getServersApiPingSettings())
change("servers.api-ping.settings.update", "mm_servers_api_ping_settings_update_plan", "План API-ping серверов", "mm:servers:write", z.object({ settings: z.object({ enabled: z.boolean().optional(), intervalSec: z.number().int().min(10).max(86400).optional() }).strict().refine(value => Object.keys(value).length > 0) }).strict(), readServersApiPingConfiguration, async input => { const result = await updateServersApiPingSettings(input.settings as Parameters<typeof updateServersApiPingSettings>[0]); await refreshScheduler(); return result })
@@ -25,6 +25,7 @@ export function registerLegacyFlowAnalyticsAction(register: typeof add) {
{ serverIds: context.connection.policy.scope.serverIds, maxRows: 20000, authorize }), context.signal)
return {
serverId: input.serverId, source: "PostgreSQL/flow_buckets", observedAt: new Date().toISOString(), sourceWindow: result.sourceWindow,
timeBasis: result.timeBasis, admittedTotals: result.admittedTotals,
bpsNow: result.bpsNow, bytes: result.bytes, packets: result.packets, conversations: result.conversations,
uniqueSrc: result.uniqueSrc, uniqueDst: result.uniqueDst, rxSeries: result.rxSeries, txSeries: result.txSeries,
applications: result.applications, protocols: result.protocols, countries: result.countries,
@@ -72,7 +72,7 @@ const router = createServer(async (req, res) => {
const certificate = catalogs["/certificate"].find(row => row[".id"] === fields[".id"])
assert(certificate && !("name" in fields))
commands.at(-1)!.name = certificate.name
catalogs["/file"].push({ ".id": `**New${++serial}`, name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(fakeP12)), type: "file" })
catalogs["/file"].unshift({ ".id": `**New${++serial}`, name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(fakeP12)), type: "file" })
if (mode === "lost") { req.socket.destroy(); return }
} else if (req.method === "DELETE" && path.startsWith("/file/")) {
catalogs["/file"] = catalogs["/file"].filter(row => row[".id"] !== path.slice("/file/".length))
@@ -53,12 +53,19 @@ export async function acceptIpsecUserCreate(actor: AuthUser) {
: path === "/certificate/sign" && ("days-valid" in fields || "name" in fields) ? ("days-valid" in fields ? "unknown parameter days-valid" : "missing =.id=")
: path === "/certificate/export-certificate" && "name" in fields ? "unknown parameter name" : undefined
if (refusal) { res.statusCode = 400; res.end(JSON.stringify({ error: 400, message: "Bad Request", detail: refusal })); return }
if ((mode === "export-refused" && path === "/certificate/export-certificate") || (mode === "cleanup-refused" && req.method === "DELETE" && path.startsWith("/file/"))) {
res.statusCode = 400; res.end(JSON.stringify({ error: 400, detail: "NEVER_NATIVE_ERROR_DETAILS" })); return
}
let result: Row = {}
if (mode !== "ignored") {
if (path === "/certificate/sign") {
const row = catalogs["/certificate"].find(row => row[".id"] === fields[".id"]); assert(row)
Object.assign(row, { ca: fields.ca, "digest-algorithm": "sha256", fingerprint: "synthetic-fingerprint", "invalid-before": "2026-01-01", "invalid-after": "2030-01-01", "private-key": "true", "serial-number": "synthetic" })
} else if (path === "/certificate/export-certificate") assert(catalogs["/certificate"].some(row => row[".id"] === fields[".id"])), catalogs["/file"].push({ ".id": `**New${++serial}`, name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(fakeP12)), type: "file" })
} else if (path === "/certificate/export-certificate") {
assert(catalogs["/certificate"].some(row => row[".id"] === fields[".id"]))
catalogs["/file"].unshift({ ".id": `**New${++serial}`, name: `${fields["file-name"]}.p12`, size: String(Buffer.byteLength(fakeP12)), type: "file" })
if (mode === "other-file-changed") catalogs["/file"][1].size = "99"
}
else {
const catalog = Object.keys(catalogs).find(value => path === value || path.startsWith(value + "/")); assert(catalog)
if (req.method === "PUT") { result = { ".id": `*new${++serial}`, ...fields, "native-default": "NEVER_NATIVE_DEFAULT" }; catalogs[catalog].push(result) }
@@ -121,6 +128,23 @@ export async function acceptIpsecUserCreate(actor: AuthUser) {
assert.equal(failedExport.json().state, "partial")
assert.deepEqual(catalogs["/ip/ipsec/identity"], base["/ip/ipsec/identity"])
assert.deepEqual(catalogs["/ip/ipsec/mode-config"], base["/ip/ipsec/mode-config"])
reset(); mode = "other-file-changed"
const unrelatedChange = await rest.inject({ method: "POST", url: "/api/ipsec/users", payload: { serverId, name: "Alice" } })
assert.equal(unrelatedChange.statusCode, 502)
assert.equal(unrelatedChange.json().completedCommands, 2)
assert.equal(catalogs["/file"].length, 2, "failed proof must stop before deletion")
for (const [fault, stage, completed] of [["export-refused", "экспорт .p12", 2], ["cleanup-refused", "удаление временного .p12", 3]] as const) {
reset(); mode = fault
const response = await rest.inject({ method: "POST", url: "/api/ipsec/users", payload: { serverId, name: "Alice", staticIp: "192.0.2.7" } })
assert.equal(response.statusCode, 502)
const error = response.json()
assert.equal(error.completedCommands, completed)
assert(error.error.includes(stage))
assert(error.error.includes("HTTP 400"))
hidden(error)
assert.deepEqual(catalogs["/ip/ipsec/identity"], base["/ip/ipsec/identity"])
assert.deepEqual(catalogs["/ip/ipsec/mode-config"], base["/ip/ipsec/mode-config"])
}
reset(); const rawStart = commands.length
for (const request of [{ name: "Alice" }, { name: "Alice", authMethod: "pre-shared-key", psk: "NEVER_RAW" }, { name: "Alice", privateKeyExport: true, staticIp: "bad" }]) await assert.rejects(prepare(request))
assert.equal(commands.length, rawStart)
@@ -17,11 +17,15 @@ export async function acceptOspfRouteProofMcp(actor: AuthUser) {
const router = createServer(async (req, res) => {
res.setHeader("content-type", "application/json")
if (req.method === "GET" && req.url === tablePath) { reads++; res.end(JSON.stringify(tables)); return }
if (!["/rest/routing/route", "/rest/routing/route/get"].includes(req.url ?? "")) { res.statusCode = 404; res.end("{}"); return }
const url = new URL(req.url ?? "/", "http://fixture.invalid")
if (!["/rest/routing/route", "/rest/routing/route/get"].includes(url.pathname)) { res.statusCode = 404; res.end("{}"); return }
reads++
if (beforeReply) { const hook = beforeReply; beforeReply = undefined; await hook() }
if (nativeError) { res.statusCode = 400; res.end('{"message":"NEVER_ROUTE_PROVIDER_TOKEN"}'); return }
if (req.method === "GET") { res.end(JSON.stringify(rows)); return }
if (req.method === "GET") {
if (url.searchParams.get("dst-address") !== selection.prefix || url.searchParams.get("routing-table") !== selection.table) { res.statusCode = 400; res.end("{}"); return }
res.end(JSON.stringify(rows)); return
}
let body = ""; for await (const chunk of req) body += chunk
const key = JSON.parse(body)["value-name"]
if (key === "ospf.metric" && afterMetric) { const hook = afterMetric; afterMetric = undefined; await hook() }
@@ -36,7 +36,7 @@ export function registerOspfRouteProofActions(register: typeof add) {
if (error instanceof McpError) throw error
throw new McpError("STATE_UNAVAILABLE", input.expectAbsent === true
? "Отсутствие точного префикса во всей выбранной таблице не подтверждено"
: "Активный OSPF-маршрут, его метрика или выбранный выход не подтверждены; предел источника 10000 строк и 1 МиБ", 409)
: "Активный OSPF-маршрут, его метрика или выход через указанный интерфейс не подтверждены", 409)
}
},
})
@@ -35,9 +35,11 @@ export async function acceptWireguardImport(actor: AuthUser) {
for (const hidden of [privateKey, peerOne, peerTwo, generated, content, "NEVER_IMPORT_"]) assert.equal(text.includes(hidden), false, "Public response must omit raw imported/native keys and configuration")
}
const router = createServer((req, res) => {
const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, ""))
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname)
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
res.setHeader("content-type", "application/json")
if (req.method === "GET") { reads++; if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }; res.end(JSON.stringify(rows[path])); return }
if (req.method === "GET") { reads++; if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }; res.end(JSON.stringify(selectedRows())); return }
if (!["PUT", "PATCH"].includes(req.method ?? "")) { res.statusCode = 400; res.end("{}"); return }
let raw = ""
req.on("data", chunk => { raw += chunk }); req.on("end", () => {
@@ -24,18 +24,23 @@ export async function acceptWireguardInterfaceCreation(actor: AuthUser) {
initial["/interface"] = [{ ".id": "*af", name: "wg-foreign", type: "wg" }]
initial["/ip/address"] = [{ ".id": "*vf", address: "203.0.113.2/24", interface: "ether1", dynamic: "false", disabled: "false" }]
initial["/ipv6/address"] = []
initial["/ip/route"] = Array.from({ length: 12_608 }, (_, index) => ({
".id": `*r${index.toString(16)}`, dynamic: "true", bgp: "true", gateway: "192.0.2.1", "immediate-gw": "192.0.2.1%ether1",
}))
let rows = structuredClone(initial), mode = "normal", reads = 0, commands: string[] = [], counter = 0
let afterWrite: (() => Promise<unknown>) | undefined
const reset = () => { rows = structuredClone(initial); mode = "normal"; commands = []; counter = 0; afterWrite = undefined }
const router = createServer((req, res) => {
res.setHeader("content-type", "application/json")
const path = decodeURIComponent((req.url ?? "").slice(5))
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname)
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
if (req.method === "GET") {
reads++
if (mode === "provider-error") { res.statusCode = 400; res.end('{"message":"NEVER_PROVIDER_SECRET"}'); return }
if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }
rows["/interface/wireguard/peers"][0].rx = String(reads)
res.end(JSON.stringify(rows[path])); return
res.end(JSON.stringify(selectedRows())); return
}
const base = ["/interface/wireguard/peers", "/interface/wireguard", "/ip/address", "/ipv6/address"].find(base => path === base || path.startsWith(base + "/"))
if (!base || !["PUT", "PATCH"].includes(req.method ?? "")) { res.statusCode = 400; res.end("{}"); return }
@@ -26,7 +26,9 @@ export async function acceptWireguardInterfaceDeletion(actor: AuthUser) {
let duringRead: (() => Promise<unknown>) | undefined
const router = createServer((req, res) => {
res.setHeader("content-type", "application/json")
const path = decodeURIComponent((req.url ?? "").slice(5))
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname)
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
if (req.method === "GET") {
gets++
const respond = () => {
@@ -34,7 +36,7 @@ export async function acceptWireguardInterfaceDeletion(actor: AuthUser) {
if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }
// Runtime traffic and DHCP lease clocks must not become configuration drift.
rows["/interface/wireguard/peers"].find(row => row[".id"] === "*pf")!.rx = String(gets)
res.end(JSON.stringify(rows[path]))
res.end(JSON.stringify(selectedRows()))
}
if (duringRead) { const hook = duringRead; duringRead = undefined; void hook().then(respond, () => { res.statusCode = 503; res.end("{}"); }); return }
respond(); return
@@ -89,7 +91,8 @@ export async function acceptWireguardInterfaceDeletion(actor: AuthUser) {
await assert.rejects(prepare(true), (error: { code?: string }) => error.code === "DEPENDENCY_CONFLICT")
assert.equal(commands.length, 0, "Native alias must not acquire cascade permission")
for (const [path, row] of [
["/interface/list/member", { interface: "wg-cascade", list: "LAN" }], ["/interface/vxlan", { interface: "wg-cascade" }], ["/ip/route", { gateway: "198.19.244.2%wg-cascade" }],
["/interface/list/member", { interface: "wg-cascade", list: "LAN" }], ["/interface/vxlan", { interface: "wg-cascade" }],
["/ip/route", { gateway: "wg-cascade" }], ["/ip/route", { dynamic: "true", "immediate-gw": "198.19.244.2%wg-cascade" }],
["/routing/filter/rule", { rule: 'set gw "wg-cascade";' }], ["/ip/firewall/filter", { "in-interface": "wg-cascade" }], ["/ip/dhcp-server", { interface: "wg-cascade" }],
["/routing/filter/rule", { rule: 'set gw "wg\\2dcascade";' }],
["/ip/ipsec/peer", { "local-address": "198.19.244.1" }], ["/routing/bgp/session", { dynamic: "true", "local.address": "2001:0db8:0244:0:0:0:0:1" }],
@@ -31,13 +31,15 @@ export async function acceptWireguardInterfaceUpdate(actor: AuthUser) {
const reset = () => { rows = structuredClone(initial); commands = []; mode = "normal"; afterWrite = undefined }
const router = createServer((req, res) => {
res.setHeader("content-type", "application/json")
const path = decodeURIComponent((req.url ?? "").slice(5))
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname)
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
if (req.method === "GET") {
reads++
if (mode === "provider-error") { res.statusCode = 500; res.end('{"message":"NEVER_PROVIDER_SECRET"}'); return }
if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }
rows["/interface/wireguard/peers"][0].rx = String(reads)
res.end(JSON.stringify(rows[path])); return
res.end(JSON.stringify(selectedRows())); return
}
const base = ["/interface/wireguard", "/ip/address", "/ipv6/address"].find(base => path === base || path.startsWith(base + "/"))
if (!base || !["PUT", "PATCH", "DELETE"].includes(req.method ?? "")) { res.statusCode = 400; res.end("{}"); return }
@@ -33,13 +33,15 @@ export async function acceptWireguardPeerCreation(actor: AuthUser) {
const reset = () => { rows = structuredClone(initial); mode = "normal"; commands = []; afterWrite = undefined }
const router = createServer((req, res) => {
res.setHeader("content-type", "application/json")
const path = decodeURIComponent((req.url ?? "").slice(5)), base = "/interface/wireguard/peers"
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname), base = "/interface/wireguard/peers"
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
if (req.method === "GET") {
reads++
if (mode === "provider-error" || mode === "dependency-error" && path === "/ip/address") { res.statusCode = 400; res.end('{"message":"NEVER_PROVIDER_SECRET"}'); return }
if (!Object.hasOwn(rows, path)) { res.statusCode = 404; res.end("{}"); return }
rows[base][0].rx = String(reads)
res.end(JSON.stringify(rows[path])); return
res.end(JSON.stringify(selectedRows())); return
}
if (!["PUT", "PATCH"].includes(req.method ?? "") || !(path === base || path === base + "/*c")) { res.statusCode = 400; res.end("{}"); return }
let raw = ""
@@ -32,8 +32,10 @@ export async function acceptWireguardRevisionRestore(actor:AuthUser){
const reset=()=>{rows=structuredClone(initial);commands=[];mode="normal";afterWrite=undefined}
const router=createServer(async(req,res)=>{
res.setHeader("content-type","application/json")
const path=decodeURIComponent((req.url??"").slice(5))
if(req.method==="GET"){reads++;if(!Object.hasOwn(rows,path)){res.statusCode=404;res.end("{}");return}res.end(JSON.stringify(rows[path]));return}
const [pathname, query] = (req.url ?? "").slice(5).split("?")
const path = decodeURIComponent(pathname)
const selectedRows = () => rows[path].filter(row => !query || (row.dynamic === "true") === query.startsWith("dynamic=true"))
if(req.method==="GET"){reads++;if(!Object.hasOwn(rows,path)){res.statusCode=404;res.end("{}");return}res.end(JSON.stringify(selectedRows()));return}
let raw="";for await(const chunk of req)raw+=chunk
const body=raw?JSON.parse(raw)as Record<string,string>:{},base=["/interface/wireguard/peers","/interface/wireguard","/ip/address","/ipv6/address"].find(p=>path===p||path.startsWith(p+"/"))
if(!base||!["PUT","PATCH","DELETE"].includes(req.method??"")){res.statusCode=400;res.end("{}");return}
+2 -8
View File
@@ -25,12 +25,10 @@ import { getFlowHostStatus, reconcileFlowHost, withFlowConfigurationLock } from
import {
ensureHostKeys,
getTrafficFlowSettingsRow,
toTrafficFlowSettingsDto,
} from "../services/traffic-flow-settings.js"
import {
getFlowListenerState,
drainExistingTrafficFlowWriters,
getFlowRuntimeCounters,
readTrafficFlowSettingsStatus,
purgeTrafficFlowStore,
listFlowTalkers,
} from "../services/traffic-flow-ingest.js"
@@ -168,11 +166,7 @@ const trafficFlowRoutes: FastifyPluginAsyncZod = async (app) => {
reconcile: () => withFlowConfigurationLock(reconcileFlowHost),
}))
await app.register(createTrafficFlowSettingsRoutes({
read: async () => {
const settings = await toTrafficFlowSettingsDto(getFlowListenerState())
const { packetsReceived, lastExporterIp, lastError, lastDatagramAt,dataQuality } = await getFlowRuntimeCounters()
return { ...settings, packetsReceived, lastExporterIp, lastError, lastDatagramAt,dataQuality }
},
read: readTrafficFlowSettingsStatus,
updateWithListener: updateTrafficFlowSettingsWithListener,
ensureKeys: ensureHostKeys,
hasPrivateKey: async () => Boolean((await getTrafficFlowSettingsRow()).hostPrivateKey),
+11 -3
View File
@@ -17,7 +17,8 @@ import { readTrafficLiveSample, TrafficLiveSourceChanged } from "../services/tra
import { MikrotikClient, MikrotikError } from "../services/mikrotik.js"
import { getEnabledServerById } from "../services/wireguard-live.js"
import { readTrafficCollectionContext } from "../services/traffic-collection-state.js"
import { readServerTrafficHistory, readServerTrafficInterfaces } from "../services/traffic-history-read.js"
import { readServerTrafficHistory, readServerTrafficHistoryWithSeries, readServerTrafficInterfaces } from "../services/traffic-history-read.js"
import { mergeBuiltTraffic } from "../services/traffic-rate.js"
import {
buildBoundInterfaceTraffic,
buildUserTrafficList,
@@ -93,8 +94,15 @@ const trafficRoutes: FastifyPluginAsyncZod = async (app) => {
const rangeEndMs = Date.now(), rangeStartMs = rangeEndMs - rangeToMinutes(q.range) * 60000
const allServers = await db.select().from(servers).where(eq(servers.enabled, true))
const context = await readTrafficCollectionContext(allServers.map(server => server.id))
const data = await Promise.all(allServers.map(server => readServerTrafficHistory(server, rangeStartMs, rangeEndMs, undefined, {}, context)))
return reply.send({ servers: data })
const data = await Promise.all(allServers.map(server => readServerTrafficHistoryWithSeries(server, rangeStartMs, rangeEndMs, undefined, {}, context)))
const aggregate = mergeBuiltTraffic(data.map(item => item.series))
return reply.send({
servers: data.map(item => item.history),
peaks: {
rx: aggregate.observations?.rxPeak ? aggregate.rxPeak : null,
tx: aggregate.observations?.txPeak ? aggregate.txPeak : null,
},
})
})
app.get("/traffic/servers/:id/live", async (req, reply) => {
const p = req.params as { id?: string | number }
@@ -96,12 +96,14 @@ export function normalizeIpsecExistingExportPassphrase(raw: string | undefined,
}
export function projectIpsecExistingCertificateExport(state: IpsecExistingCertificateExportState, filename: string, after: IpsecExistingCertificateExportState) {
validateIpsecExistingCertificateExportState(after)
const old = state.catalogs["/file"], candidates = after.catalogs["/file"].filter(row => row.name === filename && !old.some(prior => idKey(prior[".id"]) === idKey(row[".id"])))
const old = state.catalogs["/file"], files = after.catalogs["/file"]
const candidates = files.filter(row => row.name === filename && !old.some(prior => idKey(prior[".id"]) === idKey(row[".id"])))
if (candidates.length !== 1) throw fail("Не подтверждён единственный receipt-owned файл экспорта", "VERIFICATION_FAILED")
const row = candidates[0], size = Number(row.size)
if (!Number.isSafeInteger(size) || size <= 0 || size > MAX_OWNED_FILE_BYTES) throw fail("Размер приватного файла вне предела точного /file/get (61438 байт)", "VERIFICATION_FAILED")
const projected = structuredClone(state)
projected.catalogs["/file"].push(structuredClone(row))
// The new file can appear anywhere. The proof still checks the relative order and contents of every pre-existing row.
projected.catalogs["/file"].splice(files.indexOf(row), 0, structuredClone(row))
return { projected, file: { id: row[".id"], name: filename, size } }
}
export function projectIpsecExistingCertificateCleanup(state: IpsecExistingCertificateExportState, file: { id: string; name: string; size: number }) {
@@ -69,7 +69,8 @@ assert.throws(() => project(withCert, cert.commands[1], { observedRow: signed })
const signedState = project(withCert, cert.commands[1], { observedRow: signed, ownedCertificateIds: new Set(["*new"]) })
assert.deepEqual(signedState.catalogs["/certificate"].slice(0, 2), state.catalogs["/certificate"])
assert.throws(() => project(withCert, cert.commands[1], { observedRow: { ...signed, opaque: "changed" }, ownedCertificateIds: new Set(["*new"]) }), /исходные поля/)
const withFile = project(signedState, cert.commands[2], { assignedId: "*export", observedRow: { ".id": "*export", name: "mm-ipsec-fixture.p12", size: "256", type: "file" } })
const withFile = project(signedState, cert.commands[2], { assignedId: "*export", observedRow: { ".id": "*export", name: "mm-ipsec-fixture.p12", size: "256", type: "file" }, exportIndex: 0 })
assert.equal(withFile.catalogs["/file"][0][".id"], "*export")
assert.deepEqual(project(withFile, cert.commands[3], { assignedId: "*export" }).catalogs["/file"], state.catalogs["/file"])
assert.throws(() => project(state, psk.commands[0], { assignedId: "*l", observedRow: { ".id": "*l", ...psk.commands[0].fields } }), /ACK/)
assert.throws(() => project(state, psk.commands[0], { assignedId: "*new", observedRow: { ".id": "*new", ...psk.commands[0].fields, "remote-id": "key-id:mallory" } }), /Поля/)
@@ -105,7 +105,7 @@ export function planIpsecUserCreate(state: IpsecUserCreateState, server: ServerR
/** Native defaults are admitted solely for the ACK-owned newly created row.
* Signing can change only documented generated fields of that new certificate. */
export function projectIpsecUserCreate(state: IpsecUserCreateState, command: IpsecUserCreateCommand, options: { assignedId?: string; observedRow?: Row; ownedCertificateIds?: Set<string> } = {}) {
export function projectIpsecUserCreate(state: IpsecUserCreateState, command: IpsecUserCreateCommand, options: { assignedId?: string; observedRow?: Row; ownedCertificateIds?: Set<string>; exportIndex?: number } = {}) {
const next = structuredClone(state)
const path = command.action === "sign" ? "/certificate" : command.action === "export" || command.action === "cleanup" ? "/file" : command.path
const rows = next.catalogs[path]
@@ -131,7 +131,13 @@ export function projectIpsecUserCreate(state: IpsecUserCreateState, command: Ips
for (const flag of ["dynamic", "builtin", "default"]) if (row[flag] !== undefined && observedBoolean(row[flag]) !== false) throw fail("Создан системный объект", "VERIFICATION_FAILED")
const fields = command.action === "export" ? { name: command.name! } : command.fields
for (const [key, value] of Object.entries(fields)) if (row[key] !== value && !(["disabled", "responder"].includes(key) && observedBoolean(row[key]) !== null && observedBoolean(row[key]) === observedBoolean(value))) throw fail("Поля нового объекта не подтверждены", "VERIFICATION_FAILED")
rows.push(structuredClone(row))
if (command.action === "export") {
const index = options.exportIndex
if (index === undefined || !Number.isInteger(index) || index < 0 || index > rows.length) throw fail("Не подтверждена позиция нового файла экспорта", "VERIFICATION_FAILED")
rows.splice(index, 0, structuredClone(row))
} else {
rows.push(structuredClone(row))
}
}
return next
}
+24 -10
View File
@@ -2,7 +2,7 @@ import { randomBytes, randomUUID } from "node:crypto"
import type { IpsecCertBundle } from "@mmapp/contracts/ipsec"
import { getServerRowById, type ServerRow } from "../modules/servers/repository/servers-repository.js"
import { McpError, fingerprint, privateFingerprinter } from "../modules/mcp/security.js"
import { MAX_OWNED_FILE_BYTES, MikrotikClient } from "./mikrotik.js"
import { MAX_OWNED_FILE_BYTES, MikrotikClient, MikrotikError } from "./mikrotik.js"
import { readIpsecPeerDeleteState, ipsecPeerDeleteRows } from "./ipsec-peer-delete.js"
import { ipsecUserUpdateValue } from "./ipsec-user-update.js"
import { sameWireguardEndpoint } from "./wireguard-source.js"
@@ -37,6 +37,15 @@ export async function createIpsecUserGuarded(server: ServerRow, raw: unknown, ex
let completedCommands = 0, confirmedWrites = 0, bundle: IpsecCertBundle | undefined, artifactReceipt: Record<string, unknown> | undefined
const stopped = (code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", message: string) => new IpsecUserCreateExecutionError(code, message, confirmedWrites, completedCommands, plan.commands.length)
for (const command of plan.commands) {
const stage = {
create: command.path === "/certificate" ? "создание сертификата" : command.path === "/ip/ipsec/mode-config" ? "назначение персонального IP" : "создание привязки IKEv2",
update: "обновление персонального IP",
sign: "подпись сертификата",
export: "экспорт .p12",
cleanup: "удаление временного .p12",
}[command.action]
const stoppedAtStage = (code: "MUTATION_OUTCOME_UNKNOWN" | "VERIFICATION_FAILED", detail: string) =>
stopped(code, `Создание IKEv2 остановлено на этапе «${stage}». ${detail}`)
let dispatched = false, response: unknown, assignedId: string | undefined
const fence = async () => { const fresh = await readIpsecUserCreateState(server, source); if (fresh.proof !== state.proof) throw new McpError("STATE_CONFLICT", "IPsec изменился после durable intent; команда не отправлена", 409); await source(); dispatched = true }
try {
@@ -57,7 +66,8 @@ export async function createIpsecUserGuarded(server: ServerRow, raw: unknown, ex
confirmedWrites++
} catch (error) {
if (!dispatched && !completedCommands && error instanceof McpError) throw error
throw stopped(dispatched ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", "Создание IPsec остановлено; повтор native-команды запрещён, требуется сверка")
const detail = !dispatched ? "Команда не отправлена." : error instanceof MikrotikError ? `RouterOS вернул HTTP ${error.statusCode}.` : "Ответ RouterOS не подтверждён."
throw stoppedAtStage(dispatched ? "MUTATION_OUTCOME_UNKNOWN" : "VERIFICATION_FAILED", `${detail} Перед повтором требуется сверка.`)
}
let after: IpsecUserCreateState
try {
@@ -67,23 +77,27 @@ export async function createIpsecUserGuarded(server: ServerRow, raw: unknown, ex
const deadline = Date.now() + 90000
while (!after.catalogs["/certificate"].find(row => row.name === command.name)?.["invalid-after"] && Date.now() < deadline) { await new Promise(resolve => setTimeout(resolve, 1000)); after = await readIpsecUserCreateState(server, source) }
}
} catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Команда отправлена, но read-back недоступен; повтор запрещён") }
} catch { throw stoppedAtStage("MUTATION_OUTCOME_UNKNOWN", "Команда отправлена. Не удалось перечитать состояние RouterOS.") }
let exportIndex: number | undefined
if (command.action === "export") {
const files = after.catalogs["/file"]
const candidates = files.filter(row => row.name === command.name && !state.catalogs["/file"].some(old => old[".id"] === row[".id"]))
if (candidates.length !== 1) throw stoppedAtStage("VERIFICATION_FAILED", "В списке RouterOS не найден единственный новый файл с ожидаемым именем.")
assignedId = candidates[0][".id"]
exportIndex = files.indexOf(candidates[0])
}
try {
const path = command.action === "sign" ? "/certificate" : command.action === "export" ? "/file" : command.path
if (command.action === "create") {
const ack = response as Record<string, unknown> | null
if (typeof ack?.[".id"] !== "string") throw Error("missing create ACK")
assignedId = ack[".id"]
} else if (command.action === "export") {
const candidates = after.catalogs["/file"].filter(row => row.name === command.name && !state.catalogs["/file"].some(old => old[".id"] === row[".id"]))
if (candidates.length !== 1) throw Error("missing unique export file")
assignedId = candidates[0][".id"]
}
const observedRow = after.catalogs[path].find(row => command.action === "sign" ? row.name === command.name : row[".id"] === assignedId)
const projected = projectIpsecUserCreate(state, command, { assignedId, observedRow, ownedCertificateIds })
const projected = projectIpsecUserCreate(state, command, { assignedId, observedRow, ownedCertificateIds, exportIndex })
if (await ipsecUserCreateProof(projected) !== after.proof) throw Error("configuration mismatch")
if (command.action === "create" && command.path === "/certificate") ownedCertificateIds.add(assignedId!)
} catch { throw stopped("VERIFICATION_FAILED", "Не подтверждены команда и сохранность полного состояния IPsec; повтор запрещён") }
} catch { throw stoppedAtStage("VERIFICATION_FAILED", "Проверка результата команды и состояния IPsec не пройдена.") }
state = after; completedCommands++
try {
await source(); await options.checkpoint?.({ receiptId, commandCount: plan.commands.length, completedCommands, afterProof: state.proof })
@@ -100,7 +114,7 @@ export async function createIpsecUserGuarded(server: ServerRow, raw: unknown, ex
bundle = { user: body.name, serverEndpoint: plan.serverEndpoint, filename: file.name, contentB64: p12B64, mime: "application/x-pkcs12", passphrase, sswanFilename: `${plan.certName}.sswan`, sswanContent: buildSswanConfig({ name: `IKEv2 ${plan.serverEndpoint}`, serverEndpoint: plan.serverEndpoint, serverId: plan.serverEndpoint, p12B64 }), instructions: buildClientInstructions({ userName: body.name, serverEndpoint: plan.serverEndpoint, p12Filename: file.name, passphrase, dns: plan.dns }) }
} finally { content.fill(0) }
}
} catch { throw stopped("MUTATION_OUTCOME_UNKNOWN", "Команда проверена, но checkpoint или приватный артефакт не сохранён; повтор запрещён") }
} catch { throw stoppedAtStage("MUTATION_OUTCOME_UNKNOWN", "Команда подтверждена. Не удалось прочитать приватный файл или сохранить квитанцию.") }
}
const reader = { get: async (path: string) => { await source(); if (!Object.hasOwn(state.catalogs, path)) throw new McpError("STATE_UNAVAILABLE", "Неизвестный каталог истории IPsec", 502); return structuredClone(state.catalogs[path]) } } as unknown as MikrotikClient
let historyRecorded = false
+22 -1
View File
@@ -1,5 +1,5 @@
import assert from "node:assert/strict"
import { readOspfRouteProof } from "./ospf-route-proof.js"
import { readOspfRouteAbsence, readOspfRouteProof } from "./ospf-route-proof.js"
import { chrFixtureVrf } from "../modules/mcp/chr-fixture-context.js"
const selection = { prefix: "198.19.1.2/32", table: "local-vrf", interface: "owned-gre" }
@@ -10,6 +10,27 @@ const client = { get: async () => rows, post: async (_path: string, body: Record
const proof = await readOspfRouteProof(client, selection)
assert.equal(proof.metric, 60); assert.equal(JSON.stringify(proof).includes("NEVER_"), false)
const original = structuredClone(rows)
const largeRib = Array.from({ length: 12_608 }, (_, index) => ({
".id": `*r${index.toString(16)}`, "dst-address": `10.${index >> 8}.${index & 255}.0/24`, "routing-table": selection.table,
}))
const exactSelection = { ...selection, table: "test & vrf", interface: "wg test" }
const largeTableClient = {
get: async (path: string) => {
const url = new URL(path, "http://fixture.invalid")
if (url.pathname === "/routing/table") return [{ ".id": "*t", name: exactSelection.table }]
assert.equal(url.pathname, "/routing/route")
assert.equal(url.searchParams.get("routing-table"), exactSelection.table)
const prefix = url.searchParams.get("dst-address")
assert(prefix)
return [...largeRib, { ...original[0], "routing-table": exactSelection.table, "immediate-gw": exactSelection.interface }]
.filter(row => row["routing-table"] === exactSelection.table && row["dst-address"] === prefix)
},
post: async () => { throw new Error("unexpected getter") },
} as unknown as Parameters<typeof readOspfRouteProof>[0]
assert.equal((await readOspfRouteProof(largeTableClient, exactSelection)).interface, "wg test")
await assert.rejects(readOspfRouteProof(largeTableClient, { ...exactSelection, interface: "wg" }), /selected interface/)
await assert.rejects(readOspfRouteAbsence(largeTableClient, exactSelection, async () => {}), /matching route/)
assert.equal((await readOspfRouteAbsence(largeTableClient, { ...exactSelection, prefix: "198.19.1.3/32" }, async () => {})).absenceVerified, true)
rows = [{ ...original[0], ospf: "", active: "", "ospf.metric": "" }]
getters = []; assert.equal((await readOspfRouteProof(client, selection)).metric, 40); assert.deepEqual(getters, ["ospf", "active", "ospf.metric"])
for (const patch of [{ ospf: "false" }, { active: "false" }, { contribution: "unreachable" }, { "ospf.metric": "unknown" }, { "ospf.metric": "16777216" }, { "immediate-gw": "198.18.1.2%owned-gre-other" }, { "immediate-gw": "198.18.1.2%owned-gre@foreign-vrf" }]) {
+13 -3
View File
@@ -25,10 +25,18 @@ async function readBoundedRows(client: Pick<MikrotikClient, "get">, path: string
return rows
}
function routeLookupPath(prefix: string, table: string, includeScopedDestinations = false) {
const properties = ".id,dst-address,routing-table,afi,ospf,active,ospf.metric,immediate-gw,contribution"
const destination = includeScopedDestinations ? "" : `&dst-address=${encodeURIComponent(prefix)}`
return `/routing/route?.proplist=${properties}&routing-table=${encodeURIComponent(table)}${destination}`
}
export async function readOspfRouteAbsence(client: Pick<MikrotikClient, "get">, selection: { prefix: string; table: string }, beforeRead: () => Promise<unknown>) {
const prefix = canonicalOspfRouteProofPrefix(selection.prefix)
if (!prefix || !selection.table || selection.table.length > 200) throw new OspfRouteProofError("Invalid OSPF route absence context")
const routes = await readBoundedRows(client, "/routing/route", beforeRead)
// RouterOS can render link-local destinations as fe80::%interface/64.
const linkLocal = /^fe[89ab][0-9a-f]:/i.test(prefix)
const routes = await readBoundedRows(client, routeLookupPath(prefix, selection.table, linkLocal), beforeRead)
for (const row of routes) {
const observed = row["dst-address"]
const scoped = typeof observed === "string" ? /^([0-9a-f:]+)%[^/%]+(\/\d+)$/i.exec(observed) : null
@@ -52,7 +60,7 @@ export async function readOspfRouteProof(client: Pick<MikrotikClient, "get" | "p
const prefix = canonicalOspfRouteProofPrefix(selection.prefix)
if (!prefix || !selection.table || !selection.interface || selection.table.length > 200 || selection.interface.length > 200) throw new OspfRouteProofError("Invalid OSPF route proof context")
const family = isIP(prefix.split("/")[0])
const rows = await readBoundedRows(client, "/routing/route", beforeRead)
const rows = await readBoundedRows(client, routeLookupPath(prefix, selection.table), beforeRead)
const matches = rows.filter(row => {
if (row["routing-table"] !== selection.table || canonicalOspfRouteProofPrefix(row["dst-address"]) !== prefix) return false
const active = flag(row.active)
@@ -74,7 +82,9 @@ export async function readOspfRouteProof(client: Pick<MikrotikClient, "get" | "p
if (metric === null) throw new OspfRouteProofError("OSPF route metric not confirmed")
const gateway = typeof row["immediate-gw"] === "string" ? row["immediate-gw"] : ""
const context = /^([^%@]+)%([^@]+)(?:@([^@]+))?$/.exec(gateway)
if (!context || isIP(context[1]) !== family || context[2] !== selection.interface || context[3] && context[3] !== selection.table) throw new OspfRouteProofError("OSPF route does not resolve through the selected interface/VRF")
const directInterface = gateway === selection.interface && !isIP(gateway)
const scopedInterface = context && isIP(context[1]) === family && context[2] === selection.interface && (!context[3] || context[3] === selection.table)
if (!directInterface && !scopedInterface) throw new OspfRouteProofError("OSPF route does not resolve through the selected interface/VRF")
await beforeRead()
return { id: String(row[".id"]), family: family === 4 ? "ipv4" : "ipv6", prefix: selection.prefix, table: selection.table, interface: selection.interface, immediateGateway: gateway, ospf: true, active: true, metric }
}
+29 -9
View File
@@ -1,5 +1,5 @@
import { withPartitionWrite, isPartitionWriteOutcomeUnknown, PartitionWriteError, type PartitionWriteOptions } from "./partition-write-transaction.js"
import { and, asc, desc, eq, gte, lte } from "drizzle-orm"
import { and, asc, desc, eq, gte, inArray, lt, lte, sql } from "drizzle-orm"
import { executeGuardedApplicationWrite } from "./config-mutation-policy.js"
import { withApplicationCollectorLock } from "./config-mutation-lock.js"
import { db, pool } from "../db/index.js"
@@ -354,17 +354,37 @@ async function updateTrafficSettingsImpl(patch: {
return await getSettingsRow()
}
export interface TrafficHistoryReadOptions { untilIso?: string; maxRows?: number }
export interface TrafficHistoryReadOptions {
untilIso?: string
maxRows?: number
interfaceNames?: readonly string[]
interfaceCountersOnly?: boolean
precedingMaxIntervalMs?: number
}
export async function readServerSamplesInRange(serverId: number, sinceIso: string, options: TrafficHistoryReadOptions = {}) {
if (options.maxRows !== undefined && (!Number.isSafeInteger(options.maxRows) || options.maxRows < 1 || options.maxRows > 100000)) throw new Error("Некорректный предел истории трафика")
let query = db.select()
.from(trafficSamples)
.where(and(
const scope = and(
eq(trafficSamples.serverId, serverId),
options.interfaceNames === undefined ? undefined : inArray(trafficSamples.interfaceName, options.interfaceNames),
options.interfaceCountersOnly ? eq(trafficSamples.peerPublicKey, "") : undefined,
)
const inside = db.select().from(trafficSamples).where(and(scope, gte(trafficSamples.sampledAt, sinceIso),
options.untilIso === undefined ? undefined : lte(trafficSamples.sampledAt, options.untilIso)))
let query = inside.$dynamic()
if (options.precedingMaxIntervalMs !== undefined) {
const earliest = new Date(Date.parse(sinceIso) - options.precedingMaxIntervalMs).toISOString()
const priorWave = db.select({ sampledAt: trafficSamples.sampledAt }).from(trafficSamples).where(and(
eq(trafficSamples.serverId, serverId),
gte(trafficSamples.sampledAt, sinceIso),
options.untilIso === undefined ? undefined : lte(trafficSamples.sampledAt, options.untilIso),
))
.orderBy(asc(trafficSamples.sampledAt)).$dynamic()
gte(trafficSamples.sampledAt, earliest),
lt(trafficSamples.sampledAt, sinceIso),
)).orderBy(desc(trafficSamples.sampledAt)).limit(1)
// The predecessor is a single poll wave from the same SQL snapshot, even for a filtered interface.
query = query.unionAll(db.select().from(trafficSamples).where(and(scope,
gte(trafficSamples.sampledAt, earliest), lt(trafficSamples.sampledAt, sinceIso),
sql`${trafficSamples.sampledAt} = (${priorWave})`,
)))
}
query = query.orderBy(asc(trafficSamples.sampledAt))
if (options.maxRows !== undefined) query = query.limit(options.maxRows + 1)
const rows = await query
if (options.maxRows !== undefined && rows.length > options.maxRows) throw new Error("История трафика превышает предел; выберите меньший диапазон")
+81 -20
View File
@@ -22,8 +22,9 @@ import {
listFlowRowsForWindow,
type PendingFlowRow,
} from "./traffic-flow-ingest.js"
import { flowDataEpoch, MAX_PENDING } from "./traffic-flow-engine.js"
import { ADMITTED_FLOW_DAY_DIM, flowDataEpoch, MAX_PENDING, OBSERVED_FLOW_IFACE_COVERAGE_DIM, OBSERVED_FLOW_IFACE_DIM } from "./traffic-flow-engine.js"
import { resolveIfaceName } from "./traffic-flow-ifaces.js"
import { factIfaceAliases } from "./traffic-flow-ifindex.js"
import { getTrafficFlowSettingsRow } from "./traffic-flow-settings.js"
import { applicationName, flowRowMatchesFilter } from "./traffic-flow-apps.js"
import { collapseGreEspRows, dedupFlowRowsMaxBytes, flowTupleKey } from "./traffic-flow-dedup.js"
@@ -106,10 +107,10 @@ async function userIfaceAllow(userId: string): Promise<Map<number, Set<string>>
return allow
}
function seriesFromRows(rows: PendingFlowRow[], minutes: number, direction: (row: PendingFlowRow) => "rx" | "tx" | "both" | null) {
function seriesFromRows(rows: PendingFlowRow[], minutes: number, direction: (row: PendingFlowRow) => "rx" | "tx" | "both" | null, now = Date.now()) {
const slots = Math.min(60, Math.max(5, minutes))
const slotMs = (minutes * 60_000) / slots
const start = Date.now() - minutes * 60_000
const start = now - minutes * 60_000
const rx = Array(slots).fill(0) as number[]
const tx = Array(slots).fill(0) as number[]
let undirectedBytes = 0
@@ -191,6 +192,35 @@ function analyticsQueryKey(q: FlowAnalyticsQuery): string {
const ANALYTICS_CACHE_TTL_MS = 2000
let analyticsCache: { key: string; at: number; dto: FlowAnalyticsDto } | null = null
async function readAdmittedFlowTotals(serverId: number, iface: string, minutes: number, now: number): Promise<FlowAnalyticsDto["admittedTotals"]> {
const end = Math.floor(now / 60_000) * 60_000
const from = new Date(end - minutes * 60_000).toISOString(), to = new Date(end).toISOString()
type Totals = { bytes: number | string; packets: number | string; count: number; missing?: number }
const params = [serverId, from, to]
const [totals] = iface
? await flowReadDbAll<Totals>(`
SELECT COALESCE(SUM(observed.bytes), 0) AS bytes, COALESCE(SUM(observed.packets), 0) AS packets,
COUNT(*)::int AS count,
COUNT(*) FILTER (WHERE coverage.bytes IS DISTINCT FROM s.bytes OR coverage.packets IS DISTINCT FROM s.packets)::int AS missing
FROM flow_minute_stats s
LEFT JOIN flow_minute_dims coverage ON coverage.server_id = s.server_id AND coverage.bucket_at = s.bucket_at
AND coverage.dim = ? AND coverage.key = 'all'
LEFT JOIN LATERAL (
SELECT SUM(d.bytes) AS bytes, SUM(d.packets) AS packets
FROM flow_minute_dims d
WHERE d.server_id = s.server_id AND d.bucket_at = s.bucket_at AND d.dim = ? AND d.key = ANY(?::text[])
) observed ON true
WHERE s.server_id = ? AND s.bucket_at >= ? AND s.bucket_at < ?
`, [OBSERVED_FLOW_IFACE_COVERAGE_DIM, OBSERVED_FLOW_IFACE_DIM, factIfaceAliases(iface, serverId), ...params])
: await flowReadDbAll<Totals>(`
SELECT COALESCE(SUM(bytes), 0) AS bytes, COALESCE(SUM(packets), 0) AS packets, COUNT(*)::int AS count
FROM flow_minute_stats WHERE server_id = ? AND bucket_at >= ? AND bucket_at < ?
`, params)
// A minute can contain writes by both old and new collectors after an upgrade.
if (!totals?.count || totals.missing) return undefined
return { bytes: flowCounter(totals.bytes), packets: flowCounter(totals.packets), from, to, minutesWithData: totals.count }
}
async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedFlowAnalyticsRead): Promise<FlowAnalyticsDto> {
const authorize = scope?.authorize ?? (async () => {})
await authorize()
@@ -198,7 +228,8 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
const top = Math.min(50, Math.max(10, settings.topN))
const windowSec = Math.max(60, q.minutes * 60)
await authorize()
const raw = await listFlowRowsForWindow(q.minutes, q.serverId, { ...(scope ? { maxRows: scope.maxRows } : {}), untilIso: new Date().toISOString() })
const now = Date.now()
const raw = await listFlowRowsForWindow(q.minutes, q.serverId, { ...(scope ? { maxRows: scope.maxRows } : {}), untilIso: new Date(now).toISOString() })
for (const row of raw) if (scope && (!scope.serverIds.includes(row.serverId) || !Number.isSafeInteger(row.bytes) || row.bytes < 0 || !Number.isSafeInteger(row.packets) || row.packets < 0)) throw new Error("Недостоверная строка аналитики NetFlow")
await authorize()
const catalog = await getServerCatalog(scope ? [q.serverId!] : undefined)
@@ -224,7 +255,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
}
const selected = ifaceFilter || allow ? raw.filter(row => matchesIngress(row) || matchesEgress(row)) : raw
const { rows, espDirections } = collapseGreEspRows(selected, ifaceFilter ? interfaceDirection : serverDirection)
const previousMinute = new Date(Math.floor(Date.now() / 60_000) * 60_000 - 60_000).toISOString()
const previousMinute = new Date(Math.floor(now / 60_000) * 60_000 - 60_000).toISOString()
const ifacesForWire = new Set<string>()
for (const row of selected) {
ifacesForWire.add(resolveIfaceName(row.serverId, row.inIface).name)
@@ -478,7 +509,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
return allow && !ifaceFilter ? (ingress ? "tx" : "rx") : (ingress ? "rx" : "tx")
}
return espDirections.get(row) ?? serverDirection(row)
})
}, now)
const rxSeries = fromBuckets.rx
const txSeries = fromBuckets.tx
@@ -524,12 +555,18 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
const greNames = ringServer ? enGreIfaceNames(topo, ringServer, [...ifacesForWire]) : []
await authorize()
const wire = ringServer ? await latestWireBps(ringServer, greNames, Boolean(scope)) : { bps: 0, bytes: 0 }
await authorize()
const admittedTotals = q.serverId !== undefined && !q.userId
? await readAdmittedFlowTotals(q.serverId, ifaceFilter, q.minutes, now)
: undefined
if (scope && (!Number.isSafeInteger(totalBytes) || !Number.isSafeInteger(totalPackets) || !Number.isSafeInteger(bytesPayload) || !Number.isSafeInteger(bytesOverlay) || !Number.isSafeInteger(bytesMesh))) throw new Error("Переполнение счётчика аналитики NetFlow")
await authorize()
if (scope && (!isDeepStrictEqual(topo, await loadFlowTopology(scope.serverIds, authorize)) || (await readFlowSourceLimits()).topN !== settings.topN)) throw new Error("Топология или предел источника NetFlow изменились во время чтения")
await authorize()
return {
timeBasis: "collector-receipt",
...(admittedTotals ? { admittedTotals } : {}),
bpsNow,
bytes: totalBytes,
packets: totalPackets,
@@ -741,8 +778,8 @@ export async function getFlowMonthly(month: string, serverId?: number | readonly
if (!bounds) {
throw Object.assign(new Error("Некорректный месяц отчёта"), { statusCode: 400 })
}
const params: Array<string | number> = [bounds.start, bounds.end]
let where = "day >= ? AND day < ? AND dim IN ('country', 'service', 'asn')"
const params: Array<string | number> = [bounds.start, bounds.end, ADMITTED_FLOW_DAY_DIM]
let where = "day >= ? AND day < ? AND dim IN ('country', 'service', 'asn', ?)"
if (Array.isArray(serverId)) {
if (serverId.some(id => !Number.isSafeInteger(id) || id <= 0)) throw new Error("Некорректная область месячного NetFlow")
if (serverId.length === 0) where += " AND false"
@@ -752,38 +789,62 @@ export async function getFlowMonthly(month: string, serverId?: number | readonly
params.push(serverId as number)
}
if (options && (!Number.isSafeInteger(options.maxKeys) || options.maxKeys < 1 || options.maxKeys > 10000)) throw new Error("Некорректный предел месячных dimension keys")
if (options) params.push(options.maxKeys + 1)
if (options) params.push(options.maxKeys + 2)
const rows = await flowReadDbAll<{ dim: string; key: string; bytes: number; packets: number }>(`
SELECT dim AS dim, key AS key, SUM(bytes) AS bytes, SUM(packets) AS packets
FROM flow_daily_dims
WHERE ${where}
GROUP BY dim, key
WITH scoped AS (
SELECT server_id, day, dim, key, bytes, packets FROM flow_daily_dims WHERE ${where}
), daily AS (
SELECT server_id, day, dim, SUM(bytes) AS bytes, SUM(packets) AS packets
FROM scoped GROUP BY server_id, day, dim
), chosen AS (
SELECT DISTINCT ON (server_id, day) dim, bytes, packets
FROM daily
ORDER BY server_id, day, bytes DESC, packets DESC, CASE dim WHEN 'service' THEN 1 WHEN 'asn' THEN 2 WHEN 'country' THEN 3 ELSE 0 END
)
SELECT dim, key, SUM(bytes) AS bytes, SUM(packets) AS packets
FROM scoped WHERE dim IN ('country', 'service', 'asn') GROUP BY dim, key
UNION ALL
SELECT 'total', CASE WHEN BOOL_AND(dim NOT IN ('country','service','asn')) IS FALSE THEN 'legacy-dimensions' ELSE 'admitted-daily' END,
COALESCE(SUM(bytes), 0), COALESCE(SUM(packets), 0)
FROM chosen
${options ? "LIMIT ?" : ""}
`, params)
if (options && rows.length > options.maxKeys) throw new Error("Месячный NetFlow превышает предел dimension keys")
if (options && rows.length > options.maxKeys + 1) throw new Error("Месячный NetFlow превышает предел dimension keys")
const countries: Array<{ key: string; bytes: number; packets: number }> = []
const services: Array<{ key: string; bytes: number; packets: number }> = []
const asns: Array<{ key: string; bytes: number; packets: number }> = []
let bytes = 0
const total = rows.find(row => row.dim === "total")
if (!total) throw new Error("Месячный итог NetFlow недоступен")
const bytes = flowCounter(total.bytes)
for (const row of rows) {
const rec = { key: row.key, bytes: flowCounter(row.bytes), packets: flowCounter(row.packets) }
if (row.dim === "country") {
countries.push(rec)
bytes = flowCounter(bytes + rec.bytes)
} else if (row.dim === "service") services.push(rec)
else if (row.dim === "asn") asns.push(rec)
}
const countryBytes = flowCounterSum(countries.map(row => row.bytes))
if (countryBytes < bytes) {
const packets = Math.max(0, flowCounter(total.packets) - flowCounterSum(countries.map(row => row.packets)))
const unknown = countries.find(row => row.key === "unknown")
if (unknown) {
unknown.bytes = flowCounter(unknown.bytes + (bytes - countryBytes))
unknown.packets = flowCounter(unknown.packets + packets)
} else {
countries.push({ key: "unknown", bytes: bytes - countryBytes, packets })
}
}
const [year, monthIndex] = month.split("-").map(Number)
const leap = year! % 4 === 0 && (year! % 100 !== 0 || year! % 400 === 0)
const daysInMonth = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][monthIndex! - 1]!
const windowSec = daysInMonth * 86_400
const countryTotal = flowCounterSum(countries.map(row => row.bytes)) || bytes || 1
return {
month,
bytes,
countries: toBreakdown(countries, countryTotal, windowSec),
services: toBreakdown(services, flowCounterSum(services.map(row => row.bytes)) || 1, windowSec),
asns: toBreakdown(asns, flowCounterSum(asns.map(row => row.bytes)) || 1, windowSec),
totalsSource: total.key === "legacy-dimensions" ? "legacy-dimensions" : "admitted-daily",
countries: toBreakdown(countries, bytes, windowSec),
services: toBreakdown(services, bytes, windowSec),
asns: toBreakdown(asns, bytes, windowSec),
}
}
@@ -12,6 +12,7 @@ import * as accumulator from "./traffic-flow-fact-accumulator.js"
import * as serial from "./serial-operation.js"
import * as parse from "./traffic-flow-parse.js"
import * as ip from "./traffic-flow-ip.js"
import * as ifindex from "./traffic-flow-ifindex.js"
import { pruneRipeSqlite } from "./traffic-flow-ripe.js"
import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js"
@@ -51,7 +52,7 @@ const dependencies:Record<string,unknown>={
"./traffic-flow-ripe.js":{enqueueRipeMisses:()=>{throw Error("unexpected RIPE enqueue")},pruneRipeSqlite},
"./traffic-flow-settings.js":{invalidateTrafficFlowSettingsCache:()=>{}},
"./traffic-flow-ifaces.js":{maybeRefreshIfaces:async()=>{}},
"./traffic-flow-ifindex.js":{canonicalFactIface:(_server:number,iface:string)=>iface},
"./traffic-flow-ifindex.js":ifindex,
"./traffic-flow-facts-filter.js":{shouldWriteFlowFact:()=>true},
"./traffic-flow-dest.js":{flowRateDirection:()=>"tx",resolveInternetDest:()=>({dest:"",ripe:null,classified:{category:"web",service:"https"},country:"XX",asn:0})},
"./traffic-flow-topology.js":{peekFlowTopology:()=>({}),peekServerCatalog:()=>({byId:new Map([[1,{type:"router"}]])}),getServerCatalog:()=>{throw Error("unexpected catalog SQL")},loadFlowTopology:()=>{throw Error("unexpected topology SQL")}},
+28 -2
View File
@@ -15,7 +15,7 @@ import { applicationName } from "./traffic-flow-apps.js"
import { enqueueRipeMisses, pruneRipeSqlite } from "./traffic-flow-ripe.js"
import { invalidateTrafficFlowSettingsCache } from "./traffic-flow-settings.js"
import { maybeRefreshIfaces } from "./traffic-flow-ifaces.js"
import { canonicalFactIface } from "./traffic-flow-ifindex.js"
import { canonicalFactIface, resolveIfaceName } from "./traffic-flow-ifindex.js"
import { shouldWriteFlowFact } from "./traffic-flow-facts-filter.js"
import { canonicalIp } from "./traffic-flow-ip.js"
import { flowRateDirection, resolveInternetDest } from "./traffic-flow-dest.js"
@@ -44,6 +44,9 @@ export const MAX_PENDING = 50_000
export const DAILY_ASN_TOP = 500
export const DAILY_RETENTION_DAYS = 396
export const MINUTE_RETENTION_HOURS = 48
export const OBSERVED_FLOW_IFACE_DIM = "observed-iface-v1"
export const OBSERVED_FLOW_IFACE_COVERAGE_DIM = "observed-iface-coverage-v1"
export const ADMITTED_FLOW_DAY_DIM = "admitted-v1"
let pendingCap = MAX_PENDING
const ensuredParts = new Set<string>()
@@ -433,9 +436,23 @@ export function queueParsedFlows(serverId: number, flows: ParsedFlowInput[]): vo
const destMeta = resolveInternetDest({src:flow.src,dst:flow.dst,proto:flow.proto,srcPort:flow.srcPort,dstPort:flow.dstPort,
serverId,inIface:flow.inIface,topo,natSrc:flow.natSrc,natDst:flow.natDst,natSrcPort:flow.natSrcPort,natDstPort:flow.natDstPort})
const classified=destMeta.classified,country=destMeta.country
const seenIfaces = new Map<string, string>()
let interfaceUncertain = false
for (const rawIface of [flow.inIface, flow.outIface]) {
const {name, index} = resolveIfaceName(serverId, rawIface)
if (name === "—") continue
if (index && name === `#${index}`) interfaceUncertain = true
if (index || !seenIfaces.has(name)) seenIfaces.set(name, index ? String(Number(index)) : name)
}
// A cold index can be an alias of another endpoint. Keep that minute out of complete interface totals.
const observedDimensions = interfaceUncertain ? [] : [
{dim:OBSERVED_FLOW_IFACE_COVERAGE_DIM,key:"all"},
...[...seenIfaces.values()].map(key => ({dim:OBSERVED_FLOW_IFACE_DIM,key})),
]
const dims=[{dim:"proto",key:protoName(flow.proto)},{dim:"app",key:applicationName(flow.proto,flow.dstPort,flow.srcPort)},
{dim:"iface",key:flow.inIface||"__unknown__"},{dim:"category",key:classified.category},{dim:"service",key:classified.service},
...(country?[{dim:"country",key:country}]:[]),{dim:"asn",key:destMeta.asn?String(destMeta.asn):"unknown"}].map(row=>({...row,cacheKey:dimKey(serverId,bucketAt,row.dim,row.key)}))
...observedDimensions,
{dim:"country",key:country||"unknown"},{dim:"asn",key:destMeta.asn?String(destMeta.asn):"unknown"}].map(row=>({...row,cacheKey:dimKey(serverId,bucketAt,row.dim,row.key)}))
const fact:FactRow|null=shouldWriteFlowFact({serverId,serverType,inIface:flow.inIface,outIface:flow.outIface,proto:flow.proto,
srcPort:flow.srcPort,dstPort:flow.dstPort,src:flow.src,dst:flow.dst,topo,dest:destMeta.dest,
natSrc:flow.natSrc,natDst:flow.natDst,natSrcPort:flow.natSrcPort,natDstPort:flow.natDstPort})?
@@ -751,6 +768,15 @@ async function upsertMinuteAndDaily(rollup:Map<string,MinuteRollup>,dims:Map<str
uniqueDst: acc.dsts.size,
conversations: acc.conversations,
})
const day = dayKey(bucketAt ?? "")
await ensureParentPartition("flow_daily_dims", day)
await dbQuery(`
INSERT INTO flow_daily_dims (server_id, day, dim, key, bytes, packets)
VALUES (?, ?, ?, 'all', ?, ?)
ON CONFLICT(server_id, day, dim, key) DO UPDATE SET
bytes = flow_daily_dims.bytes + excluded.bytes,
packets = flow_daily_dims.packets + excluded.packets
`, [Number(serverIdRaw), day, ADMITTED_FLOW_DAY_DIM, acc.bytes, acc.packets])
}
for (const [k, acc] of dims) {
const [serverIdRaw, bucketAt, dim, key] = k.split("\0")
@@ -12,19 +12,35 @@ function source(names:string[]):string {
}
function fixture() {
const original=Error("original ring validation"),ackError=Error("original transport failure"),events:string[]=[]
let badRing=false,badAck=false,quality:unknown=null
let badRing=false,badAck=false,quality:unknown=null,now=0
const run=new Function("deps",`
const exports={},flowIngestQualitySchema=deps.schema,HEARTBEAT_UNAVAILABLE="unavailable";
let lastHeartbeat=null,heartbeatFailure=null,state={bound:false,address:null},restartAttempts=4,worker={};
const FLOW_HEARTBEAT_MAX_AGE_MS=15000,performance={now:deps.now};
let lastHeartbeat=null,lastHeartbeatAt=-Infinity,heartbeatFailure=null,state={bound:false,address:null},restartAttempts=4,worker={};
const applyRingSnapshot=deps.consume,markFlowRingSnapshotUnavailable=deps.invalidate,postToWorker=deps.send;
const getEngineStats=()=>({pendingSize:0,dropped:0,packetsReceived:0});
const getTrafficFlowSettingsRow=async()=>({packetsReceived:77,lastExporterIp:null,lastError:null,lastDatagramAt:null});
${source(["applyWorkerHeartbeat","rejectWorkerHeartbeat","handleWorkerMessage","getFlowWorkerHealth","getFlowRuntimeCounters","applyHeartbeatForTests"])}
${source(["applyWorkerHeartbeat","rejectWorkerHeartbeat","handleWorkerMessage","getFlowListenerState","hasCurrentFlowHeartbeat","getFlowWorkerHealth","getFlowRuntimeCounters","applyHeartbeatForTests"])}
return {receive:handleWorkerMessage,local:applyHeartbeatForTests,inspect:()=>({lastHeartbeat,heartbeatFailure,state}),health:getFlowWorkerHealth,counters:getFlowRuntimeCounters};
`)({schema:flowIngestQualitySchema,consume:(_rows:unknown,data:unknown)=>{events.push("consume");if(badRing)throw original;quality=data},invalidate:()=>{events.push("invalidate");quality=null},send:()=>{events.push("ack");if(badAck)throw ackError}})
`)({now:()=>now,schema:flowIngestQualitySchema,consume:(_rows:unknown,data:unknown)=>{events.push("consume");if(badRing)throw original;quality=data},invalidate:()=>{events.push("invalidate");quality=null},send:()=>{events.push("ack");if(badAck)throw ackError}})
const payload=(packetsReceived=11)=>({bound:true,address:"127.0.0.1:2055",packetsReceived,lastExporterIp:"127.0.0.1",lastError:"",lastDatagramAt:"2026-10-29T10:00:00.000Z",pendingSize:1,dropped:0,rowsStored:1,workerAlive:true,rings:[],dataQuality:createFlowIngestQuality().snapshot()})
return {run,payload,events,original,ackError,ring:(value:boolean)=>{badRing=value},ack:(value:boolean)=>{badAck=value},quality:()=>quality}
return {run,payload,events,original,ackError,ring:(value:boolean)=>{badRing=value},ack:(value:boolean)=>{badAck=value},quality:()=>quality,advance:(ms:number)=>{now+=ms}}
}
test("a stale heartbeat keeps the last observed counters but cannot report a live listener", async () => {
const f = fixture()
f.run.receive({type:"heartbeat",heartbeatId:"first",payload:f.payload()})
assert.equal((await f.run.counters()).collectorCountersSource, "live")
f.advance(15_001)
assert.equal(f.run.health().bound, false)
assert.equal(f.run.health().alive, false)
const counters = await f.run.counters()
assert.equal(counters.packetsReceived, 11)
assert.equal(counters.collectorCountersSource, "last-known")
assert.equal(counters.dataQuality, null)
f.run.receive({type:"heartbeat",heartbeatId:"next",payload:f.payload(12)})
assert.equal(f.run.health().bound, true)
assert.equal((await f.run.counters()).collectorCountersSource, "live")
})
test("ring refusal is caught, marks observation unavailable, preserves original error/counters and ACKs consumption",async()=>{
const f=fixture();f.run.receive({type:"heartbeat",heartbeatId:"first",payload:f.payload()});f.events.length=0;f.ring(true)
assert.doesNotThrow(()=>f.run.receive({type:"heartbeat",heartbeatId:"bad",payload:f.payload(999)}))
@@ -11,6 +11,7 @@ import {
getFlowListenerState,
getFlowRuntimeCounters,
getFlowWorkerHealth,
readTrafficFlowSettingsStatus,
ingestParsedFlowsForServerForTests,
lastFlushUsedTransactionForTests,
maybeRefreshIfaces,
@@ -148,10 +149,16 @@ applyHeartbeatForTests({
})
assert.equal(getFlowListenerState().bound, true)
assert.equal((await getFlowRuntimeCounters()).packetsReceived, 42)
assert.equal((await getFlowRuntimeCounters()).collectorCountersSource, "live")
const settingsStatus = await readTrafficFlowSettingsStatus()
assert.equal(settingsStatus.collectorCountersSource, "live")
assert.equal(settingsStatus.packetsReceived, 42)
assert.equal(settingsStatus.listenerBound, true)
assert.equal(getFlowWorkerHealth().alive, false)
setWantListenForTests(true)
assert.equal(simulateWorkerExitForTests(), 1)
assert.equal(getFlowListenerState().bound, false)
assert.equal((await getFlowRuntimeCounters()).collectorCountersSource, "last-known")
setWantListenForTests(false)
resetFlowRingsForTests()
+34 -10
View File
@@ -40,6 +40,7 @@ import {
getTrafficFlowSettingsRow,
listHostPeers,
invalidateTrafficFlowSettingsCache,
toTrafficFlowSettingsDto,
} from "./traffic-flow-settings.js"
import { projectFlowTalkers } from "./traffic-flow-talkers-model.js"
import { resolveIfaceName } from "./traffic-flow-ifaces.js"
@@ -67,7 +68,9 @@ let listenerGeneration=0
let restartTimer: ReturnType<typeof setTimeout> | null = null
let restartAttempts = 0
let lastHeartbeat: CollectorHeartbeat | null = null
let lastHeartbeatAt = -Infinity
let heartbeatFailure:Error|null=null
const FLOW_HEARTBEAT_MAX_AGE_MS = 15_000
const HEARTBEAT_UNAVAILABLE="NetFlow: недостоверный снимок коллектора; наблюдения недоступны"
let state: FlowListenerState = { bound: false, address: null }
let wantListen = false
@@ -127,6 +130,7 @@ function applyWorkerHeartbeat(payload: CollectorHeartbeat): void {
const dataQuality=payload.dataQuality==null?null:flowIngestQualitySchema.parse(payload.dataQuality)
applyRingSnapshot(payload.rings,dataQuality)
lastHeartbeat = {...payload,dataQuality,rings:[]}
lastHeartbeatAt = performance.now()
heartbeatFailure=null
state = { bound: payload.bound, address: payload.address }
restartAttempts = 0
@@ -151,7 +155,7 @@ function handleWorkerMessage(msg: WorkerToMain): void {
}
if (msg.type === "error") {
lastHeartbeat = lastHeartbeat
? { ...lastHeartbeat, lastError: msg.payload.message, workerAlive: true }
? { ...lastHeartbeat, lastError: msg.payload.message }
: null
}
}
@@ -215,6 +219,7 @@ async function spawnWorker(authorize?: FlowLifecycleAuthorization): Promise<void
if (worker !== w) return
worker = null
state = { bound: false, address: null }
if (lastHeartbeat) lastHeartbeat = { ...lastHeartbeat, workerAlive: false, bound: false }
if (!wantListen || startupPending) return
const delay = Math.min(30_000, 1000 * 2 ** restartAttempts)
restartAttempts += 1
@@ -250,6 +255,7 @@ function stopWorkerProcess(authorize?: FlowLifecycleAuthorization): void {
// The sync public stop returns immediately; starts and DB operations drain it.
void workerLifecycle.retire(current,authorize)
worker = null
if (lastHeartbeat) lastHeartbeat = { ...lastHeartbeat, workerAlive: false, bound: false }
}
}
@@ -275,15 +281,19 @@ export function setWantListenForTests(value: boolean): void {
}
export function getFlowListenerState(): FlowListenerState {
return state
return state.bound && !hasCurrentFlowHeartbeat() ? { bound: false, address: null } : state
}
function hasCurrentFlowHeartbeat(): boolean {
return Boolean(lastHeartbeat?.workerAlive) && heartbeatFailure === null && performance.now() - lastHeartbeatAt <= FLOW_HEARTBEAT_MAX_AGE_MS
}
export function getFlowWorkerHealth(): FlowWorkerHealth {
const hb = lastHeartbeat
const mem = getEngineStats()
return {
alive: heartbeatFailure===null&&Boolean(worker) && (hb?.workerAlive ?? false),
bound: state.bound,
alive: Boolean(worker) && hasCurrentFlowHeartbeat(),
bound: getFlowListenerState().bound,
pendingSize: hb?.pendingSize ?? mem.pendingSize,
dropped: hb?.dropped ?? mem.dropped,
packetsReceived: hb?.packetsReceived ?? mem.packetsReceived,
@@ -293,17 +303,20 @@ export function getFlowWorkerHealth(): FlowWorkerHealth {
export async function getFlowRuntimeCounters() {
const hb = lastHeartbeat
if (hb) {
const current = hasCurrentFlowHeartbeat()
return {
collectorCountersSource: current ? "live" as const : "last-known" as const,
packetsReceived: hb.packetsReceived,
lastExporterIp: hb.lastExporterIp ?? null,
lastError: heartbeatFailure?HEARTBEAT_UNAVAILABLE:hb.lastError || null,
lastError: heartbeatFailure || hb.workerAlive && !current ? HEARTBEAT_UNAVAILABLE : hb.lastError || null,
lastDatagramAt: hb.lastDatagramAt ?? null,
dropped: hb.dropped ?? 0,
dataQuality:hb.dataQuality??null,
dataQuality:current ? hb.dataQuality ?? null : null,
}
}
const settings = await getTrafficFlowSettingsRow()
return {
collectorCountersSource: "last-known" as const,
packetsReceived: settings.packetsReceived,
lastExporterIp: settings.lastExporterIp ?? null,
lastError: heartbeatFailure?HEARTBEAT_UNAVAILABLE:settings.lastError || null,
@@ -313,6 +326,13 @@ export async function getFlowRuntimeCounters() {
}
}
export async function readTrafficFlowSettingsStatus() {
const settings = await toTrafficFlowSettingsDto(getFlowListenerState())
const { packetsReceived, lastExporterIp, lastError, lastDatagramAt, dataQuality, collectorCountersSource } = await getFlowRuntimeCounters()
const { bound, address } = getFlowListenerState()
return { ...settings, packetsReceived, lastExporterIp, lastError, lastDatagramAt, dataQuality, collectorCountersSource, listenerBound: bound, listenerAddress: address }
}
export async function startTrafficFlowListener(authorize?: FlowLifecycleAuthorization) {
if (databaseStop) throw new Error("NetFlow listener is paused for a database operation")
// Fence the internal stop and flush separately. The ordinary synchronous stop
@@ -546,18 +566,21 @@ export async function listStoredFlowRows(sinceIso: string, serverId?: number, op
}
export async function listFlowRowsForWindow(minutes: number, serverId?: number, options?: { maxRows?: number; untilIso?: string }): Promise<PendingFlowRow[]> {
const sinceIso = new Date(Date.now() - minutes * 60_000).toISOString()
const end = options?.untilIso === undefined ? Date.now() : Date.parse(options.untilIso)
if (!Number.isFinite(end)) throw new Error("Некорректная граница flow window")
const sinceIso = new Date(end - minutes * 60_000).toISOString()
// History remains available while the collector starts or is stopped; pending rows are merged below.
return await listStoredFlowRows(sinceIso, serverId, options)
}
export async function listFlowTalkers(minutes = 5): Promise<FlowStatsDto> {
const settings = await getTrafficFlowSettingsRow()
const runtime = await getFlowRuntimeCounters()
const rows = await listFlowRowsForWindow(minutes, undefined, { untilIso: new Date().toISOString() })
const catalog = await getServerCatalog()
const nameById = new Map([...catalog.byId].map(([id, server]) => [id, server.name]))
const projection = projectFlowTalkers(rows, minutes, settings.topN, nameById, resolveIfaceName)
const runtime = await getFlowRuntimeCounters()
const listener = getFlowListenerState()
return {
exportersOnline: projection.observedExporters,
bytesPerMin: projection.bytesPerMin,
@@ -566,12 +589,13 @@ export async function listFlowTalkers(minutes = 5): Promise<FlowStatsDto> {
topProto: projection.topProto,
talkers: projection.talkers,
dataQuality:runtime.dataQuality,
collectorCountersSource: runtime.collectorCountersSource,
lastExporterIp: runtime.lastExporterIp,
lastError: runtime.lastError,
packetsReceived: runtime.packetsReceived,
lastDatagramAt: runtime.lastDatagramAt,
listenerBound: state.bound,
listenerAddress: state.address,
listenerBound: listener.bound,
listenerAddress: listener.address,
}
}
@@ -17,6 +17,8 @@ import trafficRoutes from '../routes/traffic.js'
import { flowReadBudget } from './traffic-flow-read-policy.js'
import { resetFlowRingsForTests,applyHeartbeatForTests } from './traffic-flow-ingest.js'
import { createFlowIngestQuality } from './traffic-flow-ingest-quality.js'
import { queueParsedFlows, strictDrainEngine } from './traffic-flow-engine.js'
import { rememberServerIfaces } from './traffic-flow-ifindex.js'
import { disableRipeEnqueueForTests } from './traffic-flow-ripe.js'
import { readFlowCatalogWindow } from './traffic-flow-catalog-read.js'
import { invalidateFlowAnalyticsCache } from './traffic-flow-analytics.js'
@@ -200,6 +202,61 @@ try{
invalidateFlowAnalyticsCache()
}
console.log('PASS REST and MCP history and live analytics collapse GRE/ESP and place each confirmed ESP direction once')
const completedMinute=Math.floor(Date.now()/60000)*60000-180000
rememberServerIfaces(1,[{name:'ether-audit',ifindex:'77'}])
const wallClock=Date.now
try {
Date.now=()=>completedMinute+1000
queueParsedFlows(1,Array.from({length:211},(_,i)=>({
src:'192.0.2.1',dst:'198.18.1.1',proto:6,srcPort:1000+i,dstPort:443,
bytes:i<200?100:10,packets:1,inIface:i===210?'ether-audit':'ether-heavy',outIface:i<200?'vxlan':'77',
})))
} finally {Date.now=wallClock}
await strictDrainEngine(async()=>{},[1])
const analyticsUrl=endpoint+'/api/traffic/flow/analytics?range=5m&serverId=1&excludeOverlay=false'
const ifaceUrl=analyticsUrl+'&iface=ether-audit'
const readAnalytics=async(url:string)=>{
const response=await fetch(url)
assert.equal(response.status,200,await response.clone().text())
return await response.json() as FlowAnalyticsDto
}
const admitted=await readAnalytics(analyticsUrl)
assert.equal(admitted.timeBasis,'collector-receipt')
assert.equal(admitted.admittedTotals?.bytes,20110,'the minute aggregate includes every admitted record beyond top-N')
assert.equal(admitted.admittedTotals?.packets,211)
const smallInterface=await readAnalytics(ifaceUrl)
assert.equal(smallInterface.bytes,0,'all detail rows for the small interface are below top-N')
assert.equal(smallInterface.admittedTotals?.bytes,110,'egress is included and the same ingress/egress interface is counted once')
assert.equal(smallInterface.admittedTotals?.packets,11)
const aggregateReport=await client.callTool({name:'mm_netflow_analytics_report_get',arguments:{serverId:1,minutes:5,interface:'ether-audit'}})
assert.ok(!aggregateReport.isError,JSON.stringify(aggregateReport))
assert.equal((aggregateReport.structuredContent as {result:{analytics:FlowAnalyticsDto}}).result.analytics.admittedTotals?.bytes,110)
await pool.query("UPDATE flow_minute_stats SET bytes=bytes+1 WHERE server_id=1 AND bucket_at=$1",[new Date(completedMinute)])
invalidateFlowAnalyticsCache()
assert.equal((await readAnalytics(ifaceUrl)).admittedTotals,undefined,'mixed old/new writes in one minute must not look complete')
assert.equal((await readAnalytics(analyticsUrl)).admittedTotals?.bytes,20111)
const writerMonth=new Date(completedMinute).toISOString().slice(0,7)
const authoritativeMonthly=await fetch(endpoint+'/api/traffic/flow/monthly?month='+writerMonth+'&serverId=1')
assert.equal(authoritativeMonthly.status,200,await authoritativeMonthly.clone().text())
const auditedMonth=await authoritativeMonthly.json() as {bytes:number;totalsSource:string}
assert.equal(auditedMonth.bytes,20110)
assert.equal(auditedMonth.totalsSource,'admitted-daily')
const monthBucket=new Date('2026-09-01T00:00:00Z')
await ensurePartitionFor(pool,'flow_daily_dims','month',monthBucket)
await pool.query(`INSERT INTO flow_daily_dims(server_id,day,dim,key,bytes,packets) VALUES
(1,$1,'country','US',80,8),(1,$1,'country','unknown',20,2),
(1,$1,'service','web',120,12),(1,$1,'asn','unknown',200,20),(2,$1,'asn','unknown',900,90),
(1,$1::date+1,'service','web',40,4)`,[monthBucket])
const monthly=await fetch(endpoint+'/api/traffic/flow/monthly?month=2026-09&serverId=1')
assert.equal(monthly.status,200,await monthly.clone().text())
const monthlyDto=await monthly.json() as {bytes:number;totalsSource:string;countries:{id:string;bytes:number;packets:number}[]}
assert.equal(monthlyDto.bytes,240)
assert.equal(monthlyDto.totalsSource,'legacy-dimensions')
assert.equal(monthlyDto.countries.find(row=>row.id==='unknown')?.bytes,160)
assert.equal(monthlyDto.countries.find(row=>row.id==='unknown')?.packets,16)
console.log('PASS complete archived and per-interface totals survive top-N, mixed legacy minutes stay partial, and monthly unknown geography is counted')
const globalAccess=await createMcpConnection(mcpConnectionCreateSchema.parse({name:'owned global telemetry',mode:'observer',permissions:['mm:traffic:read'],expiresAt:new Date(Date.now()+600000).toISOString(),scope:{serverIds:[1],operationIds:['traffic.flow.talkers.read','netflow.settings.read'],allowGlobal:true},limits:{callsPerMinute:240}}),actor)
const globalClient=new Client({name:'owned-global-quality',version:'1'})
try{
+24 -10
View File
@@ -4,7 +4,7 @@ import { serverSnapshots, type Server } from "../db/schema.js"
import { readServerSamplesInRange, type TrafficHistoryReadOptions } from "./traffic-collector.js"
import { readTrafficCollectionContext } from "./traffic-collection-state.js"
import { applyTrafficFreshness } from "./traffic-freshness.js"
import { buildTrafficFromSamples, isLoopbackName } from "./traffic-rate.js"
import { buildTrafficFromSamples, isLoopbackName, trafficHistoryBoundaryLimitMs } from "./traffic-rate.js"
export type TrafficHistoryServer = Pick<Server, "id" | "name" | "host" | "site" | "country" | "enabled" | "updatedAt">
type CollectionContext = Awaited<ReturnType<typeof readTrafficCollectionContext>>
@@ -12,25 +12,39 @@ type CollectionContext = Awaited<ReturnType<typeof readTrafficCollectionContext>
/** Shared application interpretation of stored counter deltas; no device polling. */
export async function readServerTrafficHistory(server: TrafficHistoryServer, rangeStartMs: number, rangeEndMs: number, onlyInterface?: string,
options: TrafficHistoryReadOptions = {}, context?: CollectionContext) {
const [rows, snapshots, collection] = await Promise.all([
readServerSamplesInRange(server.id, new Date(rangeStartMs).toISOString(), { ...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString() }),
return (await readServerTrafficHistoryWithSeries(server, rangeStartMs, rangeEndMs, onlyInterface, options, context)).history
}
export async function readServerTrafficHistoryWithSeries(server: TrafficHistoryServer, rangeStartMs: number, rangeEndMs: number, onlyInterface?: string,
options: TrafficHistoryReadOptions = {}, context?: CollectionContext) {
const collection = context ?? await readTrafficCollectionContext([server.id])
const boundaryLimitMs = trafficHistoryBoundaryLimitMs(collection.settings?.intervalSec)
const [rows, snapshots] = await Promise.all([
readServerSamplesInRange(server.id, new Date(rangeStartMs).toISOString(), {
...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString(),
interfaceNames: onlyInterface === undefined ? undefined : [onlyInterface], interfaceCountersOnly: true,
precedingMaxIntervalMs: boundaryLimitMs,
}),
db.select({ status: serverSnapshots.status, polledAt: serverSnapshots.polledAt }).from(serverSnapshots).where(eq(serverSnapshots.serverId, server.id)).orderBy(desc(serverSnapshots.polledAt)).limit(1),
context ?? readTrafficCollectionContext([server.id]),
])
const built = buildTrafficFromSamples(rows, rangeStartMs, rangeEndMs, onlyInterface, undefined, true)
const series = buildTrafficFromSamples(rows, rangeStartMs, rangeEndMs, onlyInterface, undefined, true, boundaryLimitMs)
if (series.observations) series.observations = applyTrafficFreshness(series.observations, server, collection.settings, collection.states.get(server.id), rangeEndMs)
const status = snapshots[0]?.status === "offline" ? "offline" as const : snapshots[0]?.status === "online" ? "online" as const : "degraded" as const
return {
const history = {
volumeUnit: "GiB" as const, id: String(server.id), name: server.name || server.host, site: server.site || "—", country: server.country || "UN", status,
observations: built.observations && applyTrafficFreshness(built.observations, server, collection.settings, collection.states.get(server.id), rangeEndMs),
rxNow: built.rxNow, txNow: built.txNow, rxPeak: built.rxPeak, txPeak: built.txPeak,
rxTotal: built.rxTotalGiB, txTotal: built.txTotalGiB, sessions: built.sessions, rxSeries: built.rxSeries, txSeries: built.txSeries,
observations: series.observations,
rxNow: series.rxNow, txNow: series.txNow, rxPeak: series.rxPeak, txPeak: series.txPeak,
rxTotal: series.rxTotalGiB, txTotal: series.txTotalGiB, sessions: series.sessions, rxSeries: series.rxSeries, txSeries: series.txSeries,
}
return { history, series }
}
/** Legacy interface DTO plus independent evidence for nullable MCP state. */
export async function readServerTrafficInterfaces(server: TrafficHistoryServer, rangeEndMs = Date.now(), options: TrafficHistoryReadOptions = {}) {
const rangeStartMs = rangeEndMs - 86400000
const [rows, context] = await Promise.all([readServerSamplesInRange(server.id, new Date(rangeStartMs).toISOString(), { ...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString() }), readTrafficCollectionContext([server.id])])
const [rows, context] = await Promise.all([readServerSamplesInRange(server.id, new Date(rangeStartMs).toISOString(), {
...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString(), interfaceCountersOnly: true,
}), readTrafficCollectionContext([server.id])])
const byIface = new Map<string, typeof rows>()
for (const row of rows) {
if (isLoopbackName(row.interfaceName) || row.peerPublicKey) continue
@@ -34,6 +34,8 @@ else {
await pool.query("DELETE FROM traffic_samples WHERE server_id=$1 AND (flags & 4)=0",[serverId])
await pool.query("UPDATE traffic_samples SET sampled_at=$1 WHERE server_id=$2",[new Date(Date.now()-30_000).toISOString(),serverId])
counter="3751000";const success=await collectTrafficOnce();dto=await detail();assert.equal(dto.observations.currentState,"current");assert.equal(dto.observations.rxNow,true);assert(dto.rxNow>0.9);cases++
const overview=await get("/traffic/servers?range=1h")
assert.deepEqual(overview.peaks,{rx:dto.rxPeak,tx:dto.txPeak});cases++
await pool.query("INSERT INTO scheduler_runs(id,job_key,started_at,finished_at,status,result_json) VALUES($1,'traffic',$2,$3,'ok',$4::jsonb)",[prefix+"run",success.sampledAt,new Date().toISOString(),JSON.stringify(success)])
assert.equal((await buildSignalSnapshotFromCollectors()).trafficByServer.length,1);cases++
const succeeded=dto.observations.lastSucceededAt
+13
View File
@@ -172,3 +172,16 @@ for (const minutes of [5, 15]) {
const weighted = observed([observationSample(0,0,0),observationSample(500,62_500,62_500),observationSample(1500,312_500,312_500)])
assert.equal(weighted.rxSeries[0], 1.5)
assert.equal(weighted.rxSeries[1], 2)
const boundaryRows = [observationSample(-15_000, 0, 0), observationSample(15_000, 3_750_000, 1_875_000),
observationSample(45_000, 7_500_000, 3_750_000), observationSample(75_000, 11_250_000, 5_625_000)]
const boundary = observed(boundaryRows)
assert.equal(boundary.rxTotalGiB, 5_625_000 / 1024 ** 3)
assert.equal(boundary.txTotalGiB, 2_812_500 / 1024 ** 3)
assert(boundary.observations?.rxSeries.slice(0, 45).every(Boolean))
assert(boundary.observations?.rxSeries.slice(45).every(value => !value), "History does not extend past the last poll inside the range")
const lostBoundary = observed([boundaryRows[0], { ...boundaryRows[1], continuityKnown: false }, ...boundaryRows.slice(2)])
assert.equal(lostBoundary.rxTotalGiB, 3_750_000 / 1024 ** 3)
assert(lostBoundary.observations?.rxSeries.slice(0, 15).every(value => !value))
const staleBoundary = observed([observationSample(-100_000, 0, 0), ...boundaryRows.slice(1)])
assert.equal(staleBoundary.rxTotalGiB, 3_750_000 / 1024 ** 3)
+13 -6
View File
@@ -146,6 +146,10 @@ export function sampleSeriesKey(interfaceName: string, peerPublicKey = ""): stri
return `${interfaceName}\0${peerPublicKey}`
}
export function trafficHistoryBoundaryLimitMs(intervalSec = 30): number {
return Math.max(5, intervalSec) * 3000
}
export function buildTrafficFromSamples(
rows: TrafficSampleLike[],
rangeStartMs: number,
@@ -153,6 +157,7 @@ export function buildTrafficFromSamples(
onlyInterface?: string | readonly string[],
peerPublicKey?: string,
includeObservations=false,
boundaryLimitMs = trafficHistoryBoundaryLimitMs(),
): BuiltTrafficSeries {
const empty: BuiltTrafficSeries = {
rxNow: 0,
@@ -206,9 +211,10 @@ export function buildTrafficFromSamples(
continue
}
const sorted = [...arr].sort((a, b) => Date.parse(a.sampledAt) - Date.parse(b.sampledAt))
const sorted = arr.filter(row => Date.parse(row.sampledAt) <= rangeEndMs)
.sort((a, b) => Date.parse(a.sampledAt) - Date.parse(b.sampledAt))
const last = sorted[sorted.length - 1]
if (!last) continue
if (!last || Date.parse(last.sampledAt) < rangeStartMs) continue
for (const row of sorted) {
const t = Date.parse(row.sampledAt)
if (!Number.isFinite(t) || t < rangeStartMs || t > rangeEndMs) continue
@@ -230,15 +236,16 @@ export function buildTrafficFromSamples(
const t0 = parseIsoMs(prev.sampledAt)
const t1 = parseIsoMs(cur.sampledAt)
if(Number.isFinite(Date.parse(cur.sampledAt))&&t1>=rangeStartMs&&t1<=rangeEndMs)lastAttemptAt=Math.max(lastAttemptAt??t1,t1)
if (t1 <= rangeStartMs || (t0 < rangeStartMs && t1 - t0 > boundaryLimitMs)) continue
const sameIdentity = (prev.nativeCounterIdentity ?? null) === (cur.nativeCounterIdentity ?? null)
const identityPresent = cur.continuityKnown === undefined || typeof cur.nativeCounterIdentity === "string" && cur.nativeCounterIdentity.length > 0
const countersKnown = prev.countersKnown !== false && cur.countersKnown !== false && cur.continuityKnown !== false && identityPresent && sameIdentity
const rxBps = countersKnown ? rateBpsFromDelta(prev.rxBytes, cur.rxBytes, t0, t1) : null
const txBps = countersKnown ? rateBpsFromDelta(prev.txBytes, cur.txBytes, t0, t1) : null
if (t1 >= rangeStartMs && t1 <= rangeEndMs) {
if (rxBps !== null) rxBytesDelta += cur.rxBytes - prev.rxBytes
if (txBps !== null) txBytesDelta += cur.txBytes - prev.txBytes
}
// Attribute only the in-window fraction of a measured interval and never extend beyond its next poll.
const overlap = (t1 - Math.max(t0, rangeStartMs)) / (t1 - t0)
if (rxBps !== null) rxBytesDelta += (cur.rxBytes - prev.rxBytes) * overlap
if (txBps !== null) txBytesDelta += (cur.txBytes - prev.txBytes) * overlap
if (rxBps == null && txBps == null) continue
const rxMbps = bpsToMbps(rxBps ?? 0)
const txMbps = bpsToMbps(txBps ?? 0)
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict'
import fs from 'node:fs'
import vm from 'node:vm'
import ts from 'typescript'
import {buildTrafficFromSamples,mergeBuiltTraffic} from './traffic-rate.js'
import {buildTrafficFromSamples,mergeBuiltTraffic,trafficHistoryBoundaryLimitMs} from './traffic-rate.js'
import {applyTrafficFreshness,trafficBindingStatus} from './traffic-freshness.js'
const source=fs.readFileSync(new URL('./traffic-users.ts',import.meta.url),'utf8')
@@ -14,7 +14,7 @@ let calls=0,fail=false
const at=Date.parse('2026-10-13T00:00:00Z')
const users=Array.from({length:100},(_,i)=>({id:String(i),login:'user'+i,name:'User '+i,role:'user',active:true,
bindings:[{serverId:1,interfaceName:'ether1',peerPublicKey:'',userId:String(i),id:'binding'+i}]}))
const context=vm.createContext({Date,Map,Promise,buildTrafficFromSamples,mergeBuiltTraffic,applyTrafficFreshness,trafficBindingStatus,
const context=vm.createContext({Date,Map,Promise,buildTrafficFromSamples,mergeBuiltTraffic,trafficHistoryBoundaryLimitMs,applyTrafficFreshness,trafficBindingStatus,
listUsers:async()=>users,servers:{},db:{select:()=>({from:async()=>[{id:1,enabled:true,updatedAt:new Date(at).toISOString()}]})},
readTrafficCollectionContext:async()=>({settings:{enabled:true,intervalSec:30},states:new Map()}),
readServerSamplesInRange:async()=>{calls++;await new Promise(resolve=>setTimeout(resolve,5));if(fail)throw Error('fixture read failed');
+14 -3
View File
@@ -6,6 +6,7 @@ import { readServerSamplesInRange, type TrafficHistoryReadOptions } from "./traf
import {
buildTrafficFromSamples,
mergeBuiltTraffic,
trafficHistoryBoundaryLimitMs,
type BuiltTrafficSeries,
} from "./traffic-rate.js"
import { readTrafficCollectionContext } from "./traffic-collection-state.js"
@@ -82,6 +83,13 @@ export async function buildUserTrafficList(rangeStartMs: number, rangeEndMs: num
const sinceIso = new Date(rangeStartMs).toISOString()
const users = options.users ?? await listUsers()
const [context, allServers] = await Promise.all([readTrafficCollectionContext(options.serverIds), options.serverIds === undefined ? db.select().from(servers) : db.select().from(servers).where(inArray(servers.id, [...options.serverIds]))])
const boundaryLimitMs = trafficHistoryBoundaryLimitMs(context.settings?.intervalSec)
const interfacesByServer = new Map<number, Set<string>>()
for (const user of users) for (const binding of user.bindings) {
const names = interfacesByServer.get(binding.serverId) ?? new Set<string>()
names.add(binding.interfaceName)
interfacesByServer.set(binding.serverId, names)
}
const catalog = new Map(allServers.map(s => [s.id, s]))
const sampleCache = new Map<number, ReturnType<typeof readServerSamplesInRange>>()
@@ -93,18 +101,21 @@ export async function buildUserTrafficList(rangeStartMs: number, rangeEndMs: num
let pending = sampleCache.get(b.serverId)
if (!pending) {
if (options.serverIds !== undefined && !options.serverIds.includes(b.serverId)) throw new Error("Привязка вне выбранной области истории")
pending = readServerSamplesInRange(b.serverId, sinceIso, { ...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString() })
pending = readServerSamplesInRange(b.serverId, sinceIso, {
...options, untilIso: options.untilIso ?? new Date(rangeEndMs).toISOString(),
interfaceNames: [...interfacesByServer.get(b.serverId)!], precedingMaxIntervalMs: boundaryLimitMs,
})
sampleCache.set(b.serverId, pending)
}
const rows = await pending
const peerKey = b.peerPublicKey ?? ""
const built = buildTrafficFromSamples(rows, rangeStartMs, rangeEndMs, b.interfaceName, peerKey, true)
const built = buildTrafficFromSamples(rows, rangeStartMs, rangeEndMs, b.interfaceName, peerKey, true, boundaryLimitMs)
const server = catalog.get(b.serverId)
if (built.observations) built.observations = applyTrafficFreshness(built.observations,
server ?? { enabled: false, updatedAt: "" }, context.settings, context.states.get(b.serverId), rangeEndMs)
parts.push(built)
const last = [...rows.filter((r) =>
r.interfaceName === b.interfaceName && (r.peerPublicKey ?? "") === peerKey,
r.interfaceName === b.interfaceName && (r.peerPublicKey ?? "") === peerKey && Date.parse(r.sampledAt) >= rangeStartMs,
)]
.sort((a, c) => Date.parse(a.sampledAt) - Date.parse(c.sampledAt))
.at(-1)
@@ -3,12 +3,39 @@ import { EventEmitter } from "node:events"
import http from "node:http"
import { test, type TestContext } from "node:test"
import { isRouterOsPreHeaderGetReset, MikrotikClient } from "./mikrotik.js"
import { createWireguardDependencyReader } from "./wireguard-interface-delete.js"
import { createWireguardDependencyReader, readWireguardRouteDependencies } from "./wireguard-interface-delete.js"
import { assertGreDeleteDependencies } from "./gre-mutation-state.js"
import { wireguardReadDiagnostic } from "./wireguard-read-diagnostic.js"
type Outcome = "reset" | "late-reset" | "informational-reset" | "body-reset" | "ok" | "refused" | "invalid-json"
const reset = () => Object.assign(new Error("fixture reset"), { code: "ECONNRESET" })
test("large routing tables do not enter a new WireGuard snapshot and still protect existing interfaces", async () => {
const target = { ".id": "*a", name: "wg-new" }
const configured = { ".id": "*b", static: "true", gateway: "wg-new-backup" }
const learned = Array.from({ length: 12_608 }, (_, index) => ({
".id": `*r${index.toString(16)}`, dynamic: "true", "immediate-gw": "192.0.2.1%ether1",
}))
const requests: string[] = []
const read: ReturnType<typeof createWireguardDependencyReader> = async (path, query) => {
requests.push(path + query)
return query === "?static=true" ? [configured] : learned
}
const path = "/ip/route"
assert.deepEqual(await readWireguardRouteDependencies(read, path, false), [configured])
assert.deepEqual(requests, ["/ip/route?static=true"])
const assertSafe = (routes: Record<string, string>[]) => assertGreDeleteDependencies({ dependencies: { [path]: routes }, bindings: [], filters: [] }, target)
assertSafe(await readWireguardRouteDependencies(read, path, true))
assert.match(requests.at(-1)!, /^\/ip\/route\?dynamic=true&\.proplist=.*immediate-gw/)
configured.gateway = target.name
assert.throws(() => assertSafe([configured]), /используется/)
configured.gateway = "ether1"
learned[12_607]["immediate-gw"] = "192.0.2.1%wg-new"
const routes = await readWireguardRouteDependencies(read, path, true)
assert.throws(() => assertSafe(routes), /используется/)
await assert.rejects(readWireguardRouteDependencies(async () => learned, path, false), { code: "STATE_UNAVAILABLE" })
})
function fixture(t: TestContext, outcomes: Outcome[]) {
const paths: string[] = [], timeouts: number[] = []
t.mock.method(http, "request", (options: http.RequestOptions, receive: (res: unknown) => void) => {
@@ -41,22 +41,22 @@ async function source(server: ServerRow, authorize: () => Promise<unknown>) {
const fresh = await getServerRowById(server.id)
if (!fresh || !sameWireguardEndpoint(fresh, server)) throw fail("STATE_CONFLICT", "Источник WireGuard изменился")
}
function nativeRows(raw: unknown, required: string[] = []): Row[] {
if (!Array.isArray(raw) || raw.length > 2000 || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.values(row).some(value => typeof value !== "string") || typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"]) || required.some(key => !row[key]?.trim()))) throw fail("STATE_UNAVAILABLE", "Недостоверный каталог зависимостей WireGuard")
function nativeRows(raw: unknown, required: string[] = [], rowLimit = 2000): Row[] {
if (!Array.isArray(raw) || raw.length > rowLimit || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || Object.values(row).some(value => typeof value !== "string") || typeof row[".id"] !== "string" || !/^\*[a-z0-9]+$/i.test(row[".id"]) || required.some(key => !row[key]?.trim()))) throw fail("STATE_UNAVAILABLE", "Недостоверный каталог зависимостей WireGuard")
if (new Set(raw.map(row => row[".id"])).size !== raw.length) throw fail("STATE_UNAVAILABLE", "Повторяющиеся ID в каталоге WireGuard")
return raw as Row[]
}
/** Retry at most two distinct catalogue GETs per capture, with fresh source authority. */
export function createWireguardDependencyReader(client: Pick<MikrotikClient, "get">, authorizeSource: () => Promise<unknown>, now = () => performance.now()) {
const timeoutMs = 10_000, maxBytes = 1_048_576
const timeoutMs = 10_000
const unavailable = () => fail("STATE_UNAVAILABLE", "Не прочитан обязательный источник зависимостей WireGuard")
const retriedPaths = new Set<string>()
return async (path: string): Promise<unknown> => {
return async (path: string, query = "", maxBytes = 1_048_576): Promise<unknown> => {
await authorizeSource()
const deadline = now() + timeoutMs
try {
return await client.get(path, timeoutMs, undefined, maxBytes)
return await client.get(path + query, timeoutMs, undefined, maxBytes)
} catch (error) {
const retryableCatalogue = path === "/ip/address" || path === "/ipv6/address" || wireguardInterfaceDependencyPaths.includes(path)
if (retriedPaths.size >= 2 || retriedPaths.has(path) || !retryableCatalogue
@@ -67,14 +67,40 @@ export function createWireguardDependencyReader(client: Pick<MikrotikClient, "ge
const remaining = Math.floor(deadline - now())
if (remaining < 1) throw markWireguardReadFailure(unavailable(), path, 2, "read-deadline")
try {
return await client.get(path, remaining, undefined, maxBytes)
return await client.get(path + query, remaining, undefined, maxBytes)
} catch (error) {
throw markWireguardGetFailure(unavailable(), path, 2, error)
}
}
}
export async function readWireguardInterfaceDeleteState(server: ServerRow, id: string, authorize: () => Promise<unknown> = async () => {}): Promise<WireguardInterfaceDeleteState> {
const routePaths = new Set(["/ip/route", "/ipv6/route"])
const routeRowLimit = 50_000
const routeByteLimit = 8 * 1024 * 1024
export async function readWireguardRouteDependencies(read: ReturnType<typeof createWireguardDependencyReader>, path: string, existingInterface: boolean) {
const catalogue = async (query: string, dynamic: boolean) => {
const raw = await read(path, query, routeByteLimit)
if (Array.isArray(raw) && raw.length > routeRowLimit) throw markWireguardReadFailure(fail("STATE_UNAVAILABLE", "Превышен предел отфильтрованного каталога маршрутов WireGuard"), path, undefined, "catalogue-limit")
try {
const rows = nativeRows(raw, [], routeRowLimit)
if (rows.some(row => dynamic ? observedBoolean(row.dynamic) !== true : observedBoolean(row.dynamic) === true || observedBoolean(row.static) === false)) throw fail("STATE_UNAVAILABLE", "RouterOS вернул маршруты вне запрошенного фильтра")
return rows
} catch (error) {
if (!(error instanceof McpError)) throw error
throw markWireguardReadFailure(error, path, undefined, "invalid-catalogue")
}
}
const configured = await catalogue("?static=true", false)
if (!existingInterface) return configured
// Preserve checks on learned next hops before editing or removing an existing interface.
const learned = await catalogue("?dynamic=true&.proplist=.id,dynamic,connect,belongs-to,gateway,immediate-gw,vrf-interface,interface,local-address,routing-table", true)
const configuredIds = new Set(configured.map(row => row[".id"]))
if (learned.some(row => configuredIds.has(row[".id"]))) throw markWireguardReadFailure(fail("STATE_UNAVAILABLE", "Маршрут изменился во время сверки зависимостей WireGuard"), path, undefined, "invalid-catalogue")
return [...configured, ...learned]
}
export async function readWireguardInterfaceDeleteState(server: ServerRow, id: string, authorize: () => Promise<unknown> = async () => {}, includeLearnedRoutes?: boolean): Promise<WireguardInterfaceDeleteState> {
const base = await readWireguardPeerDeletionState(server, authorize), client = MikrotikClient.fromServer(server)
const readDependency = createWireguardDependencyReader(client, () => source(server, authorize))
let totalRows = base.interfaces.length + base.peers.length, bytes = Buffer.byteLength(JSON.stringify([base.interfaces, base.peers]))
@@ -93,8 +119,10 @@ export async function readWireguardInterfaceDeleteState(server: ServerRow, id: s
}
const ipv4 = await read("/ip/address", ["address", "interface"]), ipv6 = await read("/ipv6/address", ["address", "interface"])
const dependencies: Record<string, Row[]> = {}
for (const path of wireguardInterfaceDependencyPaths) dependencies[path] = await read(path, path === "/interface" ? ["name"] : [])
const parent = base.interfaces.find(row => row[".id"] === id)
for (const path of wireguardInterfaceDependencyPaths) dependencies[path] = routePaths.has(path)
? await readWireguardRouteDependencies(readDependency, path, includeLearnedRoutes ?? (parent !== undefined))
: await read(path, path === "/interface" ? ["name"] : [])
if (parent && [...ipv4, ...ipv6].filter(row => [parent.name, id].includes(row.interface) && !automaticLinkLocal(row)).length + base.peers.filter(row => [parent.name, id].includes(row.interface)).length > 49) throw fail("STATE_UNAVAILABLE", "Каскад WireGuard превышает предел зависимых объектов")
if (parent) for (const [path, rows] of [["/ip/address", ipv4], ["/ipv6/address", ipv6]] as const) for (const row of rows.filter(row => [parent.name, id].includes(row.interface))) {
try {
@@ -69,11 +69,11 @@ export const wireguardRestoreStateProof=(state:WireguardRestoreState)=>wireguard
export async function readWireguardRestoreState(server:ServerRow,beforeRead:()=>Promise<unknown>=async()=>{}) {
const deadline=Date.now()+45000
const authorize=async()=>{if(Date.now()>=deadline)throw fail("Истёк бюджет чтения WireGuard restore","STATE_UNAVAILABLE");await beforeRead();if(Date.now()>=deadline)throw fail("Истёк бюджет чтения WireGuard restore","STATE_UNAVAILABLE")}
const state=await readWireguardInterfaceDeleteState(server,"*0",authorize)
const state=await readWireguardInterfaceDeleteState(server,"*0",authorize,true)
if(new Set(state.base.interfaces.map(row=>row.name)).size!==state.base.interfaces.length||state.base.interfaces.some(row=>!row.name)||state.base.peers.some(peer=>state.base.interfaces.filter(row=>row.name===peer.interface||row[".id"]===peer.interface).length!==1))throw fail("Неоднозначные родители WireGuard")
// Selected-parent reads corroborate dynamic origins; the *0 inventory alone does not.
for(const parent of state.base.interfaces){
const selected=await readWireguardInterfaceDeleteState(server,parent[".id"],authorize),comparison=structuredClone(selected)
const selected=await readWireguardInterfaceDeleteState(server,parent[".id"],authorize,false),comparison=structuredClone(selected)
for(const resource of ["ipv4","ipv6"]as const)for(const row of comparison[resource]){const previous=state[resource].find(a=>a[".id"]===row[".id"]);if(previous?.dynamic===undefined)delete row.dynamic;else row.dynamic??=previous.dynamic}
if(await wireguardRestoreStateProof(comparison)!==await wireguardRestoreStateProof(state))throw fail("WireGuard изменился во время инвентаризации")
for(const resource of ["ipv4","ipv6"]as const)for(const row of state[resource]){const observed=selected[resource].find(a=>a[".id"]===row[".id"]&&a.interface===row.interface);if(observed?.dynamic!==undefined)row.dynamic=observed.dynamic}
+7
View File
@@ -38,6 +38,10 @@ export async function verifyMcpTrafficHistory(actor: AuthUser) {
assert.equal((await readTrafficCollectionContext([])).states.size, 0)
await assert.rejects(readServerSamplesInRange(52, previous.toISOString(), { maxRows: 1 }), /предел/)
await assert.rejects(readServerSamplesInRange(52, previous.toISOString(), { maxRows: 0 }), /предел/)
const clipped = await readServerSamplesInRange(52, new Date(previous.getTime() + 15_000).toISOString(), {
untilIso: sourceAt.toISOString(), maxRows: 2, interfaceNames: ["fixture-traffic-zero"], interfaceCountersOnly: true, precedingMaxIntervalMs: 90_000,
})
assert.deepEqual(clipped.map(row => new Date(row.sampledAt).toISOString()), [previous.toISOString(), sourceAt.toISOString()])
const access = await makeAccess([52, 54]), principal = await authenticateMcpToken(access.token)
const result = await invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 }) as History
assert.equal(result.rxNowMbps, 0); assert.equal(result.txNowMbps, 0); assert.equal(result.accountedRxGiB, 0); assert.equal(result.currentState, "current"); assert.equal(result.live, false)
@@ -89,6 +93,9 @@ export async function verifyMcpTrafficHistory(actor: AuthUser) {
finally { pool.query = originalQuery; await pool.query("UPDATE servers SET host='192.0.2.52' WHERE id=52") }
await pool.query("INSERT INTO traffic_samples(server_id,interface_name,sampled_at,flags) SELECT 52,'limit-history-'||n,$1,0 FROM generate_series(1,20001) n", [sourceAt.toISOString()])
await assert.rejects(invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52 }), /20000/)
const filtered = await invokeMcpTool(principal, "mm_traffic_server_history_get", { serverId: 52, interface: "fixture-traffic-zero", range: "24h" }) as History
assert.equal(filtered.accountedRxGiB, 0)
assert(filtered.rxSeriesMbps.some(value => value === 0))
await pool.query("DELETE FROM traffic_samples WHERE server_id=52 AND interface_name LIKE 'limit-history-%'")
let ownerRevokedDuringRead = false
pool.query = ((...args: unknown[]) => {
+11 -4
View File
@@ -430,6 +430,7 @@ export function FlowAnalyticsDetail({
{range === "30d" && <p className="text-sm text-muted-foreground mb-3">Календарный месяц по UTC. Фильтры интерфейса, дублей, overlay и mesh к этому отчёту не применяются.</p>}
{monthly && range === "30d" && <section aria-label="Месячный отчёт" className="flex flex-col gap-4">
<p className="text-sm">Период: {monthly.month} (UTC) · Объём: {formatBytes(monthly.bytes)}</p>
{monthly.totalsSource === "legacy-dimensions" && <p className="text-sm text-muted-foreground">Для части месяца объём оценён по сохранённым дневным сводкам.</p>}
<div><h3 className="text-sm font-medium mb-2">Страны</h3><FlowBreakdownGrid rows={monthly.countries} country monthly /></div>
<div><h3 className="text-sm font-medium mb-2">Сервисы</h3><FlowBreakdownGrid rows={monthly.services} monthly /></div>
<div><h3 className="text-sm font-medium mb-2">ASN</h3><FlowBreakdownGrid rows={monthly.asns} monthly /></div>
@@ -475,7 +476,13 @@ export function FlowAnalyticsDetail({
{analytics && <>
{analytics.sourceWindow && <p role="status" className="mb-3 text-sm text-muted-foreground">Данные за последние {analytics.sourceWindow.effectiveMinutes} мин из запрошенных {analytics.sourceWindow.requestedMinutes} мин.</p>}
<p className="mb-3 text-sm text-muted-foreground">График RX/TX показывает потоки с известным направлением. Общий объём включает также транзит с неопределённым направлением.</p>
<p className="mb-3 text-sm text-muted-foreground">
RX/TX рассчитаны по сохранённой выборке потоков с известным направлением.
{analytics.timeBasis === "collector-receipt" && " Время на графике соответствует приёму IPFIX коллектором."}
</p>
{analytics.admittedTotals && <p className="mb-3 text-sm text-muted-foreground">
Коллектор принял {formatBytes(analytics.admittedTotals.bytes)} за завершённые минуты окна. Минут с данными — {analytics.admittedTotals.minutesWithData}. Фильтры overlay, mesh и дублей к этому счётчику не применяются.
</p>}
<TrafficRxTxChart
rx={analytics?.rxSeries ?? card?.rxSeries ?? []}
tx={analytics?.txSeries ?? card?.txSeries ?? []}
@@ -489,7 +496,7 @@ export function FlowAnalyticsDetail({
items={([
{
id: "bps-now",
label: "Скорость сейчас",
label: "Скорость IPFIX",
value: fmtRate(rxNow),
hint: liveHint,
icon: <ArrowDownIcon className="size-4" />,
@@ -497,9 +504,9 @@ export function FlowAnalyticsDetail({
},
{
id: "bytes",
label: "Payload",
label: "Объём выборки",
value: formatBytes(bytes),
hint: "inner IPFIX",
hint: "Сохранённые потоки",
icon: <ArrowUpIcon className="size-4" />,
iconClassName: "text-info",
},
@@ -200,6 +200,7 @@ function NetflowSettingsContent({
? new Date(settings.lastDatagramAt).toLocaleString("ru-RU")
: settings ? "Не получен" : "—"}
{settings?.lastExporterIp ? ` · ${settings.lastExporterIp}` : ""}
{settings?.collectorCountersSource === "last-known" ? " · последние известные данные" : ""}
{settings?.lastError ? ` · ${settings.lastError}` : ""}
</p>
+6 -1
View File
@@ -1,5 +1,5 @@
import assert from "node:assert/strict"
import { projectTrafficObservations } from "./traffic-observations.js"
import { peakOfCombinedTraffic, projectTrafficObservations } from "./traffic-observations.js"
const at=Date.parse("2026-10-12T12:00:00Z"),sampledAt=new Date(at).toISOString(),expiresAt=new Date(at+90_000).toISOString()
const row={rxNow:0,txNow:0,rxPeak:0,txPeak:0,rxTotal:0,txTotal:0,rxSeries:[0,0],txSeries:[0,0]}
const obs={rxNow:true,txNow:true,rxSeries:[false,true],txSeries:[false,true],sampledAt,currentState:"current",expiresAt}
@@ -16,3 +16,8 @@ const noPeak=projectTrafficObservations({...row,volumeUnit:"GiB",observations:{.
assert.equal(noPeak.rxPeak,null);assert.equal(noPeak.txPeak,null);assert.equal(noPeak.rxTotal,0)
assert.equal(projectTrafficObservations({...row,observations:{...obs,rxPeak:true,txPeak:true}},at).rxPeak,0)
console.log('aggregate peak confidence overrides bucket confidence; measured zero and volumes retained')
assert.equal(peakOfCombinedTraffic([[10, 3, 20], [4, 15, null]]), 18)
assert.equal(peakOfCombinedTraffic([[0, null], [0, 50]]), 0)
assert.equal(peakOfCombinedTraffic([[null, 10], [4, null]]), null)
assert.equal(peakOfCombinedTraffic([[2], [4, 5]]), null)
assert.equal(peakOfCombinedTraffic([]), null)
+14
View File
@@ -6,6 +6,20 @@ const meta = z.object({ rxNow: z.boolean(), txNow: z.boolean(), rxSeries: z.arra
sampledAt: timestamp, expiresAt: timestamp.optional(), lastAttemptAt: timestamp.optional(), lastSucceededAt: timestamp.optional(), error: z.string().nullable().optional(),
currentState: z.enum(["current", "unknown", "pending", "error", "stale", "disabled"]).optional() })
export type TrafficObservationMeta = z.infer<typeof meta>
/** Sum only intervals with an observation from every series. */
export function peakOfCombinedTraffic(series: readonly (readonly (number | null)[])[]): number | null {
const length = series[0]?.length
if (!length || series.some(values => values.length !== length)) return null
let peak: number | null = null
for (let index = 0; index < length; index++) {
const slots = series.map(values => values[index])
if (slots.some(value => value == null)) continue
const sum = slots.reduce<number>((total, value) => total + (value ?? 0), 0)
peak = peak === null ? sum : Math.max(peak, sum)
}
return peak
}
const metrics = z.object({ volumeUnit: z.literal("GiB").optional(), rxNow: rate, txNow: rate, rxPeak: rate, txPeak: rate, rxTotal: rate, txTotal: rate,
rxSeries: z.array(rate), txSeries: z.array(rate), observations: meta.optional() }).refine(r =>
(!r.observations || r.observations.rxSeries.length === r.rxSeries.length && r.observations.txSeries.length === r.txSeries.length &&
+11
View File
@@ -59,6 +59,7 @@ export const flowIngestQualitySchema = z.object({
export const trafficFlowSettingsDtoSchema = z.object({
dataQuality:flowIngestQualitySchema.nullable().optional(),
collectorCountersSource: z.enum(["live", "last-known"]).optional(),
enabled: z.boolean(),
collectorIp: z.string().min(1),
flowListenPort: z.number().int().positive(),
@@ -171,6 +172,7 @@ const flowSourceWindowSchema = z.object({
export const flowStatsDtoSchema = z.object({
sourceWindow: flowSourceWindowSchema.optional(),
dataQuality:flowIngestQualitySchema.nullable().optional(),
collectorCountersSource: z.enum(["live", "last-known"]).optional(),
exportersOnline: z.number().int().nonnegative(),
bytesPerMin: z.number().nonnegative(),
uniqueSrc: z.number().int().nonnegative(),
@@ -253,6 +255,14 @@ export const flowPathRowSchema = z.object({
export const flowAnalyticsDtoSchema = z.object({
sourceWindow: flowSourceWindowSchema.optional(),
timeBasis: z.literal("collector-receipt").optional(),
admittedTotals: z.object({
bytes: z.number().int().nonnegative(),
packets: z.number().int().nonnegative(),
from: z.string().datetime(),
to: z.string().datetime(),
minutesWithData: z.number().int().positive(),
}).optional(),
bpsNow: z.number().nonnegative(),
bytes: z.number().nonnegative(),
packets: z.number().nonnegative(),
@@ -309,6 +319,7 @@ export const flowClientsDtoSchema = z.object({
export const flowMonthlyDtoSchema = z.object({
month: z.string(),
bytes: z.number().nonnegative(),
totalsSource: z.enum(["admitted-daily", "legacy-dimensions"]).optional(),
countries: z.array(flowBreakdownRowSchema),
services: z.array(flowBreakdownRowSchema),
asns: z.array(flowBreakdownRowSchema),
+10 -2
View File
@@ -15,9 +15,10 @@ for (const mode of ['servers','flows'] as const) for (const minutes of [5,15]) {
const card={id:'1',name,subtitle:'',site:'TEST',country:'RU',status:'online',rxNow:1,txNow:0,sessions:1,bytes:60000,rxSeries:[1,2,1],txSeries:[0,0,0]}
const sourceWindow=mode==='flows'&&span===15?{requestedMinutes:15,effectiveMinutes:7}:undefined
const chartMinutes=sourceWindow?.effectiveMinutes??span
const analytics={bpsNow:1000000,bytes:60000,packets:10,conversations:1,uniqueSrc:1,uniqueDst:1,topProto:'TCP',rxSeries:Array.from({length:chartMinutes},(_,i)=>1+i%3),txSeries:Array(chartMinutes).fill(0),applications:[],protocols:[],sources:[],destinations:[],interfaces:[],conversationsList:[],ifaces:[],live:false,sourceWindow}
const analytics={bpsNow:1000000,bytes:60000,packets:10,conversations:1,uniqueSrc:1,uniqueDst:1,topProto:'TCP',rxSeries:Array.from({length:chartMinutes},(_,i)=>1+i%3),txSeries:Array(chartMinutes).fill(0),applications:[],protocols:[],sources:[],destinations:[],interfaces:[],conversationsList:[],ifaces:[],live:false,sourceWindow,
timeBasis:'collector-receipt',admittedTotals:{bytes:120000,packets:20,from:new Date(end-chartMinutes*60000).toISOString(),to:new Date(end).toISOString(),minutesWithData:chartMinutes}}
if(path.endsWith('/live'))return r.fulfill({status:503,json:{error:'Live выключен в проверке истории'}})
const json=path==='/api/auth/config'?{required:false}:path==='/api/traffic/servers'?{servers:[server]}:
const json=path==='/api/auth/config'?{required:false}:path==='/api/traffic/servers'?{servers:[server],peaks:{rx:2,tx:1}}:
path==='/api/traffic/servers/1'?{server}:path==='/api/traffic/flow/exporters'?{exporters:[card]}:
path==='/api/traffic/flow/analytics'?analytics:path==='/api/traffic/flow/clients'?{clients:[]}:
path==='/api/traffic/flows'?{exportersOnline:1,bytesPerMin:1,uniqueSrc:1,uniqueDst:1,topProto:'TCP',talkers:[],listenerBound:true}:
@@ -30,6 +31,13 @@ for (const mode of ['servers','flows'] as const) for (const minutes of [5,15]) {
await page.getByRole('button',{name:`${minutes}м`,exact:true}).click()
const chart=page.getByRole('region',{name:'История RX/TX'})
await expect(chart).toBeVisible()
if(mode==='flows') {
await expect(page.getByText(/Коллектор принял 120.0 КБ/)).toBeVisible()
await expect(page.getByText('Объём выборки', {exact:true})).toBeVisible()
} else {
await expect(page.getByText('Пик суммы RX', {exact:true})).toBeVisible()
await expect(page.getByText('По общим замерам', {exact:true}).filter({visible:true})).toHaveCount(2)
}
if(mode==='flows'&&minutes===15)await expect(page.getByText('Данные за последние 7 мин из запрошенных 15 мин.')).toBeVisible()
const curves=chart.locator('path.recharts-line-curve')
await expect(curves).toHaveCount(2)