Last active
November 9, 2023 09:55
-
-
Save danmaas/c60af5fed9f55d2bc616ce302696540d to your computer and use it in GitHub Desktop.
CorePlane OkHttp DNS IPv4 preference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainApplication extends Application implements ReactApplication { | |
// ... | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
OkHttpClientProvider.setOkHttpClientFactory(new CorePlaneOkHttpClientFactory()); | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should import this:
import com.facebook.react.modules.network.OkHttpClientProvider;