Skip to content

Commit

Permalink
Merge pull request #429 from eduvpn/feature/failover
Browse files Browse the repository at this point in the history
Only enable TCP reconnect when failover is needed
  • Loading branch information
dzolnai authored Oct 4, 2024
2 parents c2af605 + 2043a52 commit 7b14189
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AlertDialog
import androidx.core.view.postDelayed
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.asLiveData
Expand All @@ -47,6 +48,7 @@ import nl.eduvpn.app.utils.ErrorDialog
import nl.eduvpn.app.utils.FormattingUtils
import nl.eduvpn.app.viewmodel.BaseConnectionViewModel
import nl.eduvpn.app.viewmodel.ConnectionStatusViewModel
import nl.eduvpn.app.viewmodel.MainViewModel
import org.eduvpn.common.Protocol

/**
Expand All @@ -62,11 +64,14 @@ class ConnectionStatusFragment : BaseFragment<FragmentConnectionStatusBinding>()

private val viewModel by viewModels<ConnectionStatusViewModel> { viewModelFactory }

private val mainViewModel by activityViewModels<MainViewModel> { viewModelFactory }

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
EduVPNApplication.get(view.context).component().inject(this)
binding.viewModel = viewModel
binding.isTcp = viewModel.isCurrentProtocolUsingTcp()
binding.failoverNeeded = mainViewModel.failoverResult.value ?: false
binding.secondsConnected = viewModel.connectionTimeLiveData.map { secondsConnected ->
val context = this@ConnectionStatusFragment.context ?: return@map null
FormattingUtils.formatDurationSeconds(
Expand Down Expand Up @@ -206,6 +211,9 @@ class ConnectionStatusFragment : BaseFragment<FragmentConnectionStatusBinding>()
updateCertExpiryObserver?.let { obs -> viewModel.timer.removeObserver(obs) }
}
}
mainViewModel.failoverResult.observe(viewLifecycleOwner) {
binding.failoverNeeded = it
}
viewModel.timer.observe(viewLifecycleOwner, updateCertExpiryObserver)
viewModel.vpnStatus.observe(viewLifecycleOwner) { status ->
binding.connectionStatus.setText(VPNConnectionService.vpnStatusToStringID(status))
Expand Down
19 changes: 4 additions & 15 deletions app/src/main/java/nl/eduvpn/app/viewmodel/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class MainViewModel @Inject constructor(
val mainParentAction = _mainParentAction.toSingleEvent()

val proxyGuardEnabled: Boolean get() = preferencesService.getCurrentProtocol() == Protocol.WireGuardWithTCP.nativeValue
private val _failoverResult = MutableLiveData(false)
val failoverResult = _failoverResult.toSingleEvent()


init {
backendService.register(
Expand Down Expand Up @@ -159,21 +162,7 @@ class MainViewModel @Inject constructor(
// Waits a bit so that the network interface has been surely set up
delay(1_000L)
backendService.startFailOver(service) {
// Failover needed, request a new profile with TCP enforced.
preferencesService.getCurrentInstance()?.let { currentInstance ->
viewModelScope.launch {
// Disconnect first, otherwise we don't have any internet :)
service.disconnect()
// Wait a bit for the disconnection to finish
delay(500L)
// Fetch a new profile, now with TCP forced
try {
backendService.getConfig(currentInstance, preferTcp = true)
} catch (ex: Exception) {
Log.w(TAG, "Could not fetch new config!", ex)
}
}
}
_failoverResult.postValue(true)
}
} catch (ex: CommonException) {
// These are just warnings, so we log them, but don't display to the user
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/res/layout/fragment_connection_status.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
name="isTcp"
type="boolean" />

<variable
name="failoverNeeded"
type="boolean" />

<import type="android.view.View" />
</data>

Expand Down Expand Up @@ -209,7 +213,7 @@
android:layout_marginTop="16dp"
android:textColor="@color/textColor"
android:textSize="14sp"
android:visibility="@{isTcp ? View.GONE : View.VISIBLE}"
android:visibility="@{isTcp || !failoverNeeded ? View.GONE : View.VISIBLE}"
android:text="@string/connection_status_connectivity_problems"
android:layout_height="wrap_content"/>

Expand All @@ -218,7 +222,7 @@
android:id="@+id/reconnect_tcp_button"
android:layout_width="wrap_content"
android:layout_marginTop="8dp"
android:visibility="@{isTcp ? View.GONE : View.VISIBLE}"
android:visibility="@{isTcp || !failoverNeeded ? View.GONE : View.VISIBLE}"
android:text="@string/connection_status_reconnect_with_tcp"
android:layout_height="wrap_content"/>
</LinearLayout>
Expand Down

0 comments on commit 7b14189

Please sign in to comment.