mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-16 07:16:38 +00:00
- Move `util/grpc` and `util/net` to `client` so `internal` packages can be accessed - Add methods to return the next best interface after the NetBird interface. - Use `IP_UNICAST_IF` sock opt to force the outgoing interface for the NetBird `net.Dialer` and `net.ListenerConfig` to avoid routing loops. The interface is picked by the new route lookup method. - Some refactoring to avoid import cycles - Old behavior is available through `NB_USE_LEGACY_ROUTING=true` env var
48 lines
941 B
Go
48 lines
941 B
Go
package net
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"syscall"
|
|
|
|
"github.com/netbirdio/netbird/client/iface/netstack"
|
|
)
|
|
|
|
var (
|
|
androidProtectSocketLock sync.Mutex
|
|
androidProtectSocket func(fd int32) bool
|
|
)
|
|
|
|
func SetAndroidProtectSocketFn(fn func(fd int32) bool) {
|
|
androidProtectSocketLock.Lock()
|
|
androidProtectSocket = fn
|
|
androidProtectSocketLock.Unlock()
|
|
}
|
|
|
|
// ControlProtectSocket is a Control function that sets the fwmark on the socket
|
|
func ControlProtectSocket(_, _ string, c syscall.RawConn) error {
|
|
if netstack.IsEnabled() {
|
|
return nil
|
|
}
|
|
var aErr error
|
|
err := c.Control(func(fd uintptr) {
|
|
androidProtectSocketLock.Lock()
|
|
defer androidProtectSocketLock.Unlock()
|
|
|
|
if androidProtectSocket == nil {
|
|
aErr = fmt.Errorf("socket protection function not set")
|
|
return
|
|
}
|
|
|
|
if !androidProtectSocket(int32(fd)) {
|
|
aErr = fmt.Errorf("failed to protect socket via Android")
|
|
}
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return aErr
|
|
}
|