Commit 0f288276 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Added support for wildcard rules from host soueces

Implements #145
parent 94ee0051
...@@ -47,17 +47,16 @@ import java.util.regex.Pattern ...@@ -47,17 +47,16 @@ import java.util.regex.Pattern
*/ */
class RuleImportService : IntentService("RuleImportService") { class RuleImportService : IntentService("RuleImportService") {
private val dnsmasqMatcher = private val dnsmasqMatcher =
Pattern.compile("^address=/([^/]+)/(?:([0-9.]+)|([0-9a-fA-F:]+))(?:/?\$|\\s+.*)").matcher("") Pattern.compile("^address=/([^/]+)/(?:([0-9.]+)|([0-9a-fA-F:]+))(?:/?\$|\\s+.*)").matcher("") // address=/xyz.com/0.0.0.0
private val dnsmasqBlockMatcher = Pattern.compile("^address=/([^/]+)/$").matcher("") private val dnsmasqBlockMatcher = Pattern.compile("^address=/([^/]+)/$").matcher("") // address=/xyz.com/
private val hostsMatcher = private val hostsMatcher =
Pattern.compile("^((?:[A-Fa-f0-9:]|[0-9.])+)\\s+([\\w._\\-]+).*") Pattern.compile("^((?:[A-Fa-f0-9:]|[0-9.])+)\\s+([*\\w._\\-]+).*")
.matcher("") .matcher("") // 0.0.0.0 xyz.com
private val domainsMatcher = Pattern.compile("^([_\\w][\\w_\\-.]+)(?:\$|\\s+.*)").matcher("") private val domainsMatcher = Pattern.compile("^([_\\w*][*\\w_\\-.]+)(?:\$|\\s+.*)").matcher("") // xyz.com
private val adblockMatcher = Pattern.compile("^\\|\\|(.*)\\^(?:\$|\\s+.*)").matcher("") private val adblockMatcher = Pattern.compile("^\\|\\|(.*)\\^(?:\$|\\s+.*)").matcher("") // ||xyz.com^
private val ruleCommitSize = 10000 private val ruleCommitSize = 10000
private var notification: NotificationCompat.Builder? = null private var notification: NotificationCompat.Builder? = null
private var ruleCount: Int = 0 private var ruleCount: Int = 0
private var checkDuplicates: Boolean = false
private var isAborted = false private var isAborted = false
companion object { companion object {
...@@ -197,6 +196,7 @@ class RuleImportService : IntentService("RuleImportService") { ...@@ -197,6 +196,7 @@ class RuleImportService : IntentService("RuleImportService") {
log("Downloading resource of $it failed.") log("Downloading resource of $it failed.")
} }
} catch (ex: java.lang.Exception) { } catch (ex: java.lang.Exception) {
ex.printStackTrace()
log("Downloading resource of $it failed ($ex)") log("Downloading resource of $it failed ($ex)")
} finally { } finally {
response?.body?.close() response?.body?.close()
...@@ -303,20 +303,7 @@ class RuleImportService : IntentService("RuleImportService") { ...@@ -303,20 +303,7 @@ class RuleImportService : IntentService("RuleImportService") {
when { when {
matcher.groupCount() == 1 -> { matcher.groupCount() == 1 -> {
val host = matcher.group(1).replace(wwwRegex, "") val host = matcher.group(1).replace(wwwRegex, "")
return if (checkDuplicates) { return createRule(host, defaultTargetV4, defaultTargetV6, Record.TYPE.ANY, sourceId)
val existingIpv4 = getDatabase().dnsRuleDao().getNonUserRule(host, Record.TYPE.A)
val existingIpv6 = getDatabase().dnsRuleDao().getNonUserRule(host, Record.TYPE.AAAA)
if (existingIpv4 == null && existingIpv6 == null) DnsRule(
Record.TYPE.ANY,
host,
defaultTargetV4,
defaultTargetV6,
importedFrom = sourceId
)
else if (existingIpv4 == null) DnsRule(Record.TYPE.A, host, defaultTargetV4, importedFrom = sourceId)
else if (existingIpv6 == null) DnsRule(Record.TYPE.AAAA, host, defaultTargetV6, importedFrom = sourceId)
else null
} else DnsRule(Record.TYPE.ANY, host, defaultTargetV4, defaultTargetV6, importedFrom = sourceId)
} }
matcher == dnsmasqMatcher -> { matcher == dnsmasqMatcher -> {
val host = matcher.group(1).replace(wwwRegex, "") val host = matcher.group(1).replace(wwwRegex, "")
...@@ -329,7 +316,7 @@ class RuleImportService : IntentService("RuleImportService") { ...@@ -329,7 +316,7 @@ class RuleImportService : IntentService("RuleImportService") {
else -> it else -> it
} }
} }
return createRuleIfNotExists(host, target, type, sourceId) return createRule(host, target, null, type, sourceId)
} }
matcher == hostsMatcher -> { matcher == hostsMatcher -> {
return if(isWhitelist) { return if(isWhitelist) {
...@@ -346,22 +333,24 @@ class RuleImportService : IntentService("RuleImportService") { ...@@ -346,22 +333,24 @@ class RuleImportService : IntentService("RuleImportService") {
} }
} }
val host = matcher.group(2).replace(wwwRegex, "") val host = matcher.group(2).replace(wwwRegex, "")
createRuleIfNotExists(host, target, type, sourceId) return createRule(host, target, null, type, sourceId)
} }
} }
} }
throw IllegalStateException() throw IllegalStateException()
} }
private fun createRuleIfNotExists(host: String, target: String, type: Record.TYPE, sourceId: Long): DnsRule? { private fun createRule(host: String, target: String, targetV6:String? = null, type: Record.TYPE, sourceId: Long): DnsRule? {
return if (checkDuplicates) { var isWildcard = false
val existingRule = getDatabase().dnsRuleDao().getNonUserRule(host, type) val alteredHost = host.let {
if (existingRule == null) DnsRule(type, host, target, importedFrom = sourceId) if(it.contains("*")) {
else null isWildcard = true
} else DnsRule(type, host, target, importedFrom = sourceId) it.replace("**", "%%").replace("*", "%")
} else it
}
return DnsRule(type, alteredHost, target, targetV6, sourceId, isWildcard)
} }
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
abortImport() abortImport()
......
...@@ -60,4 +60,10 @@ ...@@ -60,4 +60,10 @@
android:id="@+id/whitelist" android:id="@+id/whitelist"
android:text="@string/dialog_newhostsource_use_as_whitelist" android:text="@string/dialog_newhostsource_use_as_whitelist"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:text="@string/dialog_newhostsource_wildcard_info"
android:alpha="0.8"
android:layout_height="wrap_content"/>
</LinearLayout> </LinearLayout>
\ No newline at end of file
...@@ -126,6 +126,7 @@ ...@@ -126,6 +126,7 @@
<string name="dialog_newhostsource_title">New rule source</string> <string name="dialog_newhostsource_title">New rule source</string>
<string name="dialog_newhostsource_edit_title">New rule source</string> <string name="dialog_newhostsource_edit_title">New rule source</string>
<string name="dialog_newhostsource_name">Name</string> <string name="dialog_newhostsource_name">Name</string>
<string name="dialog_newhostsource_wildcard_info">You can use host lists which make use of wildcard domains.</string>
<string name="dialog_newhostsource_url">URL</string> <string name="dialog_newhostsource_url">URL</string>
<string name="dialog_newhostsource_choose_file">Choose file</string> <string name="dialog_newhostsource_choose_file">Choose file</string>
<string name="dialog_newhostsource_use_as_whitelist">Use as whitelist</string> <string name="dialog_newhostsource_use_as_whitelist">Use as whitelist</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