IpnViewModel: reset vpn state before login/reauth (#366)

Fixes ENG-3479

Clear routes when switching profiles and reauthenticating. This fixes an issue where previously set routes/DNS configs caused actions to fail.

Signed-off-by: kari-ts <kari@tailscale.com>
pull/372/head
kari-ts 2 months ago committed by GitHub
parent 45fd2e0661
commit 2d7d6e1357
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -100,9 +100,19 @@ open class IpnViewModel : ViewModel() {
} }
} }
// Need to stop running before logging in to clear routes:
// https://linear.app/tailscale/issue/ENG-3441/routesdns-is-not-cleared-when-switching-profiles-or-reauthenticating
val stopThenLogin = {
Client(viewModelScope).editPrefs(Ipn.MaskedPrefs().apply { WantRunning = false }) { result ->
result
.onSuccess { loginAction() }
.onFailure { Log.e(TAG, "Error setting wantRunning to false: ${it.message}") }
}
}
val startAction = { val startAction = {
Client(viewModelScope).start(Ipn.Options(AuthKey = authKey)) { start -> Client(viewModelScope).start(Ipn.Options(AuthKey = authKey)) { start ->
start.onFailure { completionHandler(Result.failure(it)) }.onSuccess { loginAction() } start.onFailure { completionHandler(Result.failure(it)) }.onSuccess { stopThenLogin() }
} }
} }
@ -164,11 +174,18 @@ open class IpnViewModel : ViewModel() {
} }
fun switchProfile(profile: IpnLocal.LoginProfile, completionHandler: (Result<String>) -> Unit) { fun switchProfile(profile: IpnLocal.LoginProfile, completionHandler: (Result<String>) -> Unit) {
val switchProfile = {
Client(viewModelScope).switchProfile(profile) { Client(viewModelScope).switchProfile(profile) {
startVPN() startVPN()
completionHandler(it) completionHandler(it)
} }
} }
Client(viewModelScope).editPrefs(Ipn.MaskedPrefs().apply { WantRunning = false }) { result ->
result
.onSuccess { switchProfile() }
.onFailure { Log.e(TAG, "Error setting wantRunning to false: ${it.message}") }
}
}
fun addProfile(completionHandler: (Result<String>) -> Unit) { fun addProfile(completionHandler: (Result<String>) -> Unit) {
Client(viewModelScope).addProfile { Client(viewModelScope).addProfile {

Loading…
Cancel
Save