Skip to content

Commit

Permalink
Merge pull request #1425 from hussainmohd-a/v055i
Browse files Browse the repository at this point in the history
v055i
  • Loading branch information
hussainmohd-a authored May 17, 2024
2 parents 4c30328 + 4673aac commit ed2a5a1
Show file tree
Hide file tree
Showing 43 changed files with 1,435 additions and 689 deletions.
14 changes: 12 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ plugins {
// to download blocklists for the headless variant
id "de.undercouch.download" version "5.3.0"
id 'kotlin-android'
id 'com.google.gms.google-services'
id 'com.google.firebase.crashlytics'
}

def keystorePropertiesFile = rootProject.file("keystore.properties")
Expand Down Expand Up @@ -247,8 +249,8 @@ dependencies {
fullImplementation 'com.github.kirich1409:viewbindingpropertydelegate-noreflection:1.5.9'

// from: https://jitpack.io/#celzero/firestack
download 'com.github.celzero:firestack:dd04f72717@aar'
implementation 'com.github.celzero:firestack:dd04f72717@aar'
download 'com.github.celzero:firestack:d92f398622@aar'
implementation 'com.github.celzero:firestack:d92f398622@aar'

// Work manager
implementation('androidx.work:work-runtime-ktx:2.9.0') {
Expand Down Expand Up @@ -285,6 +287,14 @@ dependencies {

// barcode scanner for wireguard
fullImplementation 'com.journeyapps:zxing-android-embedded:4.3.0'

playImplementation platform('com.google.firebase:firebase-bom:33.0.0')
playImplementation 'com.google.firebase:firebase-crashlytics-ktx'
playImplementation 'com.google.firebase:firebase-crashlytics-ndk'

websiteImplementation platform('com.google.firebase:firebase-bom:33.0.0')
websiteImplementation 'com.google.firebase:firebase-crashlytics-ktx'
websiteImplementation 'com.google.firebase:firebase-crashlytics-ndk'
}

// github.com/michel-kraemer/gradle-download-task/issues/131#issuecomment-464476903
Expand Down
29 changes: 29 additions & 0 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"project_info": {
"project_number": "974915159594",
"project_id": "rethink-dns-firewall",
"storage_bucket": "rethink-dns-firewall.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:974915159594:android:ed4f2e6c806fa816bda553",
"android_client_info": {
"package_name": "com.celzero.bravedns"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "AIzaSyBsj6i5hZNgsYopDLZlqV7jFAAp1F0y6JQ"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": []
}
}
}
],
"configuration_version": "1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,8 @@ class SummaryStatisticsAdapter(
} else {
// Glide will cache the icons against the urls. To extract the fav
// icon from the cache, first verify that the cache is available with
// the
// next dns url. If it is not available then glide will throw an error,
// do
// the duckduckgo url check in that case.
// the next dns url. If it is not available then glide will throw an
// error, do the duckduckgo url check in that case.
displayNextDnsFavIcon(query)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ import com.celzero.bravedns.util.Utilities.blocklistDownloadBasePath
import com.celzero.bravedns.util.Utilities.calculateMd5
import com.celzero.bravedns.util.Utilities.getTagValueFromJson
import com.celzero.bravedns.util.Utilities.tempDownloadBasePath
import okhttp3.ResponseBody
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.io.BufferedInputStream
import java.io.File
import java.io.FileOutputStream
Expand All @@ -57,6 +54,9 @@ import java.io.OutputStream
import java.util.concurrent.CancellationException
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit
import okhttp3.ResponseBody
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class LocalBlocklistCoordinator(val context: Context, workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams), KoinComponent {
Expand Down Expand Up @@ -89,22 +89,27 @@ class LocalBlocklistCoordinator(val context: Context, workerParams: WorkerParame
val timestamp = inputData.getLong("blocklistTimestamp", 0)

if (runAttemptCount > 3) {
Logger.w(LOG_TAG_DOWNLOAD, "Local blocklist download failed after 3 attempts")
return Result.failure()
}

if (SystemClock.elapsedRealtime() - startTime > BLOCKLIST_DOWNLOAD_TIMEOUT_MS) {
Logger.w(LOG_TAG_DOWNLOAD, "Local blocklist download timeout")
return Result.failure()
}

return when (processDownload(timestamp)) {
false -> {
if (isDownloadCancelled()) {
Logger.i(LOG_TAG_DOWNLOAD, "Local blocklist download cancelled")
notifyDownloadCancelled(context)
}
Logger.i(LOG_TAG_DOWNLOAD, "Local blocklist download failed")
Result.failure()
}
true -> {
// update the download related persistence status on download success
Logger.i(LOG_TAG_DOWNLOAD, "Local blocklist download success, updating ts: $timestamp")
updatePersistenceOnCopySuccess(timestamp)
Result.success()
}
Expand All @@ -131,21 +136,36 @@ class LocalBlocklistCoordinator(val context: Context, workerParams: WorkerParame

private suspend fun processDownload(timestamp: Long): Boolean {
// create a temp folder to download, format (timestamp ==> -timestamp)
val file = makeTempDownloadDir(timestamp) ?: return false
val file = makeTempDownloadDir(timestamp)

if (file == null) {
Logger.e(LOG_TAG_DOWNLOAD, "Error creating temp folder for download")
return false
}

Constants.ONDEVICE_BLOCKLISTS_IN_APP.forEachIndexed { _, onDeviceBlocklistsMetadata ->
val id = generateCustomDownloadId()

downloadStatuses[id] = DownloadStatus.RUNNING
val filePath = file.absolutePath + onDeviceBlocklistsMetadata.filename

if (isDownloadCancelled()) return false
Logger.i(
LOG_TAG_DOWNLOAD,
"Downloading file: ${onDeviceBlocklistsMetadata.filename}, url: ${onDeviceBlocklistsMetadata.url}, id: $id"
)

if (isDownloadCancelled()) {
Logger.i(LOG_TAG_DOWNLOAD, "Download cancelled, id: $id")
return false
}

when (startFileDownload(context, onDeviceBlocklistsMetadata.url, filePath)) {
true -> {
Logger.i(LOG_TAG_DOWNLOAD, "Download successful for id: $id")
downloadStatuses[id] = DownloadStatus.SUCCESSFUL
}
false -> {
Logger.e(LOG_TAG_DOWNLOAD, "Download failed for id: $id")
downloadStatuses[id] = DownloadStatus.FAILED
return false
}
Expand Down Expand Up @@ -232,10 +252,15 @@ class LocalBlocklistCoordinator(val context: Context, workerParams: WorkerParame
// create okhttp client with base url
val retrofit =
getBlocklistBaseBuilder(retryCount).build().create(IBlocklistDownload::class.java)
Logger.i(LOG_TAG_DOWNLOAD, "Downloading file: $fileName, url: $url")
val response = retrofit.downloadLocalBlocklistFile(url, persistentState.appVersion, "")

if (response?.isSuccessful == true) {
return downloadFile(context, response.body(), fileName)
} else {
Logger.e(
LOG_TAG_DOWNLOAD,
"Error in startFileDownload: ${response?.message()}, code: ${response?.code()}"
)
}
} catch (e: Exception) {
Logger.e(LOG_TAG_DOWNLOAD, "Error in startFileDownload: ${e.message}", e)
Expand All @@ -250,6 +275,7 @@ class LocalBlocklistCoordinator(val context: Context, workerParams: WorkerParame
}

private fun isRetryRequired(retryCount: Int): Boolean {
Logger.i(LOG_TAG_DOWNLOAD, "Retry count: $retryCount")
return retryCount < RetrofitManager.Companion.OkHttpDnsType.entries.size - 1
}

Expand Down Expand Up @@ -335,7 +361,7 @@ class LocalBlocklistCoordinator(val context: Context, workerParams: WorkerParame
File(tempDownloadBasePath(context, LOCAL_BLOCKLIST_DOWNLOAD_FOLDER_NAME, timestamp))

if (!from.isDirectory) {
Logger.d(LOG_TAG_DOWNLOAD, "Invalid from: ${from.name} dir")
Logger.i(LOG_TAG_DOWNLOAD, "Invalid from: ${from.name} dir")
return false
}

Expand Down
107 changes: 56 additions & 51 deletions app/src/full/java/com/celzero/bravedns/ui/activity/AppInfoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.TooltipCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.bumptech.glide.Glide
import com.celzero.bravedns.R
import com.celzero.bravedns.adapter.AppWiseDomainsAdapter
import com.celzero.bravedns.adapter.AppWiseIpsAdapter
import com.celzero.bravedns.database.AppInfo
import com.celzero.bravedns.database.ConnectionTrackerRepository
import com.celzero.bravedns.databinding.ActivityAppDetailsBinding
import com.celzero.bravedns.service.FirewallManager
import com.celzero.bravedns.service.FirewallManager.updateFirewallStatus
import com.celzero.bravedns.service.PersistentState
import com.celzero.bravedns.service.VpnController
import com.celzero.bravedns.ui.bottomsheet.RethinkListBottomSheet
import com.celzero.bravedns.util.Constants
import com.celzero.bravedns.util.Constants.Companion.INVALID_UID
import com.celzero.bravedns.util.Constants.Companion.VIEW_PAGER_SCREEN_TO_LOAD
Expand All @@ -65,7 +67,6 @@ class AppInfoActivity : AppCompatActivity(R.layout.activity_app_details) {
private val b by viewBinding(ActivityAppDetailsBinding::bind)

private val persistentState by inject<PersistentState>()
private val connectionTrackerRepository by inject<ConnectionTrackerRepository>()

private val ipRulesViewModel: CustomIpViewModel by viewModel()
private val domainRulesViewModel: CustomDomainViewModel by viewModel()
Expand All @@ -74,17 +75,13 @@ class AppInfoActivity : AppCompatActivity(R.layout.activity_app_details) {
private var uid: Int = INVALID_UID
private lateinit var appInfo: AppInfo

private var ipListUiState: Boolean = false
private var firewallUiState: Boolean = true

private var appStatus = FirewallManager.FirewallStatus.NONE
private var connStatus = FirewallManager.ConnectionStatus.ALLOW

private var showBypassToolTip: Boolean = true

companion object {
const val UID_INTENT_NAME = "UID"
const val LOG_THRESHOLD_SIZE = 100
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -95,9 +92,7 @@ class AppInfoActivity : AppCompatActivity(R.layout.activity_app_details) {
domainRulesViewModel.setUid(uid)
networkLogsViewModel.setUid(uid)
init()
observeNetworkLogSize()
observeAppRules()
observeDomainLogSize()
setupClickListeners()
}

Expand All @@ -109,37 +104,7 @@ class AppInfoActivity : AppCompatActivity(R.layout.activity_app_details) {
}
}

private fun observeNetworkLogSize() {
networkLogsViewModel.getConnectionsCount(uid).observe(this) {
if (it == null) return@observe

b.aadIpLogsDetail.text = it.toString()
}
}

private fun observeDomainLogSize() {
networkLogsViewModel.getAppDomainConnectionsCount(uid).observe(this) {
if (it == null) return@observe

b.aadDomainLogsDetail.text = it.toString()
}
}

private fun init() {
val domainTxtDesc =
getString(
R.string.two_argument_space,
getString(R.string.lbl_domain).replaceFirstChar { it.uppercase() },
getString(R.string.lbl_logs).replaceFirstChar { it.uppercase() }
)
val ipTxtDesc =
getString(
R.string.two_argument_space,
getString(R.string.lbl_ip).replaceFirstChar { it.uppercase() },
getString(R.string.lbl_logs).replaceFirstChar { it.uppercase() }
)
b.aadIpLogsDetailDesc.text = ipTxtDesc
b.aadDomainLogsDetailDesc.text = domainTxtDesc
io {
val appInfo = FirewallManager.getAppInfoByUid(uid)
// case: app is uninstalled but still available in RethinkDNS database
Expand Down Expand Up @@ -170,6 +135,8 @@ class AppInfoActivity : AppCompatActivity(R.layout.activity_app_details) {
return@uiCtx
}
updateFirewallStatusUi(appStatus, connStatus)
setDomainsAdapter()
setIpAdapter()
}
}
}
Expand Down Expand Up @@ -369,16 +336,59 @@ class AppInfoActivity : AppCompatActivity(R.layout.activity_app_details) {

b.aadDomainBlockCard.setOnClickListener { openCustomDomainScreen() }

b.aadIpLogsCard.setOnClickListener {
val intent = Intent(this, AppWiseIpLogsActivity::class.java)
intent.putExtra(UID_INTENT_NAME, uid)
startActivity(intent)
b.aadIpsChip.setOnClickListener { openAppWiseIpLogsActivity() }

b.aadDomainsChip.setOnClickListener { openAppWiseDomainLogsActivity() }
}

private fun openAppWiseDomainLogsActivity() {
val intent = Intent(this, AppWiseDomainLogsActivity::class.java)
intent.putExtra(UID_INTENT_NAME, uid)
startActivity(intent)
}

private fun openAppWiseIpLogsActivity() {
val intent = Intent(this, AppWiseIpLogsActivity::class.java)
intent.putExtra(UID_INTENT_NAME, uid)
startActivity(intent)
}

private fun setDomainsAdapter() {
val layoutManager = LinearLayoutManager(this)
b.aadMostContactedDomainRv.layoutManager = layoutManager
val adapter = AppWiseDomainsAdapter(this, this, uid)
networkLogsViewModel.getDomainLogsLimited(uid).observe(this) {
adapter.submitData(this.lifecycle, it)
}
b.aadMostContactedDomainRv.adapter = adapter

adapter.addLoadStateListener {
if (it.append.endOfPaginationReached) {
if (adapter.itemCount < 1) {
b.aadMostContactedDomainRl.visibility = View.GONE
b.aadMostContactedIpsRv.visibility = View.GONE
}
}
}
}

private fun setIpAdapter() {
b.aadMostContactedIpsRv.setHasFixedSize(true)
val layoutManager = LinearLayoutManager(this)
b.aadMostContactedIpsRv.layoutManager = layoutManager
val adapter = AppWiseIpsAdapter(this, this, uid)
networkLogsViewModel.getIpLogsLimited(uid).observe(this) {
adapter.submitData(this.lifecycle, it)
}
b.aadMostContactedIpsRv.adapter = adapter

b.aadDomainLogsCard.setOnClickListener {
val intent = Intent(this, AppWiseDomainLogsActivity::class.java)
intent.putExtra(UID_INTENT_NAME, uid)
startActivity(intent)
adapter.addLoadStateListener {
if (it.append.endOfPaginationReached) {
if (adapter.itemCount < 1) {
b.aadMostContactedIpsRl.visibility = View.GONE
b.aadMostContactedDomainRv.visibility = View.GONE
}
}
}
}

Expand All @@ -404,11 +414,6 @@ class AppInfoActivity : AppCompatActivity(R.layout.activity_app_details) {
alertDialog.show()
}

private fun rethinkListBottomSheet() {
val bottomSheetFragment = RethinkListBottomSheet()
bottomSheetFragment.show(this.supportFragmentManager, bottomSheetFragment.tag)
}

private fun toggleMobileData(appInfo: AppInfo) {
// toggle mobile data: change the connection status based on the current status.
// if allow -> none(app status) + metered(connection status)
Expand Down
Loading

0 comments on commit ed2a5a1

Please sign in to comment.