summaryrefslogtreecommitdiff
path: root/internal/engine/watchdog.go
blob: 899f81ff29dcc3cbc485aed2c12df21709012acb (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
package engine

import (
	"context"
	"io"
	"log"
	"net/http"
	"strings"
	"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
}

// 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) {
	w.StopWatching()

	w.server = server
	w.mode = mode
	w.ruleSets = ruleSets
	w.serverIPs = serverIPs

	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
				}
				log.Println("watchdog: sing-box not running, reconnecting...")
				if err := w.engine.Start(w.server, w.mode, w.ruleSets, w.serverIPs); 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
			}
			ip := checkExitIP()
			if ip == "" {
				log.Println("watchdog: deep check failed (no exit IP), restarting...")
				if time.Since(lastReconnect) < w.cfg.ReconnectCooldown {
					continue
				}
				if err := w.engine.Restart(w.server, w.mode, w.ruleSets, w.serverIPs); err != nil {
					log.Printf("watchdog: restart failed: %v", err)
				}
				lastReconnect = time.Now()
			}
		}
	}
}

func checkExitIP() string {
	client := &http.Client{Timeout: 5 * time.Second}
	resp, err := client.Get("http://ifconfig.me/ip")
	if err != nil {
		return ""
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(io.LimitReader(resp.Body, 64))
	if err != nil {
		return ""
	}
	return strings.TrimSpace(string(body))
}