Files
Zakk 58acf7d40f fix(service): a start that never commits leaves nothing behind on the host
Start persisted running=false, stopped the old core, published the new
process and unlocked, installed the firewall rules, DNS hijack, TUN
routes or system proxy, and only then persisted running=true. A crash in
that window made the next start skip the core and clean only TUN
residue, so redirect rules, a hijacked resolver or the system proxy kept
pointing at a dead listener.

Before it installs anything, Start now stores a host-state marker (the
transparent type, API port and TUN teardown settings) and clears it once
the start commits or is rolled back; startup and the next Start tear
that state down when they find the marker. Every host mutation and the
final commit check that the process they started is still the manager's
current one, so a core that exits during afterStart fails the start
instead of leaving the UI and the store saying running. The system-proxy
implementations persist the user's original proxy settings when they
capture them and restore from that copy when the in-memory one is gone.
A recovery that fails is logged and retried at the next start; it never
blocks the service.
2026-09-20 09:17:11 +10:00

205 lines
6.2 KiB
Go

package v2ray
import (
"net"
"runtime"
"strconv"
"time"
"github.com/v2rayA/v2rayA/db/configure"
"github.com/v2rayA/v2rayA/pkg/util/log"
)
const (
connectivityCheckInterval = 15 * time.Second
connectivityCheckTimeout = 5 * time.Second
// connectivityBackoffBase is the initial retry delay after the first consecutive
// probe failure (30 s). Subsequent failures double the delay up to
// connectivityBackoffMax to avoid a rapid stop→start oscillation loop.
connectivityBackoffBase = 30 * time.Second
// connectivityBackoffMax caps the exponential backoff delay.
connectivityBackoffMax = 120 * time.Second
)
// connectivityProbeAddr returns the core's API listener, which every mode
// has. A successful TCP dial to loopback says the core process is alive;
// it says nothing about the TUN itself. A failing TUN device is reported
// by the core's inbound and does not stop the core.
func connectivityProbeAddr(t *Template) string {
return net.JoinHostPort("127.0.0.1", strconv.Itoa(t.ApiPort))
}
// connectivityStartupDelay is the initial wait before the first connectivity
// probe. On Windows it is larger to accommodate wintun driver initialisation.
var connectivityStartupDelay time.Duration
func init() {
connectivityStartupDelay = 5 * time.Second
if runtime.GOOS == "windows" {
connectivityStartupDelay = 15 * time.Second
}
}
// probePhysicalConnectivity checks whether the core's API port is reachable.
// A TCP dial to loopback never routes through the TUN device.
func probePhysicalConnectivity(t *Template) bool {
conn, err := net.DialTimeout("tcp", connectivityProbeAddr(t), connectivityCheckTimeout)
if err != nil {
return false
}
conn.Close()
return true
}
// tunDataPathDead reports a tun-mode device that went away while the core
// process lives: the inbound closes its device when the pumps fail, and
// nothing recreates it. Every routed packet is being dropped, and no later
// probe can succeed, so this is a stop, not a pause.
func tunDataPathDead(t *Template) bool {
return t.Setting != nil && t.Setting.TransparentType == configure.TransparentTun && !tunDeviceAlive()
}
// connectivityBackoffDelay returns the delay to wait after consecutiveFailures
// consecutive probe failures, using exponential backoff capped at
// connectivityBackoffMax.
func connectivityBackoffDelay(consecutiveFailures int) time.Duration {
delay := connectivityBackoffBase
for i := 1; i < consecutiveFailures; i++ {
delay *= 2
if delay >= connectivityBackoffMax {
return connectivityBackoffMax
}
}
return delay
}
func (m *CoreProcessManager) startConnectivityMonitor(p *Process, generation uint64) {
t := p.template
if t == nil || t.Setting == nil || !IsTransparentOn(t.Setting) || t.Setting.TransparentType == configure.TransparentSystemProxy {
return
}
m.mu.Lock()
if m.testing || !m.ownsProcessLocked(p, generation) {
m.mu.Unlock()
return
}
m.stopConnectivityMonitorLocked()
stopCh := make(chan struct{})
m.connectivityStop = stopCh
m.mu.Unlock()
go m.connectivityLoop(stopCh, p, generation)
}
func (m *CoreProcessManager) connectivityLoop(stopCh chan struct{}, p *Process, generation uint64) {
// Wait for the transparent proxy to fully initialize before
// the first connectivity check. Probing too early on Windows can yield a
// false network-unavailable result while the wintun driver is still setting
// up routes, which would tear the routes down prematurely and leave the frontend
// stuck on "检测中" (Checking).
select {
case <-stopCh:
return
case <-time.After(connectivityStartupDelay):
}
failureCount := 0
for {
stop, healthy := m.syncConnectivityState(p, generation)
if stop {
return
}
var delay time.Duration
if !healthy {
failureCount++
delay = connectivityBackoffDelay(failureCount)
} else {
failureCount = 0
delay = connectivityCheckInterval
}
select {
case <-stopCh:
return
case <-time.After(delay):
}
}
}
// syncConnectivityState checks and updates the transparent proxy connectivity
// state. It returns (stop, healthy): stop=true means the monitor goroutine
// should exit; healthy=true means the local SOCKS5 probe succeeded.
func (m *CoreProcessManager) syncConnectivityState(p *Process, generation uint64) (stop bool, healthy bool) {
t := p.template
if t == nil || t.Setting == nil {
return true, false
}
m.mu.Lock()
if !m.ownsProcessLocked(p, generation) || m.testing {
m.mu.Unlock()
return true, false
}
paused := m.networkPaused
setting := t.Setting
m.mu.Unlock()
if setting.TransparentType == configure.TransparentSystemProxy || !IsTransparentOn(setting) {
return false, true
}
dead := tunDataPathDead(t)
healthy = !dead && probePhysicalConnectivity(t)
if err := m.checkProcessOwner(p, generation); err != nil {
return true, false
}
if !healthy {
if paused {
return false, false
}
if err := m.mutateHost(p, generation, func() error {
deleteTransparentProxyRulesKeepSystemProxy()
return nil
}); err != nil {
return true, false
}
m.mu.Lock()
if m.ownsProcessLocked(p, generation) && !m.testing {
m.networkPaused = true
ApiFeed.ProductMessage("running_state", map[string]interface{}{"running": false, "networkPaused": true})
m.mu.Unlock()
if dead {
// Resuming would wait for a device that is not coming
// back and rerun the whole setup, hooks included, on
// every check. The user starts the proxy again.
log.Warn("tun: the device is gone while the core is running; the transparent proxy is paused until it is started again")
return true, false
}
log.Info("connectivity lost, transparent proxy paused")
return false, false
}
m.mu.Unlock()
return true, false
}
if !paused {
return false, true
}
if err := m.setupTransparentProxy(p, generation, setting, t); err != nil {
log.Warn("failed to resume transparent proxy after network recovery: %v", err)
return m.checkProcessOwner(p, generation) != nil, true
}
m.mu.Lock()
if m.ownsProcessLocked(p, generation) && !m.testing {
m.networkPaused = false
ApiFeed.ProductMessage("running_state", map[string]interface{}{"running": true, "networkPaused": false})
m.mu.Unlock()
log.Info("connectivity restored, transparent proxy resumed")
return false, true
}
m.mu.Unlock()
return true, true
}