2024-04-08 18:56:52 +02:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-17 16:26:06 +02:00
|
|
|
"crypto/tls"
|
2024-11-19 14:14:58 +01:00
|
|
|
"fmt"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2024-04-08 18:56:52 +02:00
|
|
|
"net"
|
2024-04-11 15:29:03 +02:00
|
|
|
"os/user"
|
|
|
|
"runtime"
|
2024-07-17 16:26:06 +02:00
|
|
|
"time"
|
2024-04-08 18:56:52 +02:00
|
|
|
|
2024-07-17 16:26:06 +02:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2024-04-08 18:56:52 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"google.golang.org/grpc"
|
2024-07-17 16:26:06 +02:00
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
"google.golang.org/grpc/keepalive"
|
2024-04-08 18:56:52 +02:00
|
|
|
|
|
|
|
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 {
|
2024-11-19 14:14:58 +01:00
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, "failed to get current user: %v", err)
|
2024-04-11 15:29:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// the custom dialer requires root permissions which are not required for use cases run as non-root
|
|
|
|
if currentUser.Uid != "0" {
|
2024-11-19 14:14:58 +01:00
|
|
|
log.Debug("Not running as root, using standard dialer")
|
2024-04-11 15:29:03 +02:00
|
|
|
dialer := &net.Dialer{}
|
|
|
|
return dialer.DialContext(ctx, "tcp", addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 14:14:58 +01:00
|
|
|
log.Debug("Using nbnet.NewDialer()")
|
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)
|
2024-11-19 14:14:58 +01:00
|
|
|
return nil, fmt.Errorf("nbnet.NewDialer().DialContext: %w", err)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
})
|
|
|
|
}
|
2024-07-17 16:26:06 +02:00
|
|
|
|
|
|
|
// grpcDialBackoff is the backoff mechanism for the grpc calls
|
|
|
|
func Backoff(ctx context.Context) backoff.BackOff {
|
|
|
|
b := backoff.NewExponentialBackOff()
|
|
|
|
b.MaxElapsedTime = 10 * time.Second
|
|
|
|
b.Clock = backoff.SystemClock
|
|
|
|
return backoff.WithContext(b, ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateConnection(addr string, tlsEnabled bool) (*grpc.ClientConn, error) {
|
|
|
|
transportOption := grpc.WithTransportCredentials(insecure.NewCredentials())
|
|
|
|
|
|
|
|
if tlsEnabled {
|
|
|
|
transportOption = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
connCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
conn, err := grpc.DialContext(
|
|
|
|
connCtx,
|
|
|
|
addr,
|
|
|
|
transportOption,
|
|
|
|
WithCustomDialer(),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
|
|
|
Time: 30 * time.Second,
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("DialContext error: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|