Commit 913f3050 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

DnsRules with an empty string as target are now interpreted as whitelist entries

parent 1a03207f
......@@ -31,9 +31,11 @@ import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_dns_rules.*
import kotlinx.android.synthetic.main.item_datasource.view.*
import kotlinx.android.synthetic.main.item_datasource.view.cardContent
import kotlinx.android.synthetic.main.item_datasource.view.delete
import kotlinx.android.synthetic.main.item_datasource.view.enable
import kotlinx.android.synthetic.main.item_datasource.view.text
import kotlinx.android.synthetic.main.item_datasource_rules.view.*
import kotlinx.android.synthetic.main.item_dnsrule_host.view.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
......@@ -201,7 +203,8 @@ class DnsRuleActivity : BaseActivity() {
else -> it
}
}
val id = getDatabase().dnsRuleDao().insertIgnore(newRule)
val id = if(newRule.isWhitelistRule()) getDatabase().dnsRuleDao().insertWhitelist(newRule)
else getDatabase().dnsRuleDao().insertIgnore(newRule)
if(id != -1L) {
userDnsRules.add(insertPos, newRule)
val wereRulesShown = showUserRules
......@@ -229,10 +232,14 @@ class DnsRuleActivity : BaseActivity() {
sourceAdapter.notifyItemRemoved(sourceAdapterList.size + 1 + index)
}, editRule = {
DnsRuleDialog(this, it) { newRule ->
getDatabase().dnsRuleRepository().updateAsync(newRule)
val index = userDnsRules.indexOf(it)
userDnsRules[index] = newRule
sourceAdapter.notifyItemChanged(sourceAdapterList.size + 1 + index)
val rows = getDatabase().dnsRuleDao().updateIgnore(newRule)
if(rows > 0) {
val index = userDnsRules.indexOf(it)
userDnsRules[index] = newRule
sourceAdapter.notifyItemChanged(sourceAdapterList.size + 1 + index)
} else {
Snackbar.make(findViewById(android.R.id.content), R.string.window_dnsrules_hostalreadyexists, Snackbar.LENGTH_LONG).show()
}
}.show()
})
}
......@@ -444,6 +451,7 @@ class DnsRuleActivity : BaseActivity() {
val text = view.text
val delete = view.delete
val cardContent = view.cardContent
val whitelistIndicator = view.whitelistIndicator
lateinit var dnsRule:DnsRule
init {
......@@ -458,6 +466,7 @@ class DnsRuleActivity : BaseActivity() {
fun display(rule:DnsRule) {
dnsRule = rule
text.text = rule.host
whitelistIndicator.visibility = if(rule.isWhitelistRule()) View.VISIBLE else View.GONE
}
override fun destroy() {}
}
......
......@@ -30,12 +30,18 @@ interface DnsRuleDao {
@Update
fun update(dnsRule: DnsRule)
@Update(onConflict = OnConflictStrategy.IGNORE)
fun updateIgnore(dnsRule: DnsRule):Int
@Insert
fun insert(dnsRule: DnsRule)
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insertIgnore(dnsRule: DnsRule):Long
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertWhitelist(dnsRule: DnsRule):Long
@Query("DELETE FROM DnsRule WHERE importedFrom IS NULL")
fun deleteAllUserRules()
......@@ -66,7 +72,7 @@ interface DnsRuleDao {
@Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom IS NOT NULL")
fun getNonUserCount(): Long
@Query("SELECT CASE WHEN :type=28 THEN IFNULL(ipv6Target, target) ELSE target END FROM DnsRule WHERE host=:host AND (type = :type OR type=255) AND (importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=importedFrom),0) = 1) AND (importedFrom IS NOT NULL OR :useUserRules=1) LIMIT 1")
@Query("SELECT CASE WHEN :type=28 THEN IFNULL(ipv6Target, target) ELSE target END FROM DnsRule WHERE host=:host AND (type = :type OR type=255) AND (importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=importedFrom),0) = 1) AND (importedFrom IS NOT NULL OR :useUserRules=1) AND (SELECT COUNT(*) FROM DnsRule WHERE target='' AND host=:host)=0 LIMIT 1")
fun findRuleTarget(host: String, type: Record.TYPE, useUserRules:Boolean): String?
@Query("DELETE FROM DnsRule WHERE importedFrom=:sourceId")
......
......@@ -46,4 +46,6 @@ data class DnsRule(
// 1 = marked delete
// 2 = marked insert
var stagingType:Int? = null
fun isWhitelistRule() = target == ""
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.frostnerd.smokescreen.dialog
import android.content.Context
import android.content.DialogInterface
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.frostnerd.smokescreen.R
import com.frostnerd.smokescreen.database.entities.DnsRule
......@@ -31,6 +32,8 @@ import java.net.Inet6Address
*/
class DnsRuleDialog(context: Context, dnsRule: DnsRule? = null, onRuleCreated: (DnsRule) -> Unit) :
AlertDialog(context, context.getPreferences().theme.dialogStyle) {
private var isWhitelist = false
init {
val view = layoutInflater.inflate(R.layout.dialog_create_dnsrule, null, false)
setView(view)
......@@ -40,6 +43,9 @@ class DnsRuleDialog(context: Context, dnsRule: DnsRule? = null, onRuleCreated: (
}
setButton(DialogInterface.BUTTON_POSITIVE, context.getString(android.R.string.ok)) { _, _ ->
}
setButton(DialogInterface.BUTTON_NEUTRAL, context.getString(R.string.dialog_newdnsrule_whitelist)) { _, _ ->
}
setOnShowListener {
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
......@@ -66,17 +72,22 @@ class DnsRuleDialog(context: Context, dnsRule: DnsRule? = null, onRuleCreated: (
if (valid) {
dismiss()
val type = when {
isWhitelist -> Record.TYPE.ANY
!view.ipv4Address.text.isNullOrBlank() && !view.ipv6Address.text.isNullOrBlank() -> {
Record.TYPE.ANY
}
!view.ipv4Address.text.isNullOrBlank() -> Record.TYPE.A
else -> Record.TYPE.AAAA
}
val primaryTarget = when (type) {
val primaryTarget = if (isWhitelist) {
""
} else when (type) {
Record.TYPE.A, Record.TYPE.ANY -> view.ipv4Address.text.toString()
else -> view.ipv6Address.text.toString()
}
val secondaryTarget = when (type) {
val secondaryTarget = if (isWhitelist) {
null
} else when (type) {
Record.TYPE.AAAA, Record.TYPE.ANY -> view.ipv6Address.text.toString()
else -> null
}
......@@ -85,27 +96,45 @@ class DnsRuleDialog(context: Context, dnsRule: DnsRule? = null, onRuleCreated: (
host = view.host.text.toString(),
target = primaryTarget,
ipv6Target = secondaryTarget
) ?: DnsRule(Record.TYPE.A, view.host.text.toString(), view.ipv4Address.text.toString())
) ?: DnsRule(type, view.host.text.toString(), primaryTarget, secondaryTarget)
onRuleCreated(
newRule
)
}
}
}
if (dnsRule != null) {
view.host.setText(dnsRule.host)
when {
dnsRule.type == Record.TYPE.A -> {
view.ipv4Address.setText(dnsRule.target)
view.ipv6Address.text = null
}
dnsRule.type == Record.TYPE.AAAA -> {
view.ipv4Address.text = null
view.ipv6Address.setText(dnsRule.target)
}
dnsRule.type == Record.TYPE.ANY -> {
view.ipv4Address.setText(dnsRule.target)
view.ipv6Address.setText(dnsRule.ipv6Target)
getButton(DialogInterface.BUTTON_NEUTRAL).setOnClickListener {
isWhitelist = !isWhitelist
getButton(DialogInterface.BUTTON_NEUTRAL).text =
if (isWhitelist) context.getString(R.string.dialog_newdnsrule_specify_address)
else context.getString(R.string.dialog_newdnsrule_whitelist)
val visibility = if (isWhitelist) View.GONE else View.VISIBLE
view.ipv4Til.visibility = visibility
view.ipv6Til.visibility = visibility
}
if (dnsRule != null) {
if (dnsRule.isWhitelistRule()) {
isWhitelist = true
getButton(DialogInterface.BUTTON_NEUTRAL).text =
context.getString(R.string.dialog_newdnsrule_specify_address)
view.ipv4Til.visibility = View.GONE
view.ipv6Til.visibility = View.GONE
} else {
view.host.setText(dnsRule.host)
when {
dnsRule.type == Record.TYPE.A -> {
view.ipv4Address.setText(dnsRule.target)
view.ipv6Address.text = null
}
dnsRule.type == Record.TYPE.AAAA -> {
view.ipv4Address.text = null
view.ipv6Address.setText(dnsRule.target)
}
dnsRule.type == Record.TYPE.ANY -> {
view.ipv4Address.setText(dnsRule.target)
view.ipv6Address.setText(dnsRule.ipv6Target)
}
}
}
}
}
......
<vector android:height="24dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="?android:attr/textColor" android:pathData="M466.5,83.7l-192,-80a48.15,48.15 0,0 0,-36.9 0l-192,80C27.7,91.1 16,108.6 16,128c0,198.5 114.5,335.7 221.5,380.3 11.8,4.9 25.1,4.9 36.9,0C360.1,472.6 496,349.3 496,128c0,-19.4 -11.7,-36.9 -29.5,-44.3z"/>
</vector>
......@@ -14,14 +14,23 @@
android:id="@+id/cardContent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="32dp"
android:src="@drawable/ic_shield"
android:paddingStart="12dp"
android:id="@+id/whitelistIndicator"
android:layout_centerVertical="true"
android:visibility="gone"
android:layout_height="32dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:paddingStart="12dp"
android:layout_toEndOf="@id/whitelistIndicator"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:orientation="vertical"
android:layout_toStartOf="@id/buttonWrap"
android:layout_alignParentStart="true"
android:layout_height="wrap_content">
<TextView
......
......@@ -124,6 +124,8 @@
<string name="dialog_newhostsource_error_name_empty">Please provide a name.</string>
<string name="dialog_newdnsrule_title">New Dns rule</string>
<string name="dialog_newdnsrule_whitelist">Whitelist</string>
<string name="dialog_newdnsrule_specify_address">Address</string>
<string name="dialog_newdnsrule_hint_host">Host</string>
<string name="dialog_newdnsrule_hint_ipv4address">IPv4 address</string>
<string name="dialog_newdnsrule_hint_ipv6address">Ipv6 address</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