import { NextResponse } from "next/server";
import { getPaths } from "yt-dlp-transcript-common/lib/paths";
import { NETWORK_REQUEST_KEYS } from "yt-dlp-transcript-common/lib/networkExits";
import { networkReport, probeNetworkExits } from "yt-dlp-transcript-common/controller/networkExits";
import { NetworkSaveError, saveNetworkRequest } from "../../../settings/networkSave";
import { OpsInputError, ops } from "../_lib";
import { readRoute, redactSecrets } from "../_read";
import { directEcho } from "../../../settings/networkProbe";
export const dynamic = "force-dynamic";
// NAMED NETWORK EXITS (lane network-exits; common/lib/networkExits.ts).
//
// GET /api/ops/network[?probe=1]
// -> { ok, network: { limits, trialScope, exits: [{id, label, display, scheme,
// host, port, credentials, enabled, probe, channels}], platforms: {
: {exits, mode, kinds, pool, allRest}
// | {…, ignored}}, jars, echoUrl, rests: [{platform, exit, until, fails}],
// usage: {"1d": {"/": {requests, rateLimits, botChecks,
// rests, proxyFailures}}, "7d": …}, lastProbeAt } }
// Reads files only; no field holds a secret (credentials live in
// network.credentialsFile). `probe=1` — the operator's explicit probe —
// first asks each enabled exit's proxy to answer (connect + SOCKS
// greeting/login; no destination) and, only when network.echoUrl is set,
// fetches it through each exit and once direct to name the address each
// presents.
//
// POST { exits? | upsertExit? | removeExit? | platforms? | jars? | echoUrl? |
// credentialsFile? | startupProbe? }
// -> { ok, network: }
// One write through the network writer (settings/networkSave.ts): refused
// whole, every problem named, when it routes anything outside the trial
// (rumble and twitch listings), gives a cookie jar an exit, or sends a
// proxy URL with credentials in it.
export async function GET(request: Request) {
return readRoute(request, ["probe"], async (q) => {
const paths = getPaths();
if (q.get("probe") === "1") await probeNetworkExits(paths, undefined, new Date(), { explicit: true, getDirect: directEcho });
return { network: redactSecrets(await networkReport(paths)) };
});
}
export async function POST(request: Request) {
return ops(request, NETWORK_REQUEST_KEYS, async (body) => {
try {
return NextResponse.json({ ok: true, network: redactSecrets(await saveNetworkRequest(body)) });
} catch (e) {
if (e instanceof NetworkSaveError) throw new OpsInputError(e.message);
throw e;
}
});
}