2024-04-08 18:56:52 +02:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2024-04-11 15:29:03 +02:00
|
|
|
"os/user"
|
|
|
|
"runtime"
|
2024-04-08 18:56:52 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
nbnet "github.com/netbirdio/netbird/util/net"
|
|
|
|
)
|
|
|
|
|
|
|
|
func WithCustomDialer() grpc.DialOption {
|
|
|
|
return grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
2024-04-11 15:29:03 +02:00
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
currentUser, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to get current user: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the custom dialer requires root permissions which are not required for use cases run as non-root
|
|
|
|
if currentUser.Uid != "0" {
|
|
|
|
dialer := &net.Dialer{}
|
|
|
|
return dialer.DialContext(ctx, "tcp", addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 18:56:52 +02:00
|
|
|
conn, err := nbnet.NewDialer().DialContext(ctx, "tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to dial: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
})
|
|
|
|
}
|