Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy local blocklists files from asset to internal dir #867

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ class HomeScreenActivity : AppCompatActivity(R.layout.activity_home_screen) {

private fun removeThisMethod() {
// for version v03k
changeDefaultInternetProtocol()
removeKeyFromSharedPref()
changeDefaultToMax()

Expand Down Expand Up @@ -359,11 +358,6 @@ class HomeScreenActivity : AppCompatActivity(R.layout.activity_home_screen) {
io { appConfig.switchRethinkDnsToMax() }
}

private fun changeDefaultInternetProtocol() {
// (v053k) change the internet protocol version to IPv4 as default
persistentState.internetProtocolType = InternetProtocol.IPv4.id
}

private fun removeKeyFromSharedPref() {
// below keys are not used, remove from shared pref
val removeLocal = "local_blocklist_update_check"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.StrictMode
import android.util.Log
import com.celzero.bravedns.BuildConfig.DEBUG
import com.celzero.bravedns.scheduler.ScheduleManager
import com.celzero.bravedns.util.LocalBlocklistUtil
import com.celzero.bravedns.util.LoggerConstants.Companion.LOG_TAG_SCHEDULER
import org.koin.android.ext.android.get
import org.koin.android.ext.koin.androidContext
Expand Down Expand Up @@ -35,7 +36,10 @@ class RethinkDnsApplication : Application() {
androidContext(this@RethinkDnsApplication)
koin.loadModules(AppModules)
}
// copy the file from assets to local dir if not present
LocalBlocklistUtil(this).init()
// database refresh is used in both headless and main project
get<ScheduleManager>().scheduleDatabaseRefreshJob()
get<ScheduleManager>().scheduleDatabaseRefreshJob()

}
}
117 changes: 117 additions & 0 deletions app/src/headless/java/com/celzero/bravedns/util/LocalBlocklistUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2023 RethinkDNS and its authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.celzero.bravedns.util

import android.content.Context
import android.util.Log
import com.celzero.bravedns.service.RethinkBlocklistManager
import com.celzero.bravedns.util.Constants.Companion.ONDEVICE_BLOCKLISTS_IN_APP
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import java.io.IOException
import java.io.InputStream

class LocalBlocklistUtil(val context: Context) {

fun init() {
// check if the files are present in the internal storage
// if not present, move the files from assets to internal storage
if (!isFilesPresentInInternalStorage()) {
moveFilesFromAssetsToInternalStorage()
}
}

// create a function to check if the files are present in the internal storage
private fun isFilesPresentInInternalStorage(): Boolean {
// check if the files are present in the internal storage
// folder name: Constants.LOCAL_BLOCKLIST_DOWNLOAD_FOLDER_NAME
// file name: Constants.ONDEVICE_BLOCKLIST_FILE_TAG (just check for this file)
val dir =
Utilities.blocklistDir(
context,
Constants.LOCAL_BLOCKLIST_DOWNLOAD_FOLDER_NAME,
Constants.INIT_TIME_MS
)
?: return false
val file = File(dir.absolutePath + File.separator + Constants.ONDEVICE_BLOCKLIST_FILE_TAG)
return file.exists()
}

// create a function to move files from assets to internal storage
private fun moveFilesFromAssetsToInternalStorage() {
// get the list of files from assets
// loop through the list of files
// copy the files to internal storage
val files = context.assets.list("") ?: return

CoroutineScope(Dispatchers.IO).launch {
for (file in files) {
// check if the file contains in ONDEVICE_BLOCKLISTS_IN_APP
// values contains file name with file separator, remove the file separator
val t = ONDEVICE_BLOCKLISTS_IN_APP.firstOrNull { file.contains(it.filename.removePrefix(File.separator)) } ?: continue
moveFileToLocalDir(t.filename.removePrefix(File.separator))
}
}
}

private suspend fun moveFileToLocalDir(fileName: String) {
try {
val assetMgr = context.assets
val readStream: InputStream = assetMgr.open(fileName)
val to = makeFile(context, fileName) ?: throw IOException()

Utilities.copyWithStream(readStream, to.outputStream())
if (fileName.contains(Constants.ONDEVICE_BLOCKLIST_FILE_TAG)) {
// write the file tag json file into database
RethinkBlocklistManager.readJson(
context,
RethinkBlocklistManager.DownloadType.LOCAL,
Constants.INIT_TIME_MS
)
}
} catch (e: IOException) {
Log.e(
LoggerConstants.LOG_TAG_DOWNLOAD,
"Issue moving file from asset folder: ${e.message}",
e
)
}
}

private fun makeFile(context: Context, fileName: String): File? {
val dir =
Utilities.blocklistDir(
context,
Constants.LOCAL_BLOCKLIST_DOWNLOAD_FOLDER_NAME,
Constants.INIT_TIME_MS
)
?: return null
if (!dir.exists()) {
dir.mkdirs()
}
val file = File(dir.absolutePath + File.separator + fileName)
return if (file.exists()) {
// no need to move the file from asset if the file is already available
file
} else {
file.createNewFile()
file
}
}

}
4 changes: 2 additions & 2 deletions app/src/main/java/com/celzero/bravedns/net/go/GoVpnAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import com.celzero.bravedns.util.Constants.Companion.REMOTE_BLOCKLIST_DOWNLOAD_F
import com.celzero.bravedns.util.LoggerConstants.Companion.LOG_TAG_VPN
import com.celzero.bravedns.util.Utilities.Companion.blocklistFile
import com.celzero.bravedns.util.Utilities.Companion.isValidDnsPort
import com.celzero.bravedns.util.Utilities.Companion.remoteBlocklistFile
import com.celzero.bravedns.util.Utilities.Companion.blocklistDir
import com.celzero.bravedns.util.Utilities.Companion.showToastUiCentered
import dnsx.BraveDNS
import dnsx.Dnsx
Expand Down Expand Up @@ -313,7 +313,7 @@ class GoVpnAdapter(
if (DEBUG) Log.d(LOG_TAG_VPN, "init remote bravedns mode")
try {
val remoteDir =
remoteBlocklistFile(
blocklistDir(
context,
REMOTE_BLOCKLIST_DOWNLOAD_FOLDER_NAME,
persistentState.remoteBlocklistTimestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ object RethinkBlocklistManager : KoinComponent {
}

val dir =
Utilities.remoteBlocklistFile(context, REMOTE_BLOCKLIST_DOWNLOAD_FOLDER_NAME, timestamp)
Utilities.blocklistDir(context, REMOTE_BLOCKLIST_DOWNLOAD_FOLDER_NAME, timestamp)
?: return null
val file =
Utilities.blocklistFile(dir.absolutePath, ONDEVICE_BLOCKLIST_FILE_TAG) ?: return null
Expand Down Expand Up @@ -453,7 +453,7 @@ object RethinkBlocklistManager : KoinComponent {
}

val dir =
Utilities.remoteBlocklistFile(context, LOCAL_BLOCKLIST_DOWNLOAD_FOLDER_NAME, timestamp)
Utilities.blocklistDir(context, LOCAL_BLOCKLIST_DOWNLOAD_FOLDER_NAME, timestamp)
?: return null
val file =
Utilities.blocklistFile(dir.absolutePath, ONDEVICE_BLOCKLIST_FILE_TAG) ?: return null
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/celzero/bravedns/util/Utilities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ class Utilities {

fun hasRemoteBlocklists(ctx: Context, timestamp: Long): Boolean {
val remoteDir =
remoteBlocklistFile(ctx, REMOTE_BLOCKLIST_DOWNLOAD_FOLDER_NAME, timestamp)
blocklistDir(ctx, REMOTE_BLOCKLIST_DOWNLOAD_FOLDER_NAME, timestamp)
?: return false
val remoteFile =
blocklistFile(remoteDir.absolutePath, Constants.ONDEVICE_BLOCKLIST_FILE_TAG)
Expand All @@ -723,7 +723,7 @@ class Utilities {
return false
}

fun remoteBlocklistFile(ctx: Context?, which: String, timestamp: Long): File? {
fun blocklistDir(ctx: Context?, which: String, timestamp: Long): File? {
if (ctx == null) return null

return try {
Expand Down