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

Only enable TCP reconnect when failover is needed #429

Merged
merged 1 commit into from
Oct 4, 2024
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 @@ -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 = [email protected] ?: 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
Loading