-
-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[client] Code cleaning in net pkg and fix exit node feature on Android(…
…#2932) Code cleaning around the util/net package. The goal was to write a more understandable source code but modify nothing on the logic. Protect the WireGuard UDP listeners with marks. The implementation can support the VPN permission revocation events in thread safe way. It will be important if we start to support the running time route and DNS update features. - uniformize the file name convention: [struct_name] _ [functions] _ [os].go - code cleaning in net_linux.go - move env variables to env.go file
- Loading branch information
Showing
23 changed files
with
245 additions
and
181 deletions.
There are no files selected for viewing
This file contains 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,12 @@ | ||
package bind | ||
|
||
import ( | ||
wireguard "golang.zx2c4.com/wireguard/conn" | ||
|
||
nbnet "github.com/netbirdio/netbird/util/net" | ||
) | ||
|
||
func init() { | ||
// ControlFns is not thread safe and should only be modified during init. | ||
*wireguard.ControlFns = append(*wireguard.ControlFns, nbnet.ControlProtectSocket) | ||
} |
This file contains 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 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 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 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,31 @@ | ||
//go:build !ios | ||
|
||
package net | ||
|
||
import ( | ||
"net" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Conn wraps a net.Conn to override the Close method | ||
type Conn struct { | ||
net.Conn | ||
ID ConnectionID | ||
} | ||
|
||
// Close overrides the net.Conn Close method to execute all registered hooks after closing the connection | ||
func (c *Conn) Close() error { | ||
err := c.Conn.Close() | ||
|
||
dialerCloseHooksMutex.RLock() | ||
defer dialerCloseHooksMutex.RUnlock() | ||
|
||
for _, hook := range dialerCloseHooks { | ||
if err := hook(c.ID, &c.Conn); err != nil { | ||
log.Errorf("Error executing dialer close hook: %v", err) | ||
} | ||
} | ||
|
||
return err | ||
} |
This file contains 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,58 @@ | ||
//go:build !ios | ||
|
||
package net | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func DialUDP(network string, laddr, raddr *net.UDPAddr) (*net.UDPConn, error) { | ||
if CustomRoutingDisabled() { | ||
return net.DialUDP(network, laddr, raddr) | ||
} | ||
|
||
dialer := NewDialer() | ||
dialer.LocalAddr = laddr | ||
|
||
conn, err := dialer.Dial(network, raddr.String()) | ||
if err != nil { | ||
return nil, fmt.Errorf("dialing UDP %s: %w", raddr.String(), err) | ||
} | ||
|
||
udpConn, ok := conn.(*Conn).Conn.(*net.UDPConn) | ||
if !ok { | ||
if err := conn.Close(); err != nil { | ||
log.Errorf("Failed to close connection: %v", err) | ||
} | ||
return nil, fmt.Errorf("expected UDP connection, got different type: %T", conn) | ||
} | ||
|
||
return udpConn, nil | ||
} | ||
|
||
func DialTCP(network string, laddr, raddr *net.TCPAddr) (*net.TCPConn, error) { | ||
if CustomRoutingDisabled() { | ||
return net.DialTCP(network, laddr, raddr) | ||
} | ||
|
||
dialer := NewDialer() | ||
dialer.LocalAddr = laddr | ||
|
||
conn, err := dialer.Dial(network, raddr.String()) | ||
if err != nil { | ||
return nil, fmt.Errorf("dialing TCP %s: %w", raddr.String(), err) | ||
} | ||
|
||
tcpConn, ok := conn.(*Conn).Conn.(*net.TCPConn) | ||
if !ok { | ||
if err := conn.Close(); err != nil { | ||
log.Errorf("Failed to close connection: %v", err) | ||
} | ||
return nil, fmt.Errorf("expected TCP connection, got different type: %T", conn) | ||
} | ||
|
||
return tcpConn, nil | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains 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 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,5 @@ | ||
package net | ||
|
||
func (d *Dialer) init() { | ||
d.Dialer.Control = ControlProtectSocket | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
package net | ||
|
||
func (d *Dialer) init() { | ||
// implemented on Linux and Android only | ||
} |
This file contains 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,29 @@ | ||
package net | ||
|
||
import ( | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/netbirdio/netbird/client/iface/netstack" | ||
) | ||
|
||
const ( | ||
envDisableCustomRouting = "NB_DISABLE_CUSTOM_ROUTING" | ||
envSkipSocketMark = "NB_SKIP_SOCKET_MARK" | ||
) | ||
|
||
func CustomRoutingDisabled() bool { | ||
if netstack.IsEnabled() { | ||
return true | ||
} | ||
return os.Getenv(envDisableCustomRouting) == "true" | ||
} | ||
|
||
func SkipSocketMark() bool { | ||
if skipSocketMark := os.Getenv(envSkipSocketMark); skipSocketMark == "true" { | ||
log.Infof("%s is set to true, skipping SO_MARK", envSkipSocketMark) | ||
return true | ||
} | ||
return false | ||
} |
This file contains 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,37 @@ | ||
//go:build !ios | ||
|
||
package net | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"sync" | ||
|
||
"github.com/pion/transport/v3" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// ListenUDP listens on the network address and returns a transport.UDPConn | ||
// which includes support for write and close hooks. | ||
func ListenUDP(network string, laddr *net.UDPAddr) (transport.UDPConn, error) { | ||
if CustomRoutingDisabled() { | ||
return net.ListenUDP(network, laddr) | ||
} | ||
|
||
conn, err := NewListener().ListenPacket(context.Background(), network, laddr.String()) | ||
if err != nil { | ||
return nil, fmt.Errorf("listen UDP: %w", err) | ||
} | ||
|
||
packetConn := conn.(*PacketConn) | ||
udpConn, ok := packetConn.PacketConn.(*net.UDPConn) | ||
if !ok { | ||
if err := packetConn.Close(); err != nil { | ||
log.Errorf("Failed to close connection: %v", err) | ||
} | ||
return nil, fmt.Errorf("expected UDPConn, got different type: %T", udpConn) | ||
} | ||
|
||
return &UDPConn{UDPConn: udpConn, ID: packetConn.ID, seenAddrs: &sync.Map{}}, nil | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains 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,6 @@ | ||
package net | ||
|
||
// init configures the net.ListenerConfig Control function to set the fwmark on the socket | ||
func (l *ListenerConfig) init() { | ||
l.ListenConfig.Control = ControlProtectSocket | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
package net | ||
|
||
func (l *ListenerConfig) init() { | ||
// implemented on Linux and Android only | ||
} |
Oops, something went wrong.