133 lines
15 KiB
TypeScript
133 lines
15 KiB
TypeScript
import { eq } from "drizzle-orm"
|
||
import { setTimeout as delay } from "node:timers/promises"
|
||
import { db } from "../db/index.js"
|
||
import { servers } from "../db/schema.js"
|
||
import { MikrotikClient, encodeRosId } from "./mikrotik.js"
|
||
import { getServerRowById } from "../modules/servers/repository/servers-repository.js"
|
||
import { fingerprint, McpError } from "../modules/mcp/security.js"
|
||
import { executeGuardedApplicationWrite } from "./config-mutation-policy.js"
|
||
import { withApplicationMutationLock, withConfigMutationLock } from "./config-mutation-lock.js"
|
||
import { withFlowConfigurationLock, preflightFlowHost, reconcileFlowHost, getFlowHostStatus } from "./traffic-flow-host-agent.js"
|
||
import { ensureHostKeys, getTrafficFlowSettingsRow, invalidateTrafficFlowSettingsCache, upsertHostPeer, enableTrafficFlowIngest } from "./traffic-flow-settings.js"
|
||
import { startTrafficFlowListener, refreshFlowExporterMap, getFlowListenerState } from "./traffic-flow-ingest.js"
|
||
import { invalidateFlowCatalogCache } from "./traffic-flow-topology.js"
|
||
import { listTrafficFlowHostFiles } from "./traffic-flow-host-files.js"
|
||
import { allocateOverlayAddress, FLOW_CACHE_ENTRIES, FLOW_TARGET_SRC_AUTO } from "./traffic-flow-overlay.js"
|
||
import { overlayConfigRow, overlayPeerEndpoint, readOverlayState, validateOverlayConfiguration, type OverlayOptions, type OverlayRow, type OverlayState } from "./traffic-flow-overlay-state.js"
|
||
import type { TrafficFlowOverlayResult } from "@mmapp/contracts/traffic-flow"
|
||
|
||
const wg="/interface/wireguard",peerPath=wg+"/peers",addressPath="/ip/address",routePath="/ip/route",filterPath="/ip/firewall/filter",flowPath="/ip/traffic-flow",ipfixPath=flowPath+"/ipfix",targetPath=flowPath+"/target"
|
||
const id=(row:OverlayRow)=>row[".id"]??row.id
|
||
const matches=(row:OverlayRow,fields:OverlayRow)=>Object.entries(fields).every(([key,value])=>row[key]===value)
|
||
const same=(left:unknown,right:unknown)=>fingerprint(left)===fingerprint(right)
|
||
const conflict=()=>new McpError("STATE_CONFLICT","Источник overlay изменился; нужен новый план",409)
|
||
const unknown=()=>new McpError("MUTATION_OUTCOME_UNKNOWN","Эффект шага overlay не подтверждён; запись не повторяется, нужна сверка",502)
|
||
export const ipfixFields=Object.fromEntries(["bytes","packets","src-address","dst-address","protocol","src-port","dst-port","in-interface","out-interface","gateway","first-forwarded","last-forwarded","nat-src-address","nat-dst-address","nat-src-port","nat-dst-port"].map(key=>[key,"yes"]))
|
||
export type OverlayExecutionReceipt={configurationVerified:boolean;afterProof:string;collectorListening:boolean;hostInstallationRequired:boolean;linuxHostApplied:boolean;completedStages:string[]}
|
||
|
||
/** One staged executor is used by REST and MCP. Each acknowledged native write is
|
||
* read back before the next stage; no command is retried or silently skipped. */
|
||
export async function executeFlowOverlay(serverId:number,opts:OverlayOptions,control:{authorize?:()=>Promise<unknown>;signal?:AbortSignal;expectedProof?:string}={}) :Promise<TrafficFlowOverlayResult&{receipt:OverlayExecutionReceipt}> {
|
||
const authorize=async()=>{if(control.signal?.aborted)throw new McpError("READ_CANCELLED","Overlay отменён",409);await control.authorize?.()}
|
||
await authorize()
|
||
return withApplicationMutationLock(()=>withConfigMutationLock([serverId],()=>withFlowConfigurationLock(async()=>{
|
||
let state=await readOverlayState(serverId,opts,authorize,control.signal)
|
||
if(control.expectedProof&&state.proof!==control.expectedProof)throw conflict()
|
||
const endpoint=validateOverlayConfiguration(state,opts),ids=state.catalog.map(row=>row.id)
|
||
const address=state.server.mgmtTunnelIp||allocateOverlayAddress(state.settings.prefix,state.settings.collectorIp,serverId,new Set(state.catalog.map(row=>row.mgmtTunnelIp).filter(Boolean)))
|
||
const cidr=address+"/"+state.settings.prefix.split("/")[1],routeDst=state.settings.collectorIp+"/32",steps:string[]=[]
|
||
const existingAddresses=(state.router[addressPath]??[]).filter(row=>row.interface==="wg-flow")
|
||
const existingRoutes=(state.router[routePath]??[]).filter(row=>row["dst-address"]===routeDst)
|
||
const existingFirewall=(state.router[filterPath]??[]).filter(row=>row.comment==="mm-wg-flow")
|
||
if(existingAddresses.length>1||existingAddresses.some(row=>row.address!==cidr||row.disabled==="true")||existingRoutes.length>1||existingRoutes.some(row=>row.gateway!=="wg-flow"||row.disabled==="true")||existingFirewall.length>1||existingFirewall.some(row=>!matches(row,{chain:"input",protocol:"udp","dst-port":"13232",action:"accept"})||row.disabled==="true"))throw new McpError("STATE_CONFLICT","Существующий адрес, маршрут или firewall wg-flow несовместим с overlay",409)
|
||
await authorize()
|
||
if(state.agentConfigured&&!state.settings.hasHostPublicKey){if(!(await getFlowHostStatus()).reachable)throw new McpError("STATE_UNAVAILABLE","Агент хоста не готов к созданию overlay",409)}
|
||
else await preflightFlowHost()
|
||
await authorize()
|
||
const source=()=>readOverlayState(serverId,opts,authorize,control.signal)
|
||
const fence=async()=>{await authorize();if((await source()).proof!==state.proof)throw conflict()}
|
||
const client=async()=>{await fence();const row=await getServerRowById(serverId);await authorize();if(!row)throw conflict();return MikrotikClient.fromServer(row)}
|
||
const dbStage=async(name:string,write:()=>Promise<unknown>,allowed:(before:OverlayState,after:OverlayState)=>boolean)=>{
|
||
await fence();const before=state;await authorize();await write();await authorize();const after=await source()
|
||
if(!allowed(before,after))throw unknown();state=after;steps.push(name)
|
||
}
|
||
const routerStage=async(name:string,path:string,method:"put"|"patch"|"delete"|"post",body:Record<string,string>={},rowId?:string)=>{
|
||
const native=await client(),before=state,rows=before.router[path]
|
||
if(!rows)throw conflict()
|
||
const fields=await overlayConfigRow(path,body),dispatchPath=method==="post"?path+"/set":rowId?path+"/"+encodeRosId(rowId):path
|
||
await authorize()
|
||
if(method==="put")await native.put(dispatchPath,body,15000,fence)
|
||
else if(method==="patch")await native.patch(dispatchPath,body,15000,fence)
|
||
else if(method==="delete")await native.delete(dispatchPath,10000,fence)
|
||
else {await fence();await native.post(dispatchPath,body,15000,control.signal)}
|
||
await authorize();const after=await source(),next=after.router[path]
|
||
if(!next)throw unknown()
|
||
let valid=false
|
||
if(method==="put") {
|
||
const added=next.filter(row=>!rows.some(old=>id(old)===id(row)))
|
||
valid=added.length===1&&Boolean(id(added[0]))&&matches(added[0],fields)&&same(next.filter(row=>id(row)!==id(added[0])),rows)
|
||
}else if(method==="delete")valid=same(next,rows.filter(row=>id(row)!==rowId))
|
||
else {
|
||
const target=rowId?rows.find(row=>id(row)===rowId):rows[0]
|
||
if(target)valid=same(next,rows.map(row=>row===target?{...row,...fields}:row))
|
||
else valid=method==="post"&&next.length===1&&matches(next[0],fields)
|
||
}
|
||
const oldOther={...before.configuration,router:{...before.router,[path]:null}},newOther={...after.configuration,router:{...after.router,[path]:null}}
|
||
// Creating WG also creates its generic interface row. No other implicit config is accepted.
|
||
if(path===wg&&method==="put") {
|
||
const oldInterfaces=before.router["/interface"]??[],newInterfaces=after.router["/interface"]??[],added=newInterfaces.filter(row=>!oldInterfaces.some(old=>id(old)===id(row)))
|
||
valid=valid&&added.length===1&&added[0].name==="wg-flow"&&same(newInterfaces.filter(row=>id(row)!==id(added[0])),oldInterfaces)
|
||
oldOther.router["/interface"]=null;newOther.router["/interface"]=null
|
||
}
|
||
if(!valid||!same(oldOther,newOther))throw unknown()
|
||
state=after;steps.push(name)
|
||
}
|
||
const unchangedExceptSettings=(before:OverlayState,after:OverlayState,keys:string[])=>same(before.catalog,after.catalog)&&same(before.router,after.router)&&same(Object.fromEntries(Object.entries(before.settings).filter(([key])=>!keys.includes(key))),Object.fromEntries(Object.entries(after.settings).filter(([key])=>!keys.includes(key))))
|
||
return executeGuardedApplicationWrite({kind:"netflow-overlay",id:"1"},ids,async()=>{
|
||
if(!state.settings.hasHostPublicKey)await dbStage("host-keys",ensureHostKeys,(before,after)=>after.settings.hasHostPrivateKey&&after.settings.hasHostPublicKey&&unchangedExceptSettings(before,after,["hasHostPrivateKey","hasHostPublicKey","hostPrivateKeyProof","hostPublicKeyProof"]))
|
||
await fence();await preflightFlowHost();await authorize()
|
||
if(!(state.router[wg]??[]).some(row=>row.name==="wg-flow"))await routerStage("wireguard-interface",wg,"put",{name:"wg-flow","listen-port":"13232",mtu:"1420",comment:"MikrotikManager traffic-flow overlay"})
|
||
if(!existingAddresses.length)await routerStage("interface-address",addressPath,"put",{address:cidr,interface:"wg-flow"})
|
||
await fence();invalidateTrafficFlowSettingsCache();const hostKey=(await getTrafficFlowSettingsRow()).hostPublicKey;await authorize()
|
||
const keyFields=await overlayConfigRow(peerPath,{"public-key":hostKey})
|
||
const peers=(state.router[peerPath]??[]).filter(row=>row.interface==="wg-flow"&&row["public-key"]===keyFields["public-key"])
|
||
if(peers.length>1)throw conflict()
|
||
const peerFields={interface:"wg-flow","public-key":hostKey,"allowed-address":routeDst,comment:"MM traffic-flow collector",name:"mm-collector"}
|
||
if(peers[0]?.["endpoint-address"])await routerStage("peer-endpoint-remove",peerPath,"delete",{},id(peers[0]))
|
||
if(!peers.length||peers[0]["endpoint-address"])await routerStage("collector-peer",peerPath,"put",peerFields)
|
||
else await routerStage("collector-peer",peerPath,"patch",peerFields,id(peers[0]))
|
||
if(!existingRoutes.length)await routerStage("collector-route",routePath,"put",{"dst-address":routeDst,gateway:"wg-flow",comment:"MM traffic-flow collector"})
|
||
if(!existingFirewall.length)await routerStage("firewall-input",filterPath,"put",{chain:"input",protocol:"udp","dst-port":"13232",action:"accept",comment:"mm-wg-flow"})
|
||
const singleton=state.router[flowPath]?.[0]
|
||
await routerStage("traffic-flow",flowPath,singleton&&id(singleton)?"patch":"post",{enabled:"yes",interfaces:"all","cache-entries":FLOW_CACHE_ENTRIES,"active-flow-timeout":"1m","inactive-flow-timeout":"15s"},singleton?id(singleton):undefined)
|
||
if(state.router[ipfixPath]!==null){const fields=state.router[ipfixPath]?.[0];await routerStage("ipfix-fields",ipfixPath,fields&&id(fields)?"patch":"post",ipfixFields,fields?id(fields):undefined)}
|
||
const targets=(state.router[targetPath]??[]).filter(row=>row["dst-address"]===state.settings.collectorIp)
|
||
if(targets.length>1)throw conflict()
|
||
await routerStage("ipfix-target",targetPath,targets.length?"patch":"put",{"dst-address":state.settings.collectorIp,"src-address":FLOW_TARGET_SRC_AUTO,port:String(state.settings.flowListenPort),version:"ipfix"},targets[0]?id(targets[0]):undefined)
|
||
if(opts.disableGreFastPath)for(const row of state.router["/interface/gre"]??[])if(row["allow-fast-path"]!=="false") {
|
||
if(!id(row)||row.dynamic==="true")throw new McpError("PROTECTED_OBJECT","Динамический или неопознанный GRE нельзя менять",409)
|
||
await routerStage("gre-slow-path","/interface/gre","patch",{"allow-fast-path":"no"},id(row))
|
||
}
|
||
await fence();const native=await client(),raw=await native.get<unknown>(wg,10000,control.signal);await authorize()
|
||
const iface=(Array.isArray(raw)?raw:[]).find(row=>row&&typeof row==="object"&&row.name==="wg-flow") as Record<string,unknown>|undefined
|
||
const publicKey=String(iface?.["public-key"]??"")
|
||
if(!publicKey)throw unknown()
|
||
await dbStage("server-overlay-address",async()=>{await db.update(servers).set({mgmtTunnelIp:address,updatedAt:new Date().toISOString()}).where(eq(servers.id,serverId));invalidateFlowCatalogCache()},(before,after)=>same(before.settings,after.settings)&&same(before.router,after.router)&&same(before.catalog.map(row=>row.id===serverId?{...row,mgmtTunnelIp:address,updatedAt:"changed"}:row),after.catalog.map(row=>row.id===serverId?{...row,updatedAt:"changed"}:row)))
|
||
await dbStage("collector-host-peer",()=>upsertHostPeer({serverId,name:state.server.name||state.server.host,publicKey,allowedIps:[address+"/32"],address,endpoint:overlayPeerEndpoint(endpoint)}),(before,after)=>unchangedExceptSettings(before,after,["peersJson"])&&same(before.settings.peersJson.filter(peer=>peer.serverId!==serverId),after.settings.peersJson.filter(peer=>peer.serverId!==serverId))&&after.settings.peersJson.filter(peer=>peer.serverId===serverId).length===1)
|
||
await dbStage("enable-ingest",enableTrafficFlowIngest,(before,after)=>after.settings.enabled&&unchangedExceptSettings(before,after,["enabled"]))
|
||
let hostStatus:Awaited<ReturnType<typeof getFlowHostStatus>>|undefined,hostError:string|undefined
|
||
if(state.agentConfigured){await fence();try{hostStatus=await reconcileFlowHost();await authorize();if(hostStatus.configMatches!==true)hostError="Хост не подтвердил конфигурацию"}catch{await authorize();hostError="Применение хоста не подтверждено; нужна сверка без повтора"}steps.push(hostError?"host-pending":"host-applied")}
|
||
if(!hostError){
|
||
await fence();await startTrafficFlowListener();await authorize();await refreshFlowExporterMap();await authorize()
|
||
// Worker start acknowledges scheduling, not bind. Manual installs can legitimately lack the host interface.
|
||
if(state.agentConfigured)for(let n=0;n<100&&!getFlowListenerState().bound;n++){await delay(100,undefined,{signal:control.signal});await authorize()}
|
||
if(state.agentConfigured&&!getFlowListenerState().bound)throw unknown()
|
||
steps.push(getFlowListenerState().bound?"collector-listening":"collector-start-requested")
|
||
}
|
||
await fence()
|
||
const receipt:OverlayExecutionReceipt={configurationVerified:!hostError,afterProof:state.proof,collectorListening:getFlowListenerState().bound,hostInstallationRequired:!state.agentConfigured,linuxHostApplied:state.agentConfigured&&!hostError,completedStages:steps}
|
||
return {ok:!hostError,serverId,interfaceName:"wg-flow",address,publicKey,linuxPeerBlock:`[Peer]\nPublicKey = ${publicKey}\nAllowedIPs = ${address}/32\nEndpoint = ${overlayPeerEndpoint(endpoint)}\nPersistentKeepalive = 25`,trafficFlow:true,steps,hostFiles:state.agentConfigured?[]:await listTrafficFlowHostFiles(),stage:hostError?"host-pending":state.agentConfigured?"applied":"manual",...(hostStatus?{hostStatus}:{}),...(hostError?{hostError}:{}),receipt}
|
||
},async()=>[{kind:"netflow-settings",id:"1"},{kind:"netflow-host",id:"1"},{kind:"netflow-host-files",id:"1"}])
|
||
})))
|
||
}
|