Files
MikrotikManager/backend/src/modules/mcp/gre-native-actions.ts
T

27 lines
3.0 KiB
TypeScript

import { greNativeMutationSchema, mutateGreNativeTunnel, planGreNativeMutation, readGreNativeMutationState } from "../../services/gre-native-mutation.js"
import { greMutationProof } from "../../services/gre-mutation-state.js"
import { fingerprint } from "./security.js"
import { routerClient } from "./resources.js"
import { registerMcpService } from "./service-registry.js"
import { greMutationProtectionObjects } from "./gre-mutation-actions.js"
import type { ChangeEvidence } from "./action-types.js"
export function registerGreNativeServices(){
for(const operation of ["update","delete"]as const){
const current=async(input:Record<string,unknown>)=>greMutationProof(await readGreNativeMutationState((await routerClient(Number(input.serverId))).server,greNativeMutationSchema.parse(input.mutation)))
registerMcpService("network.gre.native."+operation,{current,execute:async input=>mutateGreNativeTunnel((await routerClient(Number(input.serverId))).server,greNativeMutationSchema.parse(input.mutation),String(input.beforeFingerprint)),verify:async(input,result)=>{
const receipt=result as {verified?:boolean;afterFingerprint?:string}|null
return receipt?.verified===true&&fingerprint(await current(input))===receipt.afterFingerprint
}})
}
}
export async function prepareGreNativeChange(serverId:number,operation:"update"|"delete",id:string|undefined,fields:unknown):Promise<ChangeEvidence>{
const mutation=greNativeMutationSchema.parse({operation,id,...(operation==="update"?{fields}:{})}),{server}=await routerClient(serverId)
const state=await readGreNativeMutationState(server,mutation)
planGreNativeMutation(mutation,state)
const before=greMutationProof(state)
return{target:{scope:"servers",serverIds:[serverId]},before,snapshots:[],objects:await greMutationProtectionObjects(serverId,state),mutationCost:2,requiresHuman:true,
steps:[{kind:"service",service:"network.gre.native."+operation,input:{serverId,mutation,beforeFingerprint:fingerprint(before)}}],
warnings:["Native GRE изменяет только сам интерфейс; адреса и зависимости не удаляются каскадно. Для rename/delete сначала снимите обнаруженные ссылки отдельными планами.","Перед записью проверяются полный видимый native proof, серверные DB-привязки, интерфейсные/маршрутные/протокольные и Firewall зависимости и зарегистрированные защиты сервера. Counters и runtime не являются конфигурацией.","После единственной native-команды проверяется точный эффект без изменения остальных объектов. Потерянный ответ или игнорирование команды не повторяются. Это проверка конфигурации, не связности; запись ревизии возвращается отдельно."]}
}