DnsRuleDao.kt 7.42 KB
Newer Older
1
2
package com.frostnerd.smokescreen.database.dao

3
import androidx.room.*
4
import com.frostnerd.smokescreen.database.converters.DnsTypeConverter
5
import com.frostnerd.smokescreen.database.entities.DnsRule
6
import org.minidns.record.Record
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

/*
 * Copyright (C) 2019 Daniel Wolf (Ch4t4r)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * You can contact the developer at daniel.wolf@frostnerd.com.
 */
@Dao
27
@TypeConverters(DnsTypeConverter::class)
28
29
interface DnsRuleDao {

30
31
32
    @Update
    fun update(dnsRule: DnsRule)

33
34
35
    @Update(onConflict = OnConflictStrategy.IGNORE)
    fun updateIgnore(dnsRule: DnsRule):Int

36
37
38
    @Insert
    fun insert(dnsRule: DnsRule)

39
40
41
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertIgnore(dnsRule: DnsRule):Long

42
43
44
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertWhitelist(dnsRule: DnsRule):Long

45
46
    @Query("DELETE FROM DnsRule WHERE importedFrom IS NULL")
    fun deleteAllUserRules()
47

48
49
50
    @Query("DELETE FROM DnsRule WHERE importedFrom IS NOT NULL")
    fun deleteAllNonUserRules()

51
52
    @Query("UPDATE DnsRule SET stagingType=1 WHERE importedFrom IS NOT NULL AND stagingType=0 AND importedFrom IN(:sources)")
    fun markNonUserRulesForDeletion(sources:List<Long>)
53

54
55
56
    @Query("UPDATE DnsRule SET stagingType=0 WHERE importedFrom=:hostSourceId AND stagingType=1")
    fun unstageRulesOfSource(hostSourceId:Long)

57
58
59
60
61
62
    @Query("DELETE FROM DnsRule WHERE stagingType=1")
    fun deleteMarkedRules()

    @Query("DELETE FROM DnsRule WHERE stagingType=2")
    fun deleteStagedRules()

Daniel Wolf's avatar
Daniel Wolf committed
63
    @Query("UPDATE OR IGNORE DnsRule SET stagingType=0 WHERE stagingType!=0")
64
    fun commitStaging()
65
66

    @Insert
67
    fun insertAll(rules: Collection<DnsRule>)
68

69
70
71
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertAllIgnoreConflict(rules: Collection<DnsRule>)

72
    @Query("SELECT COUNT(*) FROM DnsRule")
73
    fun getCount(): Long
74

75
76
77
    @Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=importedFrom),0) = 1")
    fun getActiveCount(): Long

Daniel Wolf's avatar
Daniel Wolf committed
78
79
80
    @Query("SELECT host FROM DnsRule WHERE type=255 AND isWildcard=0 AND target='' AND (importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=importedFrom),0) = 1) ORDER BY RANDOM() LIMIT :count")
    fun getRandomNonWildcardWhitelistEntries(count:Int):List<String>

81
82
    @Query("SELECT COUNT(*) FROM DnsRule WHERE isWildcard=1 AND (importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=importedFrom),0) = 1)")
    fun getActiveWildcardCount(): Long
83

84
85
    @Query("SELECT COUNT(*) FROM DnsRule WHERE isWildcard=1 AND target='' AND (importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=importedFrom),0) = 1)")
    fun getActiveWildcardWhitelistCount(): Long
86

87
88
    @Query("SELECT COUNT(*) FROM DnsRule WHERE target='' AND (importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=importedFrom),0) = 1)")
    fun getActiveWhitelistCount(): Long
89

90
91
92
    @Query("SELECT COUNT(*) FROM DnsRule WHERE stagingType=0")
    fun getNonStagedCount(): Long

93
94
95
    @Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom IS NULL")
    fun getUserCount():Long

Daniel Wolf's avatar
Daniel Wolf committed
96
97
98
    @Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom IS NOT NULL")
    fun getNonUserCount(): Long

99
    @Query("SELECT CASE WHEN :type=28 THEN IFNULL(ipv6Target, target) ELSE target END 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")
100
    fun findRuleTarget(host: String, type: Record.TYPE, useUserRules:Boolean): String?
101

Daniel Wolf's avatar
Daniel Wolf committed
102
    @Query("SELECT * FROM DnsRule where host=:host AND type=255 AND isWildcard=0 AND target='' AND (importedFrom IS NOT NULL OR :useUserRules=1) AND (importedFrom is NULL OR IFNULL((SELECT enabled FROM HostSource h WHERE h.id=importedFrom),0) = 1) LIMIT 1")
103
104
    fun findNonWildcardWhitelistEntry(host:String, useUserRules:Boolean):List<DnsRule>

105
106
107
    @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>

108
109
110
    @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?

111
112
    @Query("DELETE FROM DnsRule WHERE importedFrom=:sourceId")
    fun deleteAllFromSource(sourceId: Long)
113
114
115
116
117
118
119

    @Query("SELECT * FROM DnsRule WHERE importedFrom IS NULL ORDER BY host LIMIT :limit OFFSET :offset")
    fun getAllUserRules(offset:Int, limit:Int):List<DnsRule>

    @Query("SELECT * FROM DnsRule WHERE importedFrom IS NULL ORDER BY host")
    fun getAllUserRules():List<DnsRule>

120
121
    @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>
Daniel Wolf's avatar
Daniel Wolf committed
122

Daniel Wolf's avatar
Daniel Wolf committed
123

124
    @Query("SELECT * FROM DnsRule WHERE importedFrom IS NOT NULL AND ((:includeWhitelistEntries=1 AND target='') OR (:includeNonWhitelistEntries=1 AND target!='')) ORDER BY host, target LIMIT :limit OFFSET :offset")
125
    fun getAllNonUserRules(offset:Int, limit:Int, includeWhitelistEntries: Boolean, includeNonWhitelistEntries: Boolean):List<DnsRule>
Daniel Wolf's avatar
Daniel Wolf committed
126

127
128
    @Delete
    fun remove(rule:DnsRule)
129

130
    @Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom=:hostSourceId AND stagingType=0")
131
    fun getCountForHostSource(hostSourceId:Long):Int
132
133
134
135

    @Query("SELECT * FROM DnsRule WHERE importedFrom IS NOT NULL AND host=:host AND type=:type AND stagingType=2 LIMIT 1")
    fun getNonUserRule(host:String, type:Record.TYPE):DnsRule?

136
}