Skip to content

Commit

Permalink
Add daita grpc and ui
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-mullvad committed Aug 27, 2024
1 parent 02f0718 commit 28f97a9
Show file tree
Hide file tree
Showing 34 changed files with 327 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ private val DUMMY_RELAY_1 =
Provider(
providerId = ProviderId("PROVIDER RENTED"),
ownership = Ownership.Rented,
)
),
daita = false
)
private val DUMMY_RELAY_2 =
RelayItem.Location.Relay(
Expand All @@ -35,7 +36,8 @@ private val DUMMY_RELAY_2 =
),
active = true,
provider =
Provider(providerId = ProviderId("PROVIDER OWNED"), ownership = Ownership.MullvadOwned)
Provider(providerId = ProviderId("PROVIDER OWNED"), ownership = Ownership.MullvadOwned),
daita = false
)
private val DUMMY_RELAY_CITY_1 =
RelayItem.Location.City(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private fun PreviewLocationInfo() {
isVisible = true,
isExpanded = true,
location = null,
isUsingDaita = false,
inAddress = null,
outAddress = ""
)
Expand All @@ -48,6 +49,7 @@ fun LocationInfo(
isVisible: Boolean,
isExpanded: Boolean,
location: GeoIpLocation?,
isUsingDaita: Boolean,
inAddress: Triple<String, Int, TransportProtocol>?,
outAddress: String
) {
Expand All @@ -61,15 +63,12 @@ fun LocationInfo(
.then(modifier)
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = location?.hostname ?: "",
color =
if (isExpanded) {
colorExpanded
} else {
colorCollapsed
},
style = MaterialTheme.typography.labelLarge.copy(fontWeight = FontWeight.SemiBold)
RelayHostname(
hostname = location?.hostname,
isUsingDaita = isUsingDaita,
isExpanded = isExpanded,
colorExpanded = colorExpanded,
colorCollapsed = colorCollapsed,
)
Chevron(
isExpanded = isExpanded,
Expand Down Expand Up @@ -119,3 +118,38 @@ fun LocationInfo(
)
}
}

@Composable
private fun RelayHostname(
hostname: String?,
isUsingDaita: Boolean,
isExpanded: Boolean,
colorExpanded: Color,
colorCollapsed: Color
) {
val hostnameTitle =
when {
hostname != null && isUsingDaita -> {
stringResource(
id = R.string.connected_using_daita,
hostname,
stringResource(
id = R.string.daita,
)
)
}
hostname != null -> hostname
else -> ""
}

Text(
text = hostnameTitle,
color =
if (isExpanded) {
colorExpanded
} else {
colorCollapsed
},
style = MaterialTheme.typography.labelLarge.copy(fontWeight = FontWeight.SemiBold),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.mullvad.mullvadvpn.compose.dialog

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootGraph
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.EmptyDestinationsNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.lib.theme.AppTheme

@Preview
@Composable
private fun PreviewDaitaInfoDialog() {
AppTheme { DaitaInfo(EmptyDestinationsNavigator) }
}

@Destination<RootGraph>(style = DestinationStyle.Dialog::class)
@Composable
fun DaitaInfo(navigator: DestinationsNavigator) {
InfoDialog(
message =
stringResource(
id = R.string.daita_info,
stringResource(id = R.string.daita),
stringResource(id = R.string.daita_full),
),
additionalInfo =
stringResource(id = R.string.daita_warning, stringResource(id = R.string.daita)),
onDismiss = dropUnlessResumed { navigator.navigateUp() },
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private fun generateRelayItemRelay(
cityCode: GeoLocationId.City,
hostName: String,
active: Boolean = true,
daita: Boolean = true,
) =
RelayItem.Location.Relay(
id =
Expand All @@ -60,6 +61,7 @@ private fun generateRelayItemRelay(
),
active = active,
provider = Provider(ProviderId("Provider"), Ownership.MullvadOwned),
daita = daita
)

private fun String.generateCountryCode() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ object TunnelStatePreviewData {

fun generateConnectingState(featureIndicators: Int, quantumResistant: Boolean) =
TunnelState.Connecting(
endpoint = generateTunnelEndpoint(quantumResistant = quantumResistant),
endpoint = generateTunnelEndpoint(quantumResistant = quantumResistant, daita = false),
location = generateLocation(),
featureIndicators = generateFeatureIndicators(featureIndicators)
)

fun generateConnectedState(featureIndicators: Int, quantumResistant: Boolean) =
TunnelState.Connected(
endpoint = generateTunnelEndpoint(quantumResistant = quantumResistant),
endpoint = generateTunnelEndpoint(quantumResistant = quantumResistant, daita = true),
location = generateLocation(),
featureIndicators = generateFeatureIndicators(featureIndicators)
)
Expand All @@ -39,15 +39,16 @@ object TunnelStatePreviewData {
)
}

private fun generateTunnelEndpoint(quantumResistant: Boolean): TunnelEndpoint =
private fun generateTunnelEndpoint(quantumResistant: Boolean, daita: Boolean): TunnelEndpoint =
TunnelEndpoint(
endpoint = generateEndpoint(TransportProtocol.Udp),
quantumResistant = quantumResistant,
obfuscation =
ObfuscationEndpoint(
endpoint = generateEndpoint(TransportProtocol.Tcp),
ObfuscationType.Udp2Tcp
)
),
daita = daita
)

private fun generateEndpoint(transportProtocol: TransportProtocol) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ private fun ConnectionInfo(state: ConnectUiState) {
isVisible = state.showLocationInfo,
isExpanded = expanded,
location = state.location,
isUsingDaita = state.tunnelState.isUsingDaita(),
inAddress = state.inAddress,
outAddress = state.outAddress,
modifier =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.ramcosta.composedestinations.annotation.RootGraph
import com.ramcosta.composedestinations.generated.destinations.AutoConnectAndLockdownModeDestination
import com.ramcosta.composedestinations.generated.destinations.ContentBlockersInfoDestination
import com.ramcosta.composedestinations.generated.destinations.CustomDnsInfoDestination
import com.ramcosta.composedestinations.generated.destinations.DaitaInfoDestination
import com.ramcosta.composedestinations.generated.destinations.DnsDestination
import com.ramcosta.composedestinations.generated.destinations.LocalNetworkSharingInfoDestination
import com.ramcosta.composedestinations.generated.destinations.MalwareInfoDestination
Expand Down Expand Up @@ -224,13 +225,15 @@ fun VpnSettings(
},
navigateToLocalNetworkSharingInfo =
dropUnlessResumed { navigator.navigate(LocalNetworkSharingInfoDestination) },
navigateToDaitaInfo = dropUnlessResumed { navigator.navigate(DaitaInfoDestination) },
navigateToServerIpOverrides =
dropUnlessResumed { navigator.navigate(ServerIpOverridesDestination) },
onToggleBlockTrackers = vm::onToggleBlockTrackers,
onToggleBlockAds = vm::onToggleBlockAds,
onToggleBlockMalware = vm::onToggleBlockMalware,
onToggleAutoConnect = vm::onToggleAutoConnect,
onToggleLocalNetworkSharing = vm::onToggleLocalNetworkSharing,
onToggleDaita = vm::onToggleDaita,
onToggleBlockAdultContent = vm::onToggleBlockAdultContent,
onToggleBlockGambling = vm::onToggleBlockGambling,
onToggleBlockSocialMedia = vm::onToggleBlockSocialMedia,
Expand Down Expand Up @@ -273,13 +276,15 @@ fun VpnSettingsScreen(
navigateUdp2TcpInfo: () -> Unit = {},
navigateToWireguardPortInfo: (availablePortRanges: List<PortRange>) -> Unit = {},
navigateToLocalNetworkSharingInfo: () -> Unit = {},
navigateToDaitaInfo: () -> Unit = {},
navigateToWireguardPortDialog: () -> Unit = {},
navigateToServerIpOverrides: () -> Unit = {},
onToggleBlockTrackers: (Boolean) -> Unit = {},
onToggleBlockAds: (Boolean) -> Unit = {},
onToggleBlockMalware: (Boolean) -> Unit = {},
onToggleAutoConnect: (Boolean) -> Unit = {},
onToggleLocalNetworkSharing: (Boolean) -> Unit = {},
onToggleDaita: (Boolean) -> Unit = {},
onToggleBlockAdultContent: (Boolean) -> Unit = {},
onToggleBlockGambling: (Boolean) -> Unit = {},
onToggleBlockSocialMedia: (Boolean) -> Unit = {},
Expand Down Expand Up @@ -497,8 +502,19 @@ fun VpnSettingsScreen(
)
}

itemWithDivider {
item {
Spacer(modifier = Modifier.height(Dimens.cellLabelVerticalPadding))
HeaderSwitchComposeCell(
title = "DAITA",
isToggled = state.isDaitaEnabled,
isEnabled = true,
onCellClicked = { newValue -> onToggleDaita(newValue) },
onInfoClicked = navigateToDaitaInfo
)
Spacer(modifier = Modifier.height(Dimens.cellLabelVerticalPadding))
}

itemWithDivider {
InformationComposeCell(
title = stringResource(id = R.string.wireguard_port_title),
onInfoClicked = { navigateToWireguardPortInfo(state.availablePortRanges) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ data class VpnSettingsUiState(
val mtu: Mtu?,
val isAutoConnectEnabled: Boolean,
val isLocalNetworkSharingEnabled: Boolean,
val isDaitaEnabled: Boolean,
val isCustomDnsEnabled: Boolean,
val customDnsItems: List<CustomDnsItem>,
val contentBlockersOptions: DefaultDnsOptions,
Expand All @@ -31,6 +32,7 @@ data class VpnSettingsUiState(
mtu: Mtu? = null,
isAutoConnectEnabled: Boolean = false,
isLocalNetworkSharingEnabled: Boolean = false,
isDaitaEnabled: Boolean = false,
isCustomDnsEnabled: Boolean = false,
customDnsItems: List<CustomDnsItem> = emptyList(),
contentBlockersOptions: DefaultDnsOptions = DefaultDnsOptions(),
Expand All @@ -46,6 +48,7 @@ data class VpnSettingsUiState(
mtu,
isAutoConnectEnabled,
isLocalNetworkSharingEnabled,
isDaitaEnabled,
isCustomDnsEnabled,
customDnsItems,
contentBlockersOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ val uiModule = module {
single { FilterCustomListsRelayItemUseCase(get(), get()) }
single { CustomListsRelayItemUseCase(get(), get()) }
single { CustomListRelayItemsUseCase(get(), get()) }
single { FilteredRelayListUseCase(get(), get()) }
single { FilteredRelayListUseCase(get(), get(), get()) }
single { LastKnownLocationUseCase(get()) }

single { InAppNotificationController(get(), get(), get(), get(), MainScope()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,51 +53,61 @@ private fun RelayItem.Location.hasProvider(providersConstraint: Constraint<Provi
true
}

fun RelayItem.CustomList.filterOnOwnershipAndProvider(
fun RelayItem.CustomList.applyFilters(
ownership: Constraint<Ownership>,
providers: Constraint<Providers>
providers: Constraint<Providers>,
isDaitaEnabled: Boolean
): RelayItem.CustomList {
val newLocations =
locations.mapNotNull {
when (it) {
is RelayItem.Location.Country ->
it.filterOnOwnershipAndProvider(ownership, providers)
is RelayItem.Location.City -> it.filterOnOwnershipAndProvider(ownership, providers)
is RelayItem.Location.Relay -> it.filterOnOwnershipAndProvider(ownership, providers)
it.applyFilters(ownership, providers, isDaitaEnabled)
is RelayItem.Location.City -> it.applyFilters(ownership, providers, isDaitaEnabled)
is RelayItem.Location.Relay -> it.applyFilters(ownership, providers, isDaitaEnabled)
}
}
return copy(locations = newLocations)
}

fun RelayItem.Location.Country.filterOnOwnershipAndProvider(
fun RelayItem.Location.Country.applyFilters(
ownership: Constraint<Ownership>,
providers: Constraint<Providers>
providers: Constraint<Providers>,
isDaitaEnabled: Boolean
): RelayItem.Location.Country? {
val cities = cities.mapNotNull { it.filterOnOwnershipAndProvider(ownership, providers) }
val cities = cities.mapNotNull { it.applyFilters(ownership, providers, isDaitaEnabled) }
return if (cities.isNotEmpty()) {
this.copy(cities = cities)
} else {
null
}
}

private fun RelayItem.Location.City.filterOnOwnershipAndProvider(
private fun RelayItem.Location.City.applyFilters(
ownership: Constraint<Ownership>,
providers: Constraint<Providers>
providers: Constraint<Providers>,
isDaitaEnabled: Boolean
): RelayItem.Location.City? {
val relays = relays.mapNotNull { it.filterOnOwnershipAndProvider(ownership, providers) }
val relays = relays.mapNotNull { it.applyFilters(ownership, providers, isDaitaEnabled) }
return if (relays.isNotEmpty()) {
this.copy(relays = relays)
} else {
null
}
}

private fun RelayItem.Location.Relay.filterOnOwnershipAndProvider(
private fun RelayItem.Location.Relay.hasMatchingDaitaSetting(isDaitaEnabled: Boolean): Boolean {
return if (isDaitaEnabled) daita else true
}

private fun RelayItem.Location.Relay.applyFilters(
ownership: Constraint<Ownership>,
providers: Constraint<Providers>
providers: Constraint<Providers>,
isDaitaEnabled: Boolean
): RelayItem.Location.Relay? {
return if (hasOwnership(ownership) && hasProvider(providers)) {
return if (
hasMatchingDaitaSetting(isDaitaEnabled) && hasOwnership(ownership) && hasProvider(providers)
) {
this
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.stateIn
import net.mullvad.mullvadvpn.lib.daemon.grpc.ManagementService
import net.mullvad.mullvadvpn.lib.model.Constraint
import net.mullvad.mullvadvpn.lib.model.CustomDnsOptions
import net.mullvad.mullvadvpn.lib.model.DaitaSettings
import net.mullvad.mullvadvpn.lib.model.DefaultDnsOptions
import net.mullvad.mullvadvpn.lib.model.DnsOptions
import net.mullvad.mullvadvpn.lib.model.DnsState
Expand Down Expand Up @@ -70,4 +71,7 @@ class SettingsRepository(

suspend fun setLocalNetworkSharing(isEnabled: Boolean) =
managementService.setAllowLan(isEnabled)

suspend fun setDaitaSettings(daitaSettings: DaitaSettings) =
managementService.setDaitaSettings(daitaSettings)
}
Loading

0 comments on commit 28f97a9

Please sign in to comment.