Skip to content

Commit

Permalink
Merge pull request #1497 from hussainmohd-a/main
Browse files Browse the repository at this point in the history
init v055n
  • Loading branch information
hussainmohd-a authored Jun 1, 2024
2 parents df18071 + bdabf4d commit 66c98cb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
if (stats == null) return ""
val rx =
context.getString(
R.string.symbol_upload,
R.string.symbol_download,
Utilities.humanReadableByteCount(stats.rx, true)
)
val tx =
context.getString(
R.string.symbol_download,
R.string.symbol_upload,
Utilities.humanReadableByteCount(stats.tx, true)
)
return context.getString(R.string.two_argument_space, rx, tx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,12 @@ class WgConfigAdapter(private val context: Context) :
if (stats == null) return ""
val rx =
context.getString(
R.string.symbol_upload,
R.string.symbol_download,
Utilities.humanReadableByteCount(stats.rx, true)
)
val tx =
context.getString(
R.string.symbol_download,
R.string.symbol_upload,
Utilities.humanReadableByteCount(stats.tx, true)
)
return context.getString(R.string.two_argument_space, rx, tx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ class WgConfigDetailActivity : AppCompatActivity(R.layout.activity_wg_detail) {
b.interfaceEdit.setOnClickListener {
val intent = Intent(this, WgConfigEditorActivity::class.java)
intent.putExtra(WgConfigEditorActivity.INTENT_EXTRA_WG_ID, configId)
intent.putExtra(INTENT_EXTRA_WG_TYPE, wgType.value)
this.startActivity(intent)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class WgConfigEditorActivity : AppCompatActivity(R.layout.activity_wg_config_edi
}
if (
wgInterface?.listenPort?.isPresent == true &&
wgInterface?.listenPort?.get() != 1
wgInterface?.listenPort?.get() != 1 && wgType.isOneWg()
) {
b.listenPortText.setText(wgInterface?.listenPort?.get().toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import com.celzero.bravedns.R
import com.celzero.bravedns.data.AppConfig
import com.celzero.bravedns.database.AppInfo
import com.celzero.bravedns.databinding.FragmentHomeScreenBinding
import com.celzero.bravedns.scheduler.WorkScheduler
import com.celzero.bravedns.service.*
import com.celzero.bravedns.ui.activity.AlertsActivity
import com.celzero.bravedns.ui.activity.AppListActivity
Expand Down Expand Up @@ -98,6 +99,7 @@ class HomeScreenFragment : Fragment(R.layout.fragment_home_screen) {

private val persistentState by inject<PersistentState>()
private val appConfig by inject<AppConfig>()
private val workScheduler by inject<WorkScheduler>()

private var isVpnActivated: Boolean = false

Expand Down Expand Up @@ -987,11 +989,24 @@ class HomeScreenFragment : Fragment(R.layout.fragment_home_screen) {
*/
private fun maybeAutoStartVpn() {
if (isVpnActivated && !VpnController.isOn()) {
// this case will happen when the app is updated or crashed
// generate the bug report and start the vpn
triggerBugReport()
Logger.i(LOG_TAG_VPN, "start VPN (previous state)")
prepareAndStartVpn()
}
}

private fun triggerBugReport() {
if (WorkScheduler.isWorkRunning(requireContext(), WorkScheduler.APP_EXIT_INFO_JOB_TAG)) {
Logger.v(LOG_TAG_VPN, "bug report already triggered")
return
}

Logger.v(LOG_TAG_VPN, "trigger bug report")
workScheduler.scheduleOneTimeWorkForAppExitInfo()
}

// set the app mode to dns+firewall mode when vpn in lockdown state
private fun handleLockdownModeIfNeeded() {
if (VpnController.isVpnLockdown() && !appConfig.getBraveMode().isDnsFirewallMode()) {
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/com/celzero/bravedns/net/go/GoVpnAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import android.os.ParcelFileDescriptor
import android.widget.Toast
import backend.Backend
import com.celzero.bravedns.R
import com.celzero.bravedns.RethinkDnsApplication.Companion.DEBUG
import com.celzero.bravedns.data.AppConfig
import com.celzero.bravedns.data.AppConfig.Companion.FALLBACK_DNS
import com.celzero.bravedns.data.AppConfig.TunnelOptions
Expand Down Expand Up @@ -749,11 +748,11 @@ class GoVpnAdapter : KoinComponent {
}

val wgConfig = WireguardManager.getConfigById(proxyId)
val withDNS = WireguardManager.getOneWireGuardProxyId() == proxyId
val wgUserSpaceString = wgConfig?.toWgUserspaceString()
val isOneWg = WireguardManager.getOneWireGuardProxyId() == proxyId
val wgUserSpaceString = wgConfig?.toWgUserspaceString(isOneWg)
getProxies()?.addProxy(id, wgUserSpaceString)
if (withDNS) setWireGuardDns(id)
Logger.i(LOG_TAG_VPN, "add wireguard proxy with $id; dns? $withDNS")
if (isOneWg) setWireGuardDns(id)
Logger.i(LOG_TAG_VPN, "add wireguard proxy with $id; dns? $isOneWg")
} catch (e: Exception) {
Logger.e(LOG_TAG_VPN, "error adding wireguard proxy: ${e.message}", e)
// do not auto remove failed wg proxy, let the user decide via UI
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/celzero/bravedns/wireguard/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,12 @@ class Config private constructor(builder: Builder) {
*
* @return the `Config` represented as a series of "key=value" lines
*/
fun toWgUserspaceString(): String {
fun toWgUserspaceString(isOneWg: Boolean = false): String {
// Skip the listen port if we're in advanced mode.
// In advanced mode, the port may already be in use by another interface.
val skipListenPort = !isOneWg
val sb = StringBuilder()
sb.append(wgInterface?.toWgUserspaceString() ?: "")
sb.append(wgInterface?.toWgUserspaceString(skipListenPort) ?: "")
sb.append("replace_peers=true\n")
if (peers != null) {
for (peer in peers) sb.append(peer.toWgUserspaceString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class WgInterface private constructor(builder: Builder) {
*
* @return the `Interface` represented as a series of "KEY=VALUE" lines
*/
fun toWgUserspaceString(): String {
fun toWgUserspaceString(skipListenPort: Boolean): String {
val dnsServerStrings =
dnsServers
.stream()
Expand All @@ -201,7 +201,11 @@ class WgInterface private constructor(builder: Builder) {
sb.append("address=").append(Attribute.join(addresses)).append('\n')
sb.append("dns=").append(Attribute.join(dnsServerStrings)).append('\n')
sb.append("mtu=").append(mtu.orElse(1280)).append('\n')
listenPort.ifPresent { lp: Int? -> sb.append("listen_port=").append(lp).append('\n') }
// Skip the listen port if it's not set or if we're in advanced mode.
// In advanced mode, the port may already be in use by another interface.
if (!skipListenPort) {
listenPort.ifPresent { lp: Int? -> sb.append("listen_port=").append(lp).append('\n') }
}
return sb.toString()
}

Expand Down

0 comments on commit 66c98cb

Please sign in to comment.