Skip to content

Commit

Permalink
Add edge case error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pappz committed Feb 5, 2025
1 parent 8dafb61 commit c1f67df
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions client/internal/peer/wg_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ func (w *WGWatcher) periodicHandshakeCheck(ctx context.Context, ctxCancel contex
}
}

func (w *WGWatcher) wgState() (time.Time, error) {
wgState, err := w.wgIfaceStater.GetStats(w.peerKey)
if err != nil {
return time.Time{}, err
}
return wgState.LastHandshake, nil
}

// handshakeCheck checks the WireGuard handshake and return the new handshake time if it is different from the previous one
func (w *WGWatcher) handshakeCheck(lastHandshake time.Time) (*time.Time, bool) {
handshake, err := w.wgState()
Expand All @@ -132,6 +124,7 @@ func (w *WGWatcher) handshakeCheck(lastHandshake time.Time) (*time.Time, bool) {

w.log.Debugf("previous handshake, handshake: %v, %v", lastHandshake, handshake)

// the current know handshake did not change
if handshake.Equal(lastHandshake) {
w.log.Infof("WireGuard handshake timed out, closing relay connection: %v", handshake)
return nil, false
Expand All @@ -143,5 +136,19 @@ func (w *WGWatcher) handshakeCheck(lastHandshake time.Time) (*time.Time, bool) {
return nil, false
}

// error handling for handshake time in the future
if handshake.After(time.Now()) {
w.log.Infof("WireGuard handshake is in the future, closing relay connection: %v", handshake)
return nil, false
}

return &handshake, true
}

func (w *WGWatcher) wgState() (time.Time, error) {
wgState, err := w.wgIfaceStater.GetStats(w.peerKey)
if err != nil {
return time.Time{}, err
}
return wgState.LastHandshake, nil
}

0 comments on commit c1f67df

Please sign in to comment.