Docker images / prepare-release (push) Successful in 9s
Docker images / updater-image (push) Successful in 42s
Docker images / netflow-agent-image (push) Successful in 36s
Docker images / backend-test (push) Successful in 9m43s
Docker images / backend-image (push) Successful in 1m59s
Docker images / frontend-image (push) Successful in 15m47s
Docker images / notify-webhook (push) Successful in 0s
Docker images / publish-release (push) Successful in 10s
40 lines
1.8 KiB
TypeScript
40 lines
1.8 KiB
TypeScript
import { createHash, randomBytes } from "node:crypto"
|
|
import { performance } from "node:perf_hooks"
|
|
import type { IxBondBundle } from "@mmapp/contracts/internet-exchange"
|
|
import { IxConflict } from "./internet-exchange-plan.js"
|
|
|
|
export type IxImportGre = { local: string; remote: string; address: string; psk: string }
|
|
|
|
export type IxImportBundle =
|
|
| { version: 1; kind: "wireguard"; name: string; config: string }
|
|
| { version: 1; kind: "gre-ipsec"; name: string; gre: IxImportGre }
|
|
| { version: 1; kind: "bond"; bond: IxBondBundle }
|
|
|
|
const digest = (code: string) => createHash("sha256").update(code).digest("hex")
|
|
const maxLinks = 256
|
|
const expired = () => new IxConflict("Ссылка истекла или уже использована", 410)
|
|
|
|
export function createIxImportLinks(now = () => performance.now()) {
|
|
const links = new Map<string, { deadline: number; load: () => Promise<IxImportBundle> }>()
|
|
return {
|
|
issue(deadline: number, load: () => Promise<IxImportBundle>) {
|
|
const current = now()
|
|
for (const [key, entry] of links) if (entry.deadline <= current) links.delete(key)
|
|
if (deadline <= current) throw expired()
|
|
if (links.size >= maxLinks) throw new IxConflict("Слишком много активных ссылок импорта", 429)
|
|
const code = randomBytes(24).toString("base64url")
|
|
// shortcut: pending links live in this backend process, use shared storage if the backend is replicated.
|
|
links.set(digest(code), { deadline, load })
|
|
return code
|
|
},
|
|
async redeem(code: string) {
|
|
const key = digest(code), entry = links.get(key)
|
|
links.delete(key)
|
|
if (!entry || entry.deadline <= now()) throw expired()
|
|
const bundle = await entry.load()
|
|
if (entry.deadline <= now()) throw expired()
|
|
return bundle
|
|
},
|
|
}
|
|
}
|