mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-26 02:05:31 +00:00
fix(daemon): keep managing Tailscale after a main port change
Classify extra listeners by their interface address. Turning Tailscale off removes matching listeners on every port and checks whether the caller uses one.
This commit is contained in:
@@ -189,6 +189,88 @@ describe("paseo daemon bootstrap", () => {
|
||||
},
|
||||
);
|
||||
|
||||
test("keeps Tailscale on its original port across a main port change", async () => {
|
||||
const tailscaleIp = "127.0.0.2";
|
||||
const carrierNatIp = "100.127.255.254";
|
||||
const dependencies = {
|
||||
getTailscaleNetwork: () => ({
|
||||
addresses: [tailscaleIp],
|
||||
otherAddresses: new Set([carrierNatIp]),
|
||||
}),
|
||||
};
|
||||
const original = await createTestPaseoDaemon({ ...dependencies, cleanup: false });
|
||||
const oldAddress = `${tailscaleIp}:${original.port}`;
|
||||
const first = new DaemonClient({ url: `ws://127.0.0.1:${original.port}/ws` });
|
||||
const configPath = path.join(original.paseoHome, "config.json");
|
||||
const persist = (listen: string) =>
|
||||
writeFile(configPath, JSON.stringify({ version: 1, daemon: { listen } }));
|
||||
const readListen = async () => JSON.parse(await readFile(configPath, "utf8")).daemon.listen;
|
||||
let restarted: Awaited<ReturnType<typeof createPaseoDaemon>> | undefined;
|
||||
let local: DaemonClient | undefined;
|
||||
let remote: DaemonClient | undefined;
|
||||
try {
|
||||
await first.connect();
|
||||
await first.setTailscaleEnabled(true);
|
||||
const [, savedExtra] = (await readListen()).split(",");
|
||||
expect(savedExtra).toBe(oldAddress);
|
||||
|
||||
const newPort = await findFreePort();
|
||||
const newMain = `127.0.0.1:${newPort}`;
|
||||
await persist(`${newMain},${savedExtra}`);
|
||||
await first.close();
|
||||
await original.close();
|
||||
restarted = await createPaseoDaemon(
|
||||
{ ...original.config, listen: loadConfig(original.paseoHome, { env: {} }).listen },
|
||||
pino({ level: "silent" }),
|
||||
{
|
||||
...dependencies,
|
||||
builtinPlugins: new BuiltinPluginLoader(undefined, []),
|
||||
lazyTerminalWorker: true,
|
||||
},
|
||||
);
|
||||
await restarted.start();
|
||||
|
||||
local = new DaemonClient({ url: `ws://${newMain}/ws` });
|
||||
remote = new DaemonClient({ url: `ws://${oldAddress}/ws` });
|
||||
await Promise.all([local.connect(), remote.connect()]);
|
||||
expect((await local.getDaemonStatus()).tailscale).toMatchObject({
|
||||
state: "on",
|
||||
address: oldAddress,
|
||||
});
|
||||
expect(await local.setTailscaleEnabled(true)).toMatchObject({
|
||||
extraListeners: [{ address: oldAddress, state: "listening" }],
|
||||
});
|
||||
expect(await readListen()).toBe(`${newMain},${oldAddress}`);
|
||||
|
||||
const anotherTailscale = `${tailscaleIp}:${newPort}`;
|
||||
const carrierNat = `${carrierNatIp}:${original.port}`;
|
||||
await persist(`${newMain},${oldAddress},${carrierNat},${anotherTailscale}`);
|
||||
await local.reloadDaemonConfig();
|
||||
expect((await fetch(`http://${anotherTailscale}/api/health`)).ok).toBe(true);
|
||||
expect((await local.getDaemonStatus()).tailscale?.address).toBe(oldAddress);
|
||||
expect(await remote.setTailscaleEnabled(false)).toMatchObject({
|
||||
confirmationRequired: true,
|
||||
tailscale: { state: "on", address: oldAddress },
|
||||
});
|
||||
expect(await remote.setTailscaleEnabled(false, true)).toMatchObject({
|
||||
confirmationRequired: false,
|
||||
tailscale: { state: "off" },
|
||||
extraListeners: [{ address: carrierNat }],
|
||||
});
|
||||
await expect.poll(() => remote.isConnected).toBe(false);
|
||||
await expect(fetch(`http://${anotherTailscale}/api/health`)).rejects.toThrow();
|
||||
expect(await readListen()).toBe(`${newMain},${carrierNat}`);
|
||||
} finally {
|
||||
await Promise.all([first.close(), local?.close(), remote?.close()]);
|
||||
if (restarted) await restarted.stop();
|
||||
else await original.close();
|
||||
await Promise.all([
|
||||
rm(path.dirname(original.paseoHome), { recursive: true, force: true }),
|
||||
rm(original.staticDir, { recursive: true, force: true }),
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
test("reload changes only the extra TCP listeners without replacing the main endpoint", async () => {
|
||||
const daemon = await createTestPaseoDaemon({ relayEnabled: false, hostnames: [] });
|
||||
const local = new DaemonClient({ url: `ws://127.0.0.1:${daemon.port}/ws` });
|
||||
|
||||
@@ -167,13 +167,8 @@ export interface TailscaleNetwork {
|
||||
|
||||
// A saved address can outlive its interface. An address assigned to a different
|
||||
// interface belongs to that interface even when it is in the shared IPv4 range.
|
||||
function isTailscaleListener(
|
||||
target: ReturnType<typeof parseExtra>,
|
||||
port: number,
|
||||
network: TailscaleNetwork,
|
||||
) {
|
||||
function isTailscaleListener(target: ReturnType<typeof parseExtra>, network: TailscaleNetwork) {
|
||||
return (
|
||||
target.port === port &&
|
||||
!network.otherAddresses.has(target.host) &&
|
||||
(network.addresses.includes(target.host) || isTailnetIPv4(target.host))
|
||||
);
|
||||
@@ -235,7 +230,7 @@ export function createDaemonListeners(options: {
|
||||
const extraListeners = extras.status();
|
||||
const configured = extraListeners.flatMap((listener) => {
|
||||
const target = parseExtra(listener.address);
|
||||
if (main?.type !== "tcp" || !isTailscaleListener(target, main.port, network)) return [];
|
||||
if (main?.type !== "tcp" || !isTailscaleListener(target, network)) return [];
|
||||
return [{ ...listener, target }];
|
||||
});
|
||||
const current = configured.find(
|
||||
@@ -304,6 +299,13 @@ export function createDaemonListeners(options: {
|
||||
const [first, ...saved] = splitListen(persisted ?? listen);
|
||||
const live = extraListeners.map(({ address }) => address);
|
||||
if (enabled) {
|
||||
if (configured.length) {
|
||||
return {
|
||||
confirmationRequired: false as const,
|
||||
listen: persisted ?? listen,
|
||||
apply: () => Promise.resolve(),
|
||||
};
|
||||
}
|
||||
if (!tailscale.ip) throw new Error("Tailscale is not running");
|
||||
if (parseSocketTarget(first)) throw new Error("The configured main listener is a socket");
|
||||
const address = `${tailscale.ip}:${main.port}`;
|
||||
@@ -324,9 +326,7 @@ export function createDaemonListeners(options: {
|
||||
return { confirmationRequired: true as const };
|
||||
}
|
||||
const removed = new Set(configured.map(({ address }) => address));
|
||||
const remaining = saved.filter(
|
||||
(entry) => !isTailscaleListener(parseExtra(entry), main.port, network),
|
||||
);
|
||||
const remaining = saved.filter((entry) => !isTailscaleListener(parseExtra(entry), network));
|
||||
return {
|
||||
confirmationRequired: false as const,
|
||||
listen: [first, ...remaining].join(","),
|
||||
|
||||
Reference in New Issue
Block a user