import { randomUUID } from "node:crypto" import { isIPv4 } from "node:net" import { z } from "zod" import { hasPermission } from "../lib/permissions.js" import { McpError } from "../modules/mcp/security.js" import { sanitizeServerProbeReceipt } from "./server-probe-public-result.js" import { classifyServerProbeMtu, planServerProbeExecution, projectServerProbeBandwidth, projectServerProbeDns, projectServerProbePing, projectServerProbeRoute, projectServerProbeTrace, projectServerProbeMtu, ServerProbeDataError, type ServerProbeRequest, type ServerProbeView } from "./server-probe-execution-model.js" export interface ServerProbeEndpoint { id: number; enabled: boolean; host: string; port: number; useSsl: boolean; verifySsl: boolean; username: string; password: string; routingEndpointEpoch: string } export interface ServerProbeAuthority { transport?: "mcp" | "rest"; ownerId: string; connectionId?: string; credentialId?: string; requestId?: string; issuedAt?: number expiresAt: number; generation: string; policyProof: string; ownerProof: string active: boolean; operationAllowed: boolean; scopeServerIds: number[]; ownerPermissions: string[]; policyPermissions: string[] } export interface ServerProbeReceipt { receiptId: string; tool: ServerProbeRequest["tool"]; serverId: number; destinationServerId?: number state: "completed" | "failed" | "cancelled" | "unknown"; httpStatus: number; output?: string; data?: Record; errorCode?: string attemptedStages: number; completedStages: number; automaticReplay: false; forwardingVerified: false } /** All methods must acknowledge a committed durable transaction. claim is a * unique CAS by receiptId+bindingProof, never an upsert/reset. Terminal rows * never return to executable state. Lost ACK is quarantined, never retried. * Production integration must bind this to the real operation's journal. */ export interface ServerProbeJournalBinding { receiptId: string; bindingProof: string; ownerId: string; transport?: "mcp" | "rest"; connectionId?: string; requestId?: string; issuedAt?: number; serverIds: number[]; tool: string } export interface ServerProbeJournal { claim(value: ServerProbeJournalBinding): Promise<"claimed" | "exists"> intent(value: { receiptId: string; stageIndex: number; kind: string; timeoutMs: number }): Promise received(value: { receiptId: string; stageIndex: number; outcome: "received" | "source-unavailable" }): Promise finish(value: ServerProbeReceipt): Promise quarantine(value: ServerProbeJournalBinding & { errorCode: string; attemptedStages: number; completedStages: number }): Promise read(receiptId: string, bindingProof: string): Promise } export interface ServerProbeExecutionDependencies { getServer(id: number): Promise /** Metadata-only lookup restricted BEFORE querying to this trusted scope. * Never read or return destination credentials here. */ findDestination(remote: string, explicitId: number | undefined, allowedServerIds: readonly number[]): Promise<{ id: number } | undefined> readAuthority(): Promise /** Fresh deny/human-approval policy for BOTH source and bandwidth destination. */ assertProtections(serverIds: number[], authority: ServerProbeAuthority): Promise privateProof(domain: string, value: unknown): Promise seal(value: string): Promise; open(value: string): Promise post(server: ServerProbeEndpoint, path: string, body: Record, timeoutMs: number, signal: AbortSignal, beforeDispatch: () => Promise): Promise /** Admission immediately before HTTP request construction, not after the * subsequent DNS/TLS/socket queue. Authority is checked again after response. */ routes(server: ServerProbeEndpoint, timeoutMs: number, signal: AbortSignal, beforeDispatch: () => Promise): Promise dns(name: string, type: string, signal: AbortSignal, beforeDispatch: () => Promise): Promise /** Preparation/preflight do not own an operation yet. Execution requires a * real journal and refuses before any privileged read if it is absent. */ journal?: ServerProbeJournal now?(): number } export class ServerProbeExecutionError extends Error { readonly automaticReplay = false constructor(readonly code: string, message: string, readonly statusCode: number, readonly state: ServerProbeReceipt["state"] = "failed", readonly attemptedStages = 0, readonly completedStages = 0) { super(message) } } const error = (code: string, message: string, status = 409): never => { throw new ServerProbeExecutionError(code, message, status) } const proof = z.string().regex(/^[a-f0-9]{64}$/) const bindingSchema = z.object({ version: z.literal(1), receiptId: z.string().uuid(), serverId: z.number().int().positive().safe(), destinationId: z.number().int().positive().safe().optional(), sourceProof: proof, destinationProof: proof.optional(), authorityProof: proof, request: z.unknown(), preparedAt: z.number().int().nonnegative().safe(), expiresAt: z.number().int().positive().safe() }).strict() const completedReceiptSchema = z.object({ receiptId: z.string().uuid(), tool: z.enum(["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]), serverId: z.number().int().positive().safe(), destinationServerId: z.number().int().positive().safe().optional(), state: z.literal("completed"), httpStatus: z.literal(200), output: z.string().max(131072), data: z.record(z.string(), z.unknown()), attemptedStages: z.number().int().min(1).max(11), completedStages: z.number().int().min(1).max(11), automaticReplay: z.literal(false), forwardingVerified: z.literal(false) }).strict() type Binding = z.infer /** Server-private plan material only; never put bindingCiphertext into a public * tool response. The existing encrypted-plan layer may persist it unchanged. */ export interface PreparedServerProbe { receiptId: string; tool: ServerProbeRequest["tool"]; serverId: number; destinationServerId?: number; expiresAt: string; bindingCiphertext: string } function endpointValue(server: ServerProbeEndpoint) { if (!Number.isSafeInteger(server.id) || server.id <= 0 || !server.enabled || !z.string().uuid().safeParse(server.routingEndpointEpoch).success || !server.host || !Number.isSafeInteger(server.port) || server.port <= 0 || server.port > 65535) return error("SOURCE_UNAVAILABLE", "Источник диагностики выключен или не подтверждён") return { id: server.id, enabled: server.enabled, host: server.host, port: server.port, useSsl: server.useSsl, verifySsl: server.verifySsl, username: server.username, password: server.password, epoch: server.routingEndpointEpoch } } function cleanReceipt(receipt: ServerProbeReceipt, endpoints: ServerProbeEndpoint[]): ServerProbeReceipt { const secrets = [...new Set(endpoints.flatMap(server => [server.password, server.username]).filter(Boolean))] const clean = (value: unknown, field?: string): unknown => { if (typeof value === "string") { if (field === "kind" && ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"].includes(value) || field === "resolver" && value === "manager" || field === "type" && ["A", "AAAA", "MX", "NS", "TXT", "CNAME", "PTR"].includes(value) || field === "result" && ["reply", "timeout"].includes(value)) return value let result = value; for (const secret of secrets) result = result.split(secret).join("[redacted]"); return result.replace(/\bBearer\s+\S+/gi, "Bearer [redacted]").replace(/\bmmcp_[a-z]+_[A-Za-z0-9_-]+/g, "[redacted]") } if (Array.isArray(value)) return value.map(child => clean(child)) if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, clean(child, key)])) return value } // Public IDs/enums/counters are trusted structural metadata. Redact only // provider-derived measurements/text, so a short username cannot corrupt a // UUID or turn the fixed state enum into an invalid receipt. const result: ServerProbeReceipt = { ...receipt, ...(receipt.output !== undefined ? { output: clean(receipt.output) as string } : {}), ...(receipt.data !== undefined ? { data: clean(receipt.data) as Record } : {}) } if (Buffer.byteLength(JSON.stringify(result)) > 131072) throw new ServerProbeDataError() try { return sanitizeServerProbeReceipt(result) } catch { throw new ServerProbeDataError() } } function validateAuthority(authority: ServerProbeAuthority, serverIds: number[], now: number): void { const identityValid = authority.transport === "rest" ? typeof authority.requestId === "string" && authority.requestId.length > 0 && authority.requestId.length <= 256 && authority.connectionId === undefined && authority.credentialId === undefined && Number.isSafeInteger(authority.issuedAt) && authority.issuedAt! > 0 && authority.issuedAt! <= now : (authority.transport === undefined || authority.transport === "mcp") && !!authority.connectionId && !!authority.credentialId && authority.requestId === undefined if (!identityValid || !authority.active || !authority.operationAllowed || !authority.ownerId || !authority.generation || !Number.isSafeInteger(authority.expiresAt) || authority.expiresAt <= now || !proof.safeParse(authority.policyProof).success || !proof.safeParse(authority.ownerProof).success || !hasPermission(authority.ownerPermissions, "mm:servers:write") || !hasPermission(authority.policyPermissions, "mm:servers:write") || serverIds.some(id => !authority.scopeServerIds.includes(id))) error("PERMISSION_DENIED", "Нет актуального допуска диагностики обоих участников", 403) } async function untilAbort(work: Promise, signal: AbortSignal): Promise { if (signal.aborted) throw new ServerProbeExecutionError("CANCELLED", "Диагностика отменена или истёк лимит времени", 502) let abort: (() => void) | undefined try { return await Promise.race([work, new Promise((_resolve, reject) => { abort = () => reject(new ServerProbeExecutionError("CANCELLED", "Диагностика отменена или истёк лимит времени", 502)); signal.addEventListener("abort", abort, { once: true }) })]) } finally { if (abort) signal.removeEventListener("abort", abort) } } /** Inert factory. Injected tests need no DB, DNS, RouterOS or collector. No * caller JSON can supply clients, journal handles, guard callbacks or secrets. */ export function createServerProbeExecutor(deps: ServerProbeExecutionDependencies) { const now = deps.now ?? Date.now const privateEndpoint = (server: ServerProbeEndpoint) => deps.privateProof("endpoint-v1", endpointValue(server)) const prepare = async (serverId: number, raw: unknown, signal?: AbortSignal): Promise => { const plan = planServerProbeExecution(raw) if (!Number.isSafeInteger(serverId) || serverId <= 0) return error("INVALID_INPUT", "Некорректный сервер диагностики", 400) const abort = () => { if (signal?.aborted) error("CANCELLED", "Подготовка диагностики отменена", 502) } abort() const authority = await deps.readAuthority(); validateAuthority(authority, [serverId], now()); await deps.assertProtections([serverId], authority); abort() const authorityProof = await deps.privateProof("authority-v1", authority) const authorityFence = async (ids: number[]) => { abort(); const fresh = await deps.readAuthority(); validateAuthority(fresh, ids, now()); if (await deps.privateProof("authority-v1", fresh) !== authorityProof) return error("STATE_CONFLICT", "Допуск подготовки диагностики изменился"); await deps.assertProtections(ids, fresh); abort() } const source = await deps.getServer(serverId) if (!source || source.id !== serverId) return error("NOT_FOUND", "Сервер диагностики не найден", 404) await authorityFence([serverId]) const sourceProof = await privateEndpoint(source) await authorityFence([serverId]) const destinationRef = plan.request.tool === "bandwidth" ? await deps.findDestination(plan.request.bwRemoteAddress!, plan.request.dstServerId, [...authority.scopeServerIds]) : undefined if (plan.request.tool === "bandwidth" && !destinationRef) return error("DESTINATION_UNAVAILABLE", "Сервер назначения BW-теста не найден", 400) const serverIds = [...new Set([serverId, ...(destinationRef ? [destinationRef.id] : [])])] await authorityFence(serverIds) const destination = destinationRef ? await deps.getServer(destinationRef.id) : undefined await authorityFence(serverIds) if (destinationRef && (!destination || destination.id !== destinationRef.id)) return error("DESTINATION_UNAVAILABLE", "Назначение BW-теста исчезло", 409) const destinationProof = destination ? await privateEndpoint(destination) : undefined const freshSource = await deps.getServer(serverId) await authorityFence(serverIds) if (!freshSource || await privateEndpoint(freshSource) !== sourceProof) return error("STATE_CONFLICT", "Источник или допуск диагностики изменились") if (destination) { const freshDestination = await deps.getServer(destination.id); await authorityFence(serverIds); if (!freshDestination || await privateEndpoint(freshDestination) !== destinationProof) return error("STATE_CONFLICT", "Назначение BW-теста изменилось") } await authorityFence(serverIds) const preparedAt = now(), expiresAt = Math.min(authority.expiresAt, preparedAt + 300000), receiptId = randomUUID() const binding: Binding = { version: 1, receiptId, serverId, ...(destination ? { destinationId: destination.id, destinationProof } : {}), sourceProof, authorityProof, request: plan.request, preparedAt, expiresAt } const bindingCiphertext = await deps.seal(JSON.stringify(binding)) await authorityFence(serverIds) const publicationSource = await deps.getServer(serverId); await authorityFence(serverIds) if (!publicationSource || await privateEndpoint(publicationSource) !== sourceProof) return error("STATE_CONFLICT", "Источник изменился перед публикацией плана") if (destination) { const publicationDestination = await deps.getServer(destination.id); await authorityFence(serverIds); if (!publicationDestination || await privateEndpoint(publicationDestination) !== destinationProof) return error("STATE_CONFLICT", "Назначение изменилось перед публикацией плана") } await authorityFence(serverIds) return { receiptId, tool: plan.request.tool, serverId, ...(destination ? { destinationServerId: destination.id } : {}), expiresAt: new Date(expiresAt).toISOString(), bindingCiphertext } } const load = async (prepared: PreparedServerProbe) => { let binding: Binding try { if (prepared.bindingCiphertext.length > 262144) throw Error(); binding = bindingSchema.parse(JSON.parse(await deps.open(prepared.bindingCiphertext))) } catch { return error("INVALID_BINDING", "Частный план диагностики не подтверждён", 409) } const plan = planServerProbeExecution(binding.request) if (prepared.receiptId !== binding.receiptId || prepared.serverId !== binding.serverId || prepared.destinationServerId !== binding.destinationId || prepared.tool !== plan.request.tool || prepared.expiresAt !== new Date(binding.expiresAt).toISOString()) return error("INVALID_BINDING", "Привязка диагностики изменена") return { binding, plan } } /** Source/authority preconditions only: no probe, DNS, claim or journal write. * Used by the real runner's current() and private preparation evidence. */ const preflight = async (prepared: PreparedServerProbe, signal?: AbortSignal) => { const { binding, plan } = await load(prepared), serverIds = [...new Set([binding.serverId, ...(binding.destinationId ? [binding.destinationId] : [])])] const access = async () => { if (signal?.aborted || now() >= binding.expiresAt || now() < binding.preparedAt) return error("CANCELLED", "Частный план диагностики истёк или отменён", 502) const authority = await deps.readAuthority(); validateAuthority(authority, serverIds, now()) if (await deps.privateProof("authority-v1", authority) !== binding.authorityProof) return error("STATE_CONFLICT", "Допуск диагностики изменился") await deps.assertProtections(serverIds, authority) if (signal?.aborted || now() >= binding.expiresAt) return error("CANCELLED", "Частный план диагностики истёк или отменён", 502) } await access() const source = await deps.getServer(binding.serverId); await access() if (!source || source.id !== binding.serverId || await privateEndpoint(source) !== binding.sourceProof) return error("STATE_CONFLICT", "Источник диагностики изменился") if (binding.destinationId) { const destination = await deps.getServer(binding.destinationId); await access() if (!destination || destination.id !== binding.destinationId || await privateEndpoint(destination) !== binding.destinationProof) return error("STATE_CONFLICT", "Назначение диагностики изменилось") } await access() return { receiptId: binding.receiptId, bindingProof: await deps.privateProof("binding-v1", binding), tool: plan.request.tool, serverIds, expiresAt: new Date(binding.expiresAt).toISOString() } } const execute = async (prepared: PreparedServerProbe, signal?: AbortSignal): Promise => { const journal = deps.journal if (!journal) return error("JOURNAL_UNAVAILABLE", "Для запуска диагностики нужен постоянный журнал реальной операции", 503) const { binding, plan } = await load(prepared) // Network stage limits remain unchanged. Current grants, source fences and // durable claim/ACK/final receipt need a bounded control-plane allowance. // The original private plan/authority expiry always wins. const serverIds = [...new Set([binding.serverId, ...(binding.destinationId ? [binding.destinationId] : [])])], controller = new AbortController(), deadline = Math.min(binding.expiresAt, now() + Math.min(300000, plan.wallBudgetMs + 30000)) const abort = () => controller.abort(); signal?.addEventListener("abort", abort, { once: true }); if (signal?.aborted) abort() const timer = setTimeout(abort, Math.max(1, deadline - now())) let attempted = 0, completed = 0, claimed = false, currentSource: ServerProbeEndpoint | undefined, currentDestination: ServerProbeEndpoint | undefined let journalBinding: ServerProbeJournalBinding | undefined const check = async (): Promise => { if (controller.signal.aborted || now() >= deadline || now() < binding.preparedAt) error("CANCELLED", "Диагностика отменена или истёк лимит времени", 502) const authorityFence = async () => { const authority = await deps.readAuthority(); validateAuthority(authority, serverIds, now()); if (await deps.privateProof("authority-v1", authority) !== binding.authorityProof) error("STATE_CONFLICT", "Owner, policy или credential диагностики изменились"); await deps.assertProtections(serverIds, authority); return authority } await authorityFence() const source = await deps.getServer(binding.serverId) await authorityFence() if (!source || source.id !== binding.serverId || await privateEndpoint(source) !== binding.sourceProof) return error("STATE_CONFLICT", "Источник диагностики изменился") currentSource = source if (binding.destinationId) { const destination = await deps.getServer(binding.destinationId); await authorityFence(); if (!destination || destination.id !== binding.destinationId || await privateEndpoint(destination) !== binding.destinationProof) return error("STATE_CONFLICT", "Назначение или credentials BW-теста изменились"); currentDestination = destination } const authority = await authorityFence() if (controller.signal.aborted || now() >= deadline) error("CANCELLED", "Диагностика отменена или истёк лимит времени", 502) return authority } const stage = async (kind: string, timeoutMs: number, work: (beforeDispatch: () => Promise, timeout: number, signal: AbortSignal) => Promise, sourceDns = false): Promise => { await check() const index = attempted + 1, timeout = Math.min(timeoutMs, deadline - now()) if (timeout <= 0) return error("CANCELLED", "Лимит диагностики исчерпан", 502) try { await journal.intent({ receiptId: binding.receiptId, stageIndex: index, kind, timeoutMs: timeout }) } catch { return error("JOURNAL_UNKNOWN", "Подтверждение intent недоступно; диагностика не повторяется", 503) } let dispatched = false, dispatchClaimed = false const stageAbort = new AbortController(), cancelStage = () => stageAbort.abort(), stageTimer = setTimeout(cancelStage, timeout) controller.signal.addEventListener("abort", cancelStage, { once: true }); if (controller.signal.aborted) cancelStage() const beforeDispatch = async () => { if (dispatchClaimed) return error("REPLAY_FORBIDDEN", "Повтор native/DNS stage запрещён"); dispatchClaimed = true; if (stageAbort.signal.aborted) return error("CANCELLED", "Лимит stage исчерпан", 502); await check(); if (stageAbort.signal.aborted) return error("CANCELLED", "Лимит stage исчерпан", 502); dispatched = true; attempted++ } try { let result: unknown try { result = await untilAbort(work(beforeDispatch, timeout, stageAbort.signal), stageAbort.signal) } catch (failure) { if (sourceDns && dispatched && !(failure instanceof ServerProbeExecutionError)) { await check(); try { await journal.received({ receiptId: binding.receiptId, stageIndex: index, outcome: "source-unavailable" }) } catch { return error("JOURNAL_UNKNOWN", "ACK stage недоступен; повтор запрещён", 503) }; completed++; return [] } throw failure } if (!dispatched) return error("INVALID_ADAPTER", "Адаптер не подтвердил границу отправки", 503) await check(); try { await journal.received({ receiptId: binding.receiptId, stageIndex: index, outcome: "received" }) } catch { return error("JOURNAL_UNKNOWN", "ACK stage недоступен; повтор запрещён", 503) }; completed++ return result } finally { stageAbort.abort(); clearTimeout(stageTimer); controller.signal.removeEventListener("abort", cancelStage) } } try { const authority = await check(), bindingProof = await deps.privateProof("binding-v1", binding) journalBinding = { receiptId: binding.receiptId, bindingProof, ownerId: authority.ownerId, ...(authority.transport === "rest" ? { transport: "rest", requestId: authority.requestId, issuedAt: authority.issuedAt } : { transport: "mcp", connectionId: authority.connectionId }), serverIds, tool: plan.request.tool } let claim: "claimed" | "exists" try { claim = await journal.claim(journalBinding) } catch { return error("JOURNAL_UNKNOWN", "Исход durable claim неизвестен; диагностика не повторяется", 503) } if (claim === "exists") { let prior: ServerProbeReceipt | undefined try { prior = await journal.read(binding.receiptId, bindingProof) } catch { return error("JOURNAL_UNKNOWN", "Ранее использованная попытка недоступна; повтор запрещён", 503) }; await check() if (!prior || prior.state !== "completed") throw new ServerProbeExecutionError("REPLAY_FORBIDDEN", "Попытка уже использована; автоматический повтор запрещён", 409, "unknown", prior?.attemptedStages ?? 0, prior?.completedStages ?? 0) const parsed = completedReceiptSchema.safeParse(prior) const maximum = plan.request.tool === "mtu" ? 11 : ["ping", "traceroute"].includes(plan.request.tool) ? 2 : 1 if (!parsed.success || parsed.data.receiptId !== binding.receiptId || parsed.data.tool !== plan.request.tool || parsed.data.serverId !== binding.serverId || parsed.data.destinationServerId !== binding.destinationId || parsed.data.data.kind !== plan.request.tool || parsed.data.completedStages !== parsed.data.attemptedStages || parsed.data.attemptedStages > maximum) return error("RECEIPT_INVALID", "Сохранённая квитанция не соответствует частному плану; повтор запрещён", 409) return cleanReceipt(parsed.data, [currentSource!, ...(currentDestination ? [currentDestination] : [])]) } claimed = true let sourceIpv4: string | null = null if (["ping", "traceroute", "mtu"].includes(plan.request.tool)) { const host = currentSource!.host.trim() if (isIPv4(host)) sourceIpv4 = host else { const addresses = await stage("source-dns-A", 10000, (fence, _timeout, signal) => deps.dns(host, "A", signal, fence), true) if (!Array.isArray(addresses) || addresses.length > 1000 || addresses.some(value => typeof value !== "string" || !isIPv4(value))) throw new ServerProbeDataError() sourceIpv4 = addresses[0] as string | undefined ?? null } } const nativeBody = { ...plan.nativeBody, ...(sourceIpv4 ? { "src-address": sourceIpv4 } : {}) } let view: ServerProbeView if (plan.request.tool === "ping") view = projectServerProbePing(await stage("native-ping", plan.wireTimeoutMs, (fence, timeout, signal) => deps.post(currentSource!, "/tool/ping", nativeBody, timeout, signal, fence)), plan.request.target!) else if (plan.request.tool === "traceroute") view = projectServerProbeTrace(await stage("native-traceroute", plan.wireTimeoutMs, (fence, timeout, signal) => deps.post(currentSource!, "/tool/traceroute", nativeBody, timeout, signal, fence)), plan) else if (plan.request.tool === "bandwidth") view = projectServerProbeBandwidth(await stage("native-bandwidth", plan.wireTimeoutMs, (fence, timeout, signal) => deps.post(currentSource!, "/tool/bandwidth-test", { ...nativeBody, user: currentDestination!.username, password: currentDestination!.password }, timeout, signal, fence))) else if (plan.request.tool === "route") view = projectServerProbeRoute(await stage("native-route-read", plan.wireTimeoutMs, (fence, timeout, signal) => deps.routes(currentSource!, timeout, signal, fence)), plan.request.target!) else if (plan.request.tool === "dns") view = projectServerProbeDns(await stage("manager-dns-" + (plan.request.dnsType ?? "A"), plan.wireTimeoutMs, (fence, _timeout, signal) => deps.dns(plan.request.target!, plan.request.dnsType ?? "A", signal, fence)), plan.request.target!, plan.request.dnsType ?? "A") else { const measurements: Array<{ size: number; result: "reply" | "timeout" }> = [] for (const size of plan.mtuSizes!) { const result = classifyServerProbeMtu(await stage("native-mtu-" + size, plan.wireTimeoutMs, (fence, timeout, signal) => deps.post(currentSource!, "/tool/ping", { address: plan.request.target!, count: "1", size: String(size), interval: "0.2s", "do-not-fragment": "true", ...(sourceIpv4 ? { "src-address": sourceIpv4 } : {}) }, timeout, signal, fence))) measurements.push({ size, result }) if (result === "reply") break } view = projectServerProbeMtu(measurements, plan.request.target!) } await check() const receipt = cleanReceipt({ receiptId: binding.receiptId, tool: plan.request.tool, serverId: binding.serverId, ...(binding.destinationId ? { destinationServerId: binding.destinationId } : {}), state: "completed", httpStatus: 200, ...view, attemptedStages: attempted, completedStages: completed, automaticReplay: false, forwardingVerified: false }, [currentSource!, ...(currentDestination ? [currentDestination] : [])]) try { await journal.finish(receipt) } catch { return error("JOURNAL_UNKNOWN", "Подтверждение receipt недоступно; повтор запрещён", 503) }; await check() return receipt } catch (failure) { const accessCode = failure instanceof McpError ? ["READ_CANCELLED", "PLAN_EXPIRED"].includes(failure.code) ? "CANCELLED" : ["STATE_CONFLICT", "RESET_REQUIRED"].includes(failure.code) ? "STATE_CONFLICT" : ["PERMISSION_DENIED", "UNAUTHORIZED", "MCP_DISABLED", "CONNECTION_REVOKED", "OWNER_CLAIMS_REVOKED", "GRANT_INACTIVE", "PROTECTED_OBJECT", "APPROVAL_REQUIRED", "SERVER_DENIED", "CREDENTIAL_EXPIRED"].includes(failure.code) ? "PERMISSION_DENIED" : undefined : undefined const cause = failure instanceof ServerProbeExecutionError ? failure : accessCode ? new ServerProbeExecutionError(accessCode, "Допуск диагностики изменился или операция отменена", accessCode === "PERMISSION_DENIED" ? 403 : accessCode === "STATE_CONFLICT" ? 409 : 502) : new ServerProbeExecutionError(failure instanceof ServerProbeDataError ? "MEASUREMENT_UNKNOWN" : "UPSTREAM_UNAVAILABLE", failure instanceof ServerProbeDataError ? failure.message : "Диагностика недоступна; исход отправленной пробы не подтверждён", 502) attempted = Math.max(attempted, cause.attemptedStages); completed = Math.max(completed, cause.completedStages) const state = attempted || cause.state === "unknown" || cause.code === "JOURNAL_UNKNOWN" ? "unknown" : cause.code === "CANCELLED" ? "cancelled" : "failed" if (claimed || cause.code === "JOURNAL_UNKNOWN" && journalBinding) { try { if (state === "unknown") await journal.quarantine({ ...journalBinding!, errorCode: cause.code, attemptedStages: attempted, completedStages: completed }) else await journal.finish({ receiptId: binding.receiptId, tool: plan.request.tool, serverId: binding.serverId, ...(binding.destinationId ? { destinationServerId: binding.destinationId } : {}), state, httpStatus: cause.statusCode, errorCode: cause.code, attemptedStages: attempted, completedStages: completed, automaticReplay: false, forwardingVerified: false }) } catch { throw new ServerProbeExecutionError("JOURNAL_UNKNOWN", "Durable исход диагностики недоступен; повтор запрещён", 503, "unknown", attempted, completed) } } throw new ServerProbeExecutionError(cause.code, cause.message, cause.statusCode, state, attempted, completed) } finally { clearTimeout(timer); signal?.removeEventListener("abort", abort) } } return { prepare, preflight, execute } } /** Concrete production I/O mapping, deliberately unregistered. Callers must * supply the real durable operation journal and fresh authority/protections; * no in-memory fallback is available. Importing this module opens nothing. */ export type ServerProbeSourceReaders = Pick export function createProductionServerProbeExecutor(input: Pick, readers?: ServerProbeSourceReaders) { // Trusted source composition only. Supplying incomplete readers must never // fall back to a second pool checkout while the host holds storage locks. if (readers !== undefined && (!readers || typeof readers !== "object" || typeof readers.getServer !== "function" || typeof readers.findDestination !== "function")) return error("STATE_UNAVAILABLE", "Источник диагностики не подключён", 503) const source = readers === undefined ? undefined : { getServer: readers.getServer, findDestination: readers.findDestination } return createServerProbeExecutor({ ...input, getServer: source ? source.getServer : async id => (await import("../modules/servers/repository/servers-repository.js")).getServerRowById(id), findDestination: source ? source.findDestination : async (remote, explicit, allowedServerIds) => { const { pool } = await import("../db/index.js") const { resolveServerProbeRestBandwidthPeer, ServerProbeRestContractError } = await import("./server-probe-rest-contract.js") try { const selected = await resolveServerProbeRestBandwidthPeer(remote, explicit, allowedServerIds, { byId: async (id, ids) => ids.includes(id) ? (await pool.query<{ id: number; host: string; enabled: boolean }>("SELECT id,host,enabled FROM servers WHERE id=ANY($1::int[]) AND id=$2 LIMIT 1", [[...ids], id])).rows[0] : undefined, enabledByHost: async (host, ids) => (await pool.query<{ id: number; host: string; enabled: boolean }>("SELECT id,host,enabled FROM servers WHERE id=ANY($1::int[]) AND enabled=true AND lower(btrim(host))=lower(btrim($2)) LIMIT 1001", [[...ids], host])).rows, }) return { id: selected.id } } catch (failure) { if (failure instanceof ServerProbeRestContractError) throw new ServerProbeExecutionError(failure.code, failure.message, failure.statusCode) throw failure } }, privateProof: async (domain, value) => { const security = await import("../modules/mcp/security.js"); return security.privateSecretFingerprint(security.fingerprint(value), "server-probe-execution-" + domain) }, seal: async value => (await import("../modules/mcp/security.js")).encryptSecret(value), open: async value => (await import("../modules/mcp/security.js")).decryptSecret(value), post: async (server, path, body, timeout, signal, fence) => { const { MikrotikClient } = await import("./mikrotik.js"); return MikrotikClient.fromServer(server as Parameters[0]).post(path, body, timeout, signal, fence) }, routes: async (server, timeout, signal, fence) => { const { MikrotikClient } = await import("./mikrotik.js"); return MikrotikClient.fromServer(server as Parameters[0]).get("/ip/route", timeout, signal, 1048576, fence) }, dns: async (name, type, signal, fence) => { const { Resolver } = await import("node:dns/promises"), resolver = new Resolver(), abort = () => resolver.cancel() signal.addEventListener("abort", abort, { once: true }) try { await fence(); signal.throwIfAborted() switch (type) { case "A": return await resolver.resolve4(name) case "AAAA": return await resolver.resolve6(name) case "MX": return await resolver.resolveMx(name) case "NS": return await resolver.resolveNs(name) case "TXT": return await resolver.resolveTxt(name) case "CNAME": return await resolver.resolveCname(name) case "PTR": return /^[\d.]+$/.test(name) ? await resolver.reverse(name) : await resolver.resolvePtr(name.includes(".arpa") ? name : `${name}.in-addr.arpa`) default: return error("INVALID_INPUT", "Неизвестный тип DNS", 400) } } finally { signal.removeEventListener("abort", abort) } }, }) }