-
-
Notifications
You must be signed in to change notification settings - Fork 892
[client] Add WireGuard interface lifecycle monitoring #4370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
64bb6db
[client] Add WireGuard interface lifecycle monitoring
hakansa 6016dbd
[client] Enhance WireGuard interface monitoring
hakansa 88f98a0
[client] Introduce WGIfaceMonitor for enhanced WireGuard interface ma…
hakansa bf5e12d
Refactor WGIfaceMonitor to use context for lifecycle management and i…
hakansa 6cc8df9
Clarify error handling in WGIfaceMonitor.Start method documentation
hakansa f37cc27
Refactor WGIfaceMonitor.Start to return restart indication and improv…
hakansa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"runtime" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// WGIfaceMonitor monitors the WireGuard interface lifecycle and restarts the engine | ||
// if the interface is deleted externally while the engine is running. | ||
type WGIfaceMonitor struct { | ||
done chan struct{} | ||
} | ||
|
||
// NewWGIfaceMonitor creates a new WGIfaceMonitor instance. | ||
func NewWGIfaceMonitor() *WGIfaceMonitor { | ||
return &WGIfaceMonitor{ | ||
done: make(chan struct{}), | ||
} | ||
} | ||
|
||
// Start begins monitoring the WireGuard interface. | ||
// It relies on the provided context cancellation to stop. | ||
func (m *WGIfaceMonitor) Start(ctx context.Context, ifaceName string) (shouldRestart bool, err error) { | ||
defer close(m.done) | ||
|
||
// Skip on mobile platforms as they handle interface lifecycle differently | ||
if runtime.GOOS == "android" || runtime.GOOS == "ios" { | ||
log.Debugf("Interface monitor: skipped on %s platform", runtime.GOOS) | ||
return false, errors.New("not supported on mobile platforms") | ||
} | ||
|
||
if ifaceName == "" { | ||
log.Debugf("Interface monitor: empty interface name, skipping monitor") | ||
return false, errors.New("empty interface name") | ||
} | ||
|
||
// Get initial interface index to track the specific interface instance | ||
expectedIndex, err := getInterfaceIndex(ifaceName) | ||
if err != nil { | ||
log.Debugf("Interface monitor: interface %s not found, skipping monitor", ifaceName) | ||
return false, fmt.Errorf("interface %s not found: %w", ifaceName, err) | ||
} | ||
|
||
log.Infof("Interface monitor: watching %s (index: %d)", ifaceName, expectedIndex) | ||
|
||
ticker := time.NewTicker(2 * time.Second) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
log.Infof("Interface monitor: stopped for %s", ifaceName) | ||
return false, fmt.Errorf("wg interface monitor stopped: %v", ctx.Err()) | ||
case <-ticker.C: | ||
currentIndex, err := getInterfaceIndex(ifaceName) | ||
if err != nil { | ||
// Interface was deleted | ||
log.Infof("Interface monitor: %s deleted", ifaceName) | ||
return true, fmt.Errorf("interface %s deleted: %w", ifaceName, err) | ||
} | ||
|
||
// Check if interface index changed (interface was recreated) | ||
if currentIndex != expectedIndex { | ||
log.Infof("Interface monitor: %s recreated (index changed from %d to %d), restarting engine", | ||
ifaceName, expectedIndex, currentIndex) | ||
return true, nil | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
// getInterfaceIndex returns the index of a network interface by name. | ||
// Returns an error if the interface is not found. | ||
func getInterfaceIndex(name string) (int, error) { | ||
if name == "" { | ||
return 0, fmt.Errorf("empty interface name") | ||
} | ||
ifi, err := net.InterfaceByName(name) | ||
if err != nil { | ||
// Check if it's specifically a "not found" error | ||
if errors.Is(err, &net.OpError{}) { | ||
// On some systems, this might be a "not found" error | ||
return 0, fmt.Errorf("interface not found: %w", err) | ||
} | ||
return 0, fmt.Errorf("failed to lookup interface: %w", err) | ||
} | ||
if ifi == nil { | ||
return 0, fmt.Errorf("interface not found") | ||
} | ||
return ifi.Index, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably put this check in the code calling
NewWGIfaceMonitor()
instead of here, it's not the Monitor's concern whether it should run or not, especially when you're already passing an unspecified callback function.