Fix tunnel
This commit is contained in:
@@ -373,7 +373,6 @@ export async function spawnQuickTunnel(localPort, onUrlUpdate) {
|
||||
cloudflaredProcess = null;
|
||||
clearPid();
|
||||
console.log(`[Tunnel] cloudflared exit code=${code} signal=${signal}`);
|
||||
if (logTail) console.log(`[Tunnel] cloudflared log tail:\n${logTail.slice(-1500)}`);
|
||||
if (!resolved) {
|
||||
resolved = true;
|
||||
clearTimeout(timeout);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import crypto from "crypto";
|
||||
import { loadState, saveState, generateShortId, clearPid } from "./state.js";
|
||||
import { spawnQuickTunnel, killCloudflared, isCloudflaredRunning, setUnexpectedExitHandler } from "./cloudflared.js";
|
||||
import { startFunnel, stopFunnel, isTailscaleRunning, isTailscaleRunningStrict, isTailscaleLoggedIn, startLogin, startDaemonWithPassword, provisionCert } from "./tailscale.js";
|
||||
@@ -9,7 +8,6 @@ import { waitForHealth, probeUrlAlive } from "./networkProbe.js";
|
||||
initDbHooks(getSettings, updateSettings);
|
||||
|
||||
const WORKER_URL = process.env.TUNNEL_WORKER_URL || "https://abc-tunnel.us";
|
||||
const MACHINE_ID_SALT = "9router-tunnel-salt";
|
||||
|
||||
// Per-service state (independent: tunnel ≠ tailscale)
|
||||
const tunnelSvc = {
|
||||
@@ -33,43 +31,6 @@ export function isTunnelManuallyDisabled() { return tunnelSvc.cancelToken.cancel
|
||||
export function isTunnelReconnecting() { return tunnelSvc.spawnInProgress; }
|
||||
export function isTailscaleReconnecting() { return tailscaleSvc.spawnInProgress; }
|
||||
|
||||
// ─── Reachable cache: background probe of tunnel URL /api/health ─────────────
|
||||
// UI uses this to know if the public URL actually serves content (not just process alive)
|
||||
const REACHABLE_TTL_MS = 30000;
|
||||
const tunnelReachable = { value: false, url: null, fetchedAt: 0, refreshing: false };
|
||||
const tailscaleReachable = { value: false, url: null, fetchedAt: 0, refreshing: false };
|
||||
|
||||
function bgRefreshReachable(cache, url) {
|
||||
if (cache.refreshing) return;
|
||||
if (!url) { cache.value = false; cache.url = null; cache.fetchedAt = Date.now(); return; }
|
||||
cache.refreshing = true;
|
||||
probeUrlAlive(url)
|
||||
.then((ok) => { cache.value = ok; })
|
||||
.catch(() => { cache.value = false; })
|
||||
.finally(() => {
|
||||
cache.url = url;
|
||||
cache.fetchedAt = Date.now();
|
||||
cache.refreshing = false;
|
||||
});
|
||||
}
|
||||
|
||||
function readReachable(cache, url) {
|
||||
// URL changed → invalidate
|
||||
if (cache.url !== url) { cache.value = false; cache.fetchedAt = 0; }
|
||||
if (Date.now() - cache.fetchedAt > REACHABLE_TTL_MS) bgRefreshReachable(cache, url);
|
||||
return cache.value;
|
||||
}
|
||||
|
||||
function getMachineId() {
|
||||
try {
|
||||
const { machineIdSync } = require("node-machine-id");
|
||||
const raw = machineIdSync();
|
||||
return crypto.createHash("sha256").update(raw + MACHINE_ID_SALT).digest("hex").substring(0, 16);
|
||||
} catch (e) {
|
||||
return crypto.randomUUID().replace(/-/g, "").substring(0, 16);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Cloudflare Tunnel ───────────────────────────────────────────────────────
|
||||
|
||||
async function registerTunnelUrl(shortId, tunnelUrl) {
|
||||
@@ -105,7 +66,6 @@ export async function enableTunnel(localPort = 20128) {
|
||||
console.log("[Tunnel] killed existing cloudflared");
|
||||
throwIfCancelled(token, "tunnel");
|
||||
|
||||
const machineId = getMachineId();
|
||||
const existing = loadState();
|
||||
const shortId = existing?.shortId || generateShortId();
|
||||
|
||||
@@ -113,7 +73,7 @@ export async function enableTunnel(localPort = 20128) {
|
||||
if (token.cancelled) return;
|
||||
console.log(`[Tunnel] url updated: ${url}`);
|
||||
await registerTunnelUrl(shortId, url);
|
||||
saveState({ shortId, machineId, tunnelUrl: url });
|
||||
saveState({ shortId, tunnelUrl: url });
|
||||
await updateSettings({ tunnelEnabled: true, tunnelUrl: url });
|
||||
};
|
||||
|
||||
@@ -123,7 +83,7 @@ export async function enableTunnel(localPort = 20128) {
|
||||
|
||||
const publicUrl = `https://r${shortId}.abc-tunnel.us`;
|
||||
await registerTunnelUrl(shortId, tunnelUrl);
|
||||
saveState({ shortId, machineId, tunnelUrl });
|
||||
saveState({ shortId, tunnelUrl });
|
||||
await updateSettings({ tunnelEnabled: true, tunnelUrl });
|
||||
console.log(`[Tunnel] registered shortId=${shortId} publicUrl=${publicUrl}`);
|
||||
|
||||
@@ -137,11 +97,6 @@ export async function enableTunnel(localPort = 20128) {
|
||||
console.log("[Tunnel] direct 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) {
|
||||
@@ -162,10 +117,9 @@ export async function disableTunnel() {
|
||||
clearPid();
|
||||
|
||||
const state = loadState();
|
||||
if (state) saveState({ shortId: state.shortId, machineId: state.machineId, tunnelUrl: null });
|
||||
if (state) saveState({ shortId: state.shortId, tunnelUrl: null });
|
||||
|
||||
await updateSettings({ tunnelEnabled: false, tunnelUrl: "" });
|
||||
tunnelReachable.value = false; tunnelReachable.url = null; tunnelReachable.fetchedAt = Date.now();
|
||||
// Force-clear flags so a subsequent enable is not blocked by a stuck spawnInProgress
|
||||
tunnelSvc.spawnInProgress = false;
|
||||
tunnelSvc.activeLocalPort = null;
|
||||
@@ -182,8 +136,6 @@ export async function getTunnelStatus() {
|
||||
|
||||
// Lazy: skip PID probe entirely when user disabled tunnel
|
||||
const running = settingsEnabled ? isCloudflaredRunning() : false;
|
||||
// Reachable: cached background probe (never blocks the request)
|
||||
const reachable = settingsEnabled && running ? readReachable(tunnelReachable, tunnelUrl) : false;
|
||||
|
||||
return {
|
||||
enabled: settingsEnabled && running,
|
||||
@@ -191,8 +143,7 @@ export async function getTunnelStatus() {
|
||||
tunnelUrl,
|
||||
shortId,
|
||||
publicUrl,
|
||||
running,
|
||||
reachable
|
||||
running
|
||||
};
|
||||
}
|
||||
|
||||
@@ -272,12 +223,6 @@ export async function enableTailscale(localPort = 20128) {
|
||||
if (!he.message.startsWith("Health check timeout")) throw he;
|
||||
console.warn(`[Tailscale] health check timed out, will retry via watchdog`);
|
||||
}
|
||||
|
||||
if (reachableNow) {
|
||||
tailscaleReachable.value = true;
|
||||
tailscaleReachable.url = result.tunnelUrl;
|
||||
tailscaleReachable.fetchedAt = Date.now();
|
||||
}
|
||||
console.log(`[Tailscale] enable success (reachable=${reachableNow})`);
|
||||
return { success: true, tunnelUrl: result.tunnelUrl };
|
||||
} catch (e) {
|
||||
@@ -293,7 +238,6 @@ export async function disableTailscale() {
|
||||
tailscaleSvc.cancelToken.cancelled = true;
|
||||
stopFunnel();
|
||||
await updateSettings({ tailscaleEnabled: false, tailscaleUrl: "" });
|
||||
tailscaleReachable.value = false; tailscaleReachable.url = null; tailscaleReachable.fetchedAt = Date.now();
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@@ -304,14 +248,11 @@ export async function getTailscaleStatus() {
|
||||
// Skip probes entirely when disabled; check login before running (device removed = not logged in)
|
||||
const loggedIn = settingsEnabled ? isTailscaleLoggedIn() : false;
|
||||
const running = loggedIn ? isTailscaleRunning() : false;
|
||||
// Reachable: cached background probe (never blocks the request)
|
||||
const reachable = settingsEnabled && running ? readReachable(tailscaleReachable, tunnelUrl) : false;
|
||||
return {
|
||||
enabled: settingsEnabled && running,
|
||||
settingsEnabled,
|
||||
tunnelUrl,
|
||||
running,
|
||||
loggedIn,
|
||||
reachable
|
||||
loggedIn
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user