Commit 70668ca6 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

The service now uses the defined dns rules

parent db814652
......@@ -3,7 +3,10 @@ package com.frostnerd.smokescreen.database.dao
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.TypeConverters
import com.frostnerd.smokescreen.database.converters.DnsTypeConverter
import com.frostnerd.smokescreen.database.entities.DnsRule
import org.minidns.record.Record
/*
* Copyright (C) 2019 Daniel Wolf (Ch4t4r)
......@@ -24,6 +27,7 @@ import com.frostnerd.smokescreen.database.entities.DnsRule
* You can contact the developer at daniel.wolf@frostnerd.com.
*/
@Dao
@TypeConverters(DnsTypeConverter::class)
interface DnsRuleDao {
@Query("DELETE FROM DnsRule")
......@@ -46,4 +50,7 @@ interface DnsRuleDao {
@Query("SELECT COUNT(*) FROM DnsRule")
fun getCount():Long
@Query("SELECT target FROM DnsRule WHERE host=:host AND type = :type LIMIT 1")
fun findRuleTarget(host:String, type:Record.TYPE):String?
}
\ No newline at end of file
......@@ -40,10 +40,13 @@ import kotlinx.coroutines.*
import org.minidns.dnsmessage.DnsMessage
import org.minidns.dnsmessage.Question
import org.minidns.dnsname.DnsName
import org.minidns.record.A
import org.minidns.record.AAAA
import org.minidns.record.Record
import java.io.ByteArrayInputStream
import java.io.DataInputStream
import java.io.Serializable
import java.lang.IllegalStateException
import java.net.Inet4Address
import java.net.Inet6Address
import java.net.InetAddress
......@@ -213,7 +216,8 @@ class DnsVpnService : VpnService(), Runnable {
"allow_ipv4_traffic",
"dns_server_config",
"notification_allow_stop",
"notification_allow_pause"
"notification_allow_pause",
"dns_rules_enabled"
)
settingsSubscription = getPreferences().listenForChanges(relevantSettings, getPreferences().preferenceChangeListener {
changes ->
......@@ -752,7 +756,7 @@ class DnsVpnService : VpnService(), Runnable {
handle.ipv4Enabled =
!handle.ipv6Enabled || (getPreferences().enableIpv4 && (getPreferences().forceIpv4 || hasDeviceIpv4Address()))
dnsProxy = SmokeProxy(handle, createProxyBypassHandlers(), createDnsCache(), createQueryLogger())
dnsProxy = SmokeProxy(handle, createProxyBypassHandlers(), createDnsCache(), createQueryLogger(), createLocalResolver())
log("DnsProxy created, creating VPN proxy")
vpnProxy = VPNTunnelProxy(dnsProxy!!, vpnService = this, coroutineScope = CoroutineScope(
newFixedThreadPoolContext(1, "proxy-pool")), logger = object:com.frostnerd.vpntunnelproxy.Logger {
......@@ -912,6 +916,44 @@ class DnsVpnService : VpnService(), Runnable {
return dnsCache
}
private fun createLocalResolver():LocalResolver? {
if(getPreferences().dnsRulesEnabled) {
return object:LocalResolver(false) {
private val dao = getDatabase().dnsRuleDao()
private val resolveResults = mutableMapOf<Question, String>()
override suspend fun canResolve(question: Question): Boolean {
return if(question.type != Record.TYPE.A && question.type != Record.TYPE.AAAA) {
false
} else {
val resolveResult = dao.findRuleTarget(question.name.toString(), question.type)
if (resolveResult != null) {
resolveResults[question] = resolveResult
true
} else false
}
}
override suspend fun resolve(question: Question): List<Record<*>> {
val result = resolveResults.remove(question)
return result?.let {
val data = if(question.type == Record.TYPE.A) {
A(it)
} else {
AAAA(it)
}
listOf(Record(question.name.toString(), question.type, question.clazz.value, 9999, data))
} ?: throw IllegalStateException()
}
override fun cleanup() {}
}
} else {
return null
}
}
private fun createPersistedCacheEntry(
dnsName: String,
type: Record.TYPE,
......
package com.frostnerd.smokescreen.util.proxy
import com.frostnerd.dnstunnelproxy.DnsHandle
import com.frostnerd.dnstunnelproxy.DnsPacketProxy
import com.frostnerd.dnstunnelproxy.*
import com.frostnerd.dnstunnelproxy.QueryListener
import com.frostnerd.dnstunnelproxy.SimpleDnsCache
import com.frostnerd.smokescreen.service.DnsVpnService
import org.minidns.dnsmessage.DnsMessage
import org.minidns.record.A
......@@ -36,7 +34,8 @@ class SmokeProxy(
dnsHandle: DnsHandle,
proxyBypassHandles: List<DnsHandle>,
val cache: SimpleDnsCache?,
queryListener: QueryListener?
queryListener: QueryListener?,
localResolver: LocalResolver?
) :
DnsPacketProxy(
proxyBypassHandles.toMutableList().let {
......@@ -45,7 +44,8 @@ class SmokeProxy(
}.toList(),
null,
cache,
queryListener = queryListener
queryListener = queryListener,
localResolver = localResolver
)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment