Skip to content

Commit

Permalink
Fix bug that would reset location constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun authored and dlon committed Oct 27, 2023
1 parent 6ea7180 commit f55ce57
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,13 @@ class RelayListListener(
var selectedRelayItem: RelayItem? = null
private set

var selectedRelayLocation: GeographicLocationConstraint?
get() {
val settings = relaySettings as? RelaySettings.Normal
val location = settings?.relayConstraints?.location as? Constraint.Only

return location?.value?.toGeographicLocationConstraint()
}
set(value) {
connection.send(Request.SetRelayLocation(value).message)
}

var selectedWireguardConstraints: WireguardConstraints?
get() {
val settings = relaySettings as? RelaySettings.Normal
fun updateSelectedRelayLocation(value: GeographicLocationConstraint) {
connection.send(Request.SetRelayLocation(value).message)
}

return settings?.relayConstraints?.wireguardConstraints?.port?.let { port ->
WireguardConstraints(port)
}
}
set(value) {
connection.send(Request.SetWireguardConstraints(value).message)
}
fun updateSelectedWireguardConstraints(value: WireguardConstraints) {
connection.send(Request.SetWireguardConstraints(value).message)
}

var onRelayCountriesChange: ((List<RelayCountry>, RelayItem?) -> Unit)? = null
set(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ class SelectLocationViewModel(private val serviceConnectionManager: ServiceConne
@Suppress("konsist.ensure public properties use permitted names")
val enterTransitionEndAction = _enterTransitionEndAction.asSharedFlow()

fun selectRelay(relayItem: RelayItem?) {
serviceConnectionManager.relayListListener()?.selectedRelayLocation = relayItem?.location
fun selectRelay(relayItem: RelayItem) {
serviceConnectionManager
.relayListListener()
?.updateSelectedRelayLocation(relayItem.location)
serviceConnectionManager.connectionProxy()?.connect()
viewModelScope.launch { _closeAction.emit(Unit) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ class VpnSettingsViewModel(
isLocalNetworkSharingEnabled = settings?.allowLan ?: false,
isCustomDnsEnabled = settings?.isCustomDnsEnabled() ?: false,
customDnsList = settings?.addresses()?.asStringAddressList() ?: listOf(),
contentBlockersOptions = settings?.contentBlockersSettings()
?: DefaultDnsOptions(),
contentBlockersOptions =
settings?.contentBlockersSettings() ?: DefaultDnsOptions(),
isAllowLanEnabled = settings?.allowLan ?: false,
selectedObfuscation = settings?.selectedObfuscationSettings()
?: SelectedObfuscation.Off,
selectedObfuscation =
settings?.selectedObfuscationSettings() ?: SelectedObfuscation.Off,
dialogState = dialogState,
quantumResistant = settings?.quantumResistant()
?: QuantumResistantState.Off,
quantumResistant =
settings?.quantumResistant() ?: QuantumResistantState.Off,
selectedWireguardPort = settings?.getWireguardPort() ?: Constraint.Any(),
availablePortRanges = portRanges
)
Expand Down Expand Up @@ -352,8 +352,9 @@ class VpnSettingsViewModel(

fun onWireguardPortSelected(port: Constraint<Port>) {
viewModelScope.launch(dispatcher) {
serviceConnectionManager.relayListListener()?.selectedWireguardConstraints =
WireguardConstraints(port = port)
serviceConnectionManager
.relayListListener()
?.updateSelectedWireguardConstraints(WireguardConstraints(port = port))
}
hideDialog()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ sealed class Request : Message.RequestMessage() {
@Parcelize data class SetEnableSplitTunneling(val enable: Boolean) : Request()

@Parcelize
data class SetRelayLocation(val relayLocation: GeographicLocationConstraint?) : Request()
data class SetRelayLocation(val relayLocation: GeographicLocationConstraint) : Request()

@Parcelize data class SetWireGuardMtu(val mtu: Int?) : Request()

Expand All @@ -89,7 +89,7 @@ sealed class Request : Message.RequestMessage() {
@Parcelize data class SetObfuscationSettings(val settings: ObfuscationSettings?) : Request()

@Parcelize
data class SetWireguardConstraints(val wireguardConstraints: WireguardConstraints?) : Request()
data class SetWireguardConstraints(val wireguardConstraints: WireguardConstraints) : Request()

@Parcelize
data class SetWireGuardQuantumResistant(val quantumResistant: QuantumResistantState) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.os.Parcelable
import kotlinx.parcelize.Parcelize

sealed class RelaySettings : Parcelable {
@Parcelize object CustomTunnelEndpoint : RelaySettings()
@Parcelize data object CustomTunnelEndpoint : RelaySettings()

@Parcelize class Normal(val relayConstraints: RelayConstraints) : RelaySettings()
@Parcelize data class Normal(val relayConstraints: RelayConstraints) : RelaySettings()
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class MullvadDaemon(
playPurchase: PlayPurchase,
): PlayPurchaseVerifyResult

private external fun updateRelaySettings(
private external fun setRelaySettings(
daemonInterfaceAddress: Long,
update: RelaySettings
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ class RelayListListener(endpoint: ServiceEndpoint) {
}

private suspend fun updateRelayConstraints() {
val currentRelayConstraints = getCurrentRelayConstraints()

// TODO Fix so that we do not send null from the app when we want to set location constraint
// to any
val location: Constraint<LocationConstraint> =
selectedRelayLocation?.let { location ->
Constraint.Only(LocationConstraint.Location(location))
}
?: Constraint.Any()
val wireguardConstraints: WireguardConstraints? = selectedWireguardConstraints
} ?: currentRelayConstraints.location
val wireguardConstraints: WireguardConstraints =
selectedWireguardConstraints ?: currentRelayConstraints.wireguardConstraints

val update =
RelaySettings.Normal(
Expand All @@ -107,6 +111,18 @@ class RelayListListener(endpoint: ServiceEndpoint) {
daemon.await().setRelaySettings(update)
}

private suspend fun getCurrentRelayConstraints(): RelayConstraints =
when (val relaySettings = daemon.await().getSettings()?.relaySettings) {
is RelaySettings.Normal -> relaySettings.relayConstraints
else ->
RelayConstraints(
location = Constraint.Any(),
providers = Constraint.Any(),
ownership = Constraint.Any(),
wireguardConstraints = WireguardConstraints(Constraint.Any())
)
}

companion object {
private enum class Command {
SetRelayLocation,
Expand Down

0 comments on commit f55ce57

Please sign in to comment.