|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "runtime" |
| 9 | + "time" |
| 10 | + |
| 11 | + log "github.com/sirupsen/logrus" |
| 12 | +) |
| 13 | + |
| 14 | +// WGIfaceMonitor monitors the WireGuard interface lifecycle and restarts the engine |
| 15 | +// if the interface is deleted externally while the engine is running. |
| 16 | +type WGIfaceMonitor struct { |
| 17 | + done chan struct{} |
| 18 | +} |
| 19 | + |
| 20 | +// NewWGIfaceMonitor creates a new WGIfaceMonitor instance. |
| 21 | +func NewWGIfaceMonitor() *WGIfaceMonitor { |
| 22 | + return &WGIfaceMonitor{ |
| 23 | + done: make(chan struct{}), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// Start begins monitoring the WireGuard interface. |
| 28 | +// It relies on the provided context cancellation to stop. |
| 29 | +func (m *WGIfaceMonitor) Start(ctx context.Context, ifaceName string) (shouldRestart bool, err error) { |
| 30 | + defer close(m.done) |
| 31 | + |
| 32 | + // Skip on mobile platforms as they handle interface lifecycle differently |
| 33 | + if runtime.GOOS == "android" || runtime.GOOS == "ios" { |
| 34 | + log.Debugf("Interface monitor: skipped on %s platform", runtime.GOOS) |
| 35 | + return false, errors.New("not supported on mobile platforms") |
| 36 | + } |
| 37 | + |
| 38 | + if ifaceName == "" { |
| 39 | + log.Debugf("Interface monitor: empty interface name, skipping monitor") |
| 40 | + return false, errors.New("empty interface name") |
| 41 | + } |
| 42 | + |
| 43 | + // Get initial interface index to track the specific interface instance |
| 44 | + expectedIndex, err := getInterfaceIndex(ifaceName) |
| 45 | + if err != nil { |
| 46 | + log.Debugf("Interface monitor: interface %s not found, skipping monitor", ifaceName) |
| 47 | + return false, fmt.Errorf("interface %s not found: %w", ifaceName, err) |
| 48 | + } |
| 49 | + |
| 50 | + log.Infof("Interface monitor: watching %s (index: %d)", ifaceName, expectedIndex) |
| 51 | + |
| 52 | + ticker := time.NewTicker(2 * time.Second) |
| 53 | + defer ticker.Stop() |
| 54 | + |
| 55 | + for { |
| 56 | + select { |
| 57 | + case <-ctx.Done(): |
| 58 | + log.Infof("Interface monitor: stopped for %s", ifaceName) |
| 59 | + return false, fmt.Errorf("wg interface monitor stopped: %v", ctx.Err()) |
| 60 | + case <-ticker.C: |
| 61 | + currentIndex, err := getInterfaceIndex(ifaceName) |
| 62 | + if err != nil { |
| 63 | + // Interface was deleted |
| 64 | + log.Infof("Interface monitor: %s deleted", ifaceName) |
| 65 | + return true, fmt.Errorf("interface %s deleted: %w", ifaceName, err) |
| 66 | + } |
| 67 | + |
| 68 | + // Check if interface index changed (interface was recreated) |
| 69 | + if currentIndex != expectedIndex { |
| 70 | + log.Infof("Interface monitor: %s recreated (index changed from %d to %d), restarting engine", |
| 71 | + ifaceName, expectedIndex, currentIndex) |
| 72 | + return true, nil |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +// getInterfaceIndex returns the index of a network interface by name. |
| 80 | +// Returns an error if the interface is not found. |
| 81 | +func getInterfaceIndex(name string) (int, error) { |
| 82 | + if name == "" { |
| 83 | + return 0, fmt.Errorf("empty interface name") |
| 84 | + } |
| 85 | + ifi, err := net.InterfaceByName(name) |
| 86 | + if err != nil { |
| 87 | + // Check if it's specifically a "not found" error |
| 88 | + if errors.Is(err, &net.OpError{}) { |
| 89 | + // On some systems, this might be a "not found" error |
| 90 | + return 0, fmt.Errorf("interface not found: %w", err) |
| 91 | + } |
| 92 | + return 0, fmt.Errorf("failed to lookup interface: %w", err) |
| 93 | + } |
| 94 | + if ifi == nil { |
| 95 | + return 0, fmt.Errorf("interface not found") |
| 96 | + } |
| 97 | + return ifi.Index, nil |
| 98 | +} |
0 commit comments