-
-
Notifications
You must be signed in to change notification settings - Fork 909
[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
Merged
Changes from all 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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package activity | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/netip" | ||
"sync" | ||
|
||
"github.com/netbirdio/netbird/client/iface/device" | ||
"github.com/netbirdio/netbird/client/internal/lazyconn" | ||
) | ||
|
||
type bindProvider interface { | ||
GetBind() device.EndpointManager | ||
} | ||
|
||
const ( | ||
// lazyBindPort is an obscure port used for lazy peer endpoints to avoid confusion with real peers. | ||
// The actual routing is done via fakeIP in ICEBind, not by this port. | ||
lazyBindPort = 17473 | ||
) | ||
|
||
// BindListener uses lazyConn with bind implementations for direct data passing in userspace bind mode. | ||
type BindListener struct { | ||
wgIface WgInterface | ||
peerCfg lazyconn.PeerConfig | ||
done sync.WaitGroup | ||
|
||
lazyConn *lazyConn | ||
bind device.EndpointManager | ||
fakeIP netip.Addr | ||
} | ||
|
||
// NewBindListener creates a listener that passes data directly through bind using LazyConn. | ||
// It automatically derives a unique fake IP from the peer's NetBird IP in the 127.2.x.x range. | ||
func NewBindListener(wgIface WgInterface, bind device.EndpointManager, cfg lazyconn.PeerConfig) (*BindListener, error) { | ||
fakeIP, err := deriveFakeIP(wgIface, cfg.AllowedIPs) | ||
if err != nil { | ||
return nil, fmt.Errorf("derive fake IP: %w", err) | ||
} | ||
|
||
d := &BindListener{ | ||
wgIface: wgIface, | ||
peerCfg: cfg, | ||
bind: bind, | ||
fakeIP: fakeIP, | ||
} | ||
|
||
if err := d.setupLazyConn(); err != nil { | ||
return nil, fmt.Errorf("setup lazy connection: %v", err) | ||
} | ||
|
||
d.done.Add(1) | ||
return d, nil | ||
} | ||
|
||
// deriveFakeIP creates a deterministic fake IP for bind mode based on peer's NetBird IP. | ||
// Maps peer IP 100.64.x.y to fake IP 127.2.x.y (similar to relay proxy using 127.1.x.y). | ||
// It finds the peer's actual NetBird IP by checking which allowedIP is in the same subnet as our WG interface. | ||
func deriveFakeIP(wgIface WgInterface, allowedIPs []netip.Prefix) (netip.Addr, error) { | ||
if len(allowedIPs) == 0 { | ||
return netip.Addr{}, fmt.Errorf("no allowed IPs for peer") | ||
} | ||
|
||
ourNetwork := wgIface.Address().Network | ||
|
||
var peerIP netip.Addr | ||
for _, allowedIP := range allowedIPs { | ||
ip := allowedIP.Addr() | ||
if !ip.Is4() { | ||
continue | ||
} | ||
if ourNetwork.Contains(ip) { | ||
peerIP = ip | ||
break | ||
} | ||
} | ||
|
||
if !peerIP.IsValid() { | ||
return netip.Addr{}, fmt.Errorf("no peer NetBird IP found in allowed IPs") | ||
} | ||
|
||
octets := peerIP.As4() | ||
fakeIP := netip.AddrFrom4([4]byte{127, 2, octets[2], octets[3]}) | ||
return fakeIP, nil | ||
} | ||
|
||
func (d *BindListener) setupLazyConn() error { | ||
d.lazyConn = newLazyConn() | ||
d.bind.SetEndpoint(d.fakeIP, d.lazyConn) | ||
|
||
endpoint := &net.UDPAddr{ | ||
IP: d.fakeIP.AsSlice(), | ||
Port: lazyBindPort, | ||
} | ||
return d.wgIface.UpdatePeer(d.peerCfg.PublicKey, d.peerCfg.AllowedIPs, 0, endpoint, nil) | ||
} | ||
|
||
// ReadPackets blocks until activity is detected on the LazyConn or the listener is closed. | ||
func (d *BindListener) ReadPackets() { | ||
select { | ||
case <-d.lazyConn.ActivityChan(): | ||
d.peerCfg.Log.Infof("activity detected via LazyConn") | ||
case <-d.lazyConn.ctx.Done(): | ||
d.peerCfg.Log.Infof("exit from activity listener") | ||
} | ||
|
||
d.peerCfg.Log.Debugf("removing lazy endpoint for peer %s", d.peerCfg.PublicKey) | ||
if err := d.wgIface.RemovePeer(d.peerCfg.PublicKey); err != nil { | ||
d.peerCfg.Log.Errorf("failed to remove endpoint: %s", err) | ||
} | ||
|
||
_ = d.lazyConn.Close() | ||
d.bind.RemoveEndpoint(d.fakeIP) | ||
d.done.Done() | ||
} | ||
|
||
// Close stops the listener and cleans up resources. | ||
func (d *BindListener) Close() { | ||
d.peerCfg.Log.Infof("closing activity listener (LazyConn)") | ||
|
||
if err := d.lazyConn.Close(); err != nil { | ||
d.peerCfg.Log.Errorf("failed to close LazyConn: %s", err) | ||
} | ||
|
||
d.done.Wait() | ||
} |
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.