refactor: вынос ProxyOrchestrator и UI-полировка MD3
- вынос дублирующейся логики restart/start прокси и сервера из ServerViewModel и SettingsViewModel в общий ProxyOrchestrator - переход на Koin viewModelOf для ViewModel - новые сеттеры клиентского конфига в SettingsViewModel (provider, dnsMode, useUdp, manualCaptcha, useCarrierDns, debugMode, magicSwitch) - MD3: pinnedScrollBehavior у TopAppBar на всех экранах, ограничение ширины контента 840dp, скроллируемый онбординг - локализация строк уведомления и QS-тайла через ресурсы - правки текстов strings.xml (WireGuard/XRay, переименование obf_key_regen) - троттлинг опроса трафика 1с -> 3с - переименование нативной библиотеки в libfreeturn.so
This commit is contained in:
1 parent
7f683bb3f4
commit
c242647588
15 files changed
+252
-177
No files matched your search
@@ -276,7 +276,7 @@ class ProxyService : Service() {
|
||||
ConnectionStats(udpActive, udpTotal)
|
||||
}
|
||||
ProxyServiceState.setConnectionStats(stats)
|
||||
if (currentBaseStatus == "Прокси активен") {
|
||||
if (currentBaseStatus == getString(R.string.proxy_active)) {
|
||||
buildAndShowNotification()
|
||||
}
|
||||
}
|
||||
@@ -415,7 +415,7 @@ class ProxyService : Service() {
|
||||
hasConnection -> {
|
||||
ProxyServiceState.setStartupResult(StartupResult.Success)
|
||||
ProxyServiceState.markConnectedIfAbsent(SystemClock.elapsedRealtime())
|
||||
updateNotification("Прокси активен")
|
||||
updateNotification(getString(R.string.proxy_active))
|
||||
startupEmitted = true
|
||||
}
|
||||
}
|
||||
@@ -611,7 +611,7 @@ class ProxyService : Service() {
|
||||
var lastRx = android.net.TrafficStats.getUidRxBytes(uid)
|
||||
var lastTx = android.net.TrafficStats.getUidTxBytes(uid)
|
||||
while (!userStopped.get()) {
|
||||
kotlinx.coroutines.delay(1000)
|
||||
kotlinx.coroutines.delay(3000)
|
||||
val currentRx = android.net.TrafficStats.getUidRxBytes(uid)
|
||||
val currentTx = android.net.TrafficStats.getUidTxBytes(uid)
|
||||
|
||||
@@ -621,7 +621,7 @@ class ProxyService : Service() {
|
||||
currentSpeedText = "↓ ${formatSpeed(rxSpeed)} ↑ ${formatSpeed(txSpeed)}"
|
||||
lastRx = currentRx
|
||||
lastTx = currentTx
|
||||
if (currentBaseStatus == "Прокси активен") {
|
||||
if (currentBaseStatus == getString(R.string.proxy_active)) {
|
||||
buildAndShowNotification()
|
||||
}
|
||||
}
|
||||
@@ -633,7 +633,7 @@ class ProxyService : Service() {
|
||||
var text = currentBaseStatus
|
||||
val stats = ProxyServiceState.connectionStats.value
|
||||
|
||||
if (currentBaseStatus == "Прокси активен" || currentBaseStatus == getString(R.string.proxy_active)) {
|
||||
if (currentBaseStatus == getString(R.string.proxy_active)) {
|
||||
if (stats.total > 0) {
|
||||
text = String.format(
|
||||
java.util.Locale.US,
|
||||
|
||||
@@ -50,11 +50,9 @@ class ProxyTileService : TileService() {
|
||||
|
||||
private fun updateTileState() {
|
||||
val tile = qsTile ?: return
|
||||
if (isProxyRunning) {
|
||||
tile.state = Tile.STATE_ACTIVE
|
||||
} else {
|
||||
tile.state = Tile.STATE_INACTIVE
|
||||
}
|
||||
tile.state = if (isProxyRunning) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
|
||||
tile.label = getString(R.string.tile_service_label)
|
||||
tile.contentDescription = getString(R.string.tile_service_label)
|
||||
tile.icon = android.graphics.drawable.Icon.createWithResource(this, R.drawable.ic_qs_tile_nearby)
|
||||
tile.updateTile()
|
||||
}
|
||||
|
||||
@@ -3,12 +3,13 @@ package com.freeturn.app.di
|
||||
import com.freeturn.app.data.AppPreferences
|
||||
import com.freeturn.app.domain.AppUpdater
|
||||
import com.freeturn.app.domain.LocalProxyManager
|
||||
import com.freeturn.app.domain.ProxyOrchestrator
|
||||
import com.freeturn.app.domain.SshRepository
|
||||
import com.freeturn.app.viewmodel.ProxyViewModel
|
||||
import com.freeturn.app.viewmodel.ServerViewModel
|
||||
import com.freeturn.app.viewmodel.SettingsViewModel
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.androidx.viewmodel.dsl.viewModel
|
||||
import org.koin.androidx.viewmodel.dsl.viewModelOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
val appModule = module {
|
||||
@@ -16,8 +17,9 @@ val appModule = module {
|
||||
single { LocalProxyManager(androidContext()) }
|
||||
single { SshRepository(androidContext()) }
|
||||
single { AppUpdater(androidContext()) }
|
||||
single { ProxyOrchestrator(get(), get(), get()) }
|
||||
|
||||
viewModel { ProxyViewModel(get(), get()) }
|
||||
viewModel { ServerViewModel(get(), get(), get(), androidContext()) }
|
||||
viewModel { SettingsViewModel(get(), get(), get(), get(), androidContext()) }
|
||||
viewModelOf(::ProxyViewModel)
|
||||
viewModelOf(::ServerViewModel)
|
||||
viewModelOf(::SettingsViewModel)
|
||||
}
|
||||
@@ -22,6 +22,11 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* Управляет жизненным циклом прокси-сервиса: старт, стоп, отслеживание состояния.
|
||||
* Создаётся как Koin `single` и живёт в Application scope — `destroy()` не вызывается
|
||||
* намеренно, так как scope привязан к процессу приложения.
|
||||
*/
|
||||
class LocalProxyManager(private val context: Context) {
|
||||
|
||||
private val _proxyState = MutableStateFlow<ProxyState>(ProxyState.Idle)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.freeturn.app.domain
|
||||
|
||||
import com.freeturn.app.ProxyServiceState
|
||||
import com.freeturn.app.data.AppPreferences
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
import com.freeturn.app.viewmodel.ServerState
|
||||
|
||||
class ProxyOrchestrator(
|
||||
private val prefs: AppPreferences,
|
||||
private val proxyManager: LocalProxyManager,
|
||||
private val sshRepository: SshRepository
|
||||
) {
|
||||
suspend fun restartServerIfRunning() {
|
||||
val running = (sshRepository.serverState.value as? ServerState.Known)?.running == true
|
||||
if (!running) return
|
||||
val l = prefs.proxyListenFlow.first()
|
||||
val c = prefs.proxyConnectFlow.first()
|
||||
if (!l.matches(Regex("""^[\w.\-]+:\d{1,5}$""")) ||
|
||||
!c.matches(Regex("""^[\w.\-]+:\d{1,5}$"""))) return
|
||||
val opts = prefs.serverOptsFlow.first()
|
||||
val tcpMode = prefs.clientConfigFlow.first().tcpForward
|
||||
sshRepository.stopServer()
|
||||
sshRepository.startServer(
|
||||
listen = l, connect = c,
|
||||
tcpMode = tcpMode,
|
||||
obfProfile = if (opts.obfEnabled) opts.obfProfile else "none",
|
||||
obfKey = if (opts.obfEnabled) opts.obfKey else ""
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun restartProxyIfRunning() {
|
||||
if (!ProxyServiceState.isRunning.value) return
|
||||
proxyManager.stopProxy()
|
||||
withTimeoutOrNull(2_000) {
|
||||
ProxyServiceState.isRunning.first { !it }
|
||||
}
|
||||
proxyManager.startProxy(prefs.clientConfigFlow.first())
|
||||
}
|
||||
|
||||
suspend fun startServerFromPrefs() {
|
||||
val l = prefs.proxyListenFlow.first()
|
||||
val c = prefs.proxyConnectFlow.first()
|
||||
val tcpMode = prefs.clientConfigFlow.first().tcpForward
|
||||
val opts = prefs.serverOptsFlow.first()
|
||||
sshRepository.startServer(
|
||||
listen = l, connect = c,
|
||||
tcpMode = tcpMode,
|
||||
obfProfile = if (opts.obfEnabled) opts.obfProfile else "none",
|
||||
obfKey = if (opts.obfEnabled) opts.obfKey else ""
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ import androidx.compose.material3.Slider
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
@@ -47,6 +48,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
@@ -90,24 +92,16 @@ fun ClientSetupScreen(
|
||||
// (если запущен), иначе сохранённое. В !sync режиме — всегда сохранённое:
|
||||
// серверная и клиентская стороны могут различаться, и UI отражает клиента.
|
||||
val syncOn = saved.syncServerSwitches
|
||||
val effectiveTcpForward = if (syncOn) serverKnown?.tcpMode ?: saved.tcpForward else saved.tcpForward
|
||||
val effectiveObfProfile = if (syncOn) serverKnown?.obfProfile ?: serverOpts.obfProfile else serverOpts.obfProfile
|
||||
val effectiveTcpForward = if (syncOn && serverKnown?.running == true) serverKnown.tcpMode ?: saved.tcpForward else saved.tcpForward
|
||||
val effectiveObfProfile = if (syncOn && serverKnown?.running == true) serverKnown.obfProfile ?: serverOpts.obfProfile else serverOpts.obfProfile
|
||||
|
||||
val context = LocalContext.current
|
||||
|
||||
var serverAddress by rememberSaveable(saved.serverAddress) { mutableStateOf(saved.serverAddress) }
|
||||
var vkLink by rememberSaveable(saved.vkLink) { mutableStateOf(saved.vkLink) }
|
||||
var provider by rememberSaveable(saved.provider) { mutableStateOf(saved.provider) }
|
||||
var threads by rememberSaveable(saved.threads) { mutableFloatStateOf(saved.threads.toFloat()) }
|
||||
var streamsPerCred by rememberSaveable(saved.streamsPerCred) { mutableFloatStateOf(saved.streamsPerCred.toFloat()) }
|
||||
var useUdp by rememberSaveable(saved.useUdp) { mutableStateOf(saved.useUdp) }
|
||||
var manualCaptcha by rememberSaveable(saved.manualCaptcha) { mutableStateOf(saved.manualCaptcha) }
|
||||
var useCarrierDns by rememberSaveable(saved.useCarrierDns) { mutableStateOf(saved.useCarrierDns) }
|
||||
var dnsMode by rememberSaveable(saved.dnsMode) { mutableStateOf(saved.dnsMode) }
|
||||
var localPort by rememberSaveable(saved.localPort) { mutableStateOf(saved.localPort) }
|
||||
|
||||
var debugMode by rememberSaveable(saved.debugMode) { mutableStateOf(saved.debugMode) }
|
||||
var magicSwitch by rememberSaveable(saved.magicSwitch) { mutableStateOf(saved.magicSwitch) }
|
||||
var magicTurn by rememberSaveable(saved.magicTurn) { mutableStateOf(saved.magicTurn) }
|
||||
var lastSliderInt by rememberSaveable { mutableIntStateOf(saved.threads) }
|
||||
var lastStreamsInt by rememberSaveable { mutableIntStateOf(saved.streamsPerCred) }
|
||||
@@ -120,36 +114,31 @@ fun ClientSetupScreen(
|
||||
}
|
||||
}
|
||||
|
||||
// Авто-сохранение с дебаунсом 600 мс на каждое изменение поля.
|
||||
// tcpForward/bond исключены — сохраняются через setTcpForward/setBond с автоперезапуском.
|
||||
LaunchedEffect(serverAddress, vkLink, provider, threads, streamsPerCred, useUdp, manualCaptcha, useCarrierDns, localPort, dnsMode, debugMode, magicSwitch, magicTurn) {
|
||||
// Авто-сохранение с дебаунсом 600 мс для текстовых полей и ползунков
|
||||
LaunchedEffect(serverAddress, vkLink, threads, streamsPerCred, localPort, magicTurn) {
|
||||
delay(600)
|
||||
val current = settingsViewModel.clientConfig.value
|
||||
settingsViewModel.saveClientConfig(
|
||||
ClientConfig(
|
||||
current.copy(
|
||||
serverAddress = serverAddress.trim(),
|
||||
vkLink = vkLink.trim(),
|
||||
provider = provider,
|
||||
threads = threads.roundToInt(),
|
||||
streamsPerCred = streamsPerCred.roundToInt(),
|
||||
useUdp = useUdp,
|
||||
manualCaptcha = manualCaptcha,
|
||||
localPort = localPort.trim(),
|
||||
tcpForward = saved.tcpForward,
|
||||
bond = saved.bond,
|
||||
|
||||
debugMode = debugMode,
|
||||
useCarrierDns = useCarrierDns,
|
||||
dnsMode = dnsMode,
|
||||
syncServerSwitches = saved.syncServerSwitches,
|
||||
magicSwitch = magicSwitch,
|
||||
magicTurn = magicTurn.trim()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
|
||||
|
||||
Scaffold(
|
||||
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
|
||||
topBar = {
|
||||
TopAppBar(title = { Text(stringResource(R.string.client_title)) })
|
||||
TopAppBar(
|
||||
title = { Text(stringResource(R.string.client_title)) },
|
||||
scrollBehavior = scrollBehavior
|
||||
)
|
||||
},
|
||||
contentWindowInsets = WindowInsets(0, 0, 0, 0)
|
||||
) { padding ->
|
||||
@@ -190,10 +179,10 @@ fun ClientSetupScreen(
|
||||
SingleChoiceSegmentedButtonRow(modifier = Modifier.fillMaxWidth()) {
|
||||
Provider.ALL.forEachIndexed { idx, value ->
|
||||
SegmentedButton(
|
||||
selected = provider == value,
|
||||
selected = saved.provider == value,
|
||||
onClick = {
|
||||
HapticUtil.perform(context, HapticUtil.Pattern.TOGGLE_ON)
|
||||
provider = value
|
||||
settingsViewModel.setProvider(value)
|
||||
},
|
||||
shape = SegmentedButtonDefaults.itemShape(index = idx, count = Provider.ALL.size)
|
||||
) { Text(providerLabel(value)) }
|
||||
@@ -201,7 +190,7 @@ fun ClientSetupScreen(
|
||||
}
|
||||
}
|
||||
|
||||
if (provider == Provider.VK) {
|
||||
if (saved.provider == Provider.VK) {
|
||||
OutlinedTextField(
|
||||
value = vkLink.redact(privacyMode),
|
||||
onValueChange = { if (!privacyMode) vkLink = it },
|
||||
@@ -303,10 +292,10 @@ fun ClientSetupScreen(
|
||||
SingleChoiceSegmentedButtonRow(modifier = Modifier.fillMaxWidth()) {
|
||||
dnsOptions.forEachIndexed { idx, (value, label) ->
|
||||
SegmentedButton(
|
||||
selected = dnsMode == value,
|
||||
selected = saved.dnsMode == value,
|
||||
onClick = {
|
||||
HapticUtil.perform(context, HapticUtil.Pattern.TOGGLE_ON)
|
||||
dnsMode = value
|
||||
settingsViewModel.setDnsMode(value)
|
||||
},
|
||||
shape = SegmentedButtonDefaults.itemShape(index = idx, count = dnsOptions.size)
|
||||
) { Text(label) }
|
||||
@@ -326,18 +315,18 @@ fun ClientSetupScreen(
|
||||
}
|
||||
SingleChoiceSegmentedButtonRow(modifier = Modifier.fillMaxWidth()) {
|
||||
SegmentedButton(
|
||||
selected = !useUdp,
|
||||
selected = !saved.useUdp,
|
||||
onClick = {
|
||||
HapticUtil.perform(context, HapticUtil.Pattern.TOGGLE_ON)
|
||||
useUdp = false
|
||||
settingsViewModel.setUseUdp(false)
|
||||
},
|
||||
shape = SegmentedButtonDefaults.itemShape(index = 0, count = 2)
|
||||
) { Text(stringResource(R.string.tcp)) }
|
||||
SegmentedButton(
|
||||
selected = useUdp,
|
||||
selected = saved.useUdp,
|
||||
onClick = {
|
||||
HapticUtil.perform(context, HapticUtil.Pattern.TOGGLE_ON)
|
||||
useUdp = true
|
||||
settingsViewModel.setUseUdp(true)
|
||||
},
|
||||
shape = SegmentedButtonDefaults.itemShape(index = 1, count = 2)
|
||||
) { Text(stringResource(R.string.udp)) }
|
||||
@@ -349,44 +338,44 @@ fun ClientSetupScreen(
|
||||
SwitchRow(
|
||||
label = stringResource(R.string.manual_captcha),
|
||||
description = stringResource(R.string.manual_captcha_desc),
|
||||
checked = manualCaptcha,
|
||||
checked = saved.manualCaptcha,
|
||||
onCheckedChange = {
|
||||
HapticUtil.perform(context, if (it) HapticUtil.Pattern.TOGGLE_ON else HapticUtil.Pattern.TOGGLE_OFF)
|
||||
manualCaptcha = it
|
||||
settingsViewModel.setManualCaptcha(it)
|
||||
}
|
||||
)
|
||||
|
||||
SwitchRow(
|
||||
label = stringResource(R.string.use_carrier_dns),
|
||||
description = stringResource(R.string.use_carrier_dns_desc),
|
||||
checked = useCarrierDns,
|
||||
checked = saved.useCarrierDns,
|
||||
onCheckedChange = {
|
||||
HapticUtil.perform(context, if (it) HapticUtil.Pattern.TOGGLE_ON else HapticUtil.Pattern.TOGGLE_OFF)
|
||||
useCarrierDns = it
|
||||
settingsViewModel.setUseCarrierDns(it)
|
||||
}
|
||||
)
|
||||
|
||||
SwitchRow(
|
||||
label = stringResource(R.string.debug_mode),
|
||||
description = stringResource(R.string.debug_mode_desc),
|
||||
checked = debugMode,
|
||||
checked = saved.debugMode,
|
||||
onCheckedChange = {
|
||||
HapticUtil.perform(context, if (it) HapticUtil.Pattern.TOGGLE_ON else HapticUtil.Pattern.TOGGLE_OFF)
|
||||
debugMode = it
|
||||
settingsViewModel.setDebugMode(it)
|
||||
}
|
||||
)
|
||||
|
||||
SwitchRow(
|
||||
label = stringResource(R.string.magic_switch),
|
||||
description = stringResource(R.string.magic_switch_desc),
|
||||
checked = magicSwitch,
|
||||
checked = saved.magicSwitch,
|
||||
onCheckedChange = {
|
||||
HapticUtil.perform(context, if (it) HapticUtil.Pattern.TOGGLE_ON else HapticUtil.Pattern.TOGGLE_OFF)
|
||||
magicSwitch = it
|
||||
settingsViewModel.setMagicSwitch(it)
|
||||
}
|
||||
)
|
||||
|
||||
if (magicSwitch) {
|
||||
if (saved.magicSwitch) {
|
||||
OutlinedTextField(
|
||||
value = magicTurn.redact(privacyMode),
|
||||
onValueChange = { if (!privacyMode) magicTurn = it },
|
||||
@@ -511,11 +500,11 @@ fun ClientSetupScreen(
|
||||
supportingText = {
|
||||
when {
|
||||
obfKeyDraft.isBlank() -> Text(
|
||||
stringResource(R.string.wrap_key_empty_hint),
|
||||
stringResource(R.string.obf_key_empty_hint),
|
||||
color = MaterialTheme.colorScheme.error
|
||||
)
|
||||
!draftValid -> Text(
|
||||
stringResource(R.string.wrap_key_invalid_hint),
|
||||
stringResource(R.string.obf_key_invalid_hint),
|
||||
color = MaterialTheme.colorScheme.error
|
||||
)
|
||||
}
|
||||
@@ -530,7 +519,7 @@ fun ClientSetupScreen(
|
||||
enabled = draftValid,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text(stringResource(R.string.wrap_key_apply))
|
||||
Text(stringResource(R.string.obf_key_apply))
|
||||
}
|
||||
}
|
||||
if (isSshConnected) {
|
||||
@@ -548,9 +537,9 @@ fun ClientSetupScreen(
|
||||
modifier = Modifier.size(20.dp)
|
||||
)
|
||||
Spacer(Modifier.size(8.dp))
|
||||
Text(stringResource(R.string.server_wrap_regen_in_progress))
|
||||
Text(stringResource(R.string.obf_key_regen_in_progress))
|
||||
} else {
|
||||
Text(stringResource(R.string.server_wrap_regen))
|
||||
Text(stringResource(R.string.obf_key_regen))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.material3.rememberModalBottomSheetState
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
@@ -75,6 +76,7 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.draw.scale
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.semantics.contentDescription
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
@@ -615,8 +617,8 @@ private fun InfoBottomSheet(
|
||||
item {
|
||||
RepoLinkItem(
|
||||
title = stringResource(R.string.proxy_core),
|
||||
subtitle = "moroka8/vk-turn-proxy",
|
||||
url = "https://github.com/moroka8/vk-turn-proxy",
|
||||
subtitle = "samosvalishe/free-turn-proxy",
|
||||
url = "https://github.com/samosvalishe/free-turn-proxy",
|
||||
containerColor = containerColor,
|
||||
onHaptic = { HapticUtil.perform(context, HapticUtil.Pattern.SELECTION) },
|
||||
onOpen = { uriHandler.openUri(it) }
|
||||
|
||||
@@ -24,11 +24,13 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
@@ -51,10 +53,14 @@ fun LogsScreen(proxyViewModel: ProxyViewModel) {
|
||||
if (logs.isNotEmpty()) listState.animateScrollToItem(logs.lastIndex)
|
||||
}
|
||||
|
||||
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
|
||||
|
||||
Scaffold(
|
||||
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = { Text(stringResource(R.string.logs_title)) },
|
||||
scrollBehavior = scrollBehavior,
|
||||
actions = {
|
||||
IconButton(
|
||||
onClick = {
|
||||
|
||||
@@ -17,8 +17,12 @@ import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import com.freeturn.app.R
|
||||
import androidx.compose.material3.Button
|
||||
@@ -57,7 +61,7 @@ fun OnboardingScreen(
|
||||
label = "pulse"
|
||||
)
|
||||
|
||||
Box(
|
||||
BoxWithConstraints(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(
|
||||
@@ -69,16 +73,22 @@ fun OnboardingScreen(
|
||||
)
|
||||
)
|
||||
) {
|
||||
val minHeight = maxHeight
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.fillMaxWidth()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(32.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Spacer(Modifier.height(56.dp))
|
||||
Column(
|
||||
modifier = Modifier.heightIn(min = minHeight - 64.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Spacer(Modifier.height(16.dp))
|
||||
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.size(148.dp)
|
||||
@@ -168,6 +178,7 @@ fun OnboardingScreen(
|
||||
|
||||
Spacer(Modifier.height(24.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@ import androidx.compose.material3.SnackbarHost
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
@@ -58,6 +59,7 @@ import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
@@ -109,11 +111,14 @@ fun ServerManagementScreen(
|
||||
val isConnected = sshState is SshConnectionState.Connected
|
||||
val isWorking = serverState is ServerState.Working || serverState is ServerState.Checking
|
||||
val serverKnown = serverState as? ServerState.Known
|
||||
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
|
||||
|
||||
Scaffold(
|
||||
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = { Text(stringResource(R.string.server)) },
|
||||
scrollBehavior = scrollBehavior,
|
||||
actions = {
|
||||
SshStatusBadge(sshState = sshState, ip = sshConfig.ip.redact(privacyMode))
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import androidx.compose.foundation.layout.imePadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
@@ -33,6 +34,7 @@ import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.freeturn.app.R
|
||||
@@ -45,6 +47,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||
@@ -56,12 +59,13 @@ import com.freeturn.app.data.SshConfig
|
||||
import com.freeturn.app.ui.HapticUtil
|
||||
import com.freeturn.app.viewmodel.ServerViewModel
|
||||
import com.freeturn.app.viewmodel.SshConnectionState
|
||||
import com.freeturn.app.viewmodel.SettingsViewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun SshSetupScreen(
|
||||
serverViewModel: ServerViewModel,
|
||||
settingsViewModel: com.freeturn.app.viewmodel.SettingsViewModel,
|
||||
settingsViewModel: SettingsViewModel,
|
||||
onConnected: () -> Unit,
|
||||
onBack: () -> Unit
|
||||
) {
|
||||
@@ -88,11 +92,14 @@ fun SshSetupScreen(
|
||||
|
||||
val isConnecting = sshState is SshConnectionState.Connecting
|
||||
val context = LocalContext.current
|
||||
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
|
||||
|
||||
Scaffold(
|
||||
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = { Text(stringResource(R.string.connect_to_server)) },
|
||||
scrollBehavior = scrollBehavior,
|
||||
navigationIcon = {
|
||||
IconButton(onClick = {
|
||||
HapticUtil.perform(context, HapticUtil.Pattern.SELECTION)
|
||||
@@ -110,10 +117,16 @@ fun SshSetupScreen(
|
||||
.fillMaxSize()
|
||||
.padding(padding)
|
||||
.imePadding()
|
||||
.padding(horizontal = 24.dp)
|
||||
.verticalScroll(rememberScrollState()),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.widthIn(max = 840.dp)
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 24.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
Spacer(Modifier.height(4.dp))
|
||||
|
||||
if (!isConnecting) {
|
||||
@@ -147,6 +160,7 @@ fun SshSetupScreen(
|
||||
}
|
||||
|
||||
Spacer(Modifier.height(24.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import androidx.lifecycle.viewModelScope
|
||||
import com.freeturn.app.ProxyServiceState
|
||||
import com.freeturn.app.data.AppPreferences
|
||||
import com.freeturn.app.data.SshConfig
|
||||
import com.freeturn.app.domain.LocalProxyManager
|
||||
import com.freeturn.app.domain.ProxyOrchestrator
|
||||
import com.freeturn.app.domain.SshRepository
|
||||
import com.freeturn.app.ui.HapticUtil
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -20,8 +20,8 @@ import kotlinx.coroutines.withTimeoutOrNull
|
||||
|
||||
class ServerViewModel(
|
||||
private val sshRepository: SshRepository,
|
||||
private val proxyManager: LocalProxyManager,
|
||||
private val prefs: AppPreferences,
|
||||
private val orchestrator: ProxyOrchestrator,
|
||||
private val context: Context
|
||||
) : ViewModel() {
|
||||
|
||||
@@ -83,7 +83,7 @@ class ServerViewModel(
|
||||
}
|
||||
if (outcome.needsRestart) {
|
||||
startServer()
|
||||
restartProxyIfRunning()
|
||||
orchestrator.restartProxyIfRunning()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,39 +133,11 @@ class ServerViewModel(
|
||||
prefs.saveServerOpts(next)
|
||||
|
||||
val clientConfig = prefs.clientConfigFlow.first()
|
||||
if (clientConfig.syncServerSwitches) restartServerIfRunning()
|
||||
restartProxyIfRunning()
|
||||
if (clientConfig.syncServerSwitches) orchestrator.restartServerIfRunning()
|
||||
orchestrator.restartProxyIfRunning()
|
||||
} finally {
|
||||
_isRegeneratingObfKey.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun restartServerIfRunning() {
|
||||
val running = (serverState.value as? ServerState.Known)?.running == true
|
||||
if (!running) return
|
||||
val l = prefs.proxyListenFlow.first()
|
||||
val c = prefs.proxyConnectFlow.first()
|
||||
if (!l.matches(Regex("""^[\w.\-]+:\d{1,5}$""")) ||
|
||||
!c.matches(Regex("""^[\w.\-]+:\d{1,5}$"""))) return
|
||||
val opts = prefs.serverOptsFlow.first()
|
||||
val tcpMode = prefs.clientConfigFlow.first().tcpForward
|
||||
sshRepository.stopServer()
|
||||
sshRepository.startServer(
|
||||
listen = l, connect = c,
|
||||
tcpMode = tcpMode,
|
||||
obfProfile = if (opts.obfEnabled) opts.obfProfile else "none",
|
||||
obfKey = if (opts.obfEnabled) opts.obfKey else ""
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun restartProxyIfRunning() {
|
||||
if (!ProxyServiceState.isRunning.value) return
|
||||
proxyManager.stopProxy()
|
||||
withTimeoutOrNull(2_000) {
|
||||
ProxyServiceState.isRunning.first { !it }
|
||||
}
|
||||
val clientConfig = prefs.clientConfigFlow.first()
|
||||
proxyManager.startProxy(clientConfig)
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import com.freeturn.app.data.ProfilesSnapshot
|
||||
import com.freeturn.app.data.SshConfig
|
||||
import com.freeturn.app.domain.AppUpdater
|
||||
import com.freeturn.app.domain.LocalProxyManager
|
||||
import com.freeturn.app.domain.ProxyOrchestrator
|
||||
import com.freeturn.app.domain.SshRepository
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
@@ -32,6 +33,7 @@ class SettingsViewModel(
|
||||
private val proxyManager: LocalProxyManager,
|
||||
private val sshRepository: SshRepository,
|
||||
private val appUpdater: AppUpdater,
|
||||
private val orchestrator: ProxyOrchestrator,
|
||||
private val context: Context
|
||||
) : ViewModel() {
|
||||
|
||||
@@ -105,6 +107,62 @@ class SettingsViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
fun setProvider(value: String) {
|
||||
viewModelScope.launch {
|
||||
val current = prefs.clientConfigFlow.first()
|
||||
if (current.provider == value) return@launch
|
||||
profileMutex.withLock { prefs.saveClientConfig(current.copy(provider = value)) }
|
||||
}
|
||||
}
|
||||
|
||||
fun setDnsMode(value: String) {
|
||||
viewModelScope.launch {
|
||||
val current = prefs.clientConfigFlow.first()
|
||||
if (current.dnsMode == value) return@launch
|
||||
profileMutex.withLock { prefs.saveClientConfig(current.copy(dnsMode = value)) }
|
||||
}
|
||||
}
|
||||
|
||||
fun setUseUdp(enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
val current = prefs.clientConfigFlow.first()
|
||||
if (current.useUdp == enabled) return@launch
|
||||
profileMutex.withLock { prefs.saveClientConfig(current.copy(useUdp = enabled)) }
|
||||
}
|
||||
}
|
||||
|
||||
fun setManualCaptcha(enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
val current = prefs.clientConfigFlow.first()
|
||||
if (current.manualCaptcha == enabled) return@launch
|
||||
profileMutex.withLock { prefs.saveClientConfig(current.copy(manualCaptcha = enabled)) }
|
||||
}
|
||||
}
|
||||
|
||||
fun setUseCarrierDns(enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
val current = prefs.clientConfigFlow.first()
|
||||
if (current.useCarrierDns == enabled) return@launch
|
||||
profileMutex.withLock { prefs.saveClientConfig(current.copy(useCarrierDns = enabled)) }
|
||||
}
|
||||
}
|
||||
|
||||
fun setDebugMode(enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
val current = prefs.clientConfigFlow.first()
|
||||
if (current.debugMode == enabled) return@launch
|
||||
profileMutex.withLock { prefs.saveClientConfig(current.copy(debugMode = enabled)) }
|
||||
}
|
||||
}
|
||||
|
||||
fun setMagicSwitch(enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
val current = prefs.clientConfigFlow.first()
|
||||
if (current.magicSwitch == enabled) return@launch
|
||||
profileMutex.withLock { prefs.saveClientConfig(current.copy(magicSwitch = enabled)) }
|
||||
}
|
||||
}
|
||||
|
||||
fun saveProxyServerConfig(listen: String, connect: String) {
|
||||
viewModelScope.launch {
|
||||
profileMutex.withLock { prefs.saveProxyConfig(listen, connect) }
|
||||
@@ -194,7 +252,7 @@ class SettingsViewModel(
|
||||
} ?: return@launch
|
||||
|
||||
val serverRunning = (sshRepository.serverState.value as? ServerState.Known)?.running == true
|
||||
if (serverRunning) restartServerIfRunning()
|
||||
if (serverRunning) orchestrator.restartServerIfRunning()
|
||||
|
||||
if (ProxyServiceState.isRunning.value) {
|
||||
proxyManager.stopProxy()
|
||||
@@ -249,7 +307,7 @@ class SettingsViewModel(
|
||||
if (current.bond == enabled) return@launch
|
||||
val next = current.copy(bond = enabled)
|
||||
profileMutex.withLock { prefs.saveClientConfig(next) }
|
||||
restartProxyIfRunning()
|
||||
orchestrator.restartProxyIfRunning()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,8 +327,8 @@ class SettingsViewModel(
|
||||
if (!storedMatches || next.obfKey != current.obfKey) {
|
||||
profileMutex.withLock { prefs.saveServerOpts(next) }
|
||||
}
|
||||
if (sync) restartServerIfRunning()
|
||||
restartProxyIfRunning()
|
||||
if (sync) orchestrator.restartServerIfRunning()
|
||||
orchestrator.restartProxyIfRunning()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,8 +339,8 @@ class SettingsViewModel(
|
||||
val next = current.copy(syncServerSwitches = enabled)
|
||||
profileMutex.withLock { prefs.saveClientConfig(next) }
|
||||
if (enabled) {
|
||||
restartServerIfRunning()
|
||||
restartProxyIfRunning()
|
||||
orchestrator.restartServerIfRunning()
|
||||
orchestrator.restartProxyIfRunning()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -294,8 +352,8 @@ class SettingsViewModel(
|
||||
if (current.obfKey == trimmed) return@launch
|
||||
val next = current.copy(obfKey = trimmed)
|
||||
profileMutex.withLock { prefs.saveServerOpts(next) }
|
||||
if (prefs.clientConfigFlow.first().syncServerSwitches) restartServerIfRunning()
|
||||
restartProxyIfRunning()
|
||||
if (prefs.clientConfigFlow.first().syncServerSwitches) orchestrator.restartServerIfRunning()
|
||||
orchestrator.restartProxyIfRunning()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,53 +372,13 @@ class SettingsViewModel(
|
||||
val serverRunning = (sshRepository.serverState.value as? ServerState.Known)?.running == true
|
||||
if (serverRunning) {
|
||||
sshRepository.stopServer()
|
||||
startServerFromPrefs()
|
||||
orchestrator.startServerFromPrefs()
|
||||
}
|
||||
}
|
||||
restartProxyIfRunning()
|
||||
orchestrator.restartProxyIfRunning()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun startServerFromPrefs() {
|
||||
val l = prefs.proxyListenFlow.first()
|
||||
val c = prefs.proxyConnectFlow.first()
|
||||
val tcpMode = prefs.clientConfigFlow.first().tcpForward
|
||||
val opts = prefs.serverOptsFlow.first()
|
||||
sshRepository.startServer(
|
||||
listen = l, connect = c,
|
||||
tcpMode = tcpMode,
|
||||
obfProfile = if (opts.obfEnabled) opts.obfProfile else "none",
|
||||
obfKey = if (opts.obfEnabled) opts.obfKey else ""
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun restartServerIfRunning() {
|
||||
val running = (sshRepository.serverState.value as? ServerState.Known)?.running == true
|
||||
if (!running) return
|
||||
val l = prefs.proxyListenFlow.first()
|
||||
val c = prefs.proxyConnectFlow.first()
|
||||
if (!l.matches(Regex("""^[\w.\-]+:\d{1,5}$""")) ||
|
||||
!c.matches(Regex("""^[\w.\-]+:\d{1,5}$"""))) return
|
||||
val opts = prefs.serverOptsFlow.first()
|
||||
val tcpMode = prefs.clientConfigFlow.first().tcpForward
|
||||
sshRepository.stopServer()
|
||||
sshRepository.startServer(
|
||||
listen = l, connect = c,
|
||||
tcpMode = tcpMode,
|
||||
obfProfile = if (opts.obfEnabled) opts.obfProfile else "none",
|
||||
obfKey = if (opts.obfEnabled) opts.obfKey else ""
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun restartProxyIfRunning() {
|
||||
if (!ProxyServiceState.isRunning.value) return
|
||||
proxyManager.stopProxy()
|
||||
withTimeoutOrNull(2_000) {
|
||||
ProxyServiceState.isRunning.first { !it }
|
||||
}
|
||||
proxyManager.startProxy(prefs.clientConfigFlow.first())
|
||||
}
|
||||
|
||||
// --- Updates ---
|
||||
fun checkForUpdate() {
|
||||
viewModelScope.launch { appUpdater.checkForUpdate(silent = false) }
|
||||
|
||||
Reference in new issue
Block a user