DnsQueryRepository.kt 4.21 KB
Newer Older
1
2
package com.frostnerd.smokescreen.database.repository

3
4
import android.content.Context
import com.frostnerd.smokescreen.database.converters.StringListConverter
5
6
import com.frostnerd.smokescreen.database.dao.DnsQueryDao
import com.frostnerd.smokescreen.database.entities.DnsQuery
7
import com.frostnerd.smokescreen.getPreferences
8
import kotlinx.coroutines.*
9
10
11
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
12

Daniel Wolf's avatar
Daniel Wolf committed
13
14
/*
 * Copyright (C) 2019 Daniel Wolf (Ch4t4r)
15
 *
Daniel Wolf's avatar
Daniel Wolf committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * 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.
30
31
 */

Daniel Wolf's avatar
Daniel Wolf committed
32
class DnsQueryRepository(private val dnsQueryDao: DnsQueryDao) {
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

    fun updateAsync(dnsQuery: DnsQuery): Job {
        return GlobalScope.launch {
            dnsQueryDao.update(dnsQuery)
        }
    }

    fun insertAllAsync(dnsQueries:List<DnsQuery>): Job {
        return GlobalScope.launch {
            dnsQueryDao.insertAll(dnsQueries)
        }
    }

    fun insertAsync(dnsQuery:DnsQuery): Job {
        return GlobalScope.launch {
            dnsQueryDao.insert(dnsQuery)
        }
    }

    suspend fun getAllAsync(coroutineScope: CoroutineScope): List<DnsQuery> {
        return coroutineScope.async(start = CoroutineStart.DEFAULT) {
            dnsQueryDao.getAll()
        }.await()
    }
57

58
59
60
    fun exportQueriesAsCsvAsync(context: Context,
                                fileReadyCallback:(createdFile: File) ->Unit,
                                countUpdateCallback:(count:Long, total:Int) -> Unit):Job {
61
62
63
        val exportDir = File(context.filesDir, "queryexport/")
        exportDir.mkdirs()
        val exportFile = File(exportDir, "queries.csv")
64
65
66
67
68
        val start = context.getPreferences().exportedQueryCount
        val existed = if(start == 0 && exportFile.exists()) {
            exportFile.delete()
            false
        } else exportFile.exists()
69
        return GlobalScope.launch {
70
71
72
73
74
            val outStream = BufferedWriter(FileWriter(exportFile, true))
            if(!existed) {
                outStream.write("Name,Short Name,Type Name,Type ID,Asked Server,Answered from Cache,Question time,Response Time,Responses(JSON-Array of Base64)")
                outStream.newLine()
            }
75
76
            val builder = StringBuilder()
            val responseConverter = StringListConverter()
77
78

            val count = dnsQueryDao.getCount()
79
80
            var total = start.toLong()
            for(i in start..count step 5000) {
81
82
83
84
85
86
87
88
                val all = dnsQueryDao.getAll(5000, i.toLong())
                for (query in all) {
                    total++
                    builder.append(query.name).append(",")
                    builder.append(query.shortName).append(",")
                    builder.append(query.type.name).append(",")
                    builder.append(query.type.value).append(",")
                    builder.append(query.askedServer).append(",")
89
                    builder.append(query.responseSource?.name ?: "").append(",")
90
91
92
93
94
95
96
                    builder.append(query.questionTime).append(",")
                    builder.append(query.responseTime).append(",")
                    builder.append("\"").append(responseConverter.someObjectListToString(query.responses).replace(",", ";").replace("\"", "'")).append("\"")
                    outStream.write(builder.toString())
                    outStream.newLine()
                    outStream.flush()
                    builder.clear()
97
                    if(total % 250 == 0L) countUpdateCallback(total, count)
98
                }
99
100
            }
            outStream.close()
101
            context.getPreferences().exportedQueryCount = count
102
103
104
            fileReadyCallback(exportFile)
        }
    }
105
}