-
-
Notifications
You must be signed in to change notification settings - Fork 926
[client] Add bind activity listener to bypass udp sockets #4646
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
+759
−72
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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,13 @@ | ||
| package device | ||
|
|
||
| import ( | ||
| "net" | ||
| "net/netip" | ||
| ) | ||
|
|
||
| // EndpointManager manages fake IP to connection mappings for userspace bind implementations. | ||
| // Implemented by bind.ICEBind and bind.RelayBindJS. | ||
| type EndpointManager interface { | ||
| SetEndpoint(fakeIP netip.Addr, conn net.Conn) | ||
| RemoveEndpoint(fakeIP netip.Addr) | ||
| } |
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
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,82 @@ | ||
| package activity | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "net" | ||
| "time" | ||
| ) | ||
|
|
||
| // lazyConn detects activity when WireGuard attempts to send packets. | ||
| // It does not deliver packets, only signals that activity occurred. | ||
| type lazyConn struct { | ||
| activityCh chan struct{} | ||
| ctx context.Context | ||
| cancel context.CancelFunc | ||
| } | ||
|
|
||
| // newLazyConn creates a new lazyConn for activity detection. | ||
| func newLazyConn() *lazyConn { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| return &lazyConn{ | ||
| activityCh: make(chan struct{}, 1), | ||
| ctx: ctx, | ||
| cancel: cancel, | ||
| } | ||
| } | ||
|
|
||
| // Read blocks until the connection is closed. | ||
| func (c *lazyConn) Read(_ []byte) (n int, err error) { | ||
| <-c.ctx.Done() | ||
| return 0, io.EOF | ||
| } | ||
|
|
||
| // Write signals activity detection when ICEBind routes packets to this endpoint. | ||
| func (c *lazyConn) Write(b []byte) (n int, err error) { | ||
| if c.ctx.Err() != nil { | ||
| return 0, io.EOF | ||
| } | ||
|
|
||
| select { | ||
| case c.activityCh <- struct{}{}: | ||
| default: | ||
| } | ||
|
|
||
| return len(b), nil | ||
| } | ||
|
|
||
| // ActivityChan returns the channel that signals when activity is detected. | ||
| func (c *lazyConn) ActivityChan() <-chan struct{} { | ||
| return c.activityCh | ||
| } | ||
|
|
||
| // Close closes the connection. | ||
| func (c *lazyConn) Close() error { | ||
| c.cancel() | ||
| return nil | ||
| } | ||
|
|
||
| // LocalAddr returns the local address. | ||
| func (c *lazyConn) LocalAddr() net.Addr { | ||
| return &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: lazyBindPort} | ||
| } | ||
|
|
||
| // RemoteAddr returns the remote address. | ||
| func (c *lazyConn) RemoteAddr() net.Addr { | ||
| return &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: lazyBindPort} | ||
| } | ||
|
|
||
| // SetDeadline sets the read and write deadlines. | ||
| func (c *lazyConn) SetDeadline(_ time.Time) error { | ||
| return nil | ||
| } | ||
|
|
||
| // SetReadDeadline sets the deadline for future Read calls. | ||
| func (c *lazyConn) SetReadDeadline(_ time.Time) error { | ||
| return nil | ||
| } | ||
|
|
||
| // SetWriteDeadline sets the deadline for future Write calls. | ||
| func (c *lazyConn) SetWriteDeadline(_ time.Time) error { | ||
| return nil | ||
| } |
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 |
|---|---|---|
| @@ -1,107 +1,28 @@ | ||
| package activity | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "sync" | ||
| "sync/atomic" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
| "errors" | ||
|
|
||
| "github.com/netbirdio/netbird/client/internal/lazyconn" | ||
| ) | ||
|
|
||
| // Listener it is not a thread safe implementation, do not call Close before ReadPackets. It will cause blocking | ||
| type Listener struct { | ||
| wgIface WgInterface | ||
| peerCfg lazyconn.PeerConfig | ||
| conn *net.UDPConn | ||
| endpoint *net.UDPAddr | ||
| done sync.Mutex | ||
|
|
||
| isClosed atomic.Bool // use to avoid error log when closing the listener | ||
| } | ||
|
|
||
| func NewListener(wgIface WgInterface, cfg lazyconn.PeerConfig) (*Listener, error) { | ||
| d := &Listener{ | ||
| wgIface: wgIface, | ||
| peerCfg: cfg, | ||
| } | ||
|
|
||
| conn, err := d.newConn() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to creating activity listener: %v", err) | ||
| } | ||
| d.conn = conn | ||
| d.endpoint = conn.LocalAddr().(*net.UDPAddr) | ||
|
|
||
| if err := d.createEndpoint(); err != nil { | ||
| return nil, err | ||
| } | ||
| d.done.Lock() | ||
| cfg.Log.Infof("created activity listener: %s", conn.LocalAddr().(*net.UDPAddr).String()) | ||
| return d, nil | ||
| } | ||
|
|
||
| func (d *Listener) ReadPackets() { | ||
| for { | ||
| n, remoteAddr, err := d.conn.ReadFromUDP(make([]byte, 1)) | ||
| if err != nil { | ||
| if d.isClosed.Load() { | ||
| d.peerCfg.Log.Infof("exit from activity listener") | ||
| } else { | ||
| d.peerCfg.Log.Errorf("failed to read from activity listener: %s", err) | ||
| } | ||
| break | ||
| } | ||
|
|
||
| if n < 1 { | ||
| d.peerCfg.Log.Warnf("received %d bytes from %s, too short", n, remoteAddr) | ||
| continue | ||
| } | ||
| d.peerCfg.Log.Infof("activity detected") | ||
| break | ||
| } | ||
|
|
||
| d.peerCfg.Log.Debugf("removing lazy endpoint: %s", d.endpoint.String()) | ||
| if err := d.removeEndpoint(); err != nil { | ||
| d.peerCfg.Log.Errorf("failed to remove endpoint: %s", err) | ||
| } | ||
|
|
||
| _ = d.conn.Close() // do not care err because some cases it will return "use of closed network connection" | ||
| d.done.Unlock() | ||
| } | ||
|
|
||
| func (d *Listener) Close() { | ||
| d.peerCfg.Log.Infof("closing activity listener: %s", d.conn.LocalAddr().String()) | ||
| d.isClosed.Store(true) | ||
|
|
||
| if err := d.conn.Close(); err != nil { | ||
| d.peerCfg.Log.Errorf("failed to close UDP listener: %s", err) | ||
| } | ||
| d.done.Lock() | ||
| } | ||
|
|
||
| func (d *Listener) removeEndpoint() error { | ||
| return d.wgIface.RemovePeer(d.peerCfg.PublicKey) | ||
| } | ||
|
|
||
| func (d *Listener) createEndpoint() error { | ||
| d.peerCfg.Log.Debugf("creating lazy endpoint: %s", d.endpoint.String()) | ||
| return d.wgIface.UpdatePeer(d.peerCfg.PublicKey, d.peerCfg.AllowedIPs, 0, d.endpoint, nil) | ||
| // listener defines the contract for activity detection listeners. | ||
| type listener interface { | ||
| ReadPackets() | ||
| Close() | ||
| } | ||
|
|
||
| func (d *Listener) newConn() (*net.UDPConn, error) { | ||
| addr := &net.UDPAddr{ | ||
| Port: 0, | ||
| IP: listenIP, | ||
| // newListener creates a listener appropriate for the WireGuard interface type. | ||
| // Returns BindListener for userspace bind mode with ICEBind, otherwise UDPListener. | ||
| func newListener(wgIface WgInterface, cfg lazyconn.PeerConfig) (listener, error) { | ||
| if !wgIface.IsUserspaceBind() { | ||
| return NewUDPListener(wgIface, cfg) | ||
| } | ||
|
|
||
| conn, err := net.ListenUDP("udp", addr) | ||
| if err != nil { | ||
| log.Errorf("failed to create activity listener on %s: %s", addr, err) | ||
| return nil, err | ||
| provider, ok := wgIface.(bindProvider) | ||
| if !ok { | ||
| return nil, errors.New("interface claims userspace bind but doesn't implement bindProvider") | ||
| } | ||
|
|
||
| return conn, nil | ||
| return NewBindListener(wgIface, provider.GetBind(), cfg) | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.