fixed address resolution

This commit is contained in:
Stephan D
2026-03-03 19:37:09 +01:00
parent 41cb826d26
commit f7a1027de7

View File

@@ -52,7 +52,11 @@ func NewClient(grpcURL string, timeout time.Duration, authToken string, forceIPv
opts := []grpc.DialOption{transportCreds} opts := []grpc.DialOption{transportCreds}
if forceIPv4 { if forceIPv4 {
opts = append(opts, grpc.WithContextDialer(grpcForceIPv4Dialer)) host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, merrors.InvalidArgument("tronclient: invalid grpc address: " + address)
}
opts = append(opts, grpc.WithContextDialer(newForceIPv4Dialer(host, port)))
} }
if token := strings.TrimSpace(authToken); token != "" { if token := strings.TrimSpace(authToken); token != "" {
opts = append(opts, opts = append(opts,
@@ -71,12 +75,8 @@ func NewClient(grpcURL string, timeout time.Duration, authToken string, forceIPv
}, nil }, nil
} }
func grpcForceIPv4Dialer(ctx context.Context, address string) (net.Conn, error) { func newForceIPv4Dialer(host, port string) func(context.Context, string) (net.Conn, error) {
host, port, err := net.SplitHostPort(address) return func(ctx context.Context, _ string) (net.Conn, error) {
if err != nil {
return nil, err
}
ips, err := net.DefaultResolver.LookupIP(ctx, "ip4", host) ips, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -103,6 +103,7 @@ func grpcForceIPv4Dialer(ctx context.Context, address string) (net.Conn, error)
} }
return nil, merrors.Internal(fmt.Sprintf("failed to dial IPv4 address for %s", host)) return nil, merrors.Internal(fmt.Sprintf("failed to dial IPv4 address for %s", host))
} }
}
func normalizeGRPCAddress(grpcURL string) (string, bool, error) { func normalizeGRPCAddress(grpcURL string) (string, bool, error) {
target := strings.TrimSpace(grpcURL) target := strings.TrimSpace(grpcURL)