Commit 3617ba1a authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Allow whitelist rules to be exported

parent 250fcf26
......@@ -114,14 +114,12 @@ interface DnsRuleDao {
@Query("SELECT * FROM DnsRule WHERE importedFrom IS NULL ORDER BY host")
fun getAllUserRules():List<DnsRule>
@Query("SELECT * FROM DnsRule WHERE importedFrom IS NULL AND target != '' ORDER BY host")
fun getAllUserRulesWithoutWhitelist():List<DnsRule>
@Query("SELECT * FROM DnsRule WHERE importedFrom IS NULL AND ((:includeWhitelistEntries=1 AND target='') OR (:includeNonWhitelistEntries=1 AND target!='')) ORDER BY host")
fun getAllUserRules(includeWhitelistEntries: Boolean, includeNonWhitelistEntries: Boolean):List<DnsRule>
@Query("SELECT * FROM DnsRule WHERE importedFrom IS NOT NULL ORDER BY host LIMIT :limit OFFSET :offset")
fun getAllNonUserRules(offset:Int, limit:Int):List<DnsRule>
@Query("SELECT * FROM DnsRule WHERE importedFrom IS NOT NULL AND target != '' ORDER BY host LIMIT :limit OFFSET :offset")
fun getAllNonUserRulesWithoutWhitelist(offset:Int, limit:Int):List<DnsRule>
@Query("SELECT * FROM DnsRule WHERE importedFrom IS NOT NULL AND ((:includeWhitelistEntries=1 AND target='') OR (:includeNonWhitelistEntries=1 AND target!='')) ORDER BY host LIMIT :limit OFFSET :offset")
fun getAllNonUserRules(offset:Int, limit:Int, includeWhitelistEntries: Boolean, includeNonWhitelistEntries: Boolean):List<DnsRule>
@Delete
fun remove(rule:DnsRule)
......
......@@ -25,7 +25,7 @@ import kotlinx.android.synthetic.main.dialog_export_dnsrules.view.*
* You can contact the developer at daniel.wolf@frostnerd.com.
*/
class ExportDnsRulesDialog(context: Context,
beginExport: (exportFromSources:Boolean, exportUserRules:Boolean) -> Unit) :
beginExport: (exportFromSources:Boolean, exportUserRules:Boolean, type:ExportType) -> Unit) :
androidx.appcompat.app.AlertDialog(context, context.getPreferences().theme.dialogStyle) {
init {
......@@ -42,11 +42,16 @@ androidx.appcompat.app.AlertDialog(context, context.getPreferences().theme.dialo
dialog.dismiss()
beginExport(
view.exportSources.isChecked,
view.exportUserRules.isChecked
view.exportUserRules.isChecked,
if(view.nonwhitelist.isChecked) ExportType.NON_WHITELIST else ExportType.WHITELIST
)
}
setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(android.R.string.cancel)) { dialog, _ ->
dialog.dismiss()
}
}
}
enum class ExportType {
WHITELIST, NON_WHITELIST
}
\ No newline at end of file
......@@ -26,10 +26,7 @@ import com.frostnerd.smokescreen.*
import com.frostnerd.smokescreen.database.entities.DnsRule
import com.frostnerd.smokescreen.database.entities.HostSource
import com.frostnerd.smokescreen.database.getDatabase
import com.frostnerd.smokescreen.dialog.DnsRuleDialog
import com.frostnerd.smokescreen.dialog.ExportDnsRulesDialog
import com.frostnerd.smokescreen.dialog.HostSourceRefreshDialog
import com.frostnerd.smokescreen.dialog.NewHostSourceDialog
import com.frostnerd.smokescreen.dialog.*
import com.frostnerd.smokescreen.service.DnsVpnService
import com.frostnerd.smokescreen.service.RuleExportService
import com.frostnerd.smokescreen.service.RuleImportService
......@@ -173,12 +170,12 @@ class DnsRuleFragment : Fragment() {
if (context!!.isServiceRunning(RuleExportService::class.java)) {
context!!.startService(Intent(context!!, RuleExportService::class.java).putExtra("abort", true))
} else {
ExportDnsRulesDialog(context!!) { exportFromSources, exportUserRules ->
ExportDnsRulesDialog(context!!) { exportFromSources, exportUserRules, exportType ->
fileChosenCallback = {
val intent = Intent(context!!, RuleExportService::class.java).apply {
putExtra(
"params",
RuleExportService.Params(exportFromSources, exportUserRules, it.toString())
RuleExportService.Params(exportFromSources, exportUserRules, exportType, it.toString())
)
}
context!!.startService(intent)
......@@ -187,7 +184,7 @@ class DnsRuleFragment : Fragment() {
}
startActivityForResult(Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_TITLE, "dnsRuleExport.txt")
putExtra(Intent.EXTRA_TITLE, if(exportType == ExportType.NON_WHITELIST) "dnsRuleExport.txt" else "dnsRuleWhitelistExport.txt")
type = "text/*"
}, fileChosenRequestCode)
}.show()
......
......@@ -12,6 +12,7 @@ import com.frostnerd.smokescreen.R
import com.frostnerd.smokescreen.database.entities.DnsRule
import com.frostnerd.smokescreen.database.getDatabase
import com.frostnerd.smokescreen.dialog.DnsRuleDialog
import com.frostnerd.smokescreen.dialog.ExportType
import com.frostnerd.smokescreen.sendLocalBroadcast
import com.frostnerd.smokescreen.util.DeepActionState
import com.frostnerd.smokescreen.util.Notifications
......@@ -153,7 +154,8 @@ class RuleExportService : IntentService("RuleExportService") {
})
updateNotification(0, ruleCount)
if (params.exportUserRules && !isAborted) {
getDatabase().dnsRuleDao().getAllUserRulesWithoutWhitelist().forEach {
getDatabase().dnsRuleDao().getAllUserRules(params.exportType == ExportType.WHITELIST,
params.exportType == ExportType.NON_WHITELIST).forEach {
if (!isAborted) {
writtenCount++
writeRule(stream, it)
......@@ -169,7 +171,8 @@ class RuleExportService : IntentService("RuleExportService") {
val limit = 2000
var offset = 0
while (!isAborted && offset < nonUserRuleCount!!) {
getDatabase().dnsRuleDao().getAllNonUserRulesWithoutWhitelist(offset, limit).forEach {
getDatabase().dnsRuleDao().getAllNonUserRules(offset, limit, params.exportType == ExportType.WHITELIST,
params.exportType == ExportType.NON_WHITELIST).forEach {
if (!isAborted) {
writtenCount++
writeRule(stream, it)
......@@ -228,6 +231,7 @@ class RuleExportService : IntentService("RuleExportService") {
data class Params(
val exportFromSources: Boolean,
val exportUserRules: Boolean,
val exportType: ExportType,
val targetUri: String
) : Serializable
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:paddingLeft="@dimen/dialog_horizontal_margin"
android:paddingRight="@dimen/dialog_horizontal_margin"
android:paddingTop="@dimen/dialog_vertical_margin"
android:paddingBottom="@dimen/dialog_vertical_margin"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/dialog_horizontal_margin"
android:paddingTop="@dimen/dialog_vertical_margin"
android:paddingRight="@dimen/dialog_horizontal_margin"
android:paddingBottom="@dimen/dialog_vertical_margin">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/nonwhitelist"
android:layout_width="wrap_content"
android:checked="true"
android:layout_height="wrap_content"
android:text="@string/dialog_exportdnsrules_nonwhitelist"
android:textColor="?android:attr/textColor" />
<RadioButton
android:id="@+id/whitelist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dialog_exportdnsrules_whitelist"
android:textColor="?android:attr/textColor" />
</RadioGroup>
<CheckBox
android:layout_width="wrap_content"
android:id="@+id/exportSources"
android:text="@string/dialog_exportdnsrules_export_sources"
android:textColor="?android:attr/textColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:checked="true"
android:layout_height="wrap_content"/>
android:text="@string/dialog_exportdnsrules_export_sources"
android:textColor="?android:attr/textColor" />
<CheckBox
android:layout_width="wrap_content"
android:id="@+id/exportUserRules"
android:text="@string/dialog_exportdnsrules_export_user_rules"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textColor="?android:attr/textColor"
android:layout_height="wrap_content"/>
android:text="@string/dialog_exportdnsrules_export_user_rules"
android:textColor="?android:attr/textColor" />
</LinearLayout>
\ No newline at end of file
......@@ -146,6 +146,8 @@
<string name="dialog_newdnsrule_wildcard_info">The host supports wildcards. Use an asterisk (*) for any amount of characters, numbers and allowed special characters except period (.). Use two asterisks (**) to include the period (.).</string>
<string name="dialog_exportdnsrules_title">Export DNS rules</string>
<string name="dialog_exportdnsrules_whitelist">Export whitelist rules</string>
<string name="dialog_exportdnsrules_nonwhitelist">Export normal rules</string>
<string name="dialog_exportdnsrules_export_user_rules">Export custom host DNS rules</string>
<string name="dialog_exportdnsrules_export_sources">Export DNS rules from enabled host sources</string>
......
Markdown is supported
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