1 parent
b184444f34
commit
b39eb61c33
24 files changed
+517
-229
No files matched your search
@@ -85,6 +85,7 @@ function throwIfCancelled(token, label) {
|
||||
}
|
||||
|
||||
export async function enableTunnel(localPort = 20128) {
|
||||
console.log(`[Tunnel] enable start (port=${localPort})`);
|
||||
tunnelSvc.cancelToken = { cancelled: false };
|
||||
tunnelSvc.activeLocalPort = localPort;
|
||||
tunnelSvc.spawnInProgress = true;
|
||||
@@ -95,11 +96,13 @@ export async function enableTunnel(localPort = 20128) {
|
||||
const existing = loadState();
|
||||
if (existing?.tunnelUrl && await probeUrlAlive(existing.tunnelUrl)) {
|
||||
const publicUrl = `https://r${existing.shortId}.9router.com`;
|
||||
console.log(`[Tunnel] already running, reuse: ${existing.tunnelUrl}`);
|
||||
return { success: true, tunnelUrl: existing.tunnelUrl, shortId: existing.shortId, publicUrl, alreadyRunning: true };
|
||||
}
|
||||
}
|
||||
|
||||
killCloudflared(localPort);
|
||||
console.log("[Tunnel] killed existing cloudflared");
|
||||
throwIfCancelled(token, "tunnel");
|
||||
|
||||
const machineId = getMachineId();
|
||||
@@ -108,36 +111,46 @@ export async function enableTunnel(localPort = 20128) {
|
||||
|
||||
const onUrlUpdate = async (url) => {
|
||||
if (token.cancelled) return;
|
||||
console.log(`[Tunnel] url updated: ${url}`);
|
||||
await registerTunnelUrl(shortId, url);
|
||||
saveState({ shortId, machineId, tunnelUrl: url });
|
||||
await updateSettings({ tunnelEnabled: true, tunnelUrl: url });
|
||||
};
|
||||
|
||||
const { tunnelUrl } = await spawnQuickTunnel(localPort, onUrlUpdate);
|
||||
console.log(`[Tunnel] spawned: ${tunnelUrl}`);
|
||||
throwIfCancelled(token, "tunnel");
|
||||
|
||||
const publicUrl = `https://r${shortId}.9router.com`;
|
||||
await registerTunnelUrl(shortId, tunnelUrl);
|
||||
saveState({ shortId, machineId, tunnelUrl });
|
||||
await updateSettings({ tunnelEnabled: true, tunnelUrl });
|
||||
console.log(`[Tunnel] registered shortId=${shortId} publicUrl=${publicUrl}`);
|
||||
|
||||
// Verify direct tunnel URL is reachable first (avoid CDN-cache false positive on publicUrl)
|
||||
await waitForHealth(tunnelUrl, token);
|
||||
console.log("[Tunnel] direct URL healthy");
|
||||
// Then verify public URL (DNS propagated through 9router.com worker)
|
||||
await waitForHealth(publicUrl, token);
|
||||
console.log("[Tunnel] public URL healthy");
|
||||
|
||||
// Prime reachable cache so UI shows correct state immediately
|
||||
tunnelReachable.value = true;
|
||||
tunnelReachable.url = tunnelUrl;
|
||||
tunnelReachable.fetchedAt = Date.now();
|
||||
|
||||
console.log("[Tunnel] enable success");
|
||||
return { success: true, tunnelUrl, shortId, publicUrl };
|
||||
} catch (e) {
|
||||
console.error(`[Tunnel] enable error: ${e.message}`);
|
||||
throw e;
|
||||
} finally {
|
||||
tunnelSvc.spawnInProgress = false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function disableTunnel() {
|
||||
console.log("[Tunnel] disable");
|
||||
tunnelSvc.cancelToken.cancelled = true;
|
||||
setUnexpectedExitHandler(null);
|
||||
killCloudflared(tunnelSvc.activeLocalPort);
|
||||
@@ -177,6 +190,7 @@ export async function getTunnelStatus() {
|
||||
// ─── Tailscale Funnel ─────────────────────────────────────────────────────────
|
||||
|
||||
export async function enableTailscale(localPort = 20128) {
|
||||
console.log(`[Tailscale] enable start (port=${localPort})`);
|
||||
tailscaleSvc.cancelToken = { cancelled: false };
|
||||
tailscaleSvc.activeLocalPort = localPort;
|
||||
tailscaleSvc.spawnInProgress = true;
|
||||
@@ -185,36 +199,60 @@ export async function enableTailscale(localPort = 20128) {
|
||||
try {
|
||||
const sudoPass = getCachedPassword() || await loadEncryptedPassword() || "";
|
||||
await startDaemonWithPassword(sudoPass);
|
||||
console.log("[Tailscale] daemon ready");
|
||||
throwIfCancelled(token, "tailscale");
|
||||
|
||||
const existing = loadState();
|
||||
const shortId = existing?.shortId || generateShortId();
|
||||
const tsHostname = shortId;
|
||||
|
||||
if (!isTailscaleLoggedIn()) {
|
||||
const loggedIn = isTailscaleLoggedIn();
|
||||
console.log(`[Tailscale] loggedIn=${loggedIn}`);
|
||||
if (!loggedIn) {
|
||||
const loginResult = await startLogin(tsHostname);
|
||||
if (loginResult.authUrl) return { success: false, needsLogin: true, authUrl: loginResult.authUrl };
|
||||
if (loginResult.authUrl) {
|
||||
console.log(`[Tailscale] needs login, authUrl=${loginResult.authUrl}`);
|
||||
return { success: false, needsLogin: true, authUrl: loginResult.authUrl };
|
||||
}
|
||||
console.log("[Tailscale] login resolved alreadyLoggedIn");
|
||||
}
|
||||
throwIfCancelled(token, "tailscale");
|
||||
|
||||
stopFunnel();
|
||||
const result = await startFunnel(localPort);
|
||||
let result;
|
||||
try {
|
||||
console.log("[Tailscale] starting funnel");
|
||||
result = await startFunnel(localPort);
|
||||
} catch (e) {
|
||||
console.error(`[Tailscale] funnel error: ${e.message}`);
|
||||
// Daemon not logged in / not ready → auto-trigger login flow so user stays in-app
|
||||
if (/NoState|unexpected state|not logged in|Logged ?out|NeedsLogin/i.test(e.message || "")) {
|
||||
console.log("[Tailscale] retry via startLogin");
|
||||
const loginResult = await startLogin(tsHostname);
|
||||
if (loginResult.authUrl) return { success: false, needsLogin: true, authUrl: loginResult.authUrl };
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
throwIfCancelled(token, "tailscale");
|
||||
|
||||
if (result.funnelNotEnabled) {
|
||||
console.log(`[Tailscale] funnel not enabled, enableUrl=${result.enableUrl}`);
|
||||
return { success: false, funnelNotEnabled: true, enableUrl: result.enableUrl };
|
||||
}
|
||||
|
||||
// Strict probe: bypass cache so we don't false-negative on first invocation
|
||||
if (!isTailscaleLoggedIn() || !isTailscaleRunningStrict()) {
|
||||
console.error("[Tailscale] strict probe failed (device removed?)");
|
||||
stopFunnel();
|
||||
return { success: false, error: "Tailscale not connected. Device may have been removed. Please re-login." };
|
||||
}
|
||||
|
||||
await updateSettings({ tailscaleEnabled: true, tailscaleUrl: result.tunnelUrl });
|
||||
console.log(`[Tailscale] funnel up: ${result.tunnelUrl}`);
|
||||
|
||||
// Verify funnel actually serves /api/health
|
||||
await waitForHealth(result.tunnelUrl, token);
|
||||
console.log("[Tailscale] enable success");
|
||||
|
||||
// Prime reachable cache so UI shows correct state immediately
|
||||
tailscaleReachable.value = true;
|
||||
@@ -222,12 +260,16 @@ export async function enableTailscale(localPort = 20128) {
|
||||
tailscaleReachable.fetchedAt = Date.now();
|
||||
|
||||
return { success: true, tunnelUrl: result.tunnelUrl };
|
||||
} catch (e) {
|
||||
console.error(`[Tailscale] enable error: ${e.message}`);
|
||||
throw e;
|
||||
} finally {
|
||||
tailscaleSvc.spawnInProgress = false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function disableTailscale() {
|
||||
console.log("[Tailscale] disable");
|
||||
tailscaleSvc.cancelToken.cancelled = true;
|
||||
stopFunnel();
|
||||
await updateSettings({ tailscaleEnabled: false, tailscaleUrl: "" });
|
||||
|
||||
Reference in new issue
Block a user