Files
MikrotikManager/backend/src/services/traffic-flow-key-execution.ts
T

16 lines
855 B
TypeScript

type KeyPair = { publicKey: string; privateKey: string }
type Dependencies = {
read: () => Promise<KeyPair>
generate: () => KeyPair
installIfMissing: (keys: KeyPair) => Promise<boolean>
}
/** A conditional database write selects the winner across processes; no existing pair is rotated. */
export async function ensureTrafficFlowHostKeys(deps: Dependencies): Promise<{ publicKey: string; created: boolean }> {
const existing = await deps.read()
if (existing.publicKey && existing.privateKey) return { publicKey: existing.publicKey, created: false }
const created = await deps.installIfMissing(deps.generate())
const confirmed = await deps.read()
if (!confirmed.publicKey || !confirmed.privateKey) throw new Error("Создание ключей NetFlow не подтверждено")
return { publicKey: confirmed.publicKey, created }
}