DnsRuleDao.kt 5.11 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
    @Query("UPDATE DnsRule SET stagingType=1 WHERE importedFrom IS NOT NULL AND stagingType=0")
49
50
    fun markNonUserRulesForDeletion()

51
52
53
    @Query("UPDATE DnsRule SET stagingType=0 WHERE importedFrom=:hostSourceId AND stagingType=1")
    fun unstageRulesOfSource(hostSourceId:Long)

54
55
56
57
58
59
    @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
60
    @Query("UPDATE OR IGNORE DnsRule SET stagingType=0 WHERE stagingType!=0")
61
    fun commitStaging()
62
63

    @Insert
64
    fun insertAll(rules: Collection<DnsRule>)
65

66
67
68
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertAllIgnoreConflict(rules: Collection<DnsRule>)

69
    @Query("SELECT COUNT(*) FROM DnsRule")
70
    fun getCount(): Long
71

72
73
74
    @Query("SELECT COUNT(*) FROM DnsRule WHERE stagingType=0")
    fun getNonStagedCount(): Long

75
76
77
    @Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom IS NULL")
    fun getUserCount():Long

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

81
    @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")
82
    fun findRuleTarget(host: String, type: Record.TYPE, useUserRules:Boolean): String?
83

84
85
86
    @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>

87
88
    @Query("DELETE FROM DnsRule WHERE importedFrom=:sourceId")
    fun deleteAllFromSource(sourceId: Long)
89
90
91
92
93
94
95

    @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>

Daniel Wolf's avatar
Daniel Wolf committed
96
97
98
    @Query("SELECT * FROM DnsRule WHERE importedFrom IS NULL AND target != '' ORDER BY host")
    fun getAllUserRulesWithoutWhitelist():List<DnsRule>

Daniel Wolf's avatar
Daniel Wolf committed
99
100
101
    @Query("SELECT * FROM DnsRule WHERE importedFrom IS NOT NULL ORDER BY host LIMIT :limit OFFSET :offset")
    fun getAllNonUserRules(offset:Int, limit:Int):List<DnsRule>

Daniel Wolf's avatar
Daniel Wolf committed
102
103
104
    @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>

105
106
    @Delete
    fun remove(rule:DnsRule)
107

108
    @Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom=:hostSourceId AND stagingType=0")
109
    fun getCountForHostSource(hostSourceId:Long):Int
110
111
112
113

    @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?

114
}