mirror of
https://github.com/Kynde/airc.git
synced 2026-10-20 08:31:38 +02:00
Tailscale Funnel has been the flaky one here: the public *.ts.net path goes
dead while every local tailscale check stays green, and it takes the advertised
URL down with it. ngrok has never done that. But `publicUrl` — one string,
outranking ngrok by construction — left no way to lean on ngrok without
deleting the Funnel URL, and no place to park a third tunnel while trying it
out.
So tunnels become a group, one entry each with its own `enabled` flag:
"tunnels": {
"prefer": ["ngrok", "tailscale", "external"],
"ngrok": { "enabled": false, "domain": ..., "binary": ..., "apiUrl": ... },
"tailscale": { "enabled": false, "url": "" },
"external": { "enabled": false, "url": "" }
}
`prefer` is precedence, not exclusivity. Every enabled tunnel still reaches the
server; the first one that can name a URL is merely what gets baked into
bookmarks, the pairing QR and /api/config. Swapping the advertised route is a
reorder of that list plus a restart, with the losing tunnel left configured and
live as a spare. `external` is the slot for whatever comes next — Cloudflare, a
reverse proxy — without another schema change. A partial `prefer` ranks the
rest last rather than hiding them; an unknown name in it is a load error, since
a typo there would otherwise silently re-point the advertised URL.
Only ngrok is supervised, as before: `tailscale` and `external` are URLs airc
advertises for tunnels somebody else keeps up, which is all `publicUrl` ever
was. Because a reserved ngrok domain is knowable before the agent is up, it is
now advertised from the first request instead of after the agent reports; the
live URL replaces it when it lands (the two differ only without a reserved
domain), and both paths log through one announce().
`--no-ngrok` now means only what it says — skip the agent, let the next tunnel
down the list take over. `--no-tunnels` is new and means LAN/loopback only;
`tools/airc local` and `npm run dev` use it, which is what keeps local mode
preferring a LAN URL in pairing payloads now that disabling ngrok no longer
implies disabling everything. /healthz and /api/status gained a `tunnels` block
listing the routes in preference order with the advertised one named, and
`tools/airc status` prints that name next to the URL.
Old configs load untouched: a top-level `ngrok` block becomes `tunnels.ngrok`,
a top-level `publicUrl` becomes `tunnels.external.url`, and a file still in
that shape keeps its old precedence — publicUrl over ngrok — so pulling this
cannot silently re-point an existing pairing at a different tunnel. Only a
config that opts into `tunnels` gets the new ngrok-first default.
test/tunnels.test.js covers the ordering, both migration paths and the two
flags. Checked live against both orderings on a scratch port before restarting
the real server; with ngrok advertised, both it and the still-enabled Funnel
answer 200. Worth naming the cost: ngrok's free-tier interstitial does not
render in the Tesla browser, so the car keeps its ts.net bookmark, which works
precisely because a demoted tunnel stays up.
100 lines
4.6 KiB
JavaScript
100 lines
4.6 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
const { test } = require("node:test");
|
|
|
|
const { loadConfig, advertisedUrl, pairingPayload } = require("../src/config");
|
|
|
|
// loadConfig persists generated tokens back into the file it read, so every
|
|
// case gets its own throwaway config. Tokens are pre-set to keep the rewrite
|
|
// (and its 0600 chmod) out of the way of what's being asserted.
|
|
function withConfig(fileConfig, argv = []) {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "airc-tunnels-test-"));
|
|
const configPath = path.join(dir, "config.json");
|
|
fs.writeFileSync(configPath, JSON.stringify({
|
|
port: 9099,
|
|
viewToken: "v".repeat(32),
|
|
controlToken: "c".repeat(32),
|
|
...fileConfig,
|
|
}));
|
|
return loadConfig(["--config", configPath, "--no-tls", ...argv]).config;
|
|
}
|
|
|
|
const NGROK = { enabled: true, domain: "d.ngrok-free.dev" };
|
|
const TAILSCALE = { enabled: true, url: "https://host.example.ts.net" };
|
|
|
|
test("tunnels.prefer picks the advertised route, ngrok first by default", () => {
|
|
const config = withConfig({ tunnels: { ngrok: NGROK, tailscale: TAILSCALE } });
|
|
assert.deepStrictEqual(config.tunnels.prefer, ["ngrok", "tailscale", "external"]);
|
|
// The reserved domain is advertised before the agent is up; the live URL the
|
|
// agent reports takes over once known.
|
|
assert.strictEqual(advertisedUrl(config), "https://d.ngrok-free.dev");
|
|
assert.strictEqual(advertisedUrl(config, "https://live.ngrok-free.dev"), "https://live.ngrok-free.dev");
|
|
});
|
|
|
|
test("a lower-ranked tunnel takes over when the preferred one is off", () => {
|
|
const config = withConfig({
|
|
tunnels: { prefer: ["ngrok", "tailscale"], ngrok: { ...NGROK, enabled: false }, tailscale: TAILSCALE },
|
|
});
|
|
assert.strictEqual(advertisedUrl(config), "https://host.example.ts.net");
|
|
});
|
|
|
|
test("prefer reorders which tunnel wins", () => {
|
|
const config = withConfig({
|
|
tunnels: { prefer: ["tailscale", "ngrok"], ngrok: NGROK, tailscale: TAILSCALE },
|
|
});
|
|
assert.strictEqual(advertisedUrl(config, "https://live.ngrok-free.dev"), "https://host.example.ts.net");
|
|
});
|
|
|
|
test("a tunnel left out of prefer ranks last instead of disappearing", () => {
|
|
const config = withConfig({ tunnels: { prefer: ["tailscale"], ngrok: NGROK, tailscale: { ...TAILSCALE, enabled: false } } });
|
|
assert.deepStrictEqual(config.tunnels.prefer, ["tailscale", "ngrok", "external"]);
|
|
assert.strictEqual(advertisedUrl(config), "https://d.ngrok-free.dev");
|
|
});
|
|
|
|
test("legacy publicUrl loads as tunnels.external and keeps outranking ngrok", () => {
|
|
const config = withConfig({ publicUrl: "https://legacy.example.com/", ngrok: NGROK });
|
|
assert.deepStrictEqual(config.tunnels.external, { enabled: true, url: "https://legacy.example.com" });
|
|
assert.deepStrictEqual(config.tunnels.prefer, ["external", "ngrok", "tailscale"]);
|
|
assert.strictEqual(advertisedUrl(config, "https://live.ngrok-free.dev"), "https://legacy.example.com");
|
|
assert.strictEqual(config.publicUrl, undefined);
|
|
});
|
|
|
|
test("an explicit tunnels block overrides the legacy keys it duplicates", () => {
|
|
const config = withConfig({
|
|
ngrok: { enabled: true, domain: "legacy.ngrok-free.dev" },
|
|
tunnels: { ngrok: NGROK },
|
|
});
|
|
assert.strictEqual(advertisedUrl(config), "https://d.ngrok-free.dev");
|
|
});
|
|
|
|
test("--no-ngrok drops ngrok only; --no-tunnels drops every route", () => {
|
|
const file = { tunnels: { ngrok: NGROK, tailscale: TAILSCALE } };
|
|
assert.strictEqual(advertisedUrl(withConfig(file, ["--no-ngrok"])), "https://host.example.ts.net");
|
|
assert.strictEqual(advertisedUrl(withConfig(file, ["--no-tunnels"])), "");
|
|
});
|
|
|
|
test("pairing prefers the tunnel when one is configured and LAN when none is", () => {
|
|
const file = { tunnels: { ngrok: NGROK, tailscale: TAILSCALE } };
|
|
assert.strictEqual(pairingPayload(withConfig(file)).baseUrl, "https://d.ngrok-free.dev");
|
|
const local = pairingPayload(withConfig(file, ["--no-tunnels"]));
|
|
// With no tunnel the payload falls back to loopback, and the LAN address (a
|
|
// box with no non-internal IPv4 has none) is what pairing should prefer.
|
|
assert.strictEqual(local.publicUrl, "http://127.0.0.1:9099");
|
|
assert.strictEqual(local.baseUrl, local.lanUrls[0] || "http://127.0.0.1:9099");
|
|
});
|
|
|
|
test("bad tunnel config is rejected at load", () => {
|
|
assert.throws(
|
|
() => withConfig({ tunnels: { tailscale: { enabled: true, url: "host.example.ts.net" } } }),
|
|
/tunnels\.tailscale\.url must start with http/,
|
|
);
|
|
assert.throws(
|
|
() => withConfig({ tunnels: { prefer: ["ngrok", "cloudflare"] } }),
|
|
/unknown tunnel "cloudflare"/,
|
|
);
|
|
});
|