Skip to content

Instantly share code, notes, and snippets.

@danmaas
Last active November 9, 2023 09:55
Show Gist options
  • Save danmaas/c60af5fed9f55d2bc616ce302696540d to your computer and use it in GitHub Desktop.
Save danmaas/c60af5fed9f55d2bc616ce302696540d to your computer and use it in GitHub Desktop.
CorePlane OkHttp DNS IPv4 preference
// Customize OkHttp to add IPv4 DNS preference
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.OkHttpClientFactory;
import okhttp3.OkHttpClient;
public class CorePlaneOkHttpClientFactory implements OkHttpClientFactory {
public OkHttpClient createNewNetworkModuleClient() {
return OkHttpClientProvider.createClientBuilder()
.dns(new CorePlaneOkHttpDNSSelector(CorePlaneOkHttpDNSSelector.IPvMode.IPV4_FIRST))
.build();
}
}
import okhttp3.Dns
import java.net.Inet4Address
import java.net.Inet6Address
import java.net.InetAddress
import java.util.logging.Logger
class CorePlaneOkHttpDNSSelector(private val mode: IPvMode) : Dns {
enum class IPvMode(val code: String) {
SYSTEM("system"),
IPV6_FIRST("ipv6"),
IPV4_FIRST("ipv4"),
IPV6_ONLY("ipv6only"),
IPV4_ONLY("ipv4only");
companion object {
@JvmStatic
fun fromString(ipMode: String): IPvMode =
IPvMode.values().find { it.code == ipMode } ?: throw Exception("Unknown value $ipMode")
}
}
override fun lookup(hostname: String): List<InetAddress> {
var addresses = Dns.SYSTEM.lookup(hostname)
addresses = when (mode) {
IPvMode.IPV6_FIRST -> addresses.sortedBy { Inet4Address::class.java.isInstance(it) }
IPvMode.IPV4_FIRST -> addresses.sortedBy { Inet6Address::class.java.isInstance(it) }
IPvMode.IPV6_ONLY -> addresses.filter({ Inet6Address::class.java.isInstance(it) })
IPvMode.IPV4_ONLY -> addresses.filter({ Inet4Address::class.java.isInstance(it) })
IPvMode.SYSTEM -> addresses
}
//logger.fine("DJMOKHTTP ($hostname): " + addresses.joinToString(", ") { it.toString() })
return addresses
}
companion object {
private val logger = Logger.getLogger(CorePlaneOkHttpDNSSelector::class.java.name)
}
}
public class MainApplication extends Application implements ReactApplication {
// ...
@Override
public void onCreate() {
super.onCreate();
OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory());
}
// ...
}
@JKKholmatov
Copy link

JKKholmatov commented Aug 30, 2023

How can I apply this to an Expo app?
@andreialecu @andac-ozcan @danmaas

@JKKholmatov
Copy link

JKKholmatov commented Aug 31, 2023

when i try to compile with this i get: cannot find symbol OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory()); ^ symbol: variable OkHttpClientProvider location: class MainApplication what is the solution?

@HodayaGruz I also got this error, did You find how to solve it?

@andac-ozcan
Copy link

when i try to compile with this i get: cannot find symbol OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory()); ^ symbol: variable OkHttpClientProvider location: class MainApplication what is the solution?

@HodayaGruz I also got this error, did You find how to solve it?

You should import this:
import com.facebook.react.modules.network.OkHttpClientProvider;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment