Commit 377c6235 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Show the source of a query resolved with dns rules

Implements #182
parent df606864
Pipeline #5319 passed with stage
in 3 minutes and 13 seconds
......@@ -87,6 +87,9 @@ interface DnsRuleDao {
@Query("SELECT * FROM DnsRule d1 WHERE ((:includeWhitelistEntries=1 AND d1.target == '') OR (:includeNonWhitelistEntries=1 AND d1.target!='')) AND (d1.type = :type OR d1.type=255) AND (d1.importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=d1.importedFrom),0) = 1) AND (d1.importedFrom IS NOT NULL OR :useUserRules=1) AND :host LIKE host AND isWildcard=1")
fun findPossibleWildcardRuleTarget(host: String, type: Record.TYPE, useUserRules:Boolean, includeWhitelistEntries:Boolean, includeNonWhitelistEntries:Boolean):List<DnsRule>
@Query("SELECT * FROM DnsRule d1 WHERE d1.host=:host AND d1.target != '' AND (d1.type = :type OR d1.type=255) AND (d1.importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=d1.importedFrom),0) = 1) AND (d1.importedFrom IS NOT NULL OR :useUserRules=1) AND (SELECT COUNT(*) FROM DnsRule d2 WHERE d2.target='' AND d2.host=:host AND (d2.type = :type OR d2.type=255) AND (d2.importedFrom IS NOT NULL OR :useUserRules=1) AND (importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=importedFrom),0)))=0 AND isWildcard=0 LIMIT 1")
fun findRuleTargetEntity(host: String, type: Record.TYPE, useUserRules:Boolean):DnsRule?
@Query("DELETE FROM DnsRule WHERE importedFrom=:sourceId")
fun deleteAllFromSource(sourceId: Long)
......
......@@ -14,6 +14,10 @@ import com.frostnerd.smokescreen.database.getDatabase
import com.frostnerd.smokescreen.dialog.DnsRuleDialog
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.fragment_querylog_detail.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.minidns.record.Record
import java.text.DateFormat
import java.util.*
......@@ -46,6 +50,7 @@ class QueryLogDetailFragment : Fragment() {
return if(isTimeStampToday(timestamp)) timeFormatSameDay.format(timestamp)
else timeFormatDifferentDay.format(timestamp)
}
private var hostSourceFetchJob: Job? = null
private fun isTimeStampToday(timestamp:Long):Boolean {
return timestamp >= getStartOfDay()
......@@ -128,6 +133,7 @@ class QueryLogDetailFragment : Fragment() {
private fun updateUi() {
val query = currentQuery
if(query != null && viewCreated) {
hostSourceFetchJob?.cancel()
queryTime.text = formatTimeStamp(query.questionTime)
if(query.responseTime >= query.questionTime) {
latency.text = (query.responseTime - query.questionTime).toString() + " ms"
......@@ -155,6 +161,28 @@ class QueryLogDetailFragment : Fragment() {
if(it.isBlank()) "-"
else it
}
if(query.responseSource == QueryListener.Source.LOCALRESOLVER) {
hostSourceWrap.visibility = View.VISIBLE
hostSourceFetchJob = GlobalScope.launch {
val sourceRule = getDatabase().dnsRuleDao().findRuleTargetEntity(query.name, query.type, true)
?: getDatabase().dnsRuleDao().findPossibleWildcardRuleTarget(query.name, query. type, true, false, true).firstOrNull {
DnsRuleDialog.databaseHostToMatcher(it.host).reset(query.name).matches()
}
val text = if (sourceRule != null) {
if(sourceRule.importedFrom == null) {
getString(R.string.windows_querylogging_hostsource__user)
} else {
getDatabase().hostSourceDao().findById(sourceRule.importedFrom)?.name ?: getString(R.string.windows_querylogging_hostsource__unknown)
}
} else {
getString(R.string.windows_querylogging_hostsource__unknown)
}
if(hostSourceFetchJob?.isCancelled == false) launch(Dispatchers.Main) {
hostSource.text = text
}
}
} else hostSourceWrap.visibility = View.GONE
}
}
......
......@@ -146,6 +146,39 @@
android:layout_height="wrap_content"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="24dp"
android:id="@+id/hostSourceWrap"
android:layout_height="wrap_content">
<ImageView
android:layout_width="24dp"
android:id="@+id/hostSourceImage"
android:src="@drawable/ic_view_list"
android:tint="?android:attr/textColor"
android:layout_height="24dp"/>
<TextView
android:layout_width="wrap_content"
android:textSize="18sp"
android:layout_toEndOf="@id/hostSourceImage"
android:text="@string/windows_querylogging_hostsource"
android:textStyle="italic"
android:layout_marginStart="8dp"
android:id="@+id/hostSourceText"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:textSize="18sp"
android:id="@+id/hostSource"
android:layout_alignStart="@id/hostSourceText"
android:layout_below="@id/hostSourceText"
android:layout_marginTop="8dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="24dp"
......
......@@ -83,6 +83,9 @@
<string name="windows_querylogging_type">Type</string>
<string name="windows_querylogging_question">Question</string>
<string name="windows_querylogging_used_server">Used server</string>
<string name="windows_querylogging_hostsource">Source of DNS rule</string>
<string name="windows_querylogging_hostsource__user">User rules</string>
<string name="windows_querylogging_hostsource__unknown">Unknown</string>
<string name="windows_querylogging_protocol">Protocol</string>
<string name="windows_querylogging_latency">Latency</string>
<string name="windows_querylogging_responses">Responses</string>
......
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