Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to Google DNS if DNS lookup fails #1127

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ if (new File(rootDir, 'extras/extras.gradle').exists()) {

dependencies {
implementation project(path: ':common:data')
implementation project(path: ':common:networking')
implementation project(path: ':common:pages')

implementation deps.kotlin.stdlib
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package com.quran.labs.androidquran.module.application;

import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.quran.common.networking.dns.DnsModule;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Dns;
import okhttp3.OkHttpClient;

@Module
@Module(includes = { DnsModule.class })
public class DebugNetworkModule {

private static final int DEFAULT_READ_TIMEOUT_SECONDS = 20;
private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 15;

@Provides
@Singleton
static OkHttpClient provideOkHttpClient() {
static OkHttpClient provideOkHttpClient(Dns dns) {
return new OkHttpClient.Builder()
.readTimeout(DEFAULT_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.connectTimeout(DEFAULT_CONNECT_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.addNetworkInterceptor(new StethoInterceptor())
.dns(dns)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.quran.labs.androidquran.data.QuranDataProvider;
import com.quran.labs.androidquran.module.application.ApplicationModule;
import com.quran.labs.androidquran.module.application.DatabaseModule;
import com.quran.labs.androidquran.module.application.NetworkModule;
import com.quran.common.networking.NetworkModule;
import com.quran.labs.androidquran.pageselect.PageSelectActivity;
import com.quran.labs.androidquran.service.AudioService;
import com.quran.labs.androidquran.service.QuranDownloadService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.quran.data.source.PageSizeCalculator;
import com.quran.labs.androidquran.util.QuranSettings;

import java.io.File;

import javax.inject.Singleton;

import dagger.Module;
Expand Down Expand Up @@ -65,4 +67,9 @@ QuranSettings provideQuranSettings() {
Scheduler provideMainThreadScheduler() {
return AndroidSchedulers.mainThread();
}

@Provides
File provideCacheDirectory() {
return application.getCacheDir();
}
}
9 changes: 9 additions & 0 deletions common/networking/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'

dependencies {
compile deps.kotlin.stdlib
kapt deps.dagger.apt
implementation deps.dagger.runtime
implementation "com.squareup.okhttp3:okhttp-dnsoverhttps:${okhttpVersion}"
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package com.quran.labs.androidquran.module.application;
package com.quran.common.networking;

import com.quran.common.networking.dns.DnsModule;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Dns;
import okhttp3.OkHttpClient;

@Module
@Module(includes = { DnsModule.class })
public class NetworkModule {
private static final int DEFAULT_READ_TIMEOUT_SECONDS = 20;
private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 15;

@Provides
@Singleton
static OkHttpClient provideOkHttpClient() {
static OkHttpClient provideOkHttpClient(Dns dns) {
return new OkHttpClient.Builder()
.readTimeout(DEFAULT_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.connectTimeout(DEFAULT_CONNECT_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.dns(dns)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.quran.common.networking.dns

import dagger.Module
import dagger.Provides
import okhttp3.Cache
import okhttp3.Dns
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.dnsoverhttps.DnsOverHttps
import java.io.File
import java.net.InetAddress
import java.net.UnknownHostException

@Module
class DnsModule {

@Provides
fun providesDns(servers: List<@JvmSuppressWildcards Dns>): Dns {
return MultiDns(servers)
}

@Provides
fun provideDnsCache(cacheDirectory: File): Cache {
return Cache(cacheDirectory, 5 * 1024 * 1024L)
}

@Provides
fun provideServers(dnsCache: Cache): List<Dns> {
val bootstrapClient = OkHttpClient.Builder()
.cache(dnsCache)
.build()

val googleDns = provideGoogleDns(bootstrapClient)
return if (googleDns != null) {
listOf(Dns.SYSTEM, googleDns)
} else { listOf(Dns.SYSTEM) }
}

private fun provideGoogleDns(bootstrapClient: OkHttpClient): Dns? {
return try {
DnsOverHttps.Builder()
.client(bootstrapClient)
.url(HttpUrl.get("https://dns.google.com/experimental"))
.bootstrapDnsHosts(InetAddress.getByName("216.58.204.78"),
InetAddress.getByName("2a00:1450:4009:814:0:0:0:200e"))
.build()
} catch (exception: UnknownHostException) {
null
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.quran.common.networking.dns

import okhttp3.Dns
import java.net.InetAddress
import java.net.UnknownHostException

class MultiDns(private val servers: List<Dns>) : Dns {

override fun lookup(hostname: String): MutableList<InetAddress> {
var lastException: Exception? = null
for (i in 0 until servers.size) {
try {
return servers[i].lookup(hostname)
} catch (unknownHostException: UnknownHostException) {
lastException = unknownHostException
}
}

throw lastException!!
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include ':app'
include ':common:data'
include ':common:networking'
include ':common:pages'
include ':pages:madani'

Expand Down