mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
Fix tunnel health check
This commit is contained in:
@@ -6,7 +6,7 @@ import { cleanupProviderConnections, getSettings, updateSettings, getApiKeys } f
|
||||
import {
|
||||
enableTunnel, enableTailscale,
|
||||
isTunnelManuallyDisabled, isTunnelReconnecting, isTailscaleReconnecting,
|
||||
getTunnelService, getTailscaleService,
|
||||
getTunnelService, getTailscaleService, setTunnelUnexpectedExitCallback,
|
||||
} from "@/lib/tunnel/tunnelManager";
|
||||
import { killCloudflared, isCloudflaredRunning, ensureCloudflared } from "@/lib/tunnel/cloudflared";
|
||||
import { isTailscaleRunning } from "@/lib/tunnel/tailscale";
|
||||
@@ -83,6 +83,11 @@ export async function initializeApp() {
|
||||
// Sync mitmAlias DB → JSON cache so standalone MITM server can read it
|
||||
syncMitmAliasCache().catch(() => {});
|
||||
|
||||
// Auto-respawn tunnel when cloudflared exits unexpectedly (e.g. network change drop)
|
||||
setTunnelUnexpectedExitCallback(() => {
|
||||
safeRestartTunnel("unexpected-exit").catch(() => {});
|
||||
});
|
||||
|
||||
startWatchdog();
|
||||
startNetworkMonitor();
|
||||
autoStartMitm();
|
||||
@@ -133,13 +138,22 @@ async function safeRestartTunnel(reason) {
|
||||
if (!settings.tunnelEnabled) return;
|
||||
if (svc.cancelToken.cancelled) return;
|
||||
if (svc.spawnInProgress) return;
|
||||
if (Date.now() - svc.lastRestartAt < RESTART_COOLDOWN_MS) return;
|
||||
// Bypass cooldown when process is dead (real respawn, not restart-loop guard)
|
||||
const processDead = !isCloudflaredRunning();
|
||||
if (!processDead && Date.now() - svc.lastRestartAt < RESTART_COOLDOWN_MS) return;
|
||||
|
||||
// Alive check: process up + URL responds → skip
|
||||
// Alive check: process up + BOTH direct & public URL respond → skip
|
||||
if (isCloudflaredRunning()) {
|
||||
const state = loadState();
|
||||
const publicUrl = state?.shortId ? `https://r${state.shortId}.abc-tunnel.us` : null;
|
||||
if (publicUrl && await probeUrlAlive(publicUrl)) return;
|
||||
const directUrl = state?.tunnelUrl || null;
|
||||
if (publicUrl && directUrl) {
|
||||
const [publicOk, directOk] = await Promise.all([
|
||||
probeUrlAlive(publicUrl),
|
||||
probeUrlAlive(directUrl),
|
||||
]);
|
||||
if (publicOk && directOk) return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!await checkInternet()) return;
|
||||
|
||||
Reference in New Issue
Block a user