summaryrefslogtreecommitdiff
path: root/internal/engine/watchdog.go
blob: a27945fa04b5b117d78c34dea71456a7d463c32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package engine

import (
	"context"
	"log"
	"time"

	"vpnem/internal/config"
	"vpnem/internal/models"
)

// WatchdogConfig holds watchdog parameters.
type WatchdogConfig struct {
	CheckInterval     time.Duration // how often to check sing-box is alive (default 2s)
	DeepCheckInterval time.Duration // how often to verify exit IP (default 30s)
	ReconnectCooldown time.Duration // min time between reconnect attempts (default 5s)
}

// DefaultWatchdogConfig returns the default watchdog settings (from vpn.py).
func DefaultWatchdogConfig() WatchdogConfig {
	return WatchdogConfig{
		CheckInterval:     2 * time.Second,
		DeepCheckInterval: 30 * time.Second,
		ReconnectCooldown: 5 * time.Second,
	}
}

// Watchdog monitors sing-box and auto-reconnects on failure.
type Watchdog struct {
	engine  *Engine
	cfg     WatchdogConfig
	cancel  context.CancelFunc
	running bool

	// Reconnect parameters (set via StartWatching)
	server         models.Server
	mode           config.Mode
	ruleSets       []models.RuleSet
	serverIPs      []string
	customBypass   []string
	localProxyPort int
	policy         *models.RoutingPolicy
}

// NewWatchdog creates a new watchdog for the given engine.
func NewWatchdog(engine *Engine, cfg WatchdogConfig) *Watchdog {
	return &Watchdog{
		engine: engine,
		cfg:    cfg,
	}
}

// StartWatching begins monitoring. It stores the connection params for reconnection.
func (w *Watchdog) StartWatching(server models.Server, mode config.Mode, ruleSets []models.RuleSet, serverIPs []string, customBypass []string, localProxyPort int, policy *models.RoutingPolicy) {
	w.StopWatching()

	w.server = server
	w.mode = mode
	w.ruleSets = ruleSets
	w.serverIPs = serverIPs
	w.customBypass = append([]string{}, customBypass...)
	w.localProxyPort = localProxyPort
	w.policy = policy

	ctx, cancel := context.WithCancel(context.Background())
	w.cancel = cancel
	w.running = true

	go w.loop(ctx)
}

// StopWatching stops the watchdog.
func (w *Watchdog) StopWatching() {
	if w.cancel != nil {
		w.cancel()
	}
	w.running = false
}

// IsWatching returns whether the watchdog is active.
func (w *Watchdog) IsWatching() bool {
	return w.running
}

func (w *Watchdog) loop(ctx context.Context) {
	ticker := time.NewTicker(w.cfg.CheckInterval)
	defer ticker.Stop()

	deepTicker := time.NewTicker(w.cfg.DeepCheckInterval)
	defer deepTicker.Stop()

	lastReconnect := time.Time{}

	for {
		select {
		case <-ctx.Done():
			return

		case <-ticker.C:
			if !w.engine.IsRunning() {
				if time.Since(lastReconnect) < w.cfg.ReconnectCooldown {
					continue
				}
				localProxyPort, err := ResolveLocalProxyPort()
				if err != nil {
					log.Printf("watchdog: local proxy port selection failed: %v", err)
					continue
				}
				w.localProxyPort = localProxyPort
				log.Println("watchdog: sing-box not running, reconnecting...")
				if err := w.engine.StartFull(w.server, w.mode, w.ruleSets, w.serverIPs, w.customBypass, w.localProxyPort, w.policy); err != nil {
					log.Printf("watchdog: reconnect failed: %v", err)
				} else {
					log.Println("watchdog: reconnected successfully")
				}
				lastReconnect = time.Now()
			}

		case <-deepTicker.C:
			if !w.engine.IsRunning() {
				continue
			}
			exitIP := CheckExitIP(w.localProxyPort)
			_, probeErr := ProbeBlockedSite(w.localProxyPort, DefaultBlockedSiteProbeURL, 8*time.Second)
			if DeepCheckRequiresRestart(w.mode, exitIP, probeErr) {
				if ModeRequiresExitIP(w.mode) {
					log.Println("watchdog: deep check failed (no exit IP), restarting...")
				} else {
					log.Printf("watchdog: deep check failed for direct-final mode (%v), restarting...", probeErr)
				}
				if time.Since(lastReconnect) < w.cfg.ReconnectCooldown {
					continue
				}
				localProxyPort, err := ResolveLocalProxyPort()
				if err != nil {
					log.Printf("watchdog: local proxy port selection failed: %v", err)
					continue
				}
				w.localProxyPort = localProxyPort
				if err := w.engine.RestartFull(w.server, w.mode, w.ruleSets, w.serverIPs, w.customBypass, w.localProxyPort, w.policy); err != nil {
					log.Printf("watchdog: restart failed: %v", err)
				}
				lastReconnect = time.Now()
			}
		}
	}
}