Skip to content
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

[client] Code cleaning in net pkg #2932

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/internal/routemanager/systemops/systemops_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type ruleParams struct {

// isLegacy determines whether to use the legacy routing setup
func isLegacy() bool {
return os.Getenv("NB_USE_LEGACY_ROUTING") == "true" || nbnet.CustomRoutingDisabled() || os.Getenv(nbnet.EnvSkipSocketMark) == "true"
return os.Getenv("NB_USE_LEGACY_ROUTING") == "true" || nbnet.CustomRoutingDisabled() || nbnet.SkipSocketMar()
}

// setIsLegacy sets the legacy routing setup
Expand Down
31 changes: 31 additions & 0 deletions util/net/conn.go
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
}
58 changes: 58 additions & 0 deletions util/net/dial.go
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.
70 changes: 0 additions & 70 deletions util/net/dialer_nonios.go → util/net/dialer_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,6 @@ func (d *Dialer) Dial(network, address string) (net.Conn, error) {
return d.DialContext(context.Background(), network, address)
}

// 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
}

func callDialerHooks(ctx context.Context, connID ConnectionID, address string, resolver *net.Resolver) error {
host, _, err := net.SplitHostPort(address)
if err != nil {
Expand All @@ -127,51 +105,3 @@ func callDialerHooks(ctx context.Context, connID ConnectionID, address string, r

return result.ErrorOrNil()
}

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.
2 changes: 1 addition & 1 deletion util/net/dialer_linux.go → util/net/dialer_init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import "syscall"
// init configures the net.Dialer Control function to set the fwmark on the socket
func (d *Dialer) init() {
d.Dialer.Control = func(_, _ string, c syscall.RawConn) error {
return SetRawSocketMark(c)
return setRawSocketMark(c)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
package net

func (d *Dialer) init() {
// implemented on Linux and Android only
}
29 changes: 29 additions & 0 deletions util/net/env.go
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 SkipSocketMar() bool {
pappz marked this conversation as resolved.
Show resolved Hide resolved
if skipSocketMark := os.Getenv(envSkipSocketMark); skipSocketMark == "true" {
log.Info("%s is set to true, skipping SO_MARK", envSkipSocketMark)

Check failure on line 25 in util/net/env.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

printf: github.com/sirupsen/logrus.Info call has possible Printf formatting directive %s (govet)

Check failure on line 25 in util/net/env.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

printf: github.com/sirupsen/logrus.Info call has possible Printf formatting directive %s (govet)

Check failure on line 25 in util/net/env.go

View workflow job for this annotation

GitHub Actions / test (sqlite)

github.com/sirupsen/logrus.Info call has possible Printf formatting directive %s

Check failure on line 25 in util/net/env.go

View workflow job for this annotation

GitHub Actions / test

github.com/sirupsen/logrus.Info call has possible Printf formatting directive %s

Check failure on line 25 in util/net/env.go

View workflow job for this annotation

GitHub Actions / test (amd64, sqlite)

github.com/sirupsen/logrus.Info call has possible Printf formatting directive %s

Check failure on line 25 in util/net/env.go

View workflow job for this annotation

GitHub Actions / test (amd64, postgres)

github.com/sirupsen/logrus.Info call has possible Printf formatting directive %s

Check failure on line 25 in util/net/env.go

View workflow job for this annotation

GitHub Actions / test (386, postgres)

github.com/sirupsen/logrus.Info call has possible Printf formatting directive %s

Check failure on line 25 in util/net/env.go

View workflow job for this annotation

GitHub Actions / test (386, sqlite)

github.com/sirupsen/logrus.Info call has possible Printf formatting directive %s
return true
}
return false
}
37 changes: 37 additions & 0 deletions util/net/listen.go
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.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (
// init configures the net.ListenerConfig Control function to set the fwmark on the socket
func (l *ListenerConfig) init() {
l.ListenConfig.Control = func(_, _ string, c syscall.RawConn) error {
return SetRawSocketMark(c)
return setRawSocketMark(c)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
package net

func (l *ListenerConfig) init() {
// implemented on Linux and Android only
}
25 changes: 0 additions & 25 deletions util/net/listener_nonios.go → util/net/listener_listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net"
"sync"

"github.com/pion/transport/v3"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -146,27 +145,3 @@ func closeConn(id ConnectionID, conn net.PacketConn) error {

return err
}

// 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
}
12 changes: 0 additions & 12 deletions util/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package net

import (
"net"
"os"

"github.com/netbirdio/netbird/client/iface/netstack"

"github.com/google/uuid"
)
Expand All @@ -16,8 +13,6 @@ const (
PreroutingFwmarkRedirected = 0x1BD01
PreroutingFwmarkMasquerade = 0x1BD11
PreroutingFwmarkMasqueradeReturn = 0x1BD12

envDisableCustomRouting = "NB_DISABLE_CUSTOM_ROUTING"
)

// ConnectionID provides a globally unique identifier for network connections.
Expand All @@ -31,10 +26,3 @@ type RemoveHookFunc func(connID ConnectionID) error
func GenerateConnID() ConnectionID {
return ConnectionID(uuid.NewString())
}

func CustomRoutingDisabled() bool {
if netstack.IsEnabled() {
return true
}
return os.Getenv(envDisableCustomRouting) == "true"
}
Loading
Loading