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

android: cred import #697

Merged
merged 5 commits into from
Jul 16, 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
2 changes: 1 addition & 1 deletion nym-vpn-android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ android {
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
signingConfig = signingConfigs.getByName("debug")
signingConfig = signingConfigs.getByName(Constants.RELEASE)
}
debug {
isMinifyEnabled = false
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import net.nymtech.nymvpn.data.domain.Settings
import net.nymtech.nymvpn.ui.theme.Theme
import net.nymtech.vpn.model.Country
import net.nymtech.vpn.model.VpnMode
import java.time.Instant

interface SettingsRepository {

Expand Down Expand Up @@ -49,5 +50,9 @@ interface SettingsRepository {

suspend fun setApplicationShortcuts(enabled: Boolean)

suspend fun getCredentialExpiry(): Instant?

suspend fun saveCredentialExpiry(instant: Instant)

val settingsFlow: Flow<Settings>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.nymtech.nymvpn.data.datastore

import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand All @@ -10,6 +11,7 @@ import net.nymtech.nymvpn.ui.theme.Theme
import net.nymtech.vpn.model.Country
import net.nymtech.vpn.model.VpnMode
import timber.log.Timber
import java.time.Instant

class DataStoreSettingsRepository(private val dataStoreManager: DataStoreManager) :
SettingsRepository {
Expand All @@ -25,6 +27,7 @@ class DataStoreSettingsRepository(private val dataStoreManager: DataStoreManager
private val autoStart = booleanPreferencesKey("AUTO_START")
private val analyticsShown = booleanPreferencesKey("ANALYTICS_SHOWN")
private val applicationShortcuts = booleanPreferencesKey("APPLICATION_SHORTCUTS")
private val credentialExpiry = longPreferencesKey("CREDENTIAL_EXPIRY")

override suspend fun init() {
val firstHop = dataStoreManager.getFromStore(firstHopCountry)
Expand Down Expand Up @@ -131,6 +134,16 @@ class DataStoreSettingsRepository(private val dataStoreManager: DataStoreManager
dataStoreManager.saveToDataStore(applicationShortcuts, enabled)
}

override suspend fun getCredentialExpiry(): Instant? {
return dataStoreManager.getFromStore(credentialExpiry)?.let {
Instant.ofEpochSecond(it)
}
}

override suspend fun saveCredentialExpiry(instant: Instant) {
dataStoreManager.saveToDataStore(credentialExpiry, instant.epochSecond)
}

override val settingsFlow: Flow<Settings> =
dataStoreManager.preferencesFlow.map { prefs ->
prefs?.let { pref ->
Expand All @@ -157,6 +170,7 @@ class DataStoreSettingsRepository(private val dataStoreManager: DataStoreManager
firstHopCountry = Country.from(pref[firstHopCountry]) ?: default,
lastHopCountry = Country.from(pref[lastHopCountry]) ?: default,
isShortcutsEnabled = pref[applicationShortcuts] ?: Settings.SHORTCUTS_DEFAULT,
credentialExpiry = pref[credentialExpiry]?.let { Instant.ofEpochSecond(it) },
)
} catch (e: IllegalArgumentException) {
Timber.e(e)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.nymtech.nymvpn.data.domain
import net.nymtech.nymvpn.ui.theme.Theme
import net.nymtech.vpn.model.Country
import net.nymtech.vpn.model.VpnMode
import java.time.Instant

data class Settings(
val theme: Theme? = null,
Expand All @@ -15,6 +16,7 @@ data class Settings(
val firstHopCountry: Country = Country(),
val lastHopCountry: Country = Country(),
val isShortcutsEnabled: Boolean = SHORTCUTS_DEFAULT,
val credentialExpiry: Instant? = null,
) {
companion object {
const val FIRST_HOP_SELECTION_DEFAULT = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import net.nymtech.nymvpn.data.GatewayRepository
import net.nymtech.nymvpn.data.SecretsRepository
import net.nymtech.nymvpn.data.SettingsRepository
import net.nymtech.nymvpn.data.datastore.DataStoreGatewayRepository
import net.nymtech.nymvpn.data.datastore.DataStoreManager
import net.nymtech.nymvpn.data.datastore.DataStoreSettingsRepository
import net.nymtech.nymvpn.data.datastore.EncryptedPreferences
import net.nymtech.nymvpn.data.datastore.SecretsPreferencesRepository
import javax.inject.Singleton

@Module
Expand All @@ -37,16 +34,4 @@ class DataModule {
fun provideGatewayRepository(dataStoreManager: DataStoreManager): GatewayRepository {
return DataStoreGatewayRepository(dataStoreManager)
}

@Singleton
@Provides
fun provideEncryptedPreferences(@ApplicationContext context: Context): EncryptedPreferences {
return EncryptedPreferences(context)
}

@Singleton
@Provides
fun provideSecretsRepository(encryptedPreferences: EncryptedPreferences, @IoDispatcher dispatcher: CoroutineDispatcher): SecretsRepository {
return SecretsPreferencesRepository(encryptedPreferences, dispatcher)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package net.nymtech.nymvpn.service.vpn

import android.content.Context
import net.nymtech.nymvpn.NymVpn
import net.nymtech.nymvpn.data.SecretsRepository
import net.nymtech.nymvpn.data.SettingsRepository
import net.nymtech.vpn.VpnClient
import net.nymtech.vpn.util.InvalidCredentialException
import java.time.Instant
import javax.inject.Inject
import javax.inject.Provider

class NymVpnManager @Inject constructor(
private val settingsRepository: SettingsRepository,
private val secretsRepository: Provider<SecretsRepository>,
private val vpnClient: Provider<VpnClient>,
private val context: Context,
) : VpnManager {
Expand All @@ -23,16 +22,16 @@ class NymVpnManager @Inject constructor(
override suspend fun startVpn(foreground: Boolean): Result<Unit> {
val entryCountry = settingsRepository.getFirstHopCountry()
val exitCountry = settingsRepository.getLastHopCountry()
val credential = secretsRepository.get().getCredential()
val credentialExpiry = settingsRepository.getCredentialExpiry()
val mode = settingsRepository.getVpnMode()
return if (credential != null) {
return if (credentialExpiry != null && credentialExpiry.isAfter(Instant.now())) {
val entry = entryCountry.toEntryPoint()
val exit = exitCountry.toExitPoint()
return vpnClient.get().apply {
this.mode = mode
this.exitPoint = exit
this.entryPoint = entry
}.start(context, credential, true).also {
}.start(context, true).also {
NymVpn.requestTileServiceStateUpdate()
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ data class AppUiState(
val snackbarMessageConsumed: Boolean = true,
val vpnClientState: VpnClientState = VpnClientState(),
val settings: Settings = Settings(),
val isNonExpiredCredentialImported: Boolean = false,
val credentialExpiryTime: Instant? = null,
)
Loading
Loading