|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.networkprotection.impl.configuration |
| 18 | + |
| 19 | +import com.duckduckgo.di.scopes.VpnScope |
| 20 | +import com.duckduckgo.networkprotection.impl.VpnRemoteFeatures |
| 21 | +import com.squareup.anvil.annotations.ContributesTo |
| 22 | +import com.squareup.moshi.Moshi |
| 23 | +import dagger.Module |
| 24 | +import dagger.Provides |
| 25 | +import dagger.SingleInstanceIn |
| 26 | +import java.net.InetAddress |
| 27 | +import javax.inject.Qualifier |
| 28 | +import logcat.asLog |
| 29 | +import logcat.logcat |
| 30 | +import okhttp3.Dns |
| 31 | + |
| 32 | +interface VpnLocalDns : Dns |
| 33 | + |
| 34 | +private class VpnLocalDnsImpl( |
| 35 | + private val vpnRemoteFeatures: VpnRemoteFeatures, |
| 36 | + moshi: Moshi, |
| 37 | + private val defaultDns: Dns, |
| 38 | +) : VpnLocalDns { |
| 39 | + |
| 40 | + private val adapter = moshi.adapter(LocalDnsSettings::class.java) |
| 41 | + |
| 42 | + /** |
| 43 | + * String is the domain |
| 44 | + * DnsEntry is the DNS entry corresponding to the domain |
| 45 | + */ |
| 46 | + private val localDomains: Map<String, List<DnsEntry>> by lazy { |
| 47 | + getRemoteDnsEntries() |
| 48 | + } |
| 49 | + |
| 50 | + private val fallbackDomains: Map<String, List<DnsEntry>> = mapOf( |
| 51 | + "controller.netp.duckduckgo.com" to listOf( |
| 52 | + DnsEntry("20.253.26.112", "use"), |
| 53 | + DnsEntry("20.93.77.32", "eun"), |
| 54 | + ), |
| 55 | + ) |
| 56 | + |
| 57 | + override fun lookup(hostname: String): List<InetAddress> { |
| 58 | + logcat { "Lookup for $hostname" } |
| 59 | + |
| 60 | + val localResolution = localDomains[hostname] ?: fallbackDomains[hostname] |
| 61 | + |
| 62 | + return localResolution?.let { entries -> |
| 63 | + logcat { "Hardcoded DNS for $hostname" } |
| 64 | + |
| 65 | + entries.map { InetAddress.getByName(it.address) } |
| 66 | + } ?: defaultDns.lookup(hostname) |
| 67 | + } |
| 68 | + |
| 69 | + private fun getRemoteDnsEntries(): Map<String, List<DnsEntry>> { |
| 70 | + vpnRemoteFeatures.localDNS().getSettings()?.let { settings -> |
| 71 | + return try { |
| 72 | + adapter.fromJson(settings)?.domains |
| 73 | + } catch (t: Throwable) { |
| 74 | + logcat { "Error parsing localDNS settings: ${t.asLog()}" } |
| 75 | + null |
| 76 | + }.orEmpty() |
| 77 | + } |
| 78 | + |
| 79 | + return emptyMap() |
| 80 | + } |
| 81 | + |
| 82 | + /* |
| 83 | + "settings": { |
| 84 | + "domains": { |
| 85 | + "controller.duckduckgo.com": [ |
| 86 | + { |
| 87 | + "address": "1.2.3.4", |
| 88 | + "region": "use" |
| 89 | + }, |
| 90 | + { |
| 91 | + "address": "1.2.2.2", |
| 92 | + "region": "eun" |
| 93 | + } |
| 94 | + ] |
| 95 | + } |
| 96 | + } |
| 97 | + */ |
| 98 | + |
| 99 | + private data class LocalDnsSettings( |
| 100 | + val domains: Map<String, List<DnsEntry>>, |
| 101 | + ) |
| 102 | + |
| 103 | + private data class DnsEntry( |
| 104 | + val address: String, |
| 105 | + val region: String, |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +@ContributesTo(VpnScope::class) |
| 110 | +@Module |
| 111 | +object VpnLocalDnsModule { |
| 112 | + @Retention(AnnotationRetention.BINARY) |
| 113 | + @Qualifier |
| 114 | + private annotation class InternalApi |
| 115 | + |
| 116 | + @Provides |
| 117 | + @SingleInstanceIn(VpnScope::class) |
| 118 | + fun provideVpnLocalDns( |
| 119 | + vpnRemoteFeatures: VpnRemoteFeatures, |
| 120 | + moshi: Moshi, |
| 121 | + @InternalApi defaultDns: Dns, |
| 122 | + ): VpnLocalDns { |
| 123 | + return VpnLocalDnsImpl(vpnRemoteFeatures, moshi, defaultDns) |
| 124 | + } |
| 125 | + |
| 126 | + @Provides |
| 127 | + @InternalApi |
| 128 | + fun provideDefaultDns(): Dns { |
| 129 | + return Dns.SYSTEM |
| 130 | + } |
| 131 | +} |
0 commit comments