Restore Unix socket support (#498)

https://github.com/fullstorydev/grpcurl/issues/496

Restore -unix to working properly by default with the default dialer.
This commit is contained in:
zhyuri 2025-03-18 08:47:55 -04:00 committed by GitHub
parent 30f87c1323
commit 614b1687cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 7 deletions

View File

@ -484,12 +484,12 @@ func main() {
if *maxMsgSz > 0 { if *maxMsgSz > 0 {
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(*maxMsgSz))) opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(*maxMsgSz)))
} }
network := "tcp" if isUnixSocket != nil && isUnixSocket() && !strings.HasPrefix(target, "unix://") {
if isUnixSocket != nil && isUnixSocket() { // prepend unix:// to the address if it's not already there
network = "unix" // this is to maintain backwards compatibility because the custom dialer is replaced by
if *authority == "" { // the default dialer in grpc-go.
*authority = "localhost" // https://github.com/fullstorydev/grpcurl/pull/480
} target = "unix://" + target
} }
var creds credentials.TransportCredentials var creds credentials.TransportCredentials
if *plaintext { if *plaintext {
@ -557,7 +557,7 @@ func main() {
blockingDialTiming := dialTiming.Child("BlockingDial") blockingDialTiming := dialTiming.Child("BlockingDial")
defer blockingDialTiming.Done() defer blockingDialTiming.Done()
cc, err := grpcurl.BlockingDial(ctx, network, target, creds, opts...) cc, err := grpcurl.BlockingDial(ctx, "", target, creds, opts...)
if err != nil { if err != nil {
fail(err, "Failed to dial target host %q", target) fail(err, "Failed to dial target host %q", target)
} }

View File

@ -609,6 +609,8 @@ func ServerTransportCredentials(cacertFile, serverCertFile, serverKeyFile string
// BlockingDial is a helper method to dial the given address, using optional TLS credentials, // BlockingDial is a helper method to dial the given address, using optional TLS credentials,
// and blocking until the returned connection is ready. If the given credentials are nil, the // and blocking until the returned connection is ready. If the given credentials are nil, the
// connection will be insecure (plain-text). // connection will be insecure (plain-text).
// The network parameter should be left empty in most cases when your address is a RFC 3986
// compliant URI. The resolver from grpc-go will resolve the correct network type.
func BlockingDial(ctx context.Context, network, address string, creds credentials.TransportCredentials, opts ...grpc.DialOption) (*grpc.ClientConn, error) { func BlockingDial(ctx context.Context, network, address string, creds credentials.TransportCredentials, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
if creds == nil { if creds == nil {
creds = insecure.NewCredentials() creds = insecure.NewCredentials()
@ -645,6 +647,34 @@ func BlockingDial(ctx context.Context, network, address string, creds credential
writeResult: writeResult, writeResult: writeResult,
} }
switch network {
case "":
// no-op, use address as-is
case "tcp":
if strings.HasPrefix(address, "unix://") {
return nil, fmt.Errorf("tcp network type cannot use unix address %s", address)
}
case "unix":
if !strings.HasPrefix(address, "unix://") {
// prepend unix:// to the address if it's not already there
// this is to maintain backwards compatibility because the custom dialer is replaced by
// the default dialer in grpc-go.
// https://github.com/fullstorydev/grpcurl/pull/480
address = "unix://" + address
}
default:
// custom dialer for other networks
dialer := func(ctx context.Context, address string) (net.Conn, error) {
conn, err := (&net.Dialer{}).DialContext(ctx, network, address)
if err != nil {
// capture the error so we can provide a better message
writeResult(err)
}
return conn, err
}
opts = append([]grpc.DialOption{grpc.WithContextDialer(dialer)}, opts...)
}
// Even with grpc.FailOnNonTempDialError, this call will usually timeout in // Even with grpc.FailOnNonTempDialError, this call will usually timeout in
// the face of TLS handshake errors. So we can't rely on grpc.WithBlock() to // the face of TLS handshake errors. So we can't rely on grpc.WithBlock() to
// know when we're done. So we run it in a goroutine and then use result // know when we're done. So we run it in a goroutine and then use result