Compare commits

..
4 Commits
Author SHA1 Message Date
Denozordec 80cd3a18d7 fix(network): исправить исходный адрес пинга и обновление фильтров
Docker images / prepare-release (push) Successful in 13s
Docker images / backend-test (push) Successful in 10m58s
Docker images / frontend-image (push) Successful in 3m56s
Docker images / updater-image (push) Successful in 2m37s
Docker images / netflow-agent-image (push) Successful in 39s
Docker images / backend-image (push) Successful in 3m56s
Docker images / publish-release (push) Successful in 12s
Docker images / notify-webhook (push) Successful in 2s
Оставить выбор исходного адреса пинга маршрутизатору. Читать правила и рекурсивные шлюзы последовательно, сохранить кэш при конфликте блокировки.
2026-11-22 20:47:38 +07:00
Denozordec f5914b634a fix(mcp): учесть модуль политики в тестах маршрутизации
Docker images / prepare-release (push) Successful in 12s
Docker images / backend-test (push) Successful in 10m46s
Docker images / backend-image (push) Successful in 3m6s
Docker images / frontend-image (push) Successful in 3m23s
Docker images / updater-image (push) Successful in 1m45s
Docker images / netflow-agent-image (push) Successful in 36s
Docker images / notify-webhook (push) Successful in 2s
Docker images / publish-release (push) Successful in 10s
2026-11-22 19:48:17 +07:00
Denozordec aac425c7b2 feat(mcp): расширить управление сетью и исправить разрезы трафика
Docker images / prepare-release (push) Successful in 13s
Docker images / backend-test (push) Failing after 10m39s
Docker images / backend-image (push) Skipped
Docker images / frontend-image (push) Successful in 3m11s
Docker images / updater-image (push) Successful in 1m56s
Docker images / netflow-agent-image (push) Successful in 37s
Docker images / publish-release (push) Skipped
Docker images / notify-webhook (push) Skipped
Добавить переименование интерфейсов и возможность отключать лимиты частоты для сервисных аккаунтов.

Уточнить дедупликацию, учёт интерфейсов и геоданных, фильтрацию сессий и карту потоков.
2026-11-22 19:24:49 +07:00
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
50 changed files with 934 additions and 203 deletions

No files matched your search

+76 -31
View File
@@ -17,7 +17,7 @@ import { Separator } from "@/components/ui/separator"
import { cn } from "@/lib/utils"
import { filtersResponseSchema, filtersMutationSchema, filtersGreResponseSchema, filtersRecursiveResponseSchema } from "@/lib/filters-response"
import { firewallServersSchema } from "@/lib/firewall-response"
import { requestJson } from "@/shared/api/http-client"
import { ApiClientError, requestJson } from "@/shared/api/http-client"
import {
servers, greTunnels, serverFilterRulesets, filters as mockFiltersCatalog,
type FilterRule, type ServerFilterRuleset, type Server, type GreTunnel,
@@ -874,7 +874,7 @@ function CopyRulesSheet({
greByServer: Record<string, GreTunnel[]>
ensureGreFor?: (serverId: string) => void
recRoutesByServer: Record<string, RecursiveRouteLite[]>
ensureRecursiveFor?: (serverId: string) => void
ensureRecursiveFor?: (serverId: string) => Promise<void>
catalogReady?: GatewayReadiness
}) {
const [selected, setSelected] = useState<Set<string>>(() => new Set(sourceRules.map(r => r.id)))
@@ -1229,7 +1229,11 @@ function CopyRulesSheet({
</fieldset>
{targetId && !mappingReady && <div className="px-5 py-3 flex flex-col gap-2">
<p role="alert" className="text-sm text-destructive">Каталоги или сопоставление шлюзов не подтверждены. Копирование маршрутов заблокировано.</p>
<Button variant="outline" disabled={copying} onClick={() => { for (const id of [sourceServerId, targetId]) { ensureGreFor?.(id); ensureRecursiveFor?.(id) } }}>Обновить шлюзы</Button>
<Button variant="outline" disabled={copying} onClick={() => {
ensureGreFor?.(sourceServerId)
ensureGreFor?.(targetId)
void ensureRecursiveFor?.(sourceServerId).then(() => ensureRecursiveFor(targetId))
}}>Обновить шлюзы</Button>
</div>}
{copyError && <p role="alert" className="px-5 py-3 text-sm text-destructive">{copyError}</p>}
{copying && <p role="status" className="px-5 py-2 text-sm text-muted-foreground">Ожидание подтверждения копирования…</p>}
@@ -1268,6 +1272,28 @@ function makeApiFetch(backendUrl: string) {
}
}
const configurationBusyMessage = "Конфигурация временно занята другой операцией"
function isConfigurationBusy(error: unknown): boolean {
if (!(error instanceof ApiClientError) || error.status !== 409) return false
const payload = error.payload
return (typeof payload === "object" && payload !== null && "code" in payload && payload.code === "CONFIGURATION_BUSY")
|| /^(?:Глобальная конфигурация|Конфигурация сервера) изменяется другой операцией$/.test(error.message)
}
async function readFiltersWithRetry<T>(read: () => Promise<T>, isCurrent: () => boolean): Promise<T> {
for (const delay of [350, 1000]) {
try {
return await read()
} catch (error) {
if (!isConfigurationBusy(error) || !isCurrent()) throw error
await new Promise(resolve => setTimeout(resolve, delay))
if (!isCurrent()) throw error
}
}
return read()
}
function FiltersPage() {
const { mode, backendUrl } = useDataSource()
const mounted = useRef(true)
@@ -1336,7 +1362,10 @@ function FiltersPage() {
setGatewayReady(prev => ({ ...prev, [serverId]: { ...prev[serverId], [kind]: false } }))
try {
const path = kind === "gre" ? "/api/filters/gre-tunnels" : "/api/recursive-routes"
const payload = await apiFetch<unknown>(`${path}?serverId=${encodeURIComponent(serverId)}`)
const payload = await readFiltersWithRetry(
() => apiFetch<unknown>(`${path}?serverId=${encodeURIComponent(serverId)}`),
() => mounted.current && gatewayEpoch.current[key] === epoch,
)
if (!mounted.current || gatewayEpoch.current[key] !== epoch) return
if (kind === "gre") {
const parsed = filtersGreResponseSchema.safeParse(payload)
@@ -1352,7 +1381,9 @@ function FiltersPage() {
setGatewayErrors(prev => ({ ...prev, [serverId]: { ...prev[serverId], [kind]: undefined } }))
} catch (error) {
if (!mounted.current || gatewayEpoch.current[key] !== epoch) return
setGatewayErrors(prev => ({ ...prev, [serverId]: { ...prev[serverId], [kind]: error instanceof Error ? error.message : "Каталог шлюзов недоступен" } }))
const message = isConfigurationBusy(error) ? configurationBusyMessage
: error instanceof Error ? error.message : "Каталог шлюзов недоступен"
setGatewayErrors(prev => ({ ...prev, [serverId]: { ...prev[serverId], [kind]: message } }))
}
}, [isLive, apiFetch])
const ensureGreTunnels = useCallback((serverId: string) => loadGatewayCatalog(serverId, "gre"), [loadGatewayCatalog])
@@ -1374,7 +1405,8 @@ function FiltersPage() {
const [movePending, setMovePending] = useState(false)
const [moveError, setMoveError] = useState<string | null>(null)
const moveLock = useRef(false)
const [liveStale, setLiveStale] = useState(false)
const [rulesReadFailure, setRulesReadFailure] = useState<"busy" | "unavailable" | null>(null)
const liveStale = rulesReadFailure !== null
const readEpoch = useRef(0)
const [readyServerId, setReadyServerId] = useState<string | null>(null)
const [rulesReading, setRulesReading] = useState(false)
@@ -1394,7 +1426,7 @@ function FiltersPage() {
setRulesets(buildRulesets(servers, serverFilterRulesets))
setSelectedServerId(servers[0]?.id ?? "")
setLiveLoadState("idle")
setLiveStale(false)
setRulesReadFailure(null)
})
return
}
@@ -1454,8 +1486,9 @@ function FiltersPage() {
setReadyServerId(null)
setRulesReading(true)
try {
const payload = await apiFetch<unknown>(
`/api/filters/rules?serverId=${encodeURIComponent(serverId)}`,
const payload = await readFiltersWithRetry(
() => apiFetch<unknown>(`/api/filters/rules?serverId=${encodeURIComponent(serverId)}`),
() => mounted.current && epoch === readEpoch.current,
)
if (epoch !== readEpoch.current) return false
const parsed = filtersResponseSchema.safeParse(payload)
@@ -1469,35 +1502,47 @@ function FiltersPage() {
if (!has) return [...prev, { serverId, rules: liveRules }]
return prev.map((rs) => rs.serverId === serverId ? { ...rs, rules: liveRules } : rs)
})
setLiveStale(false)
setRulesReadFailure(null)
setReadyServerId(serverId)
setRulesReceipt(prev => ({ ...prev, [serverId]: { at: new Date().toISOString(), kind: "read" } }))
return true
} catch (err) {
if (epoch !== readEpoch.current) return false
setLiveStale(true)
toast.error("Не удалось прочитать правила с роутера", { description: String(err) })
const busy = isConfigurationBusy(err)
setRulesReadFailure(busy ? "busy" : "unavailable")
if (!busy) toast.error("Не удалось прочитать правила с роутера", {
description: err instanceof Error ? err.message : "Ошибка чтения правил",
})
return false
} finally {
if (epoch === readEpoch.current) setRulesReading(false)
}
}, [isLive, apiFetch])
const loadServerFilters = useCallback(async (serverId: string) => {
const gre = ensureGreTunnels(serverId)
const rules = loadLiveRules(serverId)
const epoch = readEpoch.current
await rules
// Both these GETs record observations under the same exclusive application lock.
if (mounted.current && epoch === readEpoch.current) await ensureRecursiveRoutes(serverId)
await gre
}, [loadLiveRules, ensureGreTunnels, ensureRecursiveRoutes])
useEffect(() => {
if (!isLive || !selectedServerId || liveLoadState !== "idle") return
if (!liveServers.some((s) => s.id === selectedServerId)) return
void Promise.all([
loadLiveRules(selectedServerId),
ensureGreTunnels(selectedServerId),
ensureRecursiveRoutes(selectedServerId),
])
}, [isLive, selectedServerId, liveLoadState, liveServers, ensureGreTunnels, ensureRecursiveRoutes, loadLiveRules])
void loadServerFilters(selectedServerId)
}, [isLive, selectedServerId, liveLoadState, liveServers, loadServerFilters])
const allServers = !isLive ? servers : liveServers
const allTunnels = !isLive ? greTunnels : (greByServer[selectedServerId] ?? [])
const selectedServer = allServers.find(s => s.id === selectedServerId) ?? allServers[0]
const totalRules = rulesets.reduce((s, r) => s + r.rules.length, 0)
const mutationsLocked = (isLive && !can("mm:filters:write")) || movePending || (isLive && (applyBusy || historyRestoring || rulesReading || readyServerId !== selectedServerId || liveStale || liveLoadState !== "idle"))
const gatewayMessages = [...new Set(Object.values(gatewayErrors[selectedServerId] ?? {}))]
.filter((message): message is string => Boolean(message) && !(rulesReadFailure === "busy" && message === configurationBusyMessage))
const gatewaysBusy = gatewayMessages.every(message => message === configurationBusyMessage)
const filterRailItems = useMemo<ServerTileItem[]>(() => (
allServers.map((s) => ({
@@ -1518,7 +1563,7 @@ function FiltersPage() {
readEpoch.current += 1
setReadyServerId(null)
setRulesReading(false)
setLiveStale(false)
setRulesReadFailure(null)
setSheetOpen(false)
setPendingDelete(null)
setDeleteError(null)
@@ -1552,7 +1597,9 @@ function FiltersPage() {
if (!mounted.current) return { ok: false, error: "Источник данных изменён" }
if (applyLock.current || applyBusy || historyRestoring) return { ok: false, error: "Дождитесь завершения текущей операции" }
if (isLive && (liveStale || liveLoadState !== "idle" || (serverId === selectedServerId && readyServerId !== serverId))) {
const error = "Роутер недоступен — изменения заблокированы"
const error = rulesReadFailure === "busy"
? configurationBusyMessage + ". Изменения недоступны до обновления правил."
: "Роутер недоступен — изменения заблокированы"
toast.error(error)
return { ok: false, error }
}
@@ -1589,23 +1636,19 @@ function FiltersPage() {
applyLock.current = false
setApplyBusy(false)
}
}, [isLive, apiFetch, applyBusy, historyRestoring, liveStale, liveLoadState, readyServerId, selectedServerId])
}, [isLive, apiFetch, applyBusy, historyRestoring, liveStale, rulesReadFailure, liveLoadState, readyServerId, selectedServerId])
const refreshFromRouter = useCallback(async () => {
if (!isLive || !selectedServerId || !mounted.current || applyLock.current || applyBusy || historyRestoring) return
applyLock.current = true
setApplyBusy(true)
try {
await loadLiveRules(selectedServerId)
await Promise.all([
ensureGreTunnels(selectedServerId),
ensureRecursiveRoutes(selectedServerId),
])
await loadServerFilters(selectedServerId)
} finally {
applyLock.current = false
setApplyBusy(false)
}
}, [isLive, selectedServerId, applyBusy, historyRestoring, loadLiveRules, ensureGreTunnels, ensureRecursiveRoutes])
}, [isLive, selectedServerId, applyBusy, historyRestoring, loadServerFilters])
useEffect(() => () => { historyEpoch.current += 1; readEpoch.current += 1 }, [selectedServerId, backendUrl, isLive])
@@ -1653,7 +1696,7 @@ function FiltersPage() {
if (!await loadLiveRules(selectedServerId)) throw new Error("Версия применена, но актуальные правила не прочитаны — обновите данные")
}
if (!mounted.current) return
setLiveStale(false)
setRulesReadFailure(null)
setReadyServerId(selectedServerId)
await loadRevisions()
if (!mounted.current) return
@@ -1879,15 +1922,17 @@ function FiltersPage() {
<Button variant="outline" size="sm" onClick={() => setLoadAttempt(n => n + 1)}>Повторить загрузку</Button>
</div>
)}
{isLive && Object.values(gatewayErrors[selectedServerId] ?? {}).some(Boolean) && <div className="border-b px-6 py-3 flex items-center gap-3">
<p role="alert" className="text-xs text-destructive">{Object.values(gatewayErrors[selectedServerId] ?? {}).filter(Boolean).join(" · ")}. Сохранены последние каталоги шлюзов.</p>
{isLive && gatewayMessages.length > 0 && <div className="border-b px-6 py-3 flex items-center gap-3">
<p role={gatewaysBusy ? "status" : "alert"} className={cn("text-xs", gatewaysBusy ? "text-muted-foreground" : "text-destructive")}>{gatewayMessages.join(" · ")}. Сохранены последние каталоги шлюзов.</p>
<Button variant="outline" size="sm" disabled={applyBusy} onClick={() => { void ensureGreTunnels(selectedServerId); void ensureRecursiveRoutes(selectedServerId) }}>Обновить шлюзы</Button>
</div>}
{isLive && rulesReading && <p role="status" className="border-b px-6 py-2 text-xs text-muted-foreground">Чтение актуальных правил устройства… Изменения временно заблокированы.</p>}
{isLive && liveStale && liveLoadState !== "error" && (
<div className="shrink-0 border-b border-amber-500/30 bg-amber-500/10 px-6 py-2.5 text-xs text-amber-700 dark:text-amber-400 flex items-center gap-2">
<AlertTriangleIcon className="size-3.5 shrink-0" />
Роутер недоступен — показан кэш. Изменения заблокированы, пока не удастся прочитать CHR.
{rulesReadFailure === "busy"
? configurationBusyMessage + ". Показаны последние правила. Изменения недоступны до обновления правил."
: "Роутер недоступен — показан кэш. Изменения заблокированы, пока не удастся прочитать CHR."}
</div>
)}
<div className="border-b px-4 py-3 flex items-center gap-3 flex-wrap shrink-0 md:px-6">
@@ -12,6 +12,7 @@ export const ACCEPTANCE_SHARDS: Record<string, (actor: AuthUser) => Promise<unkn
acceptRouteCommunityContextMcp: async actor => (await import("./routing-community-context-acceptance.js")).acceptRouteCommunityContextMcp(actor),
acceptWireguardPeerCreation: async actor => (await import("./wireguard-peer-create-acceptance.js")).acceptWireguardPeerCreation(actor),
acceptWireguardInterfaceUpdate: async actor => (await import("./wireguard-interface-update-acceptance.js")).acceptWireguardInterfaceUpdate(actor),
acceptInterfaceRename: async actor => (await import("./interface-rename-acceptance.js")).acceptInterfaceRename(actor),
acceptWireguardImport: async actor => (await import("./wireguard-import-acceptance.js")).acceptWireguardImport(actor),
verifyMcpFlowOverlay: async actor => (await import("../../test/mcp-flow-overlay.js")).verifyMcpFlowOverlay(actor),
acceptWireguardInterfaceDeletion: async actor => (await import("./wireguard-interface-delete-acceptance.js")).acceptWireguardInterfaceDeletion(actor),
+2
View File
@@ -91,6 +91,7 @@ import { registerTerminalDiagnosticActions } from "./terminal-diagnostic-actions
import { registerEventsBatchActions } from "./events-batch-actions.js"
import { registerGlobalReconciliationActions } from "./global-reconciliation-actions.js"
import { registerWireguardInterfaceUpdateActions } from "./wireguard-interface-update-actions.js"
import { registerInterfaceRenameActions } from "./interface-rename-actions.js"
import { registerWireguardImportActions } from "./wireguard-import-actions.js"
import { registerWireguardExportActions } from "./wireguard-export-actions.js"
import { registerServerConnectionActions } from "./server-connection-actions.js"
@@ -710,6 +711,7 @@ registerTerminalDiagnosticActions(add)
registerEventsBatchActions(add)
registerGlobalReconciliationActions(add)
registerWireguardInterfaceUpdateActions(add)
registerInterfaceRenameActions(add)
registerWireguardImportActions(add)
registerServerConnectionActions(add)
registerIpsecPeerDeleteActions(add)
@@ -3028,6 +3028,7 @@ try {
await soft("verifyMcpTerminalDiagnostics", async () => (await import("../../test/mcp-terminal-diagnostics.js")).verifyMcpTerminalDiagnostics(actor, app))
await soft("verifyMcpEventsBatch", async () => (await import("../../test/mcp-events-batch.js")).verifyMcpEventsBatch(actor, app))
await shardable("acceptWireguardInterfaceUpdate")
await shardable("acceptInterfaceRename")
await shardable("acceptWireguardImport")
await soft("acceptWireguardExportMcp", async () => (await import("./wireguard-export-acceptance.js")).acceptWireguardExportMcp(actor, app))
await soft("acceptProtectionRegistrationScope", async () => (await import("./protection-scope-acceptance.js")).acceptProtectionRegistrationScope(actor))
@@ -0,0 +1,110 @@
import assert from "node:assert/strict"
import { randomUUID } from "node:crypto"
import { createServer } from "node:http"
import { setTimeout as delay } from "node:timers/promises"
import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp"
import { pool } from "../../db/index.js"
import type { AuthUser } from "../../lib/permissions.js"
import { approvePlan, applyPlan, invokeMcpTool } from "./runtime.js"
import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "./store.js"
export async function acceptInterfaceRename(actor: AuthUser) {
const serverId = 174
const initial: Record<string, Record<string, string>[]> = {
"/interface": [
{ ".id": "*7", name: "wg1", type: "wg", dynamic: "false" },
{ ".id": "*8", name: "ether1", "default-name": "ether1", type: "ether" },
{ ".id": "*9", name: "radio one", type: "wifi" },
{ ".id": "*a", name: "dynamic1", type: "ppp", dynamic: "true" },
{ ".id": "*b", name: "wg1-backup", type: "wg" },
],
"/ip/address": [{ ".id": "*15", interface: "wg1", "actual-interface": "wg1", address: "192.0.2.1/30" }],
"/ipv6/address": [{ ".id": "*16", interface: "*7", address: "2001:db8::1/64" }],
"/interface/bridge/port": [{ ".id": "*14", bridge: "bridge1", interface: "ether1" }],
"/interface/wireguard/peers": [{ ".id": "*11", interface: "wg1", name: "other-site", "public-key": "fixture-public-key", "allowed-address": "192.0.2.2/32", "endpoint-port": "13235" }],
"/ip/firewall/filter": [
{ ".id": "*20", chain: "input", action: "accept", "in-interface": "wg1", protocol: "udp" },
{ ".id": "*21", chain: "forward", action: "drop", "in-interface": "!wg1", "out-interface": "wg1-backup" },
],
"/ip/route": [{ ".id": "*22", static: "true", gateway: "192.0.2.2%wg1@main", "dst-address": "198.51.100.0/24" }, ...Array.from({ length: 12_608 }, (_, i) => ({ ".id": `*r${i.toString(16)}`, dynamic: "true", gateway: "192.0.2.2%ether1" }))],
}
let rows = structuredClone(initial), reads = 0, mode: "normal" | "stale" | "lost" = "normal"
const writes: { path: string; fields: Record<string, string> }[] = []
const router = createServer(async (req, res) => {
const url = new URL(req.url ?? "/", "http://fixture.invalid"), path = url.pathname.slice(5)
res.setHeader("content-type", "application/json")
if (req.method === "GET") {
let records = rows[path] ?? []
if (url.searchParams.get("static") === "true") records = records.filter(row => row.static === "true")
const properties = url.searchParams.get(".proplist")?.split(",")
if (properties) records = records.map(row => Object.fromEntries(Object.entries(row).filter(([key]) => properties.includes(key))))
if (path === "/ip/firewall/filter") records = records.map(row => ({ ...row, packets: String(++reads) }))
res.end(JSON.stringify(records)); return
}
if (req.method !== "PATCH" || !/^\/interface\/\*[a-z0-9]+$/i.test(path)) { res.statusCode = 400; res.end("{}"); return }
let body = ""; for await (const chunk of req) body += chunk
const fields: Record<string, string> = JSON.parse(body), target = rows["/interface"].find(row => path.endsWith("/" + row[".id"]))!
const old = target.name
writes.push({ path, fields })
Object.assign(target, fields)
if (mode !== "stale") {
for (const rowsForPath of Object.values(rows)) for (const row of rowsForPath) for (const key of ["interface", "actual-interface", "in-interface", "out-interface"]) {
if (row[key] === old) row[key] = target.name
if (row[key] === "!" + old) row[key] = "!" + target.name
}
for (const row of rows["/ip/route"]) if (row.gateway.endsWith(`%${old}@main`)) row.gateway = row.gateway.replace(`%${old}@main`, `%${target.name}@main`)
}
if (mode === "lost") { req.socket.destroy(); return }
res.end(JSON.stringify(target))
})
await new Promise<void>(resolve => router.listen(0, "127.0.0.1", resolve))
let connectionId: string | undefined
const protection = randomUUID()
try {
await pool.query("INSERT INTO servers(id,name,host,port,use_ssl) VALUES($1,'interface-rename-fixture','127.0.0.1',$2,false)", [serverId, (router.address() as { port: number }).port])
const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "interface rename", mode: "approval", expiresAt: new Date(Date.now() + 1_800_000).toISOString(), permissions: ["mm:network:write"], scope: { serverIds: [serverId], operationIds: ["interfaces.rename"], allowGlobal: false }, limits: { callsPerMinute: 120 } }), actor)
connectionId = access.connection.id
const principal = await authenticateMcpToken(access.token)
const plan = async (iface: string, name: string) => (await invokeMcpTool(principal, "mm_interface_rename_plan", { serverId, interface: iface, name })) as { planId: string }
const execute = async (iface: string, name: string) => {
const { planId } = await plan(iface, name)
await approvePlan(planId, actor, true)
const { id } = await applyPlan(principal, planId, randomUUID())
for (let i = 0; i < 500; i++) {
const result = (await pool.query("SELECT state,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]
if (result && !["pending", "running"].includes(result.state)) return result
await delay(20)
}
throw new Error("Interface rename remains live")
}
await assert.rejects(plan("dynamic1", "renamed"), { code: "PROTECTED_OBJECT" })
await assert.rejects(plan("*7", "ether1"), { code: "STATE_CONFLICT" })
assert.equal(writes.length, 0)
await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "ip_filter", id: "*20", serverId, protection: "deny" }), actor.id])
await assert.rejects(plan("*7", "protected-test"), { code: "PROTECTED_OBJECT" })
await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection])
assert.equal((await execute("wg1", "wg-to-msk-gw01")).state, "applied")
assert.deepEqual(writes[0], { path: "/interface/*7", fields: { name: "wg-to-msk-gw01" } })
assert.deepEqual(rows["/ip/firewall/filter"].map(row => row["in-interface"]), ["wg-to-msk-gw01", "!wg-to-msk-gw01"])
assert.equal(rows["/ip/firewall/filter"][1]["out-interface"], "wg1-backup")
assert.equal(rows["/interface/wireguard/peers"][0].interface, "wg-to-msk-gw01")
assert.equal(rows["/ipv6/address"][0].interface, "*7")
assert.equal(rows["/ip/route"][0].gateway, "192.0.2.2%wg-to-msk-gw01@main")
assert.equal((await execute("*8", "ether-wan")).state, "applied")
assert.equal(rows["/interface/bridge/port"][0].interface, "ether-wan")
assert.equal((await execute("radio one", "wifi guest")).state, "applied")
assert.equal(writes.length, 3)
mode = "stale"
assert.equal((await execute("wg-to-msk-gw01", "wg-incomplete")).error_code, "VERIFICATION_FAILED")
assert.equal(writes.length, 4)
rows = structuredClone(initial); mode = "lost"
assert.equal((await execute("wg1", "wg-unknown")).error_code, "MUTATION_OUTCOME_UNKNOWN")
assert.equal(writes.length, 5)
} finally {
await pool.query("DELETE FROM mcp_protections WHERE id=$1", [protection])
if (connectionId) await setConnectionStatus(connectionId, "paused", actor)
await pool.query("DELETE FROM servers WHERE id=$1", [serverId])
router.closeAllConnections()
await new Promise<void>((resolve, reject) => router.close(error => error ? reject(error) : resolve()))
}
}
@@ -0,0 +1,186 @@
import { z } from "zod"
import { pool } from "../../db/index.js"
import { assertVerifiedConfigurationAccess, assertVerifiedConfigurationReadAccess } from "../../services/config-mutation-policy.js"
import { greStableRows, assertGreDeleteDependencies } from "../../services/gre-mutation-state.js"
import { observedBoolean } from "../../services/ospf-observed-values.js"
import { encodeRosId, isRouterOsNativeId } from "../../services/routeros-native-paths.js"
import type { add } from "./actions.js"
import type { ChangeEvidence } from "./action-types.js"
import { rosSnapshotSourceProof, routerClient } from "./resources.js"
import { fingerprint, McpError, privateSecretFingerprint } from "./security.js"
import { registerMcpService } from "./service-registry.js"
const service = "interfaces.rename"
const name = z.string().min(1).max(100).refine(value => value === value.trim() && !/[\u0000-\u001f\u007f]/.test(value))
const schema = z.object({ serverId: z.number().int().positive().safe(), interface: name, name }).strict()
const recordSchema = z.object({ serverId: schema.shape.serverId, id: z.string().max(200).refine(isRouterOsNativeId), from: name, to: name }).strict()
type Rename = z.infer<typeof recordSchema>
type Row = Record<string, string>
type Reference = "single" | "list" | "gateway" | "negated"
const inventory = "/interface?.proplist=.id,name,type,default-name,dynamic,builtin,default,disabled,mtu,l2mtu,comment"
const firewall = (["ip", "ipv6"] as const).flatMap(family => ["filter", "nat", "mangle", "raw"].map(table => ({
kind: `${family}_${table}`, path: `/${family}/firewall/${table}`, fields: { "in-interface": "negated", "out-interface": "negated" } as const, ordered: true,
})))
const references: { kind: string; path: string; fields: Record<string, Reference>; ordered?: boolean; wireguard?: boolean }[] = [
{ kind: "ipv4_address", path: "/ip/address", fields: { interface: "single", "actual-interface": "single" } },
{ kind: "ipv6_address", path: "/ipv6/address", fields: { interface: "single", "actual-interface": "single" } },
{ kind: "interface_list_member", path: "/interface/list/member", fields: { interface: "single" } },
{ kind: "bridge_port", path: "/interface/bridge/port", fields: { interface: "single", bridge: "single" } },
{ kind: "bridge_vlan", path: "/interface/bridge/vlan", fields: { bridge: "single", tagged: "list", untagged: "list" } },
{ kind: "vlan", path: "/interface/vlan", fields: { interface: "single" } },
{ kind: "bonding", path: "/interface/bonding", fields: { slaves: "list" } },
{ kind: "vrrp", path: "/interface/vrrp", fields: { interface: "single", "group-authority": "single" } },
{ kind: "wireguard_peer", path: "/interface/wireguard/peers?.proplist=.id,interface,name,public-key,allowed-address,endpoint-address,endpoint-port,persistent-keepalive,disabled,dynamic,comment", fields: { interface: "single" }, wireguard: true },
{ kind: "ipv4_route", path: "/ip/route?static=true", fields: { gateway: "gateway" } },
{ kind: "ipv6_route", path: "/ipv6/route?static=true", fields: { gateway: "gateway" } },
{ kind: "routing_rule", path: "/routing/rule", fields: { interface: "single" }, ordered: true },
{ kind: "vrf", path: "/ip/vrf", fields: { interfaces: "list" }, ordered: true },
{ kind: "ospf_template", path: "/routing/ospf/interface-template", fields: { interfaces: "list" }, ordered: true },
{ kind: "bfd_configuration", path: "/routing/bfd/configuration", fields: { interfaces: "list" }, ordered: true },
...firewall,
]
const fail = (code: string, message: string, status = 409) => new McpError(code, message, status)
const unstable = (path: string) => fail("STATE_UNAVAILABLE", `Не подтверждён каталог RouterOS ${path.split("?")[0]}`, 502)
const unsupported = new Set(["all", "none", "dynamic", "slave", "static", "wg-flow", "lo"])
async function catalogue(client: Awaited<ReturnType<typeof routerClient>>["client"], path: string): Promise<Row[]> {
const routes = path.endsWith("?static=true")
let raw: unknown
try { raw = await client.get<unknown>(path, 10_000, undefined, routes ? 8 * 1024 * 1024 : 1_048_576) }
catch (error) { if (error instanceof McpError) throw error; throw unstable(path) }
if (!Array.isArray(raw) || raw.length > (routes ? 50_000 : 10_000) || raw.some(row => !row || typeof row !== "object" || Array.isArray(row) || !isRouterOsNativeId(row[".id"]) || Object.values(row).some(value => typeof value !== "string")) || new Set(raw.map(row => row[".id"])).size !== raw.length) throw unstable(path)
return raw as Row[]
}
/** RouterOS can render the same typed reference as a name or a native ID. */
function interfaceRenameReference(value: string, mode: Reference, aliases: ReadonlySet<string>, replacement: string): string {
if (aliases.has(value)) return replacement
if (mode === "negated" && value.startsWith("!") && aliases.has(value.slice(1))) return `!${replacement}`
if (mode === "list" || mode === "gateway") return value.split(",").map(part => {
if (aliases.has(part)) return replacement
if (mode === "gateway") {
const match = /^([^%@]+%)?([^%@]+)(@[^%@]+)?$/.exec(part)
if (match && aliases.has(match[2])) return `${match[1] ?? ""}${replacement}${match[3] ?? ""}`
}
return part
}).join(",")
return value
}
function targetFrom(rows: Row[], identity: string) {
const target = rows.find(row => row.name === identity || row[".id"] === identity)
if (!target) throw fail("NOT_FOUND", "Интерфейс RouterOS не найден", 404)
if (!target.type || !target.name || rows.filter(row => row.name === target.name || row[".id"] === identity || row.name === identity).length !== 1) throw unstable(inventory)
if (["dynamic", "builtin", "default"].some(key => target[key] !== undefined && observedBoolean(target[key]) !== false) || unsupported.has(target.name.toLowerCase()) || target.comment === "MikrotikManager traffic-flow overlay") throw fail("PROTECTED_OBJECT", "Динамический, системный или управляемый NetFlow интерфейс нельзя переименовать этим планом")
return target
}
async function applicationReferences(move: Rename) {
const names = [move.id, move.from, move.to]
const { rows } = await pool.query<{ bound: boolean }>(`SELECT (
EXISTS(SELECT 1 FROM user_interface_bindings WHERE server_id=$1 AND interface_name=ANY($2::text[])) OR
EXISTS(SELECT 1 FROM uptime_probes WHERE src_server_id=$1 AND src_interface=ANY($2::text[])) OR
EXISTS(SELECT 1 FROM uptime_speed_probes WHERE (src_server_id=$1 AND src_interface=ANY($2::text[])) OR (dst_server_id=$1 AND dst_interface=ANY($2::text[])))
) AS bound`, [move.serverId, names])
if (rows[0]?.bound !== false) throw fail("DEPENDENCY_CONFLICT", "Имя или ID используется в настройках приложения; требуется согласованное изменение привязки")
const { rows: filters } = await pool.query<{ id: number; gateway: string; gatewayTunnelId: string }>('SELECT id, gateway, gateway_tunnel_id AS "gatewayTunnelId" FROM filter_rules WHERE server_id=$1 LIMIT 1001', [move.serverId])
if (filters.length > 1000) throw fail("STATE_UNAVAILABLE", "Не подтверждены зависимости фильтров приложения", 502)
const aliases = new Set(names)
if (filters.some(row => aliases.has(row.gatewayTunnelId) || interfaceRenameReference(row.gateway, "gateway", aliases, "\u0000") !== row.gateway)) throw fail("DEPENDENCY_CONFLICT", "Интерфейс используется BGP-фильтром приложения")
}
async function read(move: Rename, expectedSource: string, authorize: () => Promise<unknown>) {
await authorize()
const { client, server } = await routerClient(move.serverId)
if (await rosSnapshotSourceProof(server) !== expectedSource) throw fail("STATE_CONFLICT", "Источник RouterOS изменился")
const interfaces = await catalogue(client, inventory), target = targetFrom(interfaces, move.id)
if (![move.from, move.to].includes(target.name) || interfaces.some(row => row[".id"] !== move.id && row.name === move.to)) throw fail("STATE_CONFLICT", "Интерфейс изменился или новое имя уже занято")
const aliases = new Set([move.from, move.to, move.id]), current = new Set([target.name, move.id])
const marker = `\u0000${move.id}`
const affected: ChangeEvidence["objects"] = []
const sections: unknown[] = []
for (const dependency of references) {
if (dependency.wireguard && !["wg", "wireguard"].includes(target.type)) continue
await authorize()
const rows = await catalogue(client, dependency.path)
if (dependency.path.endsWith("?static=true") && rows.some(row => observedBoolean(row.dynamic) === true || observedBoolean(row.static) === false)) throw unstable(dependency.path)
const selected: Row[] = []
for (const [position, row] of rows.entries()) {
if (observedBoolean(row.dynamic) === true) continue
const projection = { ...row }
let related = false
for (const [field, mode] of Object.entries(dependency.fields)) {
if (row[field] === undefined || interfaceRenameReference(row[field], mode, aliases, marker) === row[field]) continue
if (interfaceRenameReference(row[field], mode, aliases, marker) !== interfaceRenameReference(row[field], mode, current, marker)) throw fail("DEPENDENCY_CONFLICT", `Несогласованная ссылка на интерфейс в ${dependency.path.split("?")[0]}`)
projection[field] = interfaceRenameReference(row[field], mode, current, marker)
related = true
}
if (!related) continue
affected.push({ kind: dependency.kind, id: row[".id"], serverId: move.serverId })
// These bridge VLAN memberships are derived by RouterOS from the configured ports and PVID.
if (dependency.kind === "bridge_vlan") { delete projection["current-tagged"]; delete projection["current-untagged"] }
if (dependency.ordered) projection.position = String(position)
selected.push(projection)
}
sections.push([dependency.kind, greStableRows(selected, dependency.ordered)])
}
const policyPath = "/routing/filter/rule?.proplist=.id,rule"
await authorize()
const routingFilters = await catalogue(client, policyPath)
for (const reference of [move.from, move.to]) assertGreDeleteDependencies({ dependencies: { [policyPath]: routingFilters }, bindings: [], filters: [] }, { name: reference, ".id": move.id }, "Имя интерфейса")
await applicationReferences(move)
await authorize()
if (await rosSnapshotSourceProof((await routerClient(move.serverId)).server) !== expectedSource) throw fail("STATE_CONFLICT", "Источник RouterOS изменился во время чтения")
const identities = interfaces.filter(row => row[".id"] === move.id || observedBoolean(row.dynamic) !== true)
const proof = (targetName: string) => privateSecretFingerprint(fingerprint({ interfaces: greStableRows(identities.map(row => row[".id"] === move.id ? { ...row, name: targetName } : row)), sections }), "mcp-interface-rename-v1")
return { target, affected, before: { name: target.name, proof: await proof(target.name) }, afterProof: await proof(move.to) }
}
export function registerInterfaceRenameActions(register: typeof add) {
const current = async (input: Record<string, unknown>) => (await read(recordSchema.parse(input.move), String(input.source), async () => {})).before
registerMcpService(service, {
current,
execute: async input => {
const move = recordSchema.parse(input.move), source = String(input.source)
const { client } = await routerClient(move.serverId)
try {
await client.patch(`/interface/${encodeRosId(move.id)}`, { name: move.to }, 15_000, async () => {
await assertVerifiedConfigurationAccess()
if ((await read(move, source, assertVerifiedConfigurationReadAccess)).before.proof !== input.beforeProof) throw fail("STATE_CONFLICT", "Интерфейс или его ссылки изменились перед переименованием")
await assertVerifiedConfigurationAccess()
})
} catch (error) {
if (error instanceof McpError) throw error
throw fail("MUTATION_OUTCOME_UNKNOWN", "Исход переименования не подтверждён; повтор запрещён", 503)
}
return { interfaceId: move.id, previousName: move.from, name: move.to }
},
verify: async input => {
try { return (await current(input)).proof === input.afterProof }
catch (error) { if (error instanceof McpError && error.code === "DEPENDENCY_CONFLICT") return false; throw error }
},
reconcile: async (input, authorize) => {
const { before } = await read(recordSchema.parse(input.move), String(input.source), authorize)
return { matchesOriginalState: before.proof === input.beforeProof, matchesDesiredState: before.proof === input.afterProof }
},
})
register(service, "mm_interface_rename_plan", "Переименовать интерфейс RouterOS по ID или текущему имени и проверить сохранность привязок", "network", "mm:network:write", "write", "servers", schema, {
prepare: async (input, context) => {
const request = schema.parse(input), { client, server } = await routerClient(request.serverId)
const source = await rosSnapshotSourceProof(server)
const target = targetFrom(await catalogue(client, inventory), request.interface)
if (request.name === target.name || unsupported.has(request.name.toLowerCase()) || isRouterOsNativeId(request.name)) throw fail("INVALID_INPUT", "Укажите новое незарезервированное имя интерфейса", 400)
const move = recordSchema.parse({ serverId: request.serverId, id: target[".id"], from: target.name, to: request.name })
const state = await read(move, source, async () => { context.signal?.throwIfAborted() })
if (state.before.name !== move.from) throw fail("STATE_CONFLICT", "Имя интерфейса изменилось во время подготовки")
const kind = ({ wg: "wireguard", ether: "ethernet" } as Record<string, string>)[state.target.type] ?? state.target.type
const objects = [{ kind: "server", id: String(move.serverId), serverId: move.serverId }, ...[...new Set(["interface", kind])].flatMap(kind => [move.id, move.from, move.to].map(id => ({ kind, id, serverId: move.serverId }))), ...state.affected]
return { target: { scope: "servers", serverIds: [move.serverId] }, objects, snapshots: [], before: state.before, requiresHuman: true, verificationScope: "configuration",
steps: [{ kind: "service", service, input: { serverId: move.serverId, move, source, beforeProof: state.before.proof, afterProof: state.afterProof } }],
warnings: ["Выполняется один RouterOS PATCH имени по native ID. Передача трафика может кратко прерваться.", "Проверяются идентичность интерфейса и ссылки в наблюдаемых каталогах RouterOS. Пользовательские скрипты и история метрик по имени не переписываются.", "При неясном исходе требуется сверка. Команда не повторяется автоматически."],
}
},
})
}
@@ -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)
}
},
})
+18
View File
@@ -55,6 +55,24 @@ test("unattended plans require an explicit service policy and retain server prot
denied(input, "PROTECTED_OBJECT")
})
test("service rate exemptions keep plan size, server scope and approval checks", () => {
const input = request()
assert.equal(mcpConnectionPolicySchema.safeParse({ ...input.policy, unlimitedRates: true }).success, false)
input.policy = mcpConnectionPolicySchema.parse({ ...input.policy, authority: "service", expiresAt: MCP_SERVICE_EXPIRATION, unlimitedRates: true })
input.usage = { callsInMinute: 10_000, concurrentReads: 100, mutationsInHour: 10_000 }
const read = { ...input, operationId: "logs.query" }
assert.equal(evaluateMcpPolicy(read, now).decision, "allowed")
const mutation = mcpPolicyDecisionSchema.parse(evaluateMcpPolicy(input, now))
assert.equal(mutation.unlimitedRates, true)
assert.equal(mutation.decision, "approval-required")
denied({ ...input, objectCount: input.policy.limits.objectsPerPlan + 1 }, "PLAN_LIMIT_EXCEEDED")
denied({ ...input, target: { scope: "servers", serverIds: [99] } }, "SERVER_DENIED")
denied({ ...input, protections: [{ kind: "server", id: "1", protection: "deny" }] }, "PROTECTED_OBJECT")
delete input.policy.unlimitedRates
denied(input, "BUDGET_EXCEEDED")
denied(read, "RATE_LIMITED")
})
test("observer reads permitted logs, but cannot run bandwidth or create users", () => {
const read = request("logs.query"); read.policy.mode = "observer"
assert.equal(evaluateMcpPolicy(read, now).decision, "allowed")
+9 -3
View File
@@ -12,6 +12,10 @@ export function hasUnattendedPlanAuthority(policy: McpConnectionPolicy) {
return policy.authority === "service" && policy.mode === "autonomous" && policy.allowUnattendedPlans === true
}
export function hasUnlimitedMcpRates(policy: McpConnectionPolicy) {
return policy.authority === "service" && policy.unlimitedRates === true
}
export function operationApprovalRequirement(policy: McpConnectionPolicy, operationId: string, target: McpPolicyPreviewRequest["target"], requiresHuman?: boolean): Pick<McpPolicyPreviewRequest, "objects" | "protections"> {
if (!requiresHuman || hasUnattendedPlanAuthority(policy)) return { objects: [], protections: [] }
const objects = target.scope === "servers" ? [] : [{ kind: "mcp-operation", id: operationId }]
@@ -24,6 +28,7 @@ export function evaluateMcpPolicy(payload: McpPolicyPreviewRequest, now = new Da
const input = mcpPolicyPreviewRequestSchema.parse(payload)
if (!Number.isFinite(now.getTime())) throw new Error("Некорректное время проверки MCP")
const { policy, target, usage } = input
const unlimitedRates = hasUnlimitedMcpRates(policy)
const denied: McpPolicyDecision["reasons"] = []
const approvals: McpPolicyDecision["reasons"] = []
const deny = (code: McpPolicyReasonCode, message: string) => { denied.push({ code, message }) }
@@ -32,6 +37,7 @@ export function evaluateMcpPolicy(payload: McpPolicyPreviewRequest, now = new Da
decision: denied.length ? "denied" : approvals.length ? "approval-required" : "allowed",
reasons: [...denied, ...approvals],
limits: policy.limits,
...(unlimitedRates && { unlimitedRates: true }),
})
const operation = findMcpOperation(input.operationId)
if (!operation) {
@@ -72,15 +78,15 @@ export function evaluateMcpPolicy(payload: McpPolicyPreviewRequest, now = new Da
deny("INVALID_OBJECT_SCOPE", "Объект или зависимость находится вне области запроса")
}
if (usage.callsInMinute >= policy.limits.callsPerMinute) deny("RATE_LIMITED", "Исчерпан минутный лимит вызовов")
if (!unlimitedRates && usage.callsInMinute >= policy.limits.callsPerMinute) deny("RATE_LIMITED", "Исчерпан минутный лимит вызовов")
if (operation.effect === "read") {
if (usage.concurrentReads >= policy.limits.concurrentReads) deny("RATE_LIMITED", "Исчерпан лимит одновременного чтения")
if (!unlimitedRates && usage.concurrentReads >= policy.limits.concurrentReads) deny("RATE_LIMITED", "Исчерпан лимит одновременного чтения")
return result()
}
if (policy.mode === "observer") deny("OBSERVER_WRITE_DENIED", "Наблюдателю запрещены изменения и нагрузочные проверки")
if (!input.auditAvailable) deny("AUDIT_UNAVAILABLE", "До начала изменений требуется доступный аудит")
if (usage.mutationsInHour >= policy.limits.mutationsPerHour || usage.mutationsInHour + input.objectCount > policy.limits.mutationsPerHour) deny("BUDGET_EXCEEDED", "Недостаточно часового бюджета для всех изменений плана")
if (!unlimitedRates && (usage.mutationsInHour >= policy.limits.mutationsPerHour || usage.mutationsInHour + input.objectCount > policy.limits.mutationsPerHour)) deny("BUDGET_EXCEEDED", "Недостаточно часового бюджета для всех изменений плана")
const affectedServers = new Set([
...(target.scope === "servers" ? target.serverIds : []),
...input.objects.flatMap(object => object.serverId === undefined ? [] : [object.serverId]),
@@ -20,12 +20,12 @@ const { pool, closePool } = await import("../../db/index.js")
const { SYSTEM_DATABASE_OPERATION_LOCK, SYSTEM_DATABASE_REQUEST_LOCK, withOperationLock, withSystemDatabaseOperationLock } = await import("../../db/operation-lock.js")
const { applySqlMigrations, applyStandaloneSqlMigrations } = await import("../../db/migrate.js")
const { PARTITION_WRITER_ADVISORY_KEY } = await import("../../services/partition-write-transaction.js")
const { authenticateMcpToken, createMcpConnection, updateMcpSettings } = await import("./store.js")
const { authenticateMcpToken, createMcpConnection, updateMcpConnection, updateMcpSettings } = await import("./store.js")
const { recordUnauthorizedProtocol, reserveProtocolEnvelope } = await import("./protocol-limits.js")
const { canonicalMcpUrl, encryptSecret, tokenDigest } = await import("./security.js")
const { buildApp } = await import("../../index.js")
const { sharedDatabaseGate } = await import("../../services/shared-database-gate.js")
const { invokeMcpTool } = await import("./runtime.js")
const { applyPlan, approvePlan, invokeMcpTool } = await import("./runtime.js")
const app = await buildApp({ startScheduler: false })
try {
@@ -177,6 +177,54 @@ try {
(SELECT count(*)::int FROM mcp_requests WHERE tool='protocol:unauthorized') AS unauthorized,
(SELECT max(attempts)::int FROM mcp_unauthorized_buckets) AS attempts`)
assert.deepEqual(counts, { denied: 1, unauthorized: 1, attempts: 8 })
const serviceOwner = { ...actor, permissions: [...actor.permissions, "mm:mcp:admin", "mm:dashboard:read"] }
const settings = mcpConnectionCreateSchema.parse({ name: "Service rate fixture", authority: "service", mode: "autonomous", expiresAt: null,
permissions: ["mm:dashboard:read"], scope: { serverIds: [], operationIds: ["capabilities.read", "events.batch.append"], allowGlobal: true },
limits: { callsPerMinute: 1, concurrentReads: 1, mutationsPerHour: 1 } })
const service = await createMcpConnection(settings, serviceOwner)
const servicePrincipal = await authenticateMcpToken(service.token)
const serviceId = service.connection.id
await pool.query(`INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used)
SELECT $1,kind,date_trunc(period,now())+window_shift,10 FROM (VALUES
('calls','minute',interval '0'),('protocol','minute',interval '0'),('mutations','hour',interval '0'),
('calls','minute',interval '1 minute'),('protocol','minute',interval '1 minute'),('mutations','hour',interval '1 hour')
) AS slots(kind,period,window_shift)`, [serviceId])
const leaseId = randomUUID()
await pool.query("INSERT INTO mcp_read_leases(id,connection_id,expires_at) VALUES($1,$2,now()+interval '2 minutes')", [leaseId, serviceId])
const serviceRequest = () => app.inject({ method: "POST", url: "/mcp", headers: {
authorization: `Bearer ${service.token}`, accept: "application/json, text/event-stream", "mcp-protocol-version": "2025-06-18",
}, payload: { jsonrpc: "2.0", id: 1, method: "tools/list", params: {} } })
try {
assert.equal((await serviceRequest()).statusCode, 429)
await assert.rejects(invokeMcpTool(servicePrincipal, "mm_capabilities_get", {}), { code: "RATE_LIMITED" })
const updated = await updateMcpConnection(serviceId, { ...settings, unlimitedRates: true }, serviceOwner)
assert.equal(updated.policy.unlimitedRates, true)
assert.equal((await authenticateMcpToken(service.token)).credentialId, servicePrincipal.credentialId)
assert.equal((await serviceRequest()).statusCode, 200)
await invokeMcpTool(servicePrincipal, "mm_capabilities_get", {})
const eventType = `service-rate-fixture-${randomUUID()}`
const plan = await invokeMcpTool(servicePrincipal, "mm_events_batch_append_plan", { events: [
{ level: "info", eventType, sourceModule: "system", title: "Service rate fixture", message: "Synthetic journal entry" },
] }) as { planId: string; state: string }
assert.equal(plan.state, "awaiting-approval")
await approvePlan(plan.planId, serviceOwner, true)
const operation = await applyPlan(servicePrincipal, plan.planId, randomUUID())
let outcome: string | undefined
for (let i = 0; i < 250; i++) {
outcome = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operation.id])).rows[0]?.state
if (outcome && !["pending", "running"].includes(outcome)) break
await delay(20)
}
assert.equal(outcome, "applied")
assert.equal((await pool.query("SELECT count(*)::int AS n FROM events WHERE event_type=$1", [eventType])).rows[0].n, 1)
const restored = await updateMcpConnection(serviceId, settings, serviceOwner)
assert.equal(restored.policy.unlimitedRates, undefined)
assert.equal((await serviceRequest()).statusCode, 429)
await assert.rejects(invokeMcpTool(servicePrincipal, "mm_capabilities_get", {}), { code: "RATE_LIMITED" })
} finally {
await pool.query("DELETE FROM mcp_read_leases WHERE id=$1", [leaseId])
}
console.log("MCP request accounting respects the backup/restore lock independently of the partition lease")
} finally {
await app.close()
+3 -2
View File
@@ -1,6 +1,7 @@
import { randomUUID } from "node:crypto"
import { requestAccountingWrite, validatePrincipal, type McpPrincipal } from "./store.js"
import { accessGeneration, tokenDigest } from "./security.js"
import { hasUnlimitedMcpRates } from "./policy.js"
export async function reserveProtocolEnvelope(principal: McpPrincipal) {
return requestAccountingWrite(async client => {
@@ -9,8 +10,8 @@ export async function reserveProtocolEnvelope(principal: McpPrincipal) {
const connection = await validatePrincipal(principal, client)
// Protocol negotiation and metadata have their own cap; tools retain their
// stricter policy budget. A single metadata endpoint cannot bypass limits.
const limit = Math.max(10, Math.min(3000, connection.policy.limits.callsPerMinute * 3))
const { rows } = await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'protocol',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+1 WHERE mcp_budget_buckets.used<$2 RETURNING used", [connection.id, limit])
const limit = hasUnlimitedMcpRates(connection.policy) ? null : Math.max(10, Math.min(3000, connection.policy.limits.callsPerMinute * 3))
const { rows } = await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'protocol',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used::bigint+1,2147483647) WHERE $2::integer IS NULL OR mcp_budget_buckets.used<$2 RETURNING used", [connection.id, limit])
if (!rows.length) {
const { rows: [denied] } = await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'protocol-denied',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used+1,2147483647) RETURNING used", [connection.id])
if (denied.used === 1) await client.query("INSERT INTO mcp_requests(id,connection_id,actor_id,tool,status,input,error_code) VALUES($1,$2,$3,'protocol:rate-limit','denied','{}','RATE_LIMITED')", [randomUUID(), connection.id, connection.owner_id])
@@ -51,6 +51,7 @@ function fixture(service='routing.community') {
'./routing-profile-authority.js':{routingProfileAuthorityBindingSchema:{parse:value=>value}},
'./routing-community-scoped-request.js':{},'./routing-community-scoped-checkpoint.js':{},'./routing-community-scoped-recovery-source.js':{},
'./security.js':{fingerprint,redact,McpError,decryptSecretExisting:async value=>{decryptCalls++;return value;},privateFingerprinterExisting:async()=>{if(keyUnavailable)throw Error('key unavailable');return mac;}},
'./policy.js':{hasUnlimitedMcpRates:()=>assert.fail('Forward transport verification must not evaluate rate policy')},
'./store.js':{},'./routing-reservation-contract.js':{},'./routing-reservation-transaction.js':{},'./routing-reservation-runtime.js':{}
};
const verifier=load('routing-reservation-store.ts',providers).assertPersistentRoutingForwardTransportCharge;
@@ -18,6 +18,7 @@ import { routingReservationDeadline, transitionRoutingReservation, type RoutingR
import { routingReservationTransaction, type RoutingReservationConnect } from "./routing-reservation-transaction.js"
import { lockRoutingReservationAdmissionSource } from "./routing-reservation-runtime.js"
import type { StoredPlan } from "./store.js"
import { hasUnlimitedMcpRates } from "./policy.js"
const uuid = z.string().uuid(), proof = z.string().regex(/^[a-f0-9]{64}$/), stamp = z.string().datetime()
const principalSchema = z.object({ connectionId: uuid, credentialId: uuid, credentialExpiresAt: stamp }).strict()
@@ -271,7 +272,8 @@ async function reserveWithinOwner(db: PoolClient, operationId: string) {
// Legacy overlap stays charged, including allocations predating audit anchors.
const charges = await readHistoricalChargesWithinOwner(db, value.authority.packet.profileId, connection, createdAt, deadline)
if (charges.totalCharges + 2 > value.profile.maxSwitchesPerHour) throw fail("Insufficient durable profile switch slots")
const budget = await db.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) SELECT $1,'mutations',$2,2 WHERE $3::int>=2 ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+2 WHERE mcp_budget_buckets.used<=$3::int-2 RETURNING used", [connection.id, budgetBucket, connection.policy.limits.mutationsPerHour])
const limit = hasUnlimitedMcpRates(connection.policy) ? null : connection.policy.limits.mutationsPerHour
const budget = await db.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) SELECT $1,'mutations',$2,2 WHERE $3::int IS NULL OR $3::int>=2 ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used::bigint+2,2147483647) WHERE $3::int IS NULL OR mcp_budget_buckets.used<=$3::int-2 RETURNING used", [connection.id, budgetBucket, limit])
if (budget.rowCount !== 1) throw fail("Insufficient connection mutation budget for forward and inverse")
const binding = bindingSchema.parse({ revision: 1, id: randomUUID(), planId: value.row.plan_id, operationId, stepIndex: 0, sourceReceiptId: value.input.receiptId,
inverseReceiptId: randomUUID(), profileId: value.authority.packet.profileId, profileVersion: value.authority.packet.profileVersion, principal: value.principal,
@@ -3,6 +3,7 @@ import { randomUUID } from "node:crypto"
import { basename, dirname, join, resolve } from "node:path"
import { readFile, writeFile } from "node:fs/promises"
import type { PoolClient } from "pg"
import { MCP_SERVICE_EXPIRATION } from "@mmapp/contracts/mcp"
/** Actual SQL/crypto numbered-schema acceptance only; no public tool registration,
* native callback, controller tick, reservation refund or fixture DELETE. */
@@ -49,11 +50,12 @@ export async function acceptRoutingReservationSql(options: { runtimeAdmission?:
ids.push(...(await pool.query("INSERT INTO servers(name,host,port,enabled,username,password,use_ssl,verify_ssl) VALUES($1,'127.0.0.1',9,true,'SYNTHETIC','SYNTHETIC',false,true) RETURNING id", ["reservation-synthetic-" + randomUUID()])).rows.map(row => Number(row.id)))
await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1")
const contextSources = (await createRoutingSourceContext(await readRoutingEndpoints(ids))).expected
const fixture = async (mutations = 10, maxSwitches = 3, autoRecovery = true) => {
const fixture = async (mutations = 10, maxSwitches = 3, autoRecovery = true, unlimitedRates = false) => {
const connectionId = randomUUID(), credentialId = randomUUID(), profileId = randomUUID(), ownerId = "reservation-owner-" + randomUUID()
const now = Date.now(), expiresAt = new Date(now + 600000).toISOString(), generation = await security.accessGeneration()
const permissions = ["mm:network:read", "mm:network:write", "mm:filters:write"]
const policy = mcpConnectionPolicySchema.parse({ id: connectionId, version: 1, status: "active", mode: "approval", permissions, expiresAt,
const policy = mcpConnectionPolicySchema.parse({ id: connectionId, version: 1, status: "active", permissions,
...(unlimitedRates ? { authority: "service", mode: "autonomous", expiresAt: MCP_SERVICE_EXPIRATION, unlimitedRates: true } : { mode: "approval", expiresAt }),
scope: { serverIds: ids, operationIds: ["routing.analyze", "routing.community.switch", "routing.path.verify", "operations.recover"], allowGlobal: false },
limits: { callsPerMinute: 100, mutationsPerHour: mutations, concurrentReads: 4, serversPerPlan: 2, objectsPerPlan: 20 } })
await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'synthetic reservation',$2,$3::jsonb,$4::jsonb,$5,$6,$7)", [connectionId, ownerId, JSON.stringify(permissions), JSON.stringify(policy), generation, expiresAt, new Date(now - 1000)])
@@ -295,6 +297,13 @@ export async function acceptRoutingReservationSql(options: { runtimeAdmission?:
assert.equal(await used(f.connectionId), 2)
names.push("short by one rolling-hour profile slot across source operations")
}
{
const f = await fixture(1, 3, true, true), first = await f.makeSource(), second = await f.makeSource()
assert.equal((await store.reserve(first.operationId)).created, true)
await assert.rejects(store.reserve(second.operationId), /profile switch slots/)
assert.equal(await used(f.connectionId), 2)
names.push("unlimited service connection still obeys the routing profile switch limit")
}
{
const f = await fixture(10, 3), source = await f.makeSource()
await pool.query("UPDATE mcp_routing_profiles SET state=jsonb_build_object('switches',jsonb_build_array(to_char(clock_timestamp() AT TIME ZONE 'UTC','YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"'),to_char(clock_timestamp() AT TIME ZONE 'UTC','YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"'))) WHERE id=$1", [f.profileId])
+4 -4
View File
@@ -31,7 +31,7 @@ import { readPollRuntimePolicySource, type PollRuntimePolicySource } from "./pol
import { withVerifiedConfigurationChange, assertVerifiedConfigurationAccess } from "../../services/config-mutation-policy.js"
import { beginMcpOperationAdmission, insertMcpOperationAdmission, retireMcpOperationAdmission, withMcpOperationExecutionOrigin, stripMcpOperationExecutionPrivateResult, type McpOperationAdmission } from "./operation-execution-origin.js"
import { createOspfOwnerSourceGuard, verifyOspfOwnerGraph, verifyOspfOwnerGraphAtPhase } from "./ospf-owner-graph.js"
import { evaluateMcpPolicy, hasUnattendedPlanAuthority, operationApprovalRequirement } from "./policy.js"
import { evaluateMcpPolicy, hasUnattendedPlanAuthority, hasUnlimitedMcpRates, operationApprovalRequirement } from "./policy.js"
import { findAction, findActionByOperation } from "./actions.js"
import { refuseExpiredOperationReplay } from "./operation-replay-tombstones.js"
import { accessGeneration, decryptSecret, encryptSecret, fingerprint, privateSecretFingerprint, McpError, redact } from "./security.js"
@@ -138,7 +138,7 @@ async function reserveCall(principal: McpPrincipal, toolName: string, input: Rec
const result = evaluateMcpPolicy(evidence)
await client.query("INSERT INTO mcp_requests(id,connection_id,actor_id,tool,server_ids,status,input,error_code) VALUES($1,$2,$3,$4,$5,$6,$7::jsonb,$8)", [requestId, connection.id, connection.owner_id, toolName, evidence.target.scope === "servers" ? evidence.target.serverIds : [], result.decision === "denied" ? "denied" : "running", JSON.stringify(redact(input)), result.decision === "denied" ? result.reasons[0]?.code : null])
if (result.decision !== "denied") {
await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'calls',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+1", [connection.id])
await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'calls',date_trunc('minute',now()),1) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used::bigint+1,2147483647)", [connection.id])
if (action.operation.effect === "read") await client.query("INSERT INTO mcp_read_leases(id,connection_id,expires_at) VALUES($1,$2,now()+interval '2 minutes')", [requestId, connection.id])
await client.query("UPDATE mcp_connections SET last_used_at=now() WHERE id=$1", [connection.id])
}
@@ -461,8 +461,8 @@ async function admitPlan(principal: McpPrincipal, planId: string, idempotencyKey
await assertOriginalMcpTemporaryGrant(client,connection.id,principal.credentialId,originalServiceGrant)
}
const mutationCost = reservedRouting ? 2 : changeMutationCost(evidence) + (evidence.recovery ? 1 : 0)
if (input.usage.mutationsInHour + mutationCost > connection.policy.limits.mutationsPerHour) throw new McpError("BUDGET_EXCEEDED", "Недостаточно бюджета для всех шагов плана", 429)
if (!reservedRouting) await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'mutations',date_trunc('hour',now()),$2) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=mcp_budget_buckets.used+EXCLUDED.used", [connection.id, mutationCost])
if (!hasUnlimitedMcpRates(connection.policy) && input.usage.mutationsInHour + mutationCost > connection.policy.limits.mutationsPerHour) throw new McpError("BUDGET_EXCEEDED", "Недостаточно бюджета для всех шагов плана", 429)
if (!reservedRouting) await client.query("INSERT INTO mcp_budget_buckets(connection_id,kind,bucket,used) VALUES($1,'mutations',date_trunc('hour',now()),$2) ON CONFLICT(connection_id,kind,bucket) DO UPDATE SET used=LEAST(mcp_budget_buckets.used::bigint+EXCLUDED.used,2147483647)", [connection.id, mutationCost])
if (delegated) {
// Execution receipts use the delegating owner's ID. Object protections still require a human decision.
plan.evidence.approvalSource = "connection-policy"
@@ -64,6 +64,7 @@ export async function acceptServerProbeStorageHostSql() {
let raw = ""; for await (const chunk of request) raw += chunk
const body = JSON.parse(raw) as Record<string, unknown>
assert.equal(body.address, "127.0.0.1"); assert.equal(body.count, "1")
assert.equal(Object.hasOwn(body, "src-address"), false)
if (requests === 2) {
response.once("close", secondClosed)
secondValidated()
@@ -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}
+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 }
}
@@ -103,9 +103,27 @@ for (const type of ["A", "AAAA", "MX", "NS", "TXT", "CNAME", "PTR"]) {
{
const f = fixture(); f.raw([]); await failure(f.executor.execute(await f.executor.prepare(1, request("mtu"))), "MEASUREMENT_UNKNOWN"); assert.equal(f.counts().sends, 1)
}
for (const host of ["192.0.2.1", "router.example"]) {
const f = fixture()
f.source.host = host
f.raw(["192.0.2.1"])
const post = f.deps.post
f.deps.post = async (...args) => {
const source = args[2]["src-address"] ?? "10.205.1.2"
f.raw(source === "10.205.1.2"
? [{ seq: "0", ttl: "64", time: "25ms", sent: "1", received: "1", "packet-loss": "0" }]
: [{ seq: "0", status: "timeout", sent: "1", received: "0", "packet-loss": "100" }])
return post(...args)
}
const prepared = await f.executor.prepare(1, request("ping", { target: "10.205.1.1", pingCount: 1 }))
const result = await f.executor.execute(prepared)
assert.match(result.output!, /sent=1 received=1 packet-loss=0/)
assert.deepEqual(f.counts(), { sends: 1, dnsCalls: 0 })
cases++
}
{
const f = fixture(); f.source.host = "router.example"; f.sourceFailure()
const prepared = await f.executor.prepare(1, request("ping")), result = await f.executor.execute(prepared)
const prepared = await f.executor.prepare(1, request("traceroute")), result = await f.executor.execute(prepared)
assert.equal(result.state, "completed"); assert.equal(f.counts().dnsCalls, 1); assert.equal(f.counts().sends, 1)
assert.equal((f.events.find(event => event.kind === "native")!.value as { body: Record<string, string> }).body["src-address"], undefined)
assert.equal(f.events.filter(event => event.kind === "received").length, 2); cases++
@@ -137,7 +155,7 @@ for (const kind of ["claim", "intent", "receipt", "received"] as const) {
}
{
const f = fixture(); f.source.host = "router.example"; f.raw(["192.0.2.1"])
const prepared = await f.executor.prepare(1, request("ping")); f.lost("received")
const prepared = await f.executor.prepare(1, request("traceroute")); f.lost("received")
await failure(f.executor.execute(prepared), "JOURNAL_UNKNOWN"); assert.equal(f.counts().dnsCalls, 1); assert.equal(f.counts().sends, 0); assert.equal(f.events.filter(event => event.kind === "received").length, 1)
}
{
@@ -232,7 +232,8 @@ export function createServerProbeExecutor(deps: ServerProbeExecutionDependencies
}
claimed = true
let sourceIpv4: string | null = null
if (["ping", "traceroute", "mtu"].includes(plan.request.tool)) {
// Ping must let RouterOS choose the source for the route, including WireGuard.
if (plan.request.tool === "traceroute" || plan.request.tool === "mtu") {
const host = currentSource!.host.trim()
if (isIPv4(host)) sourceIpv4 = host
else {
@@ -118,6 +118,20 @@ try {
assert.ok(filtered.bytes >= 12_000)
assert.equal(filtered.ifaces[0]?.name, "ether1")
const egress = await buildFlowAnalytics({ minutes: 5, serverId, iface: "ether2" })
assert.equal(egress.bytes, 12_000)
assert.deepEqual(egress.interfaces.map(row => [row.id, row.bytes]), [["ether2", 12_000]])
assert.equal(egress.conversationsList[0]?.accountedIface, "ether2")
assert.equal(egress.paths?.[0]?.ifaces, "ether1, ether2")
assert.equal(egress.conversationsList[0]?.dstCountry, "unknown")
assert.equal(egress.mapEdges?.[0]?.toCountry, "unknown")
for (const rows of [egress.applications, egress.categories, egress.services, egress.asns, egress.countries,
egress.mapEdges, egress.protocols, egress.sources, egress.destinations, egress.paths, egress.conversationsList, egress.interfaces]) {
assert.equal(rows?.reduce((sum, row) => sum + row.bytes, 0), egress.bytes)
}
assert.equal(egress.asns?.[0]?.id, "unknown")
assert.equal(egress.countries?.[0]?.percent, 100)
const miss = await buildFlowAnalytics({ minutes: 5, serverId, iface: "wg-flow" })
assert.equal(miss.conversations, 0)
@@ -175,7 +189,7 @@ try {
assert.equal(deduped.bytes, 12_000)
assert.equal(deduped.conversations, 1)
assert.equal(deduped.dedupApplied, true)
assert.equal(deduped.interfaces.length, 2)
assert.deepEqual(deduped.interfaces.map(row => [row.id, row.bytes]), [["ether1", 12_000]])
} finally {
resetFlowRingsForTests()
resetIfaceCacheForTests()
@@ -287,6 +301,7 @@ try {
])
const degraded = await buildFlowAnalytics({ minutes: 5, serverId, skipHeavy: true })
assert.equal(degraded.degraded, true)
assert.equal(degraded.conversations, 1)
assert.equal(degraded.conversationsList.length, 0)
assert.ok((degraded.bytes ?? 0) >= 12_000)
const exporters = await listFlowExporters(5)
+57 -63
View File
@@ -49,7 +49,7 @@ export interface FlowAnalyticsQuery {
serverId?: number
userId?: string
iface?: string
/** Default true: один 5-tuple = max байт по ifaces. */
/** Default true: один 5-tuple за минуту = max байт по ifaces. */
dedup?: boolean
/** Default true: скрыть GRE/WG между клиентами JH. */
excludeMesh?: boolean
@@ -305,25 +305,25 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
else if (plane === "client_mesh") bytesMesh += r.bytes
if (!shouldKeepPlane(plane, { excludeMesh, excludeOverlay })) continue
matched.push(r)
const ifaceKey = resolved.name
const prevIf = ifacesMap.get(ifaceKey) ?? { bytes: 0, packets: 0, index: resolved.index }
prevIf.bytes += r.bytes
prevIf.packets += r.packets
ifacesMap.set(ifaceKey, prevIf)
}
const working = wantDedup ? dedupFlowRowsMaxBytes(matched) : matched
const conversationsRaw = new Set(matched.map((r) => `${flowTupleKey(r)}|${r.inIface}`)).size
const conversations = wantDedup ? new Set(working.map(flowTupleKey)).size : conversationsRaw
let totalBytes = 0
let totalPackets = 0
for (const r of working) {
const resolved = resolveIfaceName(r.serverId, r.inIface)
const outResolved = resolveIfaceName(r.serverId, r.outIface)
const ingressAccounts = ifaceFilter || allow ? matchesIngress(r) : resolved.name !== "—"
const accounted = ingressAccounts ? resolved : outResolved
const ifaceTotal = ifacesMap.get(accounted.name) ?? { bytes: 0, packets: 0, index: accounted.index }
ifaceTotal.bytes += r.bytes
ifaceTotal.packets += r.packets
ifacesMap.set(accounted.name, ifaceTotal)
totalBytes += r.bytes
totalPackets += r.packets
srcs.add(r.src)
dsts.add(r.dst)
const destMeta = resolveInternetDest({
src: r.src,
dst: r.dst,
@@ -332,6 +332,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
dstPort: r.dstPort,
serverId: r.serverId,
inIface: resolved.name,
outIface: outResolved.name,
topo,
natSrc: r.natSrc,
natDst: r.natDst,
@@ -339,28 +340,27 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
natDstPort: r.natDstPort,
})
const ep = destMeta.endpoints
const src = ep.packetSrc || r.src
const dst = ep.packetDst || r.dst
srcs.add(src)
dsts.add(dst)
if (destMeta.dest) peers.add(destMeta.dest)
const app = applicationName(r.proto, ep.peerPort || r.dstPort, ep.otherPort || r.srcPort)
const ripe = destMeta.ripe
const classified = destMeta.classified
bump(applications, app, r.bytes, r.packets)
bump(protocols, protoName(r.proto), r.bytes, r.packets)
bump(sources, r.src, r.bytes, r.packets)
bump(destinations, r.dst, r.bytes, r.packets)
bump(sources, src, r.bytes, r.packets)
bump(destinations, dst, r.bytes, r.packets)
bump(categories, classified.category, r.bytes, r.packets)
bump(services, classified.service, r.bytes, r.packets)
if (ripe?.ok && ripe.asn) {
const asnId = String(ripe.asn)
const asnLabel = ripe.holder ? `AS${ripe.asn} ${ripe.holder}` : `AS${ripe.asn}`
bump(asns, asnId, r.bytes, r.packets, asnLabel)
}
const dstCountry = destMeta.country && destMeta.country !== "unknown" ? destMeta.country : ""
if (dstCountry) {
bump(countries, dstCountry, r.bytes, r.packets)
}
const asnId = destMeta.asn ? String(destMeta.asn) : "unknown"
const asnLabel = destMeta.asn ? `AS${destMeta.asn}${ripe?.holder ? ` ${ripe.holder}` : ""}` : "ASN неизвестен"
bump(asns, asnId, r.bytes, r.packets, asnLabel)
const dstCountry = destMeta.country || "unknown"
bump(countries, dstCountry, r.bytes, r.packets, dstCountry === "unknown" ? "Страна неизвестна" : undefined)
if (!skipHeavy) {
const outResolved = resolveIfaceName(r.serverId, r.outIface)
const plane = classifyFlowPlane({
src: r.src,
dst: r.dst,
@@ -370,11 +370,13 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
inIface: resolved.name,
outIface: outResolved.name,
}, topo.plane)
const client = resolveClient(topo, r.serverId, resolved.name)
const client = resolveClient(topo, r.serverId, resolved.name) ?? resolveClient(topo, r.serverId, outResolved.name)
const en = resolveEn(topo, r.nextHop, outResolved.name)
const ckey = wantDedup
? flowTupleKey(r)
: `${flowTupleKey(r)}|${r.inIface}`
const outIface = outResolved.name === "—" ? "" : outResolved.name
const pathKey = JSON.stringify([client?.userId ?? "unknown", r.serverId, en?.id ?? "", dst, resolved.name, outIface,
classified.service, classified.category, plane])
const ckey = JSON.stringify([flowTupleKey(r), pathKey, accounted.name, app, asnId, dstCountry, ep.internetPeer,
ep.clientIp, ep.peerPort, ep.direction, r.nextHop])
const prev = conv.get(ckey)
if (prev) {
prev.rawBytes += r.bytes
@@ -386,8 +388,8 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
conv.set(ckey, {
serverId: String(r.serverId),
serverName: nameById.get(r.serverId) ?? String(r.serverId),
src: ep.packetSrc || r.src,
dst: ep.packetDst || r.dst,
src,
dst,
proto: r.proto,
protoName: protoName(r.proto),
srcPort: r.srcPort,
@@ -397,13 +399,14 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
bps: 0,
inIface: resolved.name,
inIfaceIndex: resolved.index,
outIface: outResolved.name !== "—" ? outResolved.name : undefined,
outIface: outIface || undefined,
accountedIface: accounted.name,
nextHop: r.nextHop || undefined,
application: app,
category: classified.category,
service: classified.service,
dstCountry: dstCountry || undefined,
dstAsn: ripe?.asn || undefined,
dstCountry,
dstAsn: destMeta.asn || undefined,
clientId: client?.userId,
clientName: client?.name,
clientIp: ep.clientIp || undefined,
@@ -419,7 +422,6 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
})
}
const pathKey = `${client?.userId || "unknown"}|${r.serverId}|${en?.id || ""}|${r.dst}|${resolved.name}`
const pathPrev = pathAcc.get(pathKey)
if (pathPrev) {
pathPrev.bytes += r.bytes
@@ -429,14 +431,14 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
id: pathKey,
clientId: client?.userId || "unknown",
clientName: client?.name || "Неизвестный клиент",
ifaces: client ? [...(topo.clientIfaces.get(r.serverId) ?? [resolved.name])].join(", ") : resolved.name,
ifaces: [...new Set([resolved.name, outIface].filter(name => name && name !== "—"))].join(", ") || "—",
serverId: String(r.serverId),
serverName: nameById.get(r.serverId) ?? String(r.serverId),
inIface: resolved.name,
outIface: outResolved.name !== "—" ? outResolved.name : "",
outIface,
enId: en ? String(en.id) : "",
enName: en?.name || "",
dst: r.dst,
dst,
service: classified.service,
category: classified.category,
plane,
@@ -446,29 +448,25 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
})
}
const toCountry = dstCountry
if (toCountry) {
const fromCountry = countryById.get(r.serverId) || "UN"
const ekey = `${r.serverId}|${toCountry}`
let edge = edgeAcc.get(ekey)
if (!edge) {
edge = {
fromId: String(r.serverId),
fromLabel: nameById.get(r.serverId) ?? String(r.serverId),
fromCountry,
toCountry,
toAsn: ripe?.asn ?? 0,
category: classified.category,
bytes: 0,
bps: 0,
catBytes: new Map(),
}
edgeAcc.set(ekey, edge)
const ekey = `${r.serverId}|${dstCountry}`
let edge = edgeAcc.get(ekey)
if (!edge) {
edge = {
fromId: String(r.serverId),
fromLabel: nameById.get(r.serverId) ?? String(r.serverId),
fromCountry: countryById.get(r.serverId) || "UN",
toCountry: dstCountry,
toAsn: destMeta.asn,
category: classified.category,
bytes: 0,
bps: 0,
catBytes: new Map(),
}
edge.bytes += r.bytes
if (ripe?.asn) edge.toAsn = ripe.asn
edge.catBytes.set(classified.category, (edge.catBytes.get(classified.category) ?? 0) + r.bytes)
edgeAcc.set(ekey, edge)
}
edge.bytes += r.bytes
if (edge.toAsn !== destMeta.asn) edge.toAsn = 0
edge.catBytes.set(classified.category, (edge.catBytes.get(classified.category) ?? 0) + r.bytes)
}
}
@@ -494,10 +492,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
const ring = ringServer && !ifaceFilter && !allow && excludeOverlay ? getRingMbps(ringServer) : null
// The live ring covers about one minute; historical charts must use the requested window.
// Keep the winning interface's individual minute buckets when deduplicating.
const chosenIfaces = wantDedup ? new Map(working.map(row => [flowTupleKey(row), row.inIface])) : null
const chartRows = chosenIfaces ? matched.filter(r => chosenIfaces.get(flowTupleKey(r)) === r.inIface) : matched
const fromBuckets = seriesFromRows(chartRows, q.minutes, row => {
const fromBuckets = seriesFromRows(working, q.minutes, row => {
if (ifaceFilter || allow) {
const ingress = matchesIngress(row), egress = matchesEgress(row)
if (ingress && egress) {
@@ -521,7 +516,6 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
bps: (v.bytes * 8) / windowSec,
}))
const ifaceRawBytes = [...ifacesMap.values()].reduce((a, v) => a + v.bytes, 0) || 1
const listener = getFlowListenerState()
const mapEdges: FlowMapEdge[] = [...edgeAcc.values()]
.map((e) => {
@@ -548,7 +542,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
.slice(0, top)
// Filtered and overlay-inclusive rates must use the same records as their totals.
const recentBytes = ring ? 0 : chartRows.reduce((sum, row) => sum + (row.bucketAt === previousMinute ? row.bytes : 0), 0)
const recentBytes = ring ? 0 : working.reduce((sum, row) => sum + (row.bucketAt === previousMinute ? row.bytes : 0), 0)
const bpsNow = ring
? (!fromBuckets.undirectedBytes && (ring.rxNow + ring.txNow) * 1_000_000) || (totalBytes * 8) / windowSec
: (recentBytes * 8) / 60 || (totalBytes * 8) / windowSec
@@ -570,7 +564,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
bpsNow,
bytes: totalBytes,
packets: totalPackets,
conversations: conv.size,
conversations,
conversationsRaw,
uniqueSrc: srcs.size,
uniqueDst: dsts.size,
@@ -588,7 +582,7 @@ async function buildFlowAnalyticsUncached(q: FlowAnalyticsQuery, scope?: ScopedF
bytes: v.bytes,
packets: v.packets,
bps: (v.bytes * 8) / windowSec,
percent: (v.bytes / ifaceRawBytes) * 100,
percent: (v.bytes / (totalBytes || 1)) * 100,
})).sort((a, b) => b.bytes - a.bytes),
asns: topN(asns, windowSec, top),
countries: topN(countries, windowSec, top),
@@ -39,6 +39,15 @@ assert.equal(across[0]?.bytes, 9_000)
const minute = "2026-11-22T06:26:00.000Z"
const nextMinute = "2026-11-22T06:27:00.000Z"
const beforeReroute = { ...a, bucketAt: minute }
const afterReroute = { ...b, bucketAt: nextMinute }
assert.deepEqual(dedupFlowRowsMaxBytes([beforeReroute, { ...b, bucketAt: minute }, afterReroute]), [
beforeReroute, afterReroute,
])
assert.deepEqual(dedupFlowRowsAcrossExporters(
[{ ...en, bucketAt: minute }, beforeReroute, afterReroute],
(x, y) => x.serverId === a.serverId ? x : y,
), [beforeReroute, afterReroute])
const gre = { ...a, bucketAt: minute, proto: 47, srcPort: 0, dstPort: 0 }
const esp = { ...gre, proto: 50, bytes: gre.bytes + gre.packets * 40, inIface: "10" }
const plain = { ...gre, dst: "198.51.100.3" }
+9 -10
View File
@@ -8,6 +8,7 @@ export interface FlowTupleRow {
srcPort: number
dstPort: number
inIface: string
bucketAt?: string
bytes: number
packets: number
}
@@ -31,7 +32,7 @@ export function dedupFlowRowsAcrossExporters<T extends FlowTupleRow>(
): T[] {
const byConv = new Map<string, T>()
for (const row of rows) {
const key = flowConversationKey(row)
const key = `${row.bucketAt ?? ""}|${flowConversationKey(row)}`
const prev = byConv.get(key)
if (!prev) {
byConv.set(key, row)
@@ -42,29 +43,27 @@ export function dedupFlowRowsAcrossExporters<T extends FlowTupleRow>(
return [...byConv.values()]
}
function ifaceKey(r: FlowTupleRow): string {
return `${flowTupleKey(r)}|${r.inIface}`
function bucketTupleKey(r: FlowTupleRow): string {
return `${r.bucketAt ?? ""}|${flowTupleKey(r)}`
}
/**
* Один 5-tuple на двух ifIndex — это один поток: сначала сумма по бакетам/iface,
* затем max байт между интерфейсами (не sum).
* Сравниваем копии 5-tuple в одном бакете: между бакетами интерфейс может меняться.
*/
export function dedupFlowRowsMaxBytes<T extends FlowTupleRow>(rows: T[]): T[] {
const byIface = new Map<string, T>()
for (const row of rows) {
const key = ifaceKey(row)
const key = `${bucketTupleKey(row)}|${row.inIface}`
const prev = byIface.get(key)
if (!prev) {
byIface.set(key, { ...row })
byIface.set(key, row)
continue
}
prev.bytes += row.bytes
prev.packets += row.packets
byIface.set(key, { ...prev, bytes: prev.bytes + row.bytes, packets: prev.packets + row.packets })
}
const byTuple = new Map<string, T>()
for (const row of byIface.values()) {
const key = flowTupleKey(row)
const key = bucketTupleKey(row)
const prev = byTuple.get(key)
if (!prev || row.bytes > prev.bytes) byTuple.set(key, row)
}
+6 -5
View File
@@ -53,17 +53,16 @@ export function destCtxForIface(
topo: FlowTopology | null | undefined,
serverId: number,
inIface: string,
nat?: Pick<InternetDestCtx, "natSrc" | "natDst" | "natSrcPort" | "natDstPort">,
nat?: Pick<InternetDestCtx, "natSrc" | "natDst" | "natSrcPort" | "natDstPort"> & { outIface?: string },
resolveIface=canonicalFactIface,
): InternetDestCtx {
const name = resolveIface(serverId, inIface) || String(inIface ?? "").trim()
const outName = nat?.outIface ? resolveIface(serverId, nat.outIface) : ""
return {
ours: flowOursHosts(topo),
boundClient: Boolean(
topo && name && (
resolveClient(topo, serverId, name)
|| topo.clientIfaces.get(serverId)?.has(name)
),
topo && [name, outName].some(iface => iface &&
(resolveClient(topo, serverId, iface) || topo.clientIfaces.get(serverId)?.has(iface))),
),
natSrc: nat?.natSrc,
natDst: nat?.natDst,
@@ -117,6 +116,7 @@ export function resolveInternetDest(opts: {
dstPort: number
serverId: number
inIface: string
outIface?: string
topo?: FlowTopology | null
natSrc?: string
natDst?: string
@@ -127,6 +127,7 @@ export function resolveInternetDest(opts: {
classify?:typeof classifyFlowDst
}): InternetDestMeta {
const ctx = destCtxForIface(opts.topo, opts.serverId, opts.inIface, {
outIface: opts.outIface,
natSrc: opts.natSrc,
natDst: opts.natDst,
natSrcPort: opts.natSrcPort,
+1 -1
View File
@@ -434,7 +434,7 @@ export function queueParsedFlows(serverId: number, flows: ParsedFlowInput[]): vo
for (const raw of flows) {
const flow = normalizeParsedFlow(raw)
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})
serverId,inIface:flow.inIface,outIface:flow.outIface,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
@@ -115,6 +115,7 @@ export function shouldWriteFlowFact(opts: {
opts.srcPort,
opts.dstPort,
destCtxForIface(opts.topo, opts.serverId, inName, {
outIface: outName,
natSrc: opts.natSrc,
natDst: opts.natDst,
natSrcPort: opts.natSrcPort,
@@ -296,6 +296,8 @@ try {
resetFlowMapHopsCacheForTests()
const six = await buildFlowMapHops({ minutes: 5, minSharePct: 5 })
assert.equal(six.totalBytes, 10_000)
const byEgress = await buildFlowMapHops({ minutes: 5, serverId: 7, iface: "gre-jh-en", minSharePct: 5 })
assert.equal(byEgress.totalBytes, six.totalBytes)
assert.equal(six.namedBytes, 600)
assert.equal(six.unclassifiedBytes, 9400)
const google = six.services?.find((s) => s.id === "svc:google")
@@ -317,6 +319,11 @@ try {
const usPath = six.countryPaths?.find((p) => p.serviceId === "cc:us" && p.enId === "9")
assert.ok(usPath)
assert.equal(usPath.clientName, "Alice")
ingestParsedFlowsForServerForTests(7, [{ ...payloadFlow("8.8.8.8", 2000), proto: 17, dstPort: 51820 }])
const mixedPeer = await buildFlowMapHops({ minutes: 5, serverId: 7, excludeOverlay: false })
assert.equal(mixedPeer.totalBytes, 12_000)
assert.equal(mixedPeer.namedBytes, 600, "WireGuard to the same peer is separate from the HTTPS service")
} finally {
resetFlowRingsForTests()
resetIfaceCacheForTests()
+27 -12
View File
@@ -8,11 +8,11 @@ import { flowRowMatchesFilter } from "./traffic-flow-apps.js"
import { OTHER_SERVICE, isNamedInternetService, mapCountryNodeId, mapCountryServiceNodeId, mapServiceNodeId, resolveRipeCountry } from "./traffic-flow-brands.js"
import type { FlowIpMeta } from "./traffic-flow-ripe.js"
import { refreshFlowCatalogInBackground } from "./traffic-flow-classify.js"
import { dedupFlowRowsAcrossExporters, dedupFlowRowsMaxBytes } from "./traffic-flow-dedup.js"
import { collapseGreEspRows, dedupFlowRowsAcrossExporters, dedupFlowRowsMaxBytes } from "./traffic-flow-dedup.js"
import { getFlowListenerState, listFlowRowsForWindow } from "./traffic-flow-ingest.js"
import { resolveIfaceName } from "./traffic-flow-ifaces.js"
import { classifyFlowPlane, shouldKeepPlane } from "./traffic-flow-planes.js"
import { destCtxForIface, mapInternetBrand } from "./traffic-flow-dest.js"
import { destCtxForIface, flowRateDirection, mapInternetBrand } from "./traffic-flow-dest.js"
import { resolveFlowEndpoints } from "./traffic-flow-ip.js"
import { geoipReadersStatus, resolveFlowIp } from "./traffic-flow-geoip.js"
import { getTrafficFlowSettingsRow } from "./traffic-flow-settings.js"
@@ -65,6 +65,7 @@ interface FromAcc {
}
interface DstAcc {
dst: string
bytes: number
proto: number
dstPort: number
@@ -316,7 +317,7 @@ function buildCountryServiceGroups(
}
function countryDestFromRipe(ripe: FlowIpMeta | null, destKey: string): { id: string; label: string; category: string } {
if (!destKey || destKey === "__other__" || !ripe?.ok) {
if (!destKey || !ripe?.ok) {
return { id: mapCountryNodeId(""), label: OTHER_SERVICE, category: MAP_COUNTRY_CATEGORY }
}
const iso = resolveRipeCountry(ripe.country, ripe.asn, ripe.holder)
@@ -440,11 +441,20 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number
}
}
const matches = (serverId: number, iface: string) =>
flowRowMatchesFilter({ serverId, inIface: iface }, resolveIfaceName(serverId, iface).name, q, allow)
const selected = raw.filter(r => matches(r.serverId, r.inIface) || matches(r.serverId, r.outIface))
const { rows } = collapseGreEspRows(selected, r => {
if (ifaceFilter) {
const ingress = matches(r.serverId, r.inIface), egress = matches(r.serverId, r.outIface)
return ingress === egress ? null : ingress ? "rx" : "tx"
}
return flowRateDirection(r.serverId, r, topo, catalog.byId.get(r.serverId)?.host)
})
const matched = []
for (const r of raw) {
for (const r of rows) {
const resolved = resolveIfaceName(r.serverId, r.inIface)
const outResolved = resolveIfaceName(r.serverId, r.outIface)
if (!flowRowMatchesFilter(r, resolved.name, q, allow)) continue
const plane = classifyFlowPlane({
src: r.src,
dst: r.dst,
@@ -578,14 +588,17 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number
srcPort: r.srcPort,
dstPort: r.dstPort,
ctx: destCtxForIface(topo, r.serverId, inName, {
outIface: outName,
natSrc: r.natSrc,
natDst: r.natDst,
natSrcPort: r.natSrcPort,
natDstPort: r.natDstPort,
}),
})
const dest = ep.internetPeer
const destKey = dest || "__other__"
const dst = ep.internetPeer
const dstPort = ep.peerPort || r.dstPort
const srcPort = ep.otherPort || r.srcPort
const destKey = dst ? JSON.stringify([dst, r.proto, dstPort, srcPort]) : ""
const client = resolveMapClient(topo, r.serverId, inName, outName)
const prevDst = dstAcc.get(destKey)
if (prevDst) {
@@ -593,10 +606,11 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number
bumpFrom(prevDst, String(r.serverId), r.bytes, client)
} else {
const acc: DstAcc = {
dst,
bytes: r.bytes,
proto: r.proto,
dstPort: ep.peerPort || r.dstPort,
srcPort: ep.otherPort || r.srcPort,
dstPort,
srcPort,
fromBytes: new Map(),
}
bumpFrom(acc, String(r.serverId), r.bytes, client)
@@ -644,9 +658,10 @@ async function buildFlowMapHopsUncached(q: FlowMapHopsQuery, minSharePct: number
return id
}
for (const [dst, acc] of dstAcc) {
const ripe = dst && dst !== "__other__" ? resolveFlowIp(dst) : null
const classified = dst && dst !== "__other__"
for (const acc of dstAcc.values()) {
const { dst } = acc
const ripe = dst ? resolveFlowIp(dst) : null
const classified = dst
? mapInternetBrand(dst, acc.proto, acc.dstPort, acc.srcPort, ripe)
: { service: OTHER_SERVICE, category: OTHER_SERVICE }
const svcId = mapServiceNodeId(classified.service)
@@ -195,6 +195,12 @@ try{
assert.equal(analytics.bytesOverlay,40200,'Plain GRE and undirected ESP remain part of the volume')
assert.equal(fromSeries(analytics.rxSeries),25900)
assert.equal(fromSeries(analytics.txSeries),12400)
assert.deepEqual(analytics.interfaces.map(row=>[row.id,row.bytes]),[['ether1',40300]])
assert.ok(analytics.conversationsList.every(row=>row.accountedIface==='ether1'))
for(const rows of [analytics.applications,analytics.categories,analytics.services,analytics.asns,analytics.countries,
analytics.mapEdges,analytics.protocols,analytics.sources,analytics.destinations,analytics.paths,analytics.conversationsList]){
assert.equal(rows?.reduce((sum,row)=>sum+row.bytes,0),analytics.bytes)
}
}
assert.equal(sample.bpsOverlay,40200*8/300)
}finally{
@@ -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}
+12 -5
View File
@@ -6,7 +6,7 @@ import Link from "next/link"
import { useTable, type ColumnDef } from "@tanstack/react-table"
import { BotIcon, CheckIcon, CopyIcon, PlusIcon, RefreshCwIcon, ServerIcon, ShieldIcon, StopCircleIcon } from "lucide-react"
import { toast } from "sonner"
import { mcpConnectionCreateSchema, mcpPageQuerySchema, mcpTargetSchema, type McpConnection, type McpOperationDescriptor, type McpPolicyDecision } from "@mmapp/contracts/mcp"
import { mcpConnectionCreateSchema, mcpLimitsSchema, mcpPageQuerySchema, mcpTargetSchema, type McpConnection, type McpOperationDescriptor, type McpPolicyDecision } from "@mmapp/contracts/mcp"
import { routeAiControllerBlockReason, routeAiProfileSchema, type RouteAiProfile } from "@mmapp/contracts/mcp-routing"
import { useDataSource } from "@/lib/data-source"
import { can, getClaims, getToken, hasPermission } from "@/lib/auth"
@@ -239,13 +239,15 @@ function ConnectionForm({ api, operations, editing, close, created }: { api: Mcp
const [name, setName] = useState(editing?.name ?? ""), [mode, setMode] = useState<McpConnection["policy"]["mode"]>(editing?.policy.mode ?? "observer")
const [service, setService] = useState(editing?.policy.authority === "service")
const [unattended, setUnattended] = useState(editing?.policy.allowUnattendedPlans === true)
const [unlimitedRates, setUnlimitedRates] = useState(editing?.policy.unlimitedRates === true)
const ratesDisabled = service && unlimitedRates
const [selectAllServers, setSelectAllServers] = useState(false)
const [serverIds, setServerIds] = useState(editing?.policy.scope.serverIds ?? []), [selected, setSelected] = useState<string[]>(editing?.policy.scope.operationIds ?? [])
const serverCatalog = useQuery(useCallback(signal => api.serverChoices(undefined, signal), [api]))
const [global, setGlobal] = useState(editing?.policy.scope.allowGlobal ?? false), [danger, setDanger] = useState(editing?.policy.dangerousPolicy ?? "approval")
const [dangerIds, setDangerIds] = useState(editing?.policy.autonomousDangerousOperationIds ?? [])
const [expires, setExpires] = useState(() => editing?.policy.expiresAt.slice(0, 16) ?? new Date(Date.now() + 30 * 86400000).toISOString().slice(0, 16))
const [limits, setLimits] = useState(editing?.policy.limits ?? { callsPerMinute: 60, concurrentReads: 4, mutationsPerHour: 10, serversPerPlan: 5, objectsPerPlan: 100 })
const [limits, setLimits] = useState(() => editing?.policy.limits ?? mcpLimitsSchema.parse({}))
const permissions = getClaims()?.permissions ?? []
const eligible = operations.filter(operation => operation.implementation === "available" && operation.requiredPermissions.every(permission => hasPermission(permissions, permission)))
const selectedIds = new Set(selected)
@@ -254,7 +256,9 @@ function ConnectionForm({ api, operations, editing, close, created }: { api: Mcp
const save = async () => {
setBusy(true); setError("")
try {
const input = mcpConnectionCreateSchema.parse({ name, ...(service ? { authority: "service", mode: "autonomous", expiresAt: null, ...(unattended && { allowUnattendedPlans: true }) } : { mode, expiresAt: new Date(expires + "Z").toISOString() }), permissions: [...new Set(eligible.filter(operation => selected.includes(operation.id)).flatMap(operation => operation.requiredPermissions))], ...(selectAllServers && { selectAllServers: true }), scope: { serverIds: selectAllServers ? [] : serverIds, operationIds: selected, allowGlobal: global }, dangerousPolicy: service ? "approval" : danger, autonomousDangerousOperationIds: service || danger !== "explicit-allow" ? [] : autonomousDangerIds, limits })
const previousLimits = editing?.policy.limits ?? mcpLimitsSchema.parse({})
const savedLimits = ratesDisabled ? { ...previousLimits, serversPerPlan: limits.serversPerPlan, objectsPerPlan: limits.objectsPerPlan } : limits
const input = mcpConnectionCreateSchema.parse({ name, ...(service ? { authority: "service", mode: "autonomous", expiresAt: null, ...(unattended && { allowUnattendedPlans: true }), ...(ratesDisabled && { unlimitedRates: true }) } : { mode, expiresAt: new Date(expires + "Z").toISOString() }), permissions: [...new Set(eligible.filter(operation => selected.includes(operation.id)).flatMap(operation => operation.requiredPermissions))], ...(selectAllServers && { selectAllServers: true }), scope: { serverIds: selectAllServers ? [] : serverIds, operationIds: selected, allowGlobal: global }, dangerousPolicy: service ? "approval" : danger, autonomousDangerousOperationIds: service || danger !== "explicit-allow" ? [] : autonomousDangerIds, limits: savedLimits })
if (editing) { await api.update(editing.id, input); created() }
else { const result = await api.create(input); created(result.token) }
} catch (failure) { setError(errorText(failure)) } finally { setBusy(false) }
@@ -270,14 +274,17 @@ function ConnectionForm({ api, operations, editing, close, created }: { api: Mcp
<OperationChoices operations={eligible} selected={selected} onChange={setSelected} />
<p className="text-xs text-muted-foreground">Выбор операций учитывает права владельца. Режим, список серверов и глобальный доступ задаются отдельно. Новые инструменты нужно добавлять вручную. Для применения планов выберите также инструменты просмотра и применения.</p>
</>}
{step === 3 && <><div className="grid gap-3 sm:grid-cols-2">{Object.entries(limits).map(([key, value]) => <Field key={key} label={({ callsPerMinute: "Вызовов в минуту", concurrentReads: "Одновременных чтений", mutationsPerHour: "Изменений в час", serversPerPlan: "Серверов на план", objectsPerPlan: "Объектов на план" } as Record<string, string>)[key]}><Input type="number" min={1} value={value} onChange={event => setLimits(current => ({ ...current, [key]: Number(event.target.value) }))} /></Field>)}</div>{service ? <>
{step === 3 && <>
{service && <Choice label="Отключить квоты частоты для этого ключа" checked={unlimitedRates} onChange={setUnlimitedRates} />}
{ratesDisabled && <p className="text-sm text-muted-foreground">Вызовы, одновременные чтения и изменения в час без квот подключения. Общая ёмкость сервера и размер плана ограничены.</p>}
<div className="grid gap-3 sm:grid-cols-2">{Object.entries(limits).filter(([key]) => !ratesDisabled || key === "serversPerPlan" || key === "objectsPerPlan").map(([key, value]) => <Field key={key} label={({ callsPerMinute: "Вызовов в минуту", concurrentReads: "Одновременных чтений", mutationsPerHour: "Изменений в час", serversPerPlan: "Серверов на план", objectsPerPlan: "Объектов на план" } as Record<string, string>)[key]}><Input type="number" min={1} value={value} onChange={event => setLimits(current => ({ ...current, [key]: Number(event.target.value) }))} /></Field>)}</div>{service ? <>
<Choice label="Выполнять планы без ручного подтверждения" checked={unattended} onChange={setUnattended} />
<p className="text-sm text-muted-foreground">Действует для всех операций, выданных ключу. Клиент должен сначала показать точный план в чате. Сервер проверяет контрольную сумму плана, а публикацию сообщения обеспечивает клиент. Защита объектов сохраняется.</p>
</> : <><NativeSelect label="Опасные действия" value={danger} onChange={value => setDanger(value as typeof danger)}><option value="approval">Всегда подтверждать</option><option value="explicit-allow">Автономно для явно выбранных действий</option></NativeSelect>{danger === "explicit-allow" && <>
<p className="text-sm text-muted-foreground">В автономном режиме выбранные здесь опасные операции смогут выполняться без подтверждения, если этого не требует защита объекта или сама операция.</p>
<OperationChoices operations={dangerous} selected={autonomousDangerIds} onChange={setDangerIds} selectAllLabel="Выбрать все опасные операции" />
</>}</>}</>}
{step === 4 && <div className="space-y-3 text-sm"><p><strong>{name || "Без названия"}</strong> · {service ? "Сервисное, бессрочное" : modeLabels[mode]}</p><p>Серверы: {selectAllServers ? "все на момент сохранения" : serverIds.join(", ") || "не выбраны"}. Операций: {selected.length}. Глобальный доступ: {global ? "да" : "нет"}.</p>{service && unattended && <p>Ручное подтверждение планов отключено. Клиент публикует план в чате перед применением.</p>}{!service && mode === "autonomous" && danger === "explicit-allow" && <p>Опасных операций выбрано для автономного выполнения: {autonomousDangerIds.length}.</p>}<p>Защита объектов продолжает действовать независимо от автономных разрешений.</p><p>После сохранения проверьте конкретное действие в симуляторе на вкладке «Разрешения».</p></div>}
{step === 4 && <div className="space-y-3 text-sm"><p><strong>{name || "Без названия"}</strong> · {service ? "Сервисное, бессрочное" : modeLabels[mode]}</p><p>Серверы: {selectAllServers ? "все на момент сохранения" : serverIds.join(", ") || "не выбраны"}. Операций: {selected.length}. Глобальный доступ: {global ? "да" : "нет"}.</p>{ratesDisabled && <p>Квоты частоты для этого ключа отключены.</p>}{service && unattended && <p>Ручное подтверждение планов отключено. Клиент публикует план в чате перед применением.</p>}{!service && mode === "autonomous" && danger === "explicit-allow" && <p>Опасных операций выбрано для автономного выполнения: {autonomousDangerIds.length}.</p>}<p>Защита объектов продолжает действовать независимо от автономных разрешений.</p><p>После сохранения проверьте конкретное действие в симуляторе на вкладке «Разрешения».</p></div>}
<div className="flex flex-wrap justify-between gap-2"><Button variant="outline" disabled={busy} onClick={() => step > 1 ? setStep(step - 1) : close()}>{step > 1 ? "Назад" : "Отмена"}</Button>{step < 4 ? <Button onClick={() => setStep(step + 1)}>Далее</Button> : <Button disabled={busy} onClick={() => { void save() }}>{busy ? "Сохранение…" : "Сохранить доступ"}</Button>}</div>
</DialogContent></Dialog>
}
+1 -1
View File
@@ -66,6 +66,6 @@ export function PolicyPreviewDecision({ result }: { result: McpPolicyDecision })
return <div role="status" className="flex flex-col gap-3 rounded-md border p-3 text-sm">
<Badge variant={result.decision === "allowed" ? "success-light" : result.decision === "denied" ? "destructive-light" : "warning-light"}>{result.decision === "allowed" ? "Разрешено" : result.decision === "denied" ? "Запрещено" : "Нужно подтверждение"}</Badge>
{result.reasons.length > 0 && <ul className="list-inside list-disc space-y-1">{result.reasons.map((item, index) => <li key={`${item.code}:${index}`}>{item.message} <span className="text-xs text-muted-foreground">({item.code})</span></li>)}</ul>}
<p className="text-xs text-muted-foreground">Лимиты подключения: {result.limits.callsPerMinute} вызовов/мин · {result.limits.concurrentReads} параллельных чтения · {result.limits.mutationsPerHour} изменений/ч · до {result.limits.serversPerPlan} серверов и {result.limits.objectsPerPlan} объектов в плане.</p>
<p className="text-xs text-muted-foreground">{result.unlimitedRates ? "Квоты частоты отключены" : `${result.limits.callsPerMinute} вызовов/мин · ${result.limits.concurrentReads} одновременных чтений · ${result.limits.mutationsPerHour} изменений/ч`} · до {result.limits.serversPerPlan} серверов и {result.limits.objectsPerPlan} объектов в плане.</p>
</div>
}
+25 -13
View File
@@ -97,25 +97,32 @@ export function FlowEntityCardView({
)
}
type SessionFilter = {
kind: "application" | "category" | "service" | "asn" | "country" | "protocol" | "source" | "destination" | "iface" | "client" | "en"
type BreakdownFilter = {
kind: "application" | "category" | "service" | "asn" | "country" | "protocol" | "source" | "destination" | "iface"
value: string
label: string
serverId?: string
}
type SessionFilter = BreakdownFilter | { kind: "path"; path: FlowPathRow; label: string }
function talkerMatchesFilter(row: FlowTalkerDto, filter: SessionFilter): boolean {
switch (filter.kind) {
case "application": return row.application === filter.value
case "category": return row.category === filter.value
case "service": return row.service === filter.value
case "asn": return String(row.dstAsn ?? "") === filter.value
case "country": return row.dstCountry === filter.value
case "asn": return String(row.dstAsn ?? "unknown") === filter.value
case "country": return (!filter.serverId || row.serverId === filter.serverId) && (row.dstCountry || "unknown") === filter.value
case "protocol": return row.protoName === filter.value
case "source": return row.src === filter.value
case "destination": return row.dst === filter.value
case "iface": return row.inIface === filter.value
case "client": return (row.clientId || "unknown") === filter.value
case "en": return (row.enId || "") === filter.value
case "iface": return (row.accountedIface ?? row.inIface) === filter.value
case "path": {
const path = filter.path
return row.serverId === path.serverId && (row.clientId || "unknown") === path.clientId &&
(row.enId || "") === path.enId && row.dst === path.dst && row.inIface === path.inIface &&
(row.outIface || "") === path.outIface && row.service === path.service &&
row.category === path.category && row.plane === path.plane
}
}
}
@@ -141,7 +148,7 @@ function FlowBreakdownGrid({
cell: ({ row }) => (
<div className="flex min-w-0 flex-col gap-1">
<span className="flex items-center gap-1.5 text-sm font-medium truncate">
{country ? <Flag code={row.original.id} /> : null}
{country && /^[a-z]{2}$/i.test(row.original.id) ? <Flag code={row.original.id} /> : null}
{row.original.label}
</span>
<div className="h-1 rounded-full bg-muted overflow-hidden">
@@ -351,7 +358,7 @@ export function FlowAnalyticsDetail({
sessionFilter ? talkerMatchesFilter(row, sessionFilter) : true,
)
function pickBreakdown(kind: SessionFilter["kind"], row: FlowBreakdownRow) {
function pickBreakdown(kind: BreakdownFilter["kind"], row: FlowBreakdownRow) {
setSessionFilter({ kind, value: row.id, label: row.label })
setSlice("sessions")
}
@@ -569,6 +576,9 @@ export function FlowAnalyticsDetail({
<p className="text-xs text-muted-foreground">Аналитика потребления</p>
{liveHint === "live" ? <Badge variant="success-light" size="sm">live</Badge> : null}
</div>
<p className="text-xs text-muted-foreground">
Разрезы рассчитаны по сохранённой выборке NetFlow. В рейтингах до 50 крупнейших строк, доли рассчитаны от всей выборки.
</p>
<Tabs value={slice} onValueChange={(v) => setSlice(String(v))} className="gap-3">
<TabsList variant="line" className="flex w-full shrink-0 flex-wrap gap-y-2 group-data-horizontal/tabs:h-auto [&_[data-slot=tabs-trigger]]:h-9 [&_[data-slot=tabs-trigger]]:flex-none">
<TabsTrigger value="applications">Приложения</TabsTrigger>
@@ -585,6 +595,7 @@ export function FlowAnalyticsDetail({
<TabsTrigger value="interfaces">Интерфейсы</TabsTrigger>
</TabsList>
<TabsContent value="applications">
<p className="mb-2 text-xs text-muted-foreground">Приложения определяются по протоколу и портам.</p>
<FlowBreakdownGrid rows={analytics?.applications ?? []} onPick={(row) => pickBreakdown("application", row)} />
</TabsContent>
<TabsContent value="categories">
@@ -602,8 +613,9 @@ export function FlowAnalyticsDetail({
<TabsContent value="map">
<FlowTrafficMap
edges={analytics?.mapEdges ?? []}
onSelectCountry={(iso) => {
setSessionFilter({ kind: "country", value: iso, label: iso })
onSelectEdge={(edge) => {
setSessionFilter({ kind: "country", value: edge.toCountry, serverId: edge.fromId,
label: edge.toCountry === "unknown" ? "Страна неизвестна" : edge.toCountry })
setSlice("sessions")
}}
/>
@@ -623,8 +635,8 @@ export function FlowAnalyticsDetail({
empty="Нет путей за период"
onPick={(row) => {
setSessionFilter({
kind: "client",
value: row.clientId,
kind: "path",
path: row,
label: `${row.clientName} → ${row.enName || row.dst}`,
})
setSlice("sessions")
+9 -9
View File
@@ -34,10 +34,10 @@ function formatBytes(n: number): string {
function FlowTrafficMap({
edges,
onSelectCountry,
onSelectEdge,
}: {
edges: FlowMapEdge[]
onSelectCountry?: (country: string) => void
onSelectEdge?: (edge: FlowMapEdge) => void
}) {
const [hover, setHover] = useState<string | null>(null)
@@ -77,7 +77,7 @@ function FlowTrafficMap({
{/^[a-z]{2}$/i.test(row.original.toCountry)
? <Flag code={row.original.toCountry} />
: null}
{row.original.toCountry}
{row.original.toCountry === "unknown" ? "Страна неизвестна" : row.original.toCountry}
{row.original.toAsn ? <span className="font-mono text-[10px] text-muted-foreground">AS{row.original.toAsn}</span> : null}
</span>
),
@@ -120,7 +120,7 @@ function FlowTrafficMap({
if (!edges.length) {
return (
<p className="text-sm text-muted-foreground py-8 text-center">
Страны подтянутся из кэша RIPEstat
Нет данных за период
</p>
)
}
@@ -150,10 +150,10 @@ function FlowTrafficMap({
className="cursor-pointer"
onMouseEnter={() => setHover(id)}
onMouseLeave={() => setHover(null)}
onClick={() => onSelectCountry?.(e.toCountry)}
onClick={() => onSelectEdge?.(e)}
>
<title>
{`${e.fromLabel} → ${e.toCountry} · ${e.category} · ${formatBytes(e.bytes)}`}
{`${e.fromLabel} → ${e.toCountry === "unknown" ? "Страна неизвестна" : e.toCountry} · ${e.category} · ${formatBytes(e.bytes)}`}
</title>
</path>
)
@@ -170,7 +170,7 @@ function FlowTrafficMap({
<g key={`d-${n.country}`}>
<circle cx={n.x} cy={n.y} r="7" className="fill-chart-2" />
<text x={n.x + 14} y={n.y + 4} className="fill-foreground text-[11px]">
{n.country}
{n.country === "unknown" ? "Без гео" : n.country}
</text>
</g>
))}
@@ -185,8 +185,8 @@ function FlowTrafficMap({
<DataGridShell
table={table}
recordCount={edges.length}
emptyMessage="Нет рёбер с известной страной"
onRowClick={(row) => onSelectCountry?.(row.toCountry)}
emptyMessage="Нет данных за период"
onRowClick={onSelectEdge}
/>
</div>
)
+3 -1
View File
@@ -123,7 +123,9 @@
В «Подключения» → «Выдать доступ» администратор MCP может выбрать «Сервисное — бессрочный автономный доступ». Выберите нужные серверы и операции. Доступ действует до отзыва и переживает истечение обычной сессии портала. По умолчанию опасные действия требуют ручного подтверждения.
На шаге 3 можно включить «Выполнять планы без ручного подтверждения». Режим действует на все выданные ключу операции. Списки серверов, права, лимиты и правила защиты объектов продолжают действовать.
На шаге 3 можно включить «Выполнять планы без ручного подтверждения». Режим действует на все выданные ключу операции. Списки серверов, права и правила защиты объектов продолжают действовать.
Опция «Отключить квоты частоты для этого ключа» доступна при создании и редактировании сервисного подключения. Она снимает квоты на HTTP-запросы MCP, вызовы инструментов, одновременные чтения и изменения в час. В API ей соответствует `unlimitedRates: true`. Ключ менять не нужно. Ограничения размера плана и общей нагрузки сервера продолжают действовать.
Если план содержит `announcement`, клиент сначала публикует отдельное сообщение в чате с ID плана, целями, изменениями и предупреждениями из `changes`. Затем вызывает `mm_change_apply` с `planId`, `idempotencyKey` и `announcedPlanDigest`, равным `announcement.digest`. Manager проверяет контрольную сумму сохранённого плана. Публикацию сообщения обеспечивает клиент. В аудите такой запуск отмечается как допуск по политике подключения.
+5 -2
View File
@@ -36,6 +36,7 @@ export const mcpConnectionPolicySchema = z.object({
status: z.enum(["active", "paused", "revoked"]),
authority: z.literal("service").optional(),
allowUnattendedPlans: z.literal(true).optional(),
unlimitedRates: z.literal(true).optional(),
expiresAt: instant,
mode: mcpModeSchema,
permissions: uniqueStrings,
@@ -45,7 +46,7 @@ export const mcpConnectionPolicySchema = z.object({
limits: mcpLimitsSchema.default(() => mcpLimitsSchema.parse({})),
}).strict().refine(policy => policy.authority === "service"
? policy.expiresAt === MCP_SERVICE_EXPIRATION && policy.mode === "autonomous" && policy.dangerousPolicy === "approval" && !policy.autonomousDangerousOperationIds.length
: !policy.allowUnattendedPlans,
: !policy.allowUnattendedPlans && !policy.unlimitedRates,
"Некорректная политика сервисного подключения")
export const mcpTargetSchema = z.discriminatedUnion("scope", [
@@ -111,6 +112,7 @@ export const mcpPolicyDecisionSchema = z.object({
decision: z.enum(["denied", "approval-required", "allowed"]),
reasons: z.array(z.object({ code: mcpPolicyReasonCodeSchema, message: z.string() }).strict()),
limits: mcpLimitsSchema,
unlimitedRates: z.literal(true).optional(),
}).strict()
export const mcpPolicyPreviewResponseSchema = z.object({
simulation: z.literal(true),
@@ -136,6 +138,7 @@ export const mcpConnectionCreateSchema = z.object({
name: z.string().trim().min(1).max(100),
authority: z.literal("service").optional(),
allowUnattendedPlans: z.literal(true).optional(),
unlimitedRates: z.literal(true).optional(),
expiresAt: instant.nullable(),
mode: mcpModeSchema.default("observer"),
permissions: uniqueStrings,
@@ -146,7 +149,7 @@ export const mcpConnectionCreateSchema = z.object({
limits: mcpLimitsSchema.default(() => mcpLimitsSchema.parse({})),
}).strict().refine(input => input.authority === "service"
? input.expiresAt === null && input.mode === "autonomous" && input.dangerousPolicy === "approval" && !input.autonomousDangerousOperationIds.length
: input.expiresAt !== null && !input.allowUnattendedPlans, "Опция доступна только бессрочному сервисному подключению. Для личного подключения укажите срок действия.")
: input.expiresAt !== null && !input.allowUnattendedPlans && !input.unlimitedRates, "Опция доступна только бессрочному сервисному подключению. Для личного подключения укажите срок действия.")
.refine(input => !input.selectAllServers || !input.scope.serverIds.length, "Выберите все серверы или укажите конкретные ID")
export const mcpConnectionSchema = z.object({
id: z.string().uuid(), name: z.string(), ownerId: z.string(),
+1
View File
@@ -147,6 +147,7 @@ export const flowTalkerDtoSchema = z.object({
inIface: z.string(),
inIfaceIndex: z.string().optional(),
outIface: z.string().optional(),
accountedIface: z.string().optional(),
nextHop: z.string().optional(),
application: z.string().optional(),
category: z.string().optional(),
+74
View File
@@ -59,3 +59,77 @@ for (const outcome of ["success", "error"] as const) {
await expect(page.getByRole("status").filter({ hasText: "Чтение актуальных правил устройства" })).toHaveCount(0)
})
}
test("Filters: чтение под общей блокировкой повторяется и не показывает роутер недоступным", async ({ page }) => {
const ruleset = { serverId: "1", rules: [
{ id: "saved", community: "65001:120", gateway: "", gatewayTunnelId: "", action: "blackhole", description: "Сохранённое правило" },
] }
let releaseRules!: () => void
const gate = new Promise<void>(resolve => { releaseRules = resolve })
let rulesReading = false, initial = true, overlaps = 0, greReads = 0, gatewayReads = 0
let lock: "free" | "brief" | "held" = "free", conflicts = 0
await page.route("**/health", route => route.fulfill({ json: { ok: true } }))
await page.route("**/api/**", async route => {
const { pathname, searchParams } = new URL(route.request().url())
const conflict = () => route.fulfill({ status: 409, json: { error: "Глобальная конфигурация изменяется другой операцией" } })
if (pathname === "/api/filters/rules" && searchParams.has("serverId")) {
if (lock === "held" || lock === "brief") {
conflicts++
if (lock === "brief") lock = "free"
return conflict()
}
if (initial) {
initial = false
rulesReading = true
await gate
rulesReading = false
}
return route.fulfill({ json: { rulesets: [ruleset], live: true } })
}
if (pathname === "/api/recursive-routes") {
gatewayReads++
if (rulesReading) {
overlaps++
return conflict()
}
if (lock === "held") return conflict()
return route.fulfill({ json: { routes: [] } })
}
if (pathname === "/api/filters/gre-tunnels") {
greReads++
return route.fulfill({ json: { tunnels: [] } })
}
return route.fulfill({ json: pathname === "/api/filters/rules" ? { rulesets: [ruleset], live: false }
: pathname === "/api/servers" ? [{ id: 1, name: "Роутер 1", host: "192.0.2.1", type: "jump-host", enabled: true, country: "RU", site: "TEST", asn: "", status: "online", latency: 1 }]
: pathname === "/api/auth/config" ? { required: false } : {} })
})
await page.goto("/filters")
await expect.poll(() => rulesReading && greReads > 0).toBe(true)
await expect(page.getByRole("button", { name: "Новое правило", exact: true })).toBeDisabled()
releaseRules()
await expect.poll(() => gatewayReads).toBe(1)
expect(overlaps).toBe(0)
const refresh = page.getByRole("button", { name: "Обновить", exact: true })
const add = page.getByRole("button", { name: "Новое правило", exact: true })
const busy = page.getByText("Конфигурация временно занята другой операцией. Показаны последние правила.", { exact: false })
lock = "brief"
await refresh.click()
await expect.poll(() => conflicts).toBe(1)
await expect(add).toBeEnabled()
await expect(busy).toHaveCount(0)
lock = "held"
await refresh.click()
await expect(busy).toBeVisible()
expect(conflicts).toBe(4)
await expect(add).toBeDisabled()
await expect(page.locator("tr[data-row-id]").first()).toContainText("65001:120")
await expect(page.getByText(/Роутер недоступен|ApiClientError|Не удалось прочитать правила с роутера/)).toHaveCount(0)
lock = "free"
await refresh.click()
await expect(add).toBeEnabled()
await expect(busy).toHaveCount(0)
})
+7 -1
View File
@@ -389,7 +389,7 @@ test('synthetic: edit autonomous dangerous operations by group or all at once',
}) })])
})
for (const unattended of [false, true]) test(`synthetic: issue service access with unattended plans ${unattended ? 'enabled' : 'disabled'}`, async ({ page }) => {
for (const { unattended, unlimited } of [{ unattended: true, unlimited: false }, { unattended: false, unlimited: true }]) test(`synthetic: issue service access with ${unlimited ? 'unlimited rates' : 'unattended plans'}`, async ({ page }) => {
const state = await setup(page, ['mm:mcp:admin', 'mm:servers:read'])
await page.goto('/mcp?tab=connections')
await page.getByRole('button', { name: 'Выдать доступ', exact: true }).click()
@@ -405,6 +405,11 @@ for (const unattended of [false, true]) test(`synthetic: issue service access wi
const unattendedChoice = dialog.getByRole('checkbox', { name: 'Выполнять планы без ручного подтверждения', exact: true })
await expect(unattendedChoice).not.toBeChecked()
if (unattended) await unattendedChoice.check()
const rateChoice = dialog.getByRole('checkbox', { name: 'Отключить квоты частоты для этого ключа', exact: true })
await expect(rateChoice).not.toBeChecked()
if (unlimited) await rateChoice.check()
await expect(dialog.getByLabel('Вызовов в минуту', { exact: true })).toHaveCount(unlimited ? 0 : 1)
await expect(dialog.getByLabel('Серверов на план', { exact: true })).toBeVisible()
await next.click()
await expect(dialog.getByText('Сервисное, бессрочное', { exact: false })).toBeVisible()
await dialog.getByRole('button', { name: 'Сохранить доступ', exact: true }).click()
@@ -414,6 +419,7 @@ for (const unattended of [false, true]) test(`synthetic: issue service access wi
scope: { serverIds: [1], operationIds: ['logs.query'], allowGlobal: false },
}) })])
expect(state.mutations[0].body.allowUnattendedPlans).toBe(unattended ? true : undefined)
expect(state.mutations[0].body.unlimitedRates).toBe(unlimited ? true : undefined)
})
test('synthetic: pause, resume, cancelled revoke, revoke, monitor denied outcome, stop automation', async ({ page }) => {
+23
View File
@@ -528,3 +528,26 @@ test('Flow: top-выборка и пустой локальный фильтр
await page.getByRole('button', { name: 'сбросить', exact: true }).click()
await expect(summary).toContainText('Показано 1 из 1 в выборке')
})
test('Flow: сессии фильтруются по учтённому интерфейсу и точному пути', async ({ page }) => {
const control = await setup(page)
const session = {
serverId: '1', serverName: 'Экспортёр тест', src: '192.0.2.1', dst: '198.51.100.1', proto: 6, protoName: 'TCP',
srcPort: 1234, dstPort: 443, bytes: 100, packets: 1, bps: 100, inIface: 'lan', outIface: 'wan',
accountedIface: 'wan', clientId: 'u1', service: 'Тест', category: 'Веб', plane: 'payload',
}
control.metrics = {
conversations: 2,
conversationsList: [session, { ...session, outIface: 'wan2', accountedIface: 'wan2' }],
interfaces: [{ id: 'wan', label: 'wan', bytes: 100, packets: 1, bps: 100, percent: 50 }],
paths: [{ ...session, id: 'test-path', clientName: 'Клиент тест', enId: '', enName: '', ifaces: 'lan, wan' }],
}
await page.getByRole('button', { name: '15м', exact: true }).click()
const summary = page.getByRole('status', { name: 'Выборка сессий' })
await page.getByRole('tab', { name: 'Интерфейсы', exact: true }).click()
await page.getByRole('row').filter({ hasText: 'wan' }).click()
await expect(summary).toContainText('Показано 1 из 2')
await page.getByRole('tab', { name: 'Пути', exact: true }).click()
await page.getByRole('row').filter({ hasText: 'Клиент тест' }).click()
await expect(summary).toContainText('Показано 1 из 2')
})