Skip to content

Commit

Permalink
Merge pull request #1240 from hussainmohd-a/v055b
Browse files Browse the repository at this point in the history
V055c: Bug fixes
  • Loading branch information
hussainmohd-a authored Mar 6, 2024
2 parents 6d8d83b + 09f29c1 commit d24918c
Show file tree
Hide file tree
Showing 29 changed files with 427 additions and 320 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
run: chmod +x gradlew
- name: Build with Gradle
if: success()
run: ./gradlew assembleWebsiteFullAlpha --info --warning-mode all
run: ./gradlew assembleWebsiteFullDebug --info --warning-mode all
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ dependencies {
fullImplementation 'com.github.kirich1409:viewbindingpropertydelegate-noreflection:1.5.9'

// from: https://jitpack.io/#celzero/firestack
download 'com.github.celzero:firestack:8dc7dafcaa@aar'
implementation 'com.github.celzero:firestack:8dc7dafcaa@aar'
download 'com.github.celzero:firestack:21000918a3@aar'
implementation 'com.github.celzero:firestack:21000918a3@aar'

// Work manager
implementation('androidx.work:work-runtime-ktx:2.9.0') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -34,8 +36,11 @@ import com.celzero.bravedns.ui.bottomsheet.AppConnectionBottomSheet
import com.celzero.bravedns.util.Logger
import com.celzero.bravedns.util.UIUtils.fetchColor
import com.celzero.bravedns.util.Utilities.removeBeginningTrailingCommas
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class AppConnectionAdapter(val context: Context, val uid: Int) :
class AppConnectionAdapter(val context: Context, val lifecycleOwner: LifecycleOwner, val uid: Int) :
PagingDataAdapter<AppConnection, AppConnectionAdapter.ConnectionDetailsViewHolder>(
DIFF_CALLBACK
),
Expand All @@ -48,7 +53,7 @@ class AppConnectionAdapter(val context: Context, val uid: Int) :
override fun areItemsTheSame(
oldConnection: AppConnection,
newConnection: AppConnection
) = oldConnection.ipAddress == newConnection.ipAddress
) = oldConnection == newConnection

override fun areContentsTheSame(
oldConnection: AppConnection,
Expand Down Expand Up @@ -124,16 +129,14 @@ class AppConnectionAdapter(val context: Context, val uid: Int) :

private fun displayTransactionDetails(appConnection: AppConnection) {
b.acdCount.text = appConnection.count.toString()
b.acdFlag.text = appConnection.flag
b.acdIpAddress.text = appConnection.ipAddress
val rule = IpRulesManager.getMostSpecificRuleMatch(uid, appConnection.ipAddress)
updateStatusUi(rule)
if (!appConnection.appOrDnsName.isNullOrEmpty()) {
b.acdDomainName.visibility = View.VISIBLE
b.acdDomainName.text = beautifyDomainString(appConnection.appOrDnsName)
} else {
b.acdDomainName.visibility = View.GONE
}
updateStatusUi(appConnection.uid, appConnection.ipAddress)
}

private fun beautifyDomainString(d: String): String {
Expand All @@ -142,26 +145,31 @@ class AppConnectionAdapter(val context: Context, val uid: Int) :
return removeBeginningTrailingCommas(d).replace(",,", ",").replace(",", ", ")
}

private fun updateStatusUi(status: IpRulesManager.IpRuleStatus) {
when (status) {
IpRulesManager.IpRuleStatus.NONE -> {
b.acdFlag.text = context.getString(R.string.ci_no_rule_initial)
}
IpRulesManager.IpRuleStatus.BLOCK -> {
b.acdFlag.text = context.getString(R.string.ci_blocked_initial)
}
IpRulesManager.IpRuleStatus.BYPASS_UNIVERSAL -> {
b.acdFlag.text = context.getString(R.string.ci_bypass_universal_initial)
}
IpRulesManager.IpRuleStatus.TRUST -> {
b.acdFlag.text = context.getString(R.string.ci_trust_initial)
private fun updateStatusUi(uid: Int, ipAddress: String) {
io {
val status = IpRulesManager.getMostSpecificRuleMatch(uid, ipAddress)
uiCtx {
when (status) {
IpRulesManager.IpRuleStatus.NONE -> {
b.acdFlag.text = context.getString(R.string.ci_no_rule_initial)
}
IpRulesManager.IpRuleStatus.BLOCK -> {
b.acdFlag.text = context.getString(R.string.ci_blocked_initial)
}
IpRulesManager.IpRuleStatus.BYPASS_UNIVERSAL -> {
b.acdFlag.text = context.getString(R.string.ci_bypass_universal_initial)
}
IpRulesManager.IpRuleStatus.TRUST -> {
b.acdFlag.text = context.getString(R.string.ci_trust_initial)
}
}

// returns the text and background color for the button
val t = getToggleBtnUiParams(status)
b.acdFlag.setTextColor(t.txtColor)
b.acdFlag.backgroundTintList = ColorStateList.valueOf(t.bgColor)
}
}

// returns the text and background color for the button
val t = getToggleBtnUiParams(status)
b.acdFlag.setTextColor(t.txtColor)
b.acdFlag.backgroundTintList = ColorStateList.valueOf(t.bgColor)
}

private fun getToggleBtnUiParams(id: IpRulesManager.IpRuleStatus): ToggleBtnUi {
Expand Down Expand Up @@ -197,4 +205,12 @@ class AppConnectionAdapter(val context: Context, val uid: Int) :
override fun notifyDataset(position: Int) {
this.notifyItemChanged(position)
}

private suspend fun uiCtx(f: suspend () -> Unit) {
withContext(Dispatchers.Main) { f() }
}

private fun io(f: suspend () -> Unit) {
lifecycleOwner.lifecycleScope.launch(Dispatchers.IO) { f() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class CustomDomainAdapter(val context: Context, val rule: CustomRulesActivity.RU
}

private suspend fun whitelist(cd: CustomDomain) {
DomainRulesManager.whitelist(cd)
DomainRulesManager.trust(cd)
}

private suspend fun block(cd: CustomDomain) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.celzero.bravedns.util.UIUtils.getDnsStatusStringRes
import com.celzero.bravedns.util.Utilities
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,4 @@ object ProxyManager : KoinComponent {
ipnProxyId != Backend.Exit
}

private fun io(f: suspend () -> Unit) {
CoroutineScope(Dispatchers.IO).launch { f() }
}
}
26 changes: 13 additions & 13 deletions app/src/full/java/com/celzero/bravedns/service/WireguardManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ import com.celzero.bravedns.wireguard.Config
import com.celzero.bravedns.wireguard.Peer
import com.celzero.bravedns.wireguard.WgInterface
import inet.ipaddr.IPAddressString
import java.io.ByteArrayInputStream
import java.io.File
import java.io.InputStream
import java.nio.charset.StandardCharsets
import java.util.Locale
import java.util.concurrent.CopyOnWriteArraySet
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import retrofit2.converter.gson.GsonConverterFactory
import java.io.ByteArrayInputStream
import java.io.File
import java.io.InputStream
import java.nio.charset.StandardCharsets
import java.util.Locale
import java.util.concurrent.CopyOnWriteArraySet

object WireguardManager : KoinComponent {

Expand Down Expand Up @@ -418,7 +418,7 @@ object WireguardManager : KoinComponent {
return cfg
}

fun addConfig(config: Config?): Config? {
suspend fun addConfig(config: Config?): Config? {
if (config == null) {
Log.e(LOG_TAG_PROXY, "error adding config")
return null
Expand All @@ -429,7 +429,7 @@ object WireguardManager : KoinComponent {
val name = config.getName().ifEmpty { "${Backend.WG}$id" }
config.setName(name)
config.setId(id)
io { writeConfigAndUpdateDb(config) }
writeConfigAndUpdateDb(config)
if (DEBUG) Log.d(LOG_TAG_PROXY, "add config: ${config.getId()}, ${config.getName()}")
return config
}
Expand Down Expand Up @@ -574,7 +574,7 @@ object WireguardManager : KoinComponent {
}
}

fun addPeer(id: Int, peer: Peer) {
suspend fun addPeer(id: Int, peer: Peer) {
// add the peer to the config
val cfg: Config
val config = configs.find { it.getId() == id }
Expand All @@ -592,11 +592,11 @@ object WireguardManager : KoinComponent {
.setInterface(config.getInterface())
.addPeers(newPeers)
.build()
Log.i(LOG_TAG_PROXY, "adding peer for config: $id, ${cfg.getName()}")
io { writeConfigAndUpdateDb(cfg) }
Log.i(LOG_TAG_PROXY, "adding peer for config: $id, ${cfg.getName()}, ${newPeers.size}")
writeConfigAndUpdateDb(cfg)
}

fun deletePeer(id: Int, peer: Peer) {
suspend fun deletePeer(id: Int, peer: Peer) {
// delete the peer from the config
val cfg: Config
val config = configs.find { it.getId() == id }
Expand Down Expand Up @@ -629,7 +629,7 @@ object WireguardManager : KoinComponent {
.addPeers(peers)
.build()
Log.i(LOG_TAG_PROXY, "deleting peer for config: $id, ${cfg.getName()}")
io { writeConfigAndUpdateDb(cfg) }
writeConfigAndUpdateDb(cfg)
}

private suspend fun writeConfigAndUpdateDb(cfg: Config, serverResponse: String = "") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.appcompat.widget.TooltipCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.bumptech.glide.Glide
import com.celzero.bravedns.R
Expand All @@ -51,7 +52,6 @@ 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
import com.celzero.bravedns.util.CustomLinearLayoutManager
import com.celzero.bravedns.util.Themes
import com.celzero.bravedns.util.UIUtils.openAndroidAppInfo
import com.celzero.bravedns.util.UIUtils.updateHtmlEncodedText
Expand Down Expand Up @@ -576,10 +576,10 @@ class AppInfoActivity :

private fun showNetworkLogsIfAny(uid: Int) {
networkLogsViewModel.setUid(uid)
b.aadConnDetailRecycler.setHasFixedSize(false)
val layoutManager = CustomLinearLayoutManager(this)
b.aadConnDetailRecycler.setHasFixedSize(true)
val layoutManager = LinearLayoutManager(this)
b.aadConnDetailRecycler.layoutManager = layoutManager
val recyclerAdapter = AppConnectionAdapter(this, uid)
val recyclerAdapter = AppConnectionAdapter(this, this, uid)
networkLogsViewModel.appNetworkLogs.observe(this) {
recyclerAdapter.submitData(this.lifecycle, it)
}
Expand All @@ -588,7 +588,6 @@ class AppInfoActivity :
val itemAnimator = DefaultItemAnimator()
itemAnimator.changeDuration = 1500
b.aadConnDetailRecycler.itemAnimator = itemAnimator
networkLogsViewModel.setFilter("")
}

private fun toggleNetworkLogState(state: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,13 @@ class WgConfigDetailActivity : AppCompatActivity(R.layout.activity_wg_detail) {
val addPeerDialog = WgAddPeerDialog(this, themeId, configId, null)
addPeerDialog.setCanceledOnTouchOutside(false)
addPeerDialog.show()
addPeerDialog.setOnDismissListener { wgPeersAdapter?.dataChanged() }
addPeerDialog.setOnDismissListener {
if (wgPeersAdapter != null) {
wgPeersAdapter?.dataChanged()
} else {
setPeersAdapter()
}
}
}

private fun setPeersAdapter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,15 +431,20 @@ class LocalBlocklistsBottomSheet : BottomSheetDialogFragment() {
persistentState.localBlocklistTimestamp
)
}

if (blocklistsExist) {
setBraveDnsLocal()
// now, rdnslocal obj is required to get/set the stamp from blocklists
// see RDNS#flagsToStamp, RDNS#stampToFlags
setBraveDnsLocal() // set remote blocklist even if stamp is not available
if (isLocalBlocklistStampAvailable()) {
updateLocalBlocklistUi()
} else {
// stamp is not available, show user the configure screen
invokeRethinkActivity()
}
} else {
removeBraveDnsLocal()
// no local blocklists found, prompt user to download
invokeRethinkActivity()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class WgAddPeerDialog(
val newPeer = builder.build()

ui {
io {
ioCtx {
if (wgPeer != null && isEditing)
WireguardManager.deletePeer(configId, wgPeer)
WireguardManager.addPeer(configId, newPeer)
Expand All @@ -123,7 +123,7 @@ class WgAddPeerDialog(
(activity as LifecycleOwner).lifecycleScope.launch(Dispatchers.Main) { f() }
}

private suspend fun io(f: suspend () -> Unit) {
private suspend fun ioCtx(f: suspend () -> Unit) {
withContext(Dispatchers.IO) { f() }
}
}
28 changes: 16 additions & 12 deletions app/src/full/java/com/celzero/bravedns/util/TunnelImporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ import com.celzero.bravedns.RethinkDnsApplication.Companion.DEBUG
import com.celzero.bravedns.service.WireguardManager
import com.celzero.bravedns.wireguard.Config
import com.celzero.bravedns.wireguard.util.ErrorMessages
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

object TunnelImporter : KoinComponent {

Expand Down Expand Up @@ -132,14 +132,18 @@ object TunnelImporter : KoinComponent {
}
}

fun importTunnel(configText: String, messageCallback: (CharSequence) -> Unit) {
try {
if (DEBUG) Log.d(Logger.LOG_TAG_PROXY, "Importing tunnel: $configText")
val config =
Config.parse(ByteArrayInputStream(configText.toByteArray(StandardCharsets.UTF_8)))
WireguardManager.addConfig(config)
} catch (e: Throwable) {
onTunnelImportFinished(listOf(e), messageCallback)
suspend fun importTunnel(configText: String, messageCallback: (CharSequence) -> Unit) {
withContext(Dispatchers.IO) {
try {
if (DEBUG) Log.d(Logger.LOG_TAG_PROXY, "Importing tunnel: $configText")
val config =
Config.parse(
ByteArrayInputStream(configText.toByteArray(StandardCharsets.UTF_8))
)
WireguardManager.addConfig(config)
} catch (e: Throwable) {
onTunnelImportFinished(listOf(e), messageCallback)
}
}
}

Expand Down
Loading

0 comments on commit d24918c

Please sign in to comment.