From 663ad9416ee1700198cb587750db56c6f7392186 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 20 Sep 2026 00:20:54 +0200 Subject: [PATCH] fix(subscription): serve last known good nodes instead of emptying the pool on fetch or parse failure --- .../fixes/14222-subscription-lastgood.md | 1 + src/lib/proxySubscription/parse.ts | 9 + .../proxySubscription/subscriptionService.ts | 31 ++- tests/unit/proxySubscription.lastgood.test.ts | 198 ++++++++++++++++++ 4 files changed, 236 insertions(+), 3 deletions(-) create mode 100644 changelog.d/fixes/14222-subscription-lastgood.md create mode 100644 tests/unit/proxySubscription.lastgood.test.ts diff --git a/changelog.d/fixes/14222-subscription-lastgood.md b/changelog.d/fixes/14222-subscription-lastgood.md new file mode 100644 index 000000000000..6b2b74ab4fc3 --- /dev/null +++ b/changelog.d/fixes/14222-subscription-lastgood.md @@ -0,0 +1 @@ +- **fix(subscription):** serve last known good nodes instead of emptying the pool on fetch or parse failure ([#14222](https://github.com/diegosouzapw/OmniRoute/pull/14222)) — thanks @maxmad64bis diff --git a/src/lib/proxySubscription/parse.ts b/src/lib/proxySubscription/parse.ts index 122adc474821..4dac7cf1e62b 100644 --- a/src/lib/proxySubscription/parse.ts +++ b/src/lib/proxySubscription/parse.ts @@ -310,6 +310,15 @@ export function parseSubscription(body: string): ParsedSubscription { return { nodes: [], needsCore: [], format: "unknown" }; } +/** + * Refuse unrecognized content before sync: `format: "unknown"` carries no + * usable node. Every recognized format (including `empty` and a + * valid-but-nodeless feed) passes and keeps the current path. + */ +export function isUsableSubscriptionContent(parsed: ParsedSubscription): boolean { + return parsed.format !== "unknown"; +} + /** Redacted node summary for storage/display (no secrets). */ export function redactedNodeSummary(parsed: ParsedSubscription): Array> { const direct = parsed.nodes.map((n) => ({ diff --git a/src/lib/proxySubscription/subscriptionService.ts b/src/lib/proxySubscription/subscriptionService.ts index b7e93bb0c2ca..e50444490b6d 100644 --- a/src/lib/proxySubscription/subscriptionService.ts +++ b/src/lib/proxySubscription/subscriptionService.ts @@ -47,7 +47,7 @@ import { } from "./fetchGuard"; import { areLocalProviderUrlsAllowed } from "@/shared/network/outboundUrlGuardPolicy"; import { withRetry } from "./fetchRetry"; -import { parseSubscription, redactedNodeSummary, type ParsedSubscription } from "./parse"; +import { isUsableSubscriptionContent, parseSubscription, redactedNodeSummary, type ParsedSubscription } from "./parse"; export type ProxySubscriptionMode = "global" | "rule"; export type ProxySubscriptionStatus = "ok" | "error" | "empty"; @@ -454,7 +454,7 @@ async function syncSubscriptionUnsafe(id: string): Promise { id, "error", `Fetch failed: ${msg}`, - null, + sub.lastNodes, new Date().toISOString(), fetchConsec ); @@ -470,6 +470,31 @@ async function syncSubscriptionUnsafe(id: string): Promise { } const parsed: ParsedSubscription = parseSubscription(body); + + // Refuse unrecognized content before any registry write: an invalid or + // temporarily broken feed must never empty the pool. Serve the persisted + // last-known-good nodes instead and leave every registry row untouched. + if (!isUsableSubscriptionContent(parsed)) { + const refuseError = subscriptionErrorCode("NO_USABLE_NODES"); + const invalidConsec = (sub.consecutiveFailures || 0) + 1; + await updateSubscriptionStatus( + id, + "error", + refuseError, + sub.lastNodes, + new Date().toISOString(), + invalidConsec + ); + return { + subscriptionId: id, + nodes: 0, + needsCore: 0, + boundProxies: 0, + status: "error", + error: refuseError, + applied: false, + }; + } const db = getDbInstance(); const keptIds: string[] = []; @@ -673,7 +698,7 @@ async function syncSubscriptionUnsafe(id: string): Promise { id, "error", `Sync write failed: ${msg}`, - null, + sub.lastNodes, new Date().toISOString(), writeConsec ); diff --git a/tests/unit/proxySubscription.lastgood.test.ts b/tests/unit/proxySubscription.lastgood.test.ts new file mode 100644 index 000000000000..bccf626ce014 --- /dev/null +++ b/tests/unit/proxySubscription.lastgood.test.ts @@ -0,0 +1,198 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import http from "node:http"; + +// A failed or invalid source must serve the persisted last_nodes instead of +// emptying the pool. Tests use real 127.0.0.1 servers; loopback fetch targets +// are allowed by the local-first policy. + +const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-sub-lastgood-")); +process.env.DATA_DIR = TEST_DATA_DIR; + +const core = await import("../../src/lib/db/core.ts"); +const proxies = await import("../../src/lib/db/proxies.ts"); +const sub = await import("../../src/lib/proxySubscription/index.ts"); + +function reset() { + core.resetDbInstance(); + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }); + fs.mkdirSync(TEST_DATA_DIR, { recursive: true }); +} + +test.after(() => { + core.resetDbInstance(); + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }); +}); + +const LAST_NODES = [ + { + name: "good-node", + type: "http", + host: "10.9.9.9", + port: 8080, + rawProtocol: "http", + hasAuth: false, + }, +]; + +function seedOkSubscription(id: string, url: string) { + const now = new Date().toISOString(); + core + .getDbInstance() + .prepare( + `INSERT INTO proxy_subscriptions + (id, name, url, enabled, mode, rule_providers, update_interval_minutes, status, error, last_nodes, last_error_at, consecutive_failures, created_at, updated_at) + VALUES (?, ?, ?, 1, 'global', NULL, 60, 'ok', NULL, ?, NULL, 0, ?, ?)` + ) + .run(id, `sub-${id}`, url, JSON.stringify(LAST_NODES), now, now); +} + +async function seedPool(id: string) { + await proxies.createProxy({ + name: "good-node", + type: "http", + host: "10.9.9.9", + port: 8080, + source: "subscription", + subscriptionId: id, + status: "active", + }); +} + +function poolSize(id: string): number { + const rows = core + .getDbInstance() + .prepare("SELECT id FROM proxy_registry WHERE subscription_id = ?") + .all(id) as Array<{ id: string }>; + return rows.length; +} + +function startBodyServer(body: string): Promise<{ url: string; close: () => Promise }> { + return new Promise((resolve) => { + const srv = http.createServer((_req, res) => { + res.writeHead(200, { "Content-Type": "text/plain" }); + res.end(body); + }); + srv.listen(0, "127.0.0.1", () => { + const addr = srv.address(); + if (!addr || typeof addr === "string") throw new Error("no addr"); + resolve({ + url: `http://127.0.0.1:${addr.port}/list`, + close: () => new Promise((r) => srv.close(() => r())), + }); + }); + }); +} + +/** URL of a port nothing listens on (connection refused through the real fetch path). */ +function deadUrl(): Promise { + return new Promise((resolve, reject) => { + const srv = http.createServer(() => {}); + srv.listen(0, "127.0.0.1", () => { + const addr = srv.address(); + if (!addr || typeof addr === "string") { + reject(new Error("no addr")); + return; + } + const port = addr.port; + srv.close(() => resolve(`http://127.0.0.1:${port}/list`)); + }); + }); +} + +// ─────────────────── Predicate contract (pure, no DB) ─────────────────── + +test("unrecognized content is refused, every recognized format passes", async () => { + const parse = await import("../../src/lib/proxySubscription/parse.ts"); + assert.equal(typeof parse.isUsableSubscriptionContent, "function"); + const { parseSubscription, isUsableSubscriptionContent } = parse; + // Invalid prose -> format "unknown" -> refused. + assert.equal( + isUsableSubscriptionContent( + parseSubscription("just some random text that is not a subscription") + ), + false + ); + // Empty body -> format "empty" -> recognized, keeps the current path (M1). + assert.equal(isUsableSubscriptionContent(parseSubscription("")), true); + // Valid-but-nodeless feed -> recognized "clash-yaml", keeps the current path (R2). + assert.equal(isUsableSubscriptionContent(parseSubscription("proxies: []\n")), true); + // Valid feed with a node -> passes. + assert.equal( + isUsableSubscriptionContent( + parseSubscription( + "proxies:\n - name: x\n type: http\n server: 1.2.3.4\n port: 8080\n" + ) + ), + true + ); +}); + +// ─────────────────── Fetch failure serves last-good ─────────────────── + +test("fetch failure keeps status error, serves last_nodes, pool untouched", async () => { + reset(); + const url = await deadUrl(); + seedOkSubscription("t1", url); + await seedPool("t1"); + try { + const res = await sub.syncSubscription("t1"); + assert.equal(res.status, "error"); + assert.equal(res.applied, false); + assert.equal(res.nodes, 0); + const row = await sub.getSubscriptionById("t1"); + assert.ok(row); + assert.deepEqual(row.lastNodes, LAST_NODES); + assert.equal(row.consecutiveFailures, 1); + assert.equal(poolSize("t1"), 1); + } finally { + reset(); + } +}); + +// ─────────────────── Invalid content refused before sync ─────────────────── + +test("invalid content is refused before sync, pool and last_nodes intact", async () => { + reset(); + const server = await startBodyServer("not a subscription at all !!!"); + try { + seedOkSubscription("t2", server.url); + await seedPool("t2"); + const res = await sub.syncSubscription("t2"); + assert.equal(res.status, "error"); + assert.equal(res.applied, false); + assert.ok((res.error ?? "").includes("NO_USABLE_NODES")); + const row = await sub.getSubscriptionById("t2"); + assert.ok(row); + assert.ok((row.error ?? "").includes("NO_USABLE_NODES")); + assert.deepEqual(row.lastNodes, LAST_NODES); + assert.equal(poolSize("t2"), 1); + } finally { + await server.close(); + reset(); + } +}); + +// ─────────────────── Consecutive-failure counter, pool intact ─────────────────── + +test("repeated invalid syncs bump the counter, pool stays intact", async () => { + reset(); + const server = await startBodyServer("still not a subscription !!!"); + try { + seedOkSubscription("t3", server.url); + await seedPool("t3"); + await sub.syncSubscription("t3"); + assert.equal((await sub.getSubscriptionById("t3"))?.consecutiveFailures, 1); + assert.equal(poolSize("t3"), 1); + await sub.syncSubscription("t3"); + assert.equal((await sub.getSubscriptionById("t3"))?.consecutiveFailures, 2); + assert.equal(poolSize("t3"), 1); + assert.deepEqual((await sub.getSubscriptionById("t3"))?.lastNodes, LAST_NODES); + } finally { + await server.close(); + reset(); + } +});