Skip to content
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 @@ -31,7 +31,12 @@ import com.masterdns.vpn.ui.components.mdv.controls.MdvBackTopAppBar
import com.masterdns.vpn.ui.theme.ConnectedGreen
import com.masterdns.vpn.ui.theme.MdvColor
import com.masterdns.vpn.ui.theme.MdvSpace
import com.masterdns.vpn.util.ResolverAnalyzer
import com.masterdns.vpn.util.ResolverImportResult
import com.masterdns.vpn.util.ResolverImportStats
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

private data class ImportedProfileDraft(
val profile: ProfileEntity,
Expand All @@ -49,7 +54,7 @@ fun ProfilesScreen(
var showEditor by remember { mutableStateOf(false) }
var editingProfile by remember { mutableStateOf<ProfileEntity?>(null) }
var importedDraft by remember { mutableStateOf<ImportedProfileDraft?>(null) }
var importedResolvers by remember { mutableStateOf<String?>(null) }
var importedResolvers by remember { mutableStateOf<ResolverImportResult?>(null) }
var profilePendingDelete by remember { mutableStateOf<ProfileEntity?>(null) }
val context = androidx.compose.ui.platform.LocalContext.current
val snackbarHostState = remember { SnackbarHostState() }
Expand All @@ -76,15 +81,23 @@ fun ProfilesScreen(
ActivityResultContracts.OpenDocument()
) { uri ->
if (uri == null) return@rememberLauncherForActivityResult
val text = readTextFromUri(context, uri).trim()
if (text.isBlank()) {
scope.launch { snackbarHostState.showSnackbar(context.getString(R.string.profiles_resolvers_empty_msg)) }
return@rememberLauncherForActivityResult
scope.launch {
val fileName = readDisplayName(context, uri) ?: "client_resolvers.txt"
val text = withContext(Dispatchers.IO) { readTextFromUri(context, uri) }
val result = withContext(Dispatchers.Default) {
ResolverAnalyzer.analyzeAndNormalize(text, fileName)
}
if (result == null || result.normalizedText.isBlank()) {
snackbarHostState.showSnackbar(context.getString(R.string.profiles_resolvers_empty_msg))
return@launch
}
importedResolvers = result
editingProfile = null
showEditor = true
snackbarHostState.showSnackbar(
context.getString(R.string.profiles_resolvers_imported_stats_msg, result.stats.summary())
)
}
importedResolvers = text
editingProfile = null
showEditor = true
scope.launch { snackbarHostState.showSnackbar(context.getString(R.string.profiles_resolvers_imported_msg)) }
}

Scaffold(
Expand Down Expand Up @@ -303,7 +316,7 @@ fun ProfileCard(
private fun ProfileEditorDialog(
profile: ProfileEntity?,
importedDraft: ImportedProfileDraft?,
importedResolvers: String?,
importedResolvers: ResolverImportResult?,
onImportToml: () -> Unit,
onImportResolvers: () -> Unit,
onSave: (ProfileEntity) -> Unit,
Expand Down Expand Up @@ -365,8 +378,8 @@ private fun ProfileEditorDialog(
encryptionKey = importedProfile.encryptionKey
resolvers = importedProfile.resolvers
}
if (!importedResolvers.isNullOrBlank()) {
resolvers = importedResolvers
if (importedResolvers != null) {
resolvers = importedResolvers.normalizedText
}
validationMessage = null
}
Expand Down Expand Up @@ -573,6 +586,9 @@ private fun ProfileEditorDialog(
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
)
}
importedResolvers?.stats?.let { stats ->
ResolverImportStatsCard(stats)
}
validationMessage?.let { message ->
Text(
text = message,
Expand Down Expand Up @@ -603,13 +619,18 @@ private fun ProfileEditorDialog(
}
val baseProfile = profile ?: importedDraft?.profile ?: ProfileEntity(name = "", domains = "")
val domainJson = gson.toJson(finalDomainList)
val updatedProfile = baseProfile.copy(
name = name.trim(),
domains = domainJson,
encryptionKey = encryptionKey.trim(),
resolvers = resolvers.trim()
)
onSave(
baseProfile.copy(
name = name.trim(),
domains = domainJson,
encryptionKey = encryptionKey.trim(),
resolvers = resolvers.trim()
)
if (importedResolvers != null) {
ResolverAnalyzer.withImportStats(updatedProfile, importedResolvers.stats)
} else {
updatedProfile
}
)
}
) {
Expand All @@ -624,6 +645,46 @@ private fun ProfileEditorDialog(
)
}

@Composable
private fun ResolverImportStatsCard(stats: ResolverImportStats) {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(containerColor = MdvColor.SurfaceHigh)
) {
Column(
modifier = Modifier.padding(10.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
Text(
stringResource(R.string.profiles_resolvers_import_preview_title),
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.SemiBold
)
Text(
stringResource(
R.string.profiles_resolvers_import_preview_body,
stats.uniqueResolvers,
stats.duplicateResolvers,
stats.invalidLines,
stats.cidrRanges
),
style = MaterialTheme.typography.bodySmall,
color = MdvColor.OnSurfaceVariant
)
if (stats.truncated || stats.skippedCidrRanges > 0) {
Text(
stringResource(
R.string.profiles_resolvers_import_preview_limits,
stats.skippedCidrRanges
),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.error
)
}
}
}
}

private val gson = Gson()

private fun readTextFromUri(context: Context, uri: Uri): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.masterdns.vpn.ui.settings
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.OpenableColumns
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -72,7 +73,10 @@ import com.masterdns.vpn.ui.components.mdv.controls.MdvBackTopAppBar
import com.masterdns.vpn.ui.components.mdv.controls.MdvTopAppBar
import com.masterdns.vpn.ui.theme.MdvColor
import com.masterdns.vpn.ui.theme.MdvSpace
import com.masterdns.vpn.util.ResolverAnalyzer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

private enum class FieldType { TEXT, BOOL, OPTION }

Expand Down Expand Up @@ -281,8 +285,20 @@ fun SettingsScreen(
val selected = profile
if (uri != null && selected != null) {
val text = readTextFromUri(context, uri)
viewModel.importResolvers(selected, text)
scope.launch { snackbarHostState.showSnackbar(context.getString(R.string.settings_resolvers_imported_msg)) }
val fileName = readDisplayName(context, uri) ?: "client_resolvers.txt"
scope.launch {
val result = withContext(Dispatchers.Default) {
ResolverAnalyzer.analyzeAndNormalize(text, fileName)
}
if (result == null || result.normalizedText.isBlank()) {
snackbarHostState.showSnackbar(context.getString(R.string.settings_resolvers_empty_msg))
return@launch
}
viewModel.importResolvers(selected, result)
snackbarHostState.showSnackbar(
context.getString(R.string.settings_resolvers_imported_stats_msg, result.stats.summary())
)
}
}
}
val pickMtuExportLauncher = rememberLauncherForActivityResult(
Expand Down Expand Up @@ -603,6 +619,14 @@ private fun readTextFromUri(context: Context, uri: Uri): String {
}.getOrDefault("")
}

private fun readDisplayName(context: Context, uri: Uri): String? {
return context.contentResolver.query(uri, null, null, null, null)?.use { cursor ->
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
if (nameIndex < 0 || !cursor.moveToFirst()) return@use null
cursor.getString(nameIndex)
}?.trim()?.takeIf { it.isNotEmpty() }
}

private fun writeTextToUri(context: Context, uri: Uri, content: String) {
runCatching {
context.contentResolver.openOutputStream(uri, "w")?.bufferedWriter()?.use {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.google.gson.reflect.TypeToken
import com.masterdns.vpn.data.local.ProfileEntity
import com.masterdns.vpn.data.repository.ProfileRepository
import com.masterdns.vpn.util.ConfigGenerator
import com.masterdns.vpn.util.ResolverAnalyzer
import com.masterdns.vpn.util.ResolverImportResult
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -73,9 +75,10 @@ class SettingsViewModel @Inject constructor(
return result
}

fun importResolvers(profile: ProfileEntity, resolversText: String) {
fun importResolvers(profile: ProfileEntity, result: ResolverImportResult) {
viewModelScope.launch {
profileRepository.updateProfile(profile.copy(resolvers = resolversText.trim()))
val updated = profile.copy(resolvers = result.normalizedText.trim())
profileRepository.updateProfile(ResolverAnalyzer.withImportStats(updated, result.stats))
}
}

Expand Down
Loading
Loading