android: For UDS, use fake IP instead of localhost

This avoids a DNS lookup, which can be slow and fail.

Fixes #11442
This commit is contained in:
Eric Anderson 2024-09-27 07:17:11 -07:00
parent d169a5de6f
commit a908b5e40d
1 changed files with 5 additions and 2 deletions

View File

@ -68,12 +68,15 @@ public final class UdsChannelBuilder {
throw new UnsupportedOperationException("OkHttpChannelBuilder not found on the classpath"); throw new UnsupportedOperationException("OkHttpChannelBuilder not found on the classpath");
} }
try { try {
// Target 'dns:///localhost' is unused, but necessary as an argument for OkHttpChannelBuilder. // Target 'dns:///127.0.0.1' is unused, but necessary as an argument for OkHttpChannelBuilder.
// An IP address is used instead of localhost to avoid a DNS lookup (see #11442). This should
// work even if IPv4 is unavailable, as the DNS resolver doesn't need working IPv4 to parse an
// IPv4 address. Unavailable IPv4 fails when we connect(), not at resolution time.
// TLS is unsupported because Conscrypt assumes the platform Socket implementation to improve // TLS is unsupported because Conscrypt assumes the platform Socket implementation to improve
// performance by using the file descriptor directly. // performance by using the file descriptor directly.
Object o = OKHTTP_CHANNEL_BUILDER_CLASS Object o = OKHTTP_CHANNEL_BUILDER_CLASS
.getMethod("forTarget", String.class, ChannelCredentials.class) .getMethod("forTarget", String.class, ChannelCredentials.class)
.invoke(null, "dns:///localhost", InsecureChannelCredentials.create()); .invoke(null, "dns:///127.0.0.1", InsecureChannelCredentials.create());
ManagedChannelBuilder<?> builder = OKHTTP_CHANNEL_BUILDER_CLASS.cast(o); ManagedChannelBuilder<?> builder = OKHTTP_CHANNEL_BUILDER_CLASS.cast(o);
OKHTTP_CHANNEL_BUILDER_CLASS OKHTTP_CHANNEL_BUILDER_CLASS
.getMethod("socketFactory", SocketFactory.class) .getMethod("socketFactory", SocketFactory.class)