feat: рефакторинг MainViewModel
This commit is contained in:
1 parent
a60668c9c4
commit
a80855c8c9
5 files changed
+449
-375
No files matched your search
@@ -0,0 +1,121 @@
|
|||||||
|
package com.freeturn.app.domain
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import com.freeturn.app.ProxyService
|
||||||
|
import com.freeturn.app.StartupResult
|
||||||
|
import com.freeturn.app.data.ClientConfig
|
||||||
|
import com.freeturn.app.viewmodel.ProxyState
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.filterNotNull
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import kotlinx.coroutines.withTimeoutOrNull
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class LocalProxyManager(private val context: Context) {
|
||||||
|
|
||||||
|
private val _proxyState = MutableStateFlow<ProxyState>(ProxyState.Idle)
|
||||||
|
val proxyState: StateFlow<ProxyState> = _proxyState.asStateFlow()
|
||||||
|
|
||||||
|
private val _customKernelExists = MutableStateFlow(false)
|
||||||
|
val customKernelExists: StateFlow<Boolean> = _customKernelExists.asStateFlow()
|
||||||
|
|
||||||
|
init {
|
||||||
|
_customKernelExists.value = File(context.filesDir, "custom_vkturn").exists()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun observeProxyLifecycle() {
|
||||||
|
ProxyService.proxyFailed.collect {
|
||||||
|
setErrorWithAutoReset("Прокси упал ${ProxyService.MAX_RESTARTS} раз — проверьте настройки")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun observeProxyServiceStatus() {
|
||||||
|
ProxyService.isRunning.collect { running ->
|
||||||
|
if (!running && _proxyState.value == ProxyState.Running) {
|
||||||
|
_proxyState.value = ProxyState.Idle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun syncInitialState() {
|
||||||
|
if (ProxyService.isRunning.value) _proxyState.value = ProxyState.Running
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun startProxy(cfg: ClientConfig) {
|
||||||
|
if (ProxyService.isRunning.value) return
|
||||||
|
if (_proxyState.value is ProxyState.Error) _proxyState.value = ProxyState.Idle
|
||||||
|
|
||||||
|
if (!cfg.isRawMode && (cfg.serverAddress.isBlank() || cfg.vkLink.isBlank())) {
|
||||||
|
setErrorWithAutoReset("Не заполнены настройки клиента")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (cfg.isRawMode && cfg.rawCommand.isBlank()) {
|
||||||
|
setErrorWithAutoReset("Не задана raw-команда")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_proxyState.value = ProxyState.Starting
|
||||||
|
|
||||||
|
ProxyService.clearLogs()
|
||||||
|
ProxyService.startupResult.value = null
|
||||||
|
context.startForegroundService(Intent(context, ProxyService::class.java))
|
||||||
|
|
||||||
|
val result = withTimeoutOrNull(5_000L) {
|
||||||
|
ProxyService.startupResult.filterNotNull().first()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_proxyState.value is ProxyState.Error) return
|
||||||
|
|
||||||
|
when (result) {
|
||||||
|
null -> setErrorWithAutoReset("Прокси не запустился")
|
||||||
|
is StartupResult.Failed -> {
|
||||||
|
stopProxy()
|
||||||
|
setErrorWithAutoReset(result.message)
|
||||||
|
}
|
||||||
|
is StartupResult.Success -> _proxyState.value = ProxyState.Running
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun stopProxy() {
|
||||||
|
context.stopService(Intent(context, ProxyService::class.java))
|
||||||
|
_proxyState.value = ProxyState.Idle
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun setErrorWithAutoReset(message: String) {
|
||||||
|
_proxyState.value = ProxyState.Error(message)
|
||||||
|
delay(3500)
|
||||||
|
if (_proxyState.value is ProxyState.Error) _proxyState.value = ProxyState.Idle
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun setCustomKernel(uri: Uri) = withContext(Dispatchers.IO) {
|
||||||
|
try {
|
||||||
|
val dest = File(context.filesDir, "custom_vkturn")
|
||||||
|
context.contentResolver.openInputStream(uri)?.use { input ->
|
||||||
|
dest.outputStream().use { output -> input.copyTo(output) }
|
||||||
|
}
|
||||||
|
dest.setExecutable(true)
|
||||||
|
withContext(Dispatchers.Main) { _customKernelExists.value = true }
|
||||||
|
ProxyService.addLog("Кастомное ядро установлено: ${dest.length() / 1024} KB")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
ProxyService.addLog("Ошибка установки ядра: ${e.message}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun clearCustomKernel() {
|
||||||
|
File(context.filesDir, "custom_vkturn").delete()
|
||||||
|
_customKernelExists.value = false
|
||||||
|
ProxyService.addLog("Кастомное ядро удалено, используется встроенное")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun clearState() {
|
||||||
|
_proxyState.value = ProxyState.Idle
|
||||||
|
_customKernelExists.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package com.freeturn.app.domain
|
||||||
|
|
||||||
|
object ServerScripts {
|
||||||
|
|
||||||
|
val checkServerState: String = """
|
||||||
|
if ls /opt/vk-turn/server-linux-* >/dev/null 2>&1; then echo "INSTALLED:YES"; else echo "INSTALLED:NO"; fi
|
||||||
|
if ps aux | grep -v grep | grep -q "server-linux-"; then echo "RUNNING:YES"; else echo "RUNNING:NO"; fi
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val fetchServerVersion: String = """
|
||||||
|
cd /opt/vk-turn 2>/dev/null &&
|
||||||
|
ARCH=${'$'}(uname -m);
|
||||||
|
if [ "${'$'}ARCH" = "x86_64" ]; then BIN="./server-linux-amd64"; else BIN="./server-linux-arm64"; fi;
|
||||||
|
timeout 2 ${'$'}BIN -version 2>&1 | head -1
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val installServer: String = """
|
||||||
|
mkdir -p /opt/vk-turn && cd /opt/vk-turn
|
||||||
|
if [ -f /opt/vk-turn/proxy.pid ]; then kill -9 ${'$'}(cat /opt/vk-turn/proxy.pid) 2>/dev/null; rm -f /opt/vk-turn/proxy.pid; fi
|
||||||
|
ARCH=${'$'}(uname -m)
|
||||||
|
if [ "${'$'}ARCH" = "x86_64" ]; then BIN="server-linux-amd64"; else BIN="server-linux-arm64"; fi
|
||||||
|
BASE_URL="https://github.com/cacggghp/vk-turn-proxy/releases/latest/download"
|
||||||
|
echo "Arch: ${'$'}ARCH | Binary: ${'$'}BIN"
|
||||||
|
_dl() { URL=${'$'}1; OUT=${'$'}2
|
||||||
|
if command -v curl >/dev/null 2>&1; then
|
||||||
|
curl -sSL -o "${'$'}OUT" "${'$'}URL" 2>&1
|
||||||
|
elif command -v wget >/dev/null 2>&1; then
|
||||||
|
wget -q -O "${'$'}OUT" "${'$'}URL" 2>&1
|
||||||
|
else
|
||||||
|
echo "ERROR: curl и wget не найдены"; return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
_dl "${'$'}BASE_URL/${'$'}BIN" "${'$'}BIN" || { echo "ERROR: скачивание бинарника не удалось"; exit 1; }
|
||||||
|
SIZE=${'$'}(wc -c < "${'$'}BIN" 2>/dev/null || echo 0)
|
||||||
|
echo "Size: ${'$'}SIZE bytes"
|
||||||
|
if [ "${'$'}SIZE" -lt 100000 ]; then echo "ERROR: файл слишком мал (${'$'}SIZE байт)"; cat "${'$'}BIN" 2>/dev/null; exit 1; fi
|
||||||
|
if _dl "${'$'}BASE_URL/checksums.txt" checksums.txt && [ -s checksums.txt ]; then
|
||||||
|
EXPECTED=${'$'}(grep "${'$'}BIN" checksums.txt | awk '{print ${'$'}1}')
|
||||||
|
if [ -n "${'$'}EXPECTED" ]; then
|
||||||
|
ACTUAL=${'$'}(sha256sum "${'$'}BIN" | awk '{print ${'$'}1}')
|
||||||
|
if [ "${'$'}EXPECTED" = "${'$'}ACTUAL" ]; then
|
||||||
|
echo "SHA256: OK"
|
||||||
|
else
|
||||||
|
echo "ERROR: SHA256 не совпадает — ожидался ${'$'}EXPECTED, получен ${'$'}ACTUAL"
|
||||||
|
rm -f "${'$'}BIN" checksums.txt; exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "WARN: ${'$'}BIN не найден в checksums.txt, SHA256 пропущен"
|
||||||
|
fi
|
||||||
|
rm -f checksums.txt
|
||||||
|
else
|
||||||
|
echo "WARN: checksums.txt недоступен, SHA256 не проверен"
|
||||||
|
fi
|
||||||
|
chmod +x "${'$'}BIN" && echo "DONE"
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
fun startServer(listen: String, connect: String): String = """
|
||||||
|
cd /opt/vk-turn &&
|
||||||
|
ARCH=${'$'}(uname -m);
|
||||||
|
if [ "${'$'}ARCH" = "x86_64" ]; then BIN="server-linux-amd64"; else BIN="server-linux-arm64"; fi;
|
||||||
|
nohup ./${'$'}BIN -listen $listen -connect $connect > server.log 2>&1 &
|
||||||
|
echo ${'$'}! > proxy.pid && echo "STARTED"
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val stopServer: String = """
|
||||||
|
cd /opt/vk-turn &&
|
||||||
|
if [ -f proxy.pid ]; then kill -9 ${'$'}(cat proxy.pid) 2>/dev/null; rm -f proxy.pid; fi;
|
||||||
|
pkill -9 -f "[s]erver-linux-" 2>/dev/null;
|
||||||
|
echo "STOPPED"
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val fetchServerLogs: String = "tail -n 80 /opt/vk-turn/server.log 2>/dev/null || echo '(лог пуст)'"
|
||||||
|
}
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
package com.freeturn.app.domain
|
||||||
|
|
||||||
|
import com.freeturn.app.SSHManager
|
||||||
|
import com.freeturn.app.data.SshConfig
|
||||||
|
import com.freeturn.app.viewmodel.ServerState
|
||||||
|
import com.freeturn.app.viewmodel.SshConnectionState
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import java.time.LocalTime
|
||||||
|
import java.time.format.DateTimeFormatter
|
||||||
|
|
||||||
|
class SshRepository(private val sshManager: SSHManager = SSHManager()) {
|
||||||
|
|
||||||
|
var activeSshConfig: SshConfig? = null
|
||||||
|
private set
|
||||||
|
|
||||||
|
private val _sshState = MutableStateFlow<SshConnectionState>(SshConnectionState.Disconnected)
|
||||||
|
val sshState: StateFlow<SshConnectionState> = _sshState.asStateFlow()
|
||||||
|
|
||||||
|
private val _serverState = MutableStateFlow<ServerState>(ServerState.Unknown)
|
||||||
|
val serverState: StateFlow<ServerState> = _serverState.asStateFlow()
|
||||||
|
|
||||||
|
private val _serverVersion = MutableStateFlow<String?>(null)
|
||||||
|
val serverVersion: StateFlow<String?> = _serverVersion.asStateFlow()
|
||||||
|
|
||||||
|
private val _serverLogs = MutableStateFlow<String?>(null)
|
||||||
|
val serverLogs: StateFlow<String?> = _serverLogs.asStateFlow()
|
||||||
|
|
||||||
|
private val _sshLog = MutableStateFlow<List<String>>(emptyList())
|
||||||
|
val sshLog: StateFlow<List<String>> = _sshLog.asStateFlow()
|
||||||
|
|
||||||
|
private val timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss")
|
||||||
|
private fun timestamp(): String = LocalTime.now().format(timeFormatter)
|
||||||
|
|
||||||
|
private fun appendSshLog(vararg lines: String) {
|
||||||
|
_sshLog.update { current ->
|
||||||
|
var next = current + lines
|
||||||
|
if (next.size > 500) next = next.drop(next.size - 500)
|
||||||
|
next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun logCommand(target: String, cmd: String) {
|
||||||
|
appendSshLog(" ssh $target")
|
||||||
|
cmd.lines().filter { it.isNotBlank() }.forEach { appendSshLog(" $ $it") }
|
||||||
|
appendSshLog(" ---")
|
||||||
|
}
|
||||||
|
|
||||||
|
private val SshConfig.fp get() = hostFingerprint.ifEmpty { null }
|
||||||
|
private val SshConfig.key get() = if (authType == "SSH_KEY") sshKey else ""
|
||||||
|
|
||||||
|
suspend fun runSshCommand(cfg: SshConfig, label: String, cmd: String): String {
|
||||||
|
appendSshLog("", "=== $label [${timestamp()}] ===")
|
||||||
|
logCommand("${cfg.username}@${cfg.ip}:${cfg.port}", cmd)
|
||||||
|
val result = sshManager.executeSilentCommand(
|
||||||
|
cfg.ip, cfg.port, cfg.username, cfg.password, cmd,
|
||||||
|
knownFingerprint = cfg.fp, sshKey = cfg.key
|
||||||
|
)
|
||||||
|
result.lines().filter { it.isNotBlank() }.forEach { appendSshLog(it) }
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun connectSsh(config: SshConfig): Pair<Boolean, String?> {
|
||||||
|
_sshState.value = SshConnectionState.Connecting()
|
||||||
|
val result = runSshCommand(config, "Подключение", "echo OK")
|
||||||
|
return if (result.trim() == "OK") {
|
||||||
|
val fp = sshManager.lastSeenFingerprint ?: config.hostFingerprint
|
||||||
|
activeSshConfig = config.copy(hostFingerprint = fp)
|
||||||
|
_sshState.value = SshConnectionState.Connected(config.ip)
|
||||||
|
checkServerState(config)
|
||||||
|
Pair(true, sshManager.lastSeenFingerprint)
|
||||||
|
} else {
|
||||||
|
_sshState.value = SshConnectionState.Error(result.removePrefix("ERROR: "))
|
||||||
|
Pair(false, null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun disconnect() {
|
||||||
|
activeSshConfig = null
|
||||||
|
_sshState.value = SshConnectionState.Disconnected
|
||||||
|
_serverState.value = ServerState.Unknown
|
||||||
|
_serverVersion.value = null
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun checkServerState(config: SshConfig? = null) {
|
||||||
|
val cfg = config ?: activeSshConfig ?: return
|
||||||
|
if (cfg.ip.isEmpty()) {
|
||||||
|
_serverState.value = ServerState.Unknown
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_serverState.value = ServerState.Checking
|
||||||
|
|
||||||
|
val result = runSshCommand(cfg, "Проверка состояния", ServerScripts.checkServerState)
|
||||||
|
_serverState.value = if (result.startsWith("ERROR")) {
|
||||||
|
ServerState.Error(result.removePrefix("ERROR: "))
|
||||||
|
} else {
|
||||||
|
val known = ServerState.Known(
|
||||||
|
installed = result.contains("INSTALLED:YES"),
|
||||||
|
running = result.contains("RUNNING:YES")
|
||||||
|
)
|
||||||
|
if (known.installed) fetchServerVersion(cfg)
|
||||||
|
known
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun fetchServerVersion(config: SshConfig? = null) {
|
||||||
|
val cfg = config ?: activeSshConfig ?: return
|
||||||
|
if (cfg.ip.isEmpty()) return
|
||||||
|
val out = runSshCommand(cfg, "Запрос версии", ServerScripts.fetchServerVersion)
|
||||||
|
_serverVersion.value = out.trim().takeIf { it.isNotEmpty() && !it.startsWith("ERROR") }
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun installServer() {
|
||||||
|
val cfg = activeSshConfig ?: return
|
||||||
|
if (cfg.ip.isEmpty()) return
|
||||||
|
_serverState.value = ServerState.Working("Установка vk-turn-proxy...")
|
||||||
|
|
||||||
|
val result = runSshCommand(cfg, "Установка", ServerScripts.installServer)
|
||||||
|
if (!result.contains("DONE")) {
|
||||||
|
val errorMsg = result.lines()
|
||||||
|
.filter { it.isNotBlank() }
|
||||||
|
.takeLast(4)
|
||||||
|
.joinToString("\n")
|
||||||
|
.ifBlank { "Нет вывода от команды — см. SSH-лог" }
|
||||||
|
_serverState.value = ServerState.Error(errorMsg)
|
||||||
|
} else {
|
||||||
|
delay(500)
|
||||||
|
checkServerState(cfg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun startServer(listen: String, connect: String): Boolean {
|
||||||
|
val cfg = activeSshConfig ?: return false
|
||||||
|
if (cfg.ip.isEmpty()) return false
|
||||||
|
|
||||||
|
_serverState.value = ServerState.Working("Запуск сервера...")
|
||||||
|
runSshCommand(cfg, "Запуск", ServerScripts.startServer(listen, connect))
|
||||||
|
delay(2000)
|
||||||
|
checkServerState(cfg)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun stopServer() {
|
||||||
|
val cfg = activeSshConfig ?: return
|
||||||
|
if (cfg.ip.isEmpty()) return
|
||||||
|
_serverState.value = ServerState.Working("Остановка сервера...")
|
||||||
|
|
||||||
|
runSshCommand(cfg, "Остановка", ServerScripts.stopServer)
|
||||||
|
delay(2000)
|
||||||
|
checkServerState(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun fetchServerLogs() {
|
||||||
|
val cfg = activeSshConfig ?: return
|
||||||
|
if (cfg.ip.isEmpty()) return
|
||||||
|
_serverLogs.value = "…"
|
||||||
|
val out = runSshCommand(cfg, "server.log", ServerScripts.fetchServerLogs)
|
||||||
|
_serverLogs.value = out.trim().ifEmpty { "(лог пуст)" }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateServerState(state: ServerState) {
|
||||||
|
_serverState.value = state
|
||||||
|
}
|
||||||
|
|
||||||
|
fun clearSshLog() {
|
||||||
|
_sshLog.value = emptyList()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resetAll() {
|
||||||
|
disconnect()
|
||||||
|
_serverLogs.value = null
|
||||||
|
_sshLog.value = emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.freeturn.app.viewmodel
|
||||||
|
|
||||||
|
// ── SSH connection states ──────────────────────────────────────────────────
|
||||||
|
sealed class SshConnectionState {
|
||||||
|
object Disconnected : SshConnectionState()
|
||||||
|
data class Connecting(val step: String = "Подключение к серверу...") : SshConnectionState()
|
||||||
|
data class Connected(val ip: String) : SshConnectionState()
|
||||||
|
data class Error(val message: String) : SshConnectionState()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Remote server states ───────────────────────────────────────────────────
|
||||||
|
sealed class ServerState {
|
||||||
|
object Unknown : ServerState()
|
||||||
|
object Checking : ServerState()
|
||||||
|
data class Known(val installed: Boolean, val running: Boolean) : ServerState()
|
||||||
|
data class Working(val action: String) : ServerState()
|
||||||
|
data class Error(val message: String) : ServerState()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Local proxy client states ──────────────────────────────────────────────
|
||||||
|
sealed class ProxyState {
|
||||||
|
object Idle : ProxyState()
|
||||||
|
object Starting : ProxyState()
|
||||||
|
object Running : ProxyState()
|
||||||
|
data class Error(val message: String) : ProxyState()
|
||||||
|
}
|
||||||
@@ -7,126 +7,52 @@ import android.net.Uri
|
|||||||
import androidx.lifecycle.AndroidViewModel
|
import androidx.lifecycle.AndroidViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.freeturn.app.ProxyService
|
import com.freeturn.app.ProxyService
|
||||||
import com.freeturn.app.SSHManager
|
|
||||||
import com.freeturn.app.StartupResult
|
|
||||||
import com.freeturn.app.data.AppPreferences
|
import com.freeturn.app.data.AppPreferences
|
||||||
import com.freeturn.app.ui.HapticUtil
|
|
||||||
import com.freeturn.app.data.ClientConfig
|
import com.freeturn.app.data.ClientConfig
|
||||||
import com.freeturn.app.data.SshConfig
|
import com.freeturn.app.data.SshConfig
|
||||||
import kotlinx.coroutines.Dispatchers
|
import com.freeturn.app.domain.LocalProxyManager
|
||||||
import kotlinx.coroutines.delay
|
import com.freeturn.app.domain.SshRepository
|
||||||
|
import com.freeturn.app.ui.HapticUtil
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.filterNotNull
|
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.flow.update
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import kotlinx.coroutines.withTimeoutOrNull
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
// ── SSH connection states ──────────────────────────────────────────────────
|
|
||||||
sealed class SshConnectionState {
|
|
||||||
object Disconnected : SshConnectionState()
|
|
||||||
data class Connecting(val step: String = "Подключение к серверу...") : SshConnectionState()
|
|
||||||
data class Connected(val ip: String) : SshConnectionState()
|
|
||||||
data class Error(val message: String) : SshConnectionState()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Remote server states ───────────────────────────────────────────────────
|
|
||||||
sealed class ServerState {
|
|
||||||
object Unknown : ServerState()
|
|
||||||
object Checking : ServerState()
|
|
||||||
data class Known(val installed: Boolean, val running: Boolean) : ServerState()
|
|
||||||
data class Working(val action: String) : ServerState()
|
|
||||||
data class Error(val message: String) : ServerState()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Local proxy client states ──────────────────────────────────────────────
|
|
||||||
sealed class ProxyState {
|
|
||||||
object Idle : ProxyState()
|
|
||||||
object Starting : ProxyState()
|
|
||||||
object Running : ProxyState()
|
|
||||||
data class Error(val message: String) : ProxyState()
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Возвращает сохранённый отпечаток хоста или null при первом подключении */
|
|
||||||
private val SshConfig.fp get() = hostFingerprint.ifEmpty { null }
|
|
||||||
|
|
||||||
/** Возвращает приватный ключ если выбран key-режим аутентификации, иначе пустую строку */
|
|
||||||
private val SshConfig.key get() = if (authType == "SSH_KEY") sshKey else ""
|
|
||||||
|
|
||||||
class MainViewModel(application: Application) : AndroidViewModel(application) {
|
class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
|
|
||||||
private val prefs = AppPreferences(application)
|
private val prefs = AppPreferences(application)
|
||||||
private val sshManager = SSHManager()
|
private val sshRepository = SshRepository()
|
||||||
|
private val proxyManager = LocalProxyManager(application)
|
||||||
|
|
||||||
/**
|
val sshState: StateFlow<SshConnectionState> = sshRepository.sshState
|
||||||
* Конфиг, с которым было выполнено последнее успешное SSH-подключение.
|
val serverState: StateFlow<ServerState> = sshRepository.serverState
|
||||||
* Хранит пароль в памяти, не зависит от асинхронного чтения EncryptedSharedPreferences.
|
val serverVersion: StateFlow<String?> = sshRepository.serverVersion
|
||||||
* Все операции с сервером используют его вместо sshConfig.value.
|
val serverLogs: StateFlow<String?> = sshRepository.serverLogs
|
||||||
*/
|
val sshLog: StateFlow<List<String>> = sshRepository.sshLog
|
||||||
@Volatile private var activeSshConfig: SshConfig? = null
|
|
||||||
|
|
||||||
private val _sshState = MutableStateFlow<SshConnectionState>(SshConnectionState.Disconnected)
|
|
||||||
val sshState: StateFlow<SshConnectionState> = _sshState.asStateFlow()
|
|
||||||
|
|
||||||
private val _serverState = MutableStateFlow<ServerState>(ServerState.Unknown)
|
|
||||||
val serverState: StateFlow<ServerState> = _serverState.asStateFlow()
|
|
||||||
|
|
||||||
private val _serverVersion = MutableStateFlow<String?>(null)
|
|
||||||
val serverVersion: StateFlow<String?> = _serverVersion.asStateFlow()
|
|
||||||
|
|
||||||
private val _serverLogs = MutableStateFlow<String?>(null)
|
|
||||||
val serverLogs: StateFlow<String?> = _serverLogs.asStateFlow()
|
|
||||||
|
|
||||||
// Накопительный лог всех SSH-операций (команда + вывод)
|
|
||||||
private val _sshLog = MutableStateFlow<List<String>>(emptyList())
|
|
||||||
val sshLog: StateFlow<List<String>> = _sshLog.asStateFlow()
|
|
||||||
|
|
||||||
private fun appendSshLog(vararg lines: String) {
|
|
||||||
_sshLog.update { current ->
|
|
||||||
var next = current + lines
|
|
||||||
if (next.size > 500) next = next.drop(next.size - 500)
|
|
||||||
next
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun logCommand(target: String, cmd: String) {
|
|
||||||
appendSshLog(" ssh $target")
|
|
||||||
cmd.lines().filter { it.isNotBlank() }.forEach { appendSshLog(" \$ $it") }
|
|
||||||
appendSshLog(" ---")
|
|
||||||
}
|
|
||||||
|
|
||||||
// P2-2: единый форматтер времени (DateTimeFormatter — thread-safe, не требует Locale)
|
|
||||||
private val timeFormatter = java.time.format.DateTimeFormatter.ofPattern("HH:mm:ss")
|
|
||||||
private fun timestamp(): String = java.time.LocalTime.now().format(timeFormatter)
|
|
||||||
|
|
||||||
// P2-2: обобщённая SSH-операция — логирование + выполнение + вывод в SSH-лог
|
|
||||||
private suspend fun runSshCommand(cfg: SshConfig, label: String, cmd: String): String {
|
|
||||||
appendSshLog("", "=== $label [${timestamp()}] ===")
|
|
||||||
logCommand("${cfg.username}@${cfg.ip}:${cfg.port}", cmd)
|
|
||||||
val result = sshManager.executeSilentCommand(
|
|
||||||
cfg.ip, cfg.port, cfg.username, cfg.password, cmd,
|
|
||||||
knownFingerprint = cfg.fp, sshKey = cfg.key
|
|
||||||
)
|
|
||||||
result.lines().filter { it.isNotBlank() }.forEach { appendSshLog(it) }
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private val _proxyState = MutableStateFlow<ProxyState>(ProxyState.Idle)
|
|
||||||
val proxyState: StateFlow<ProxyState> = _proxyState.asStateFlow()
|
|
||||||
|
|
||||||
|
val proxyState: StateFlow<ProxyState> = proxyManager.proxyState
|
||||||
val logs: StateFlow<List<String>> = ProxyService.logs
|
val logs: StateFlow<List<String>> = ProxyService.logs
|
||||||
|
val customKernelExists: StateFlow<Boolean> = proxyManager.customKernelExists
|
||||||
|
|
||||||
private val _isInitialized = MutableStateFlow(false)
|
private val _isInitialized = MutableStateFlow(false)
|
||||||
val isInitialized: StateFlow<Boolean> = _isInitialized.asStateFlow()
|
val isInitialized: StateFlow<Boolean> = _isInitialized.asStateFlow()
|
||||||
|
|
||||||
private val _customKernelExists = MutableStateFlow(false)
|
init {
|
||||||
val customKernelExists: StateFlow<Boolean> = _customKernelExists.asStateFlow()
|
viewModelScope.launch {
|
||||||
|
prefs.onboardingDoneFlow.first()
|
||||||
|
_isInitialized.value = true
|
||||||
|
}
|
||||||
|
viewModelScope.launch {
|
||||||
|
proxyManager.observeProxyLifecycle()
|
||||||
|
}
|
||||||
|
viewModelScope.launch {
|
||||||
|
proxyManager.observeProxyServiceStatus()
|
||||||
|
}
|
||||||
|
proxyManager.syncInitialState()
|
||||||
|
}
|
||||||
|
|
||||||
val sshConfig: StateFlow<SshConfig> = prefs.sshConfigFlow
|
val sshConfig: StateFlow<SshConfig> = prefs.sshConfigFlow
|
||||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), SshConfig())
|
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), SshConfig())
|
||||||
@@ -143,309 +69,86 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
|||||||
val proxyConnect: StateFlow<String> = prefs.proxyConnectFlow
|
val proxyConnect: StateFlow<String> = prefs.proxyConnectFlow
|
||||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), "127.0.0.1:40537")
|
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), "127.0.0.1:40537")
|
||||||
|
|
||||||
init {
|
|
||||||
viewModelScope.launch {
|
|
||||||
// Wait for first real DataStore read before showing UI
|
|
||||||
prefs.onboardingDoneFlow.first()
|
|
||||||
_isInitialized.value = true
|
|
||||||
}
|
|
||||||
|
|
||||||
viewModelScope.launch {
|
|
||||||
ProxyService.proxyFailed.collect {
|
|
||||||
setErrorWithAutoReset("Прокси упал ${ProxyService.MAX_RESTARTS} раз — проверьте настройки")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Сбрасываем состояние когда сервис остановился (нормальный выход, не краш)
|
|
||||||
viewModelScope.launch {
|
|
||||||
ProxyService.isRunning.collect { running ->
|
|
||||||
if (!running && _proxyState.value == ProxyState.Running) {
|
|
||||||
_proxyState.value = ProxyState.Idle
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ProxyService.isRunning.value) _proxyState.value = ProxyState.Running
|
|
||||||
|
|
||||||
_customKernelExists.value =
|
|
||||||
File(getApplication<Application>().filesDir, "custom_vkturn").exists()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── SSH ────────────────────────────────────────────────────────────────
|
// ── SSH ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
fun connectSsh(config: SshConfig) {
|
fun connectSsh(config: SshConfig) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
prefs.saveSshConfig(config)
|
prefs.saveSshConfig(config)
|
||||||
}
|
val (success, fp) = sshRepository.connectSsh(config)
|
||||||
_sshState.value = SshConnectionState.Connecting()
|
if (success) {
|
||||||
|
if (config.hostFingerprint.isEmpty() && fp != null) {
|
||||||
viewModelScope.launch {
|
prefs.saveSshFingerprint(fp)
|
||||||
val result = runSshCommand(config, "Подключение", "echo OK")
|
|
||||||
if (result.trim() == "OK") {
|
|
||||||
val fp = sshManager.lastSeenFingerprint ?: config.hostFingerprint
|
|
||||||
if (config.hostFingerprint.isEmpty()) {
|
|
||||||
sshManager.lastSeenFingerprint?.let { prefs.saveSshFingerprint(it) }
|
|
||||||
}
|
}
|
||||||
activeSshConfig = config.copy(hostFingerprint = fp)
|
|
||||||
HapticUtil.perform(getApplication(), HapticUtil.Pattern.SUCCESS)
|
HapticUtil.perform(getApplication(), HapticUtil.Pattern.SUCCESS)
|
||||||
_sshState.value = SshConnectionState.Connected(config.ip)
|
|
||||||
checkServerState(config)
|
|
||||||
} else {
|
} else {
|
||||||
HapticUtil.perform(getApplication(), HapticUtil.Pattern.ERROR)
|
HapticUtil.perform(getApplication(), HapticUtil.Pattern.ERROR)
|
||||||
_sshState.value = SshConnectionState.Error(result.removePrefix("ERROR: "))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun disconnectSsh() {
|
fun disconnectSsh() {
|
||||||
activeSshConfig = null
|
sshRepository.disconnect()
|
||||||
_sshState.value = SshConnectionState.Disconnected
|
|
||||||
_serverState.value = ServerState.Unknown
|
|
||||||
_serverVersion.value = null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun reconnectSsh() {
|
fun reconnectSsh() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val cfg = activeSshConfig
|
val cfg = sshRepository.activeSshConfig
|
||||||
?: sshConfig.value.takeIf { it.ip.isNotEmpty() }
|
?: sshConfig.value.takeIf { it.ip.isNotEmpty() }
|
||||||
?: prefs.sshConfigFlow.first()
|
?: prefs.sshConfigFlow.first()
|
||||||
if (cfg.ip.isNotEmpty()) connectSsh(cfg)
|
if (cfg.ip.isNotEmpty()) connectSsh(cfg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Server management ──────────────────────────────────────────────────
|
// ── Server management ──────────────────────────────────────────────────
|
||||||
|
|
||||||
fun checkServerState(config: SshConfig? = null) {
|
fun checkServerState(config: SshConfig? = null) {
|
||||||
val cfg = config ?: activeSshConfig ?: sshConfig.value
|
viewModelScope.launch { sshRepository.checkServerState(config ?: sshConfig.value) }
|
||||||
// P2-4: сброс в Unknown вместо зависания в Checking при пустом IP
|
|
||||||
if (cfg.ip.isEmpty()) {
|
|
||||||
_serverState.value = ServerState.Unknown
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_serverState.value = ServerState.Checking
|
|
||||||
|
|
||||||
val checkCmd = """
|
|
||||||
if ls /opt/vk-turn/server-linux-* >/dev/null 2>&1; then echo "INSTALLED:YES"; else echo "INSTALLED:NO"; fi
|
|
||||||
if ps aux | grep -v grep | grep -q "server-linux-"; then echo "RUNNING:YES"; else echo "RUNNING:NO"; fi
|
|
||||||
""".trimIndent()
|
|
||||||
|
|
||||||
viewModelScope.launch {
|
|
||||||
val result = runSshCommand(cfg, "Проверка состояния", checkCmd)
|
|
||||||
_serverState.value = if (result.startsWith("ERROR")) {
|
|
||||||
ServerState.Error(result.removePrefix("ERROR: "))
|
|
||||||
} else {
|
|
||||||
val known = ServerState.Known(
|
|
||||||
installed = result.contains("INSTALLED:YES"),
|
|
||||||
running = result.contains("RUNNING:YES")
|
|
||||||
)
|
|
||||||
if (known.installed) fetchServerVersion(cfg)
|
|
||||||
known
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun fetchServerVersion(config: SshConfig? = null) {
|
fun fetchServerVersion(config: SshConfig? = null) {
|
||||||
val cfg = config ?: activeSshConfig ?: sshConfig.value
|
viewModelScope.launch { sshRepository.fetchServerVersion(config ?: sshConfig.value) }
|
||||||
if (cfg.ip.isEmpty()) return
|
|
||||||
val cmd = """
|
|
||||||
cd /opt/vk-turn 2>/dev/null &&
|
|
||||||
ARCH=${'$'}(uname -m);
|
|
||||||
if [ "${'$'}ARCH" = "x86_64" ]; then BIN="./server-linux-amd64"; else BIN="./server-linux-arm64"; fi;
|
|
||||||
timeout 2 ${'$'}BIN -version 2>&1 | head -1
|
|
||||||
""".trimIndent()
|
|
||||||
viewModelScope.launch {
|
|
||||||
val out = sshManager.executeSilentCommand(cfg.ip, cfg.port, cfg.username, cfg.password, cmd,
|
|
||||||
knownFingerprint = cfg.fp, sshKey = cfg.key)
|
|
||||||
_serverVersion.value = out.trim().takeIf { it.isNotEmpty() && !it.startsWith("ERROR") }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun installServer() {
|
fun installServer() {
|
||||||
val cfg = activeSshConfig ?: sshConfig.value
|
viewModelScope.launch { sshRepository.installServer() }
|
||||||
if (cfg.ip.isEmpty()) return
|
|
||||||
_serverState.value = ServerState.Working("Установка vk-turn-proxy...")
|
|
||||||
|
|
||||||
val script = """
|
|
||||||
mkdir -p /opt/vk-turn && cd /opt/vk-turn
|
|
||||||
if [ -f /opt/vk-turn/proxy.pid ]; then kill -9 ${'$'}(cat /opt/vk-turn/proxy.pid) 2>/dev/null; rm -f /opt/vk-turn/proxy.pid; fi
|
|
||||||
ARCH=${'$'}(uname -m)
|
|
||||||
if [ "${'$'}ARCH" = "x86_64" ]; then BIN="server-linux-amd64"; else BIN="server-linux-arm64"; fi
|
|
||||||
BASE_URL="https://github.com/cacggghp/vk-turn-proxy/releases/latest/download"
|
|
||||||
echo "Arch: ${'$'}ARCH | Binary: ${'$'}BIN"
|
|
||||||
_dl() { URL=${'$'}1; OUT=${'$'}2
|
|
||||||
if command -v curl >/dev/null 2>&1; then
|
|
||||||
curl -sSL -o "${'$'}OUT" "${'$'}URL" 2>&1
|
|
||||||
elif command -v wget >/dev/null 2>&1; then
|
|
||||||
wget -q -O "${'$'}OUT" "${'$'}URL" 2>&1
|
|
||||||
else
|
|
||||||
echo "ERROR: curl и wget не найдены"; return 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
_dl "${'$'}BASE_URL/${'$'}BIN" "${'$'}BIN" || { echo "ERROR: скачивание бинарника не удалось"; exit 1; }
|
|
||||||
SIZE=${'$'}(wc -c < "${'$'}BIN" 2>/dev/null || echo 0)
|
|
||||||
echo "Size: ${'$'}SIZE bytes"
|
|
||||||
if [ "${'$'}SIZE" -lt 100000 ]; then echo "ERROR: файл слишком мал (${'$'}SIZE байт)"; cat "${'$'}BIN" 2>/dev/null; exit 1; fi
|
|
||||||
if _dl "${'$'}BASE_URL/checksums.txt" checksums.txt && [ -s checksums.txt ]; then
|
|
||||||
EXPECTED=${'$'}(grep "${'$'}BIN" checksums.txt | awk '{print ${'$'}1}')
|
|
||||||
if [ -n "${'$'}EXPECTED" ]; then
|
|
||||||
ACTUAL=${'$'}(sha256sum "${'$'}BIN" | awk '{print ${'$'}1}')
|
|
||||||
if [ "${'$'}EXPECTED" = "${'$'}ACTUAL" ]; then
|
|
||||||
echo "SHA256: OK"
|
|
||||||
else
|
|
||||||
echo "ERROR: SHA256 не совпадает — ожидался ${'$'}EXPECTED, получен ${'$'}ACTUAL"
|
|
||||||
rm -f "${'$'}BIN" checksums.txt; exit 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "WARN: ${'$'}BIN не найден в checksums.txt, SHA256 пропущен"
|
|
||||||
fi
|
|
||||||
rm -f checksums.txt
|
|
||||||
else
|
|
||||||
echo "WARN: checksums.txt недоступен, SHA256 не проверен"
|
|
||||||
fi
|
|
||||||
chmod +x "${'$'}BIN" && echo "DONE"
|
|
||||||
""".trimIndent()
|
|
||||||
|
|
||||||
viewModelScope.launch {
|
|
||||||
val result = runSshCommand(cfg, "Установка", script)
|
|
||||||
if (!result.contains("DONE")) {
|
|
||||||
val errorMsg = result.lines()
|
|
||||||
.filter { it.isNotBlank() }
|
|
||||||
.takeLast(4)
|
|
||||||
.joinToString("\n")
|
|
||||||
.ifBlank { "Нет вывода от команды — см. SSH-лог" }
|
|
||||||
_serverState.value = ServerState.Error(errorMsg)
|
|
||||||
} else {
|
|
||||||
delay(500)
|
|
||||||
checkServerState(cfg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isValidHostPort(s: String): Boolean =
|
|
||||||
s.matches(Regex("""^[\w.\-]+:\d{1,5}$"""))
|
|
||||||
|
|
||||||
fun startServer() {
|
fun startServer() {
|
||||||
val cfg = activeSshConfig ?: sshConfig.value
|
|
||||||
if (cfg.ip.isEmpty()) return
|
|
||||||
|
|
||||||
val l = proxyListen.value
|
val l = proxyListen.value
|
||||||
val c = proxyConnect.value
|
val c = proxyConnect.value
|
||||||
if (!isValidHostPort(l) || !isValidHostPort(c)) {
|
if (!l.matches(Regex("""^[\w.\-]+:\d{1,5}$""")) || !c.matches(Regex("""^[\w.\-]+:\d{1,5}$"""))) {
|
||||||
_serverState.value = ServerState.Error("Неверный формат адреса (ожидается host:port)")
|
sshRepository.updateServerState(ServerState.Error("Неверный формат адреса (ожидается host:port)"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_serverState.value = ServerState.Working("Запуск сервера...")
|
viewModelScope.launch { sshRepository.startServer(l, c) }
|
||||||
|
|
||||||
val script = """
|
|
||||||
cd /opt/vk-turn &&
|
|
||||||
ARCH=${'$'}(uname -m);
|
|
||||||
if [ "${'$'}ARCH" = "x86_64" ]; then BIN="server-linux-amd64"; else BIN="server-linux-arm64"; fi;
|
|
||||||
nohup ./${'$'}BIN -listen $l -connect $c > server.log 2>&1 &
|
|
||||||
echo ${'$'}! > proxy.pid && echo "STARTED"
|
|
||||||
""".trimIndent()
|
|
||||||
|
|
||||||
viewModelScope.launch {
|
|
||||||
runSshCommand(cfg, "Запуск", script)
|
|
||||||
delay(2000)
|
|
||||||
checkServerState(cfg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopServer() {
|
fun stopServer() {
|
||||||
val cfg = activeSshConfig ?: sshConfig.value
|
viewModelScope.launch { sshRepository.stopServer() }
|
||||||
if (cfg.ip.isEmpty()) return
|
|
||||||
_serverState.value = ServerState.Working("Остановка сервера...")
|
|
||||||
|
|
||||||
val script = """
|
|
||||||
cd /opt/vk-turn &&
|
|
||||||
if [ -f proxy.pid ]; then kill -9 ${'$'}(cat proxy.pid) 2>/dev/null; rm -f proxy.pid; fi;
|
|
||||||
pkill -9 -f "[s]erver-linux-" 2>/dev/null;
|
|
||||||
echo "STOPPED"
|
|
||||||
""".trimIndent()
|
|
||||||
|
|
||||||
viewModelScope.launch {
|
|
||||||
runSshCommand(cfg, "Остановка", script)
|
|
||||||
delay(2000)
|
|
||||||
checkServerState(cfg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun fetchServerLogs() {
|
fun fetchServerLogs() {
|
||||||
val cfg = activeSshConfig ?: sshConfig.value
|
viewModelScope.launch { sshRepository.fetchServerLogs() }
|
||||||
if (cfg.ip.isEmpty()) return
|
|
||||||
_serverLogs.value = "…"
|
|
||||||
val logCmd = "tail -n 80 /opt/vk-turn/server.log 2>/dev/null || echo '(лог пуст)'"
|
|
||||||
viewModelScope.launch {
|
|
||||||
val out = runSshCommand(cfg, "server.log", logCmd)
|
|
||||||
_serverLogs.value = out.trim().ifEmpty { "(лог пуст)" }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clearSshLog() {
|
fun clearSshLog() {
|
||||||
_sshLog.value = emptyList()
|
sshRepository.clearSshLog()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Local proxy ────────────────────────────────────────────────────────
|
// ── Local proxy ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
fun startProxy(context: Context) {
|
fun startProxy(context: Context) {
|
||||||
if (ProxyService.isRunning.value) return
|
|
||||||
|
|
||||||
if (_proxyState.value is ProxyState.Error) _proxyState.value = ProxyState.Idle
|
|
||||||
|
|
||||||
val cfg = clientConfig.value
|
|
||||||
if (!cfg.isRawMode && (cfg.serverAddress.isBlank() || cfg.vkLink.isBlank())) {
|
|
||||||
setErrorWithAutoReset("Не заполнены настройки клиента")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (cfg.isRawMode && cfg.rawCommand.isBlank()) {
|
|
||||||
setErrorWithAutoReset("Не задана raw-команда")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_proxyState.value = ProxyState.Starting
|
|
||||||
|
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
prefs.saveClientConfig(cfg)
|
proxyManager.startProxy(clientConfig.value)
|
||||||
|
|
||||||
ProxyService.clearLogs()
|
|
||||||
ProxyService.startupResult.value = null
|
|
||||||
context.startForegroundService(Intent(context, ProxyService::class.java))
|
|
||||||
|
|
||||||
// P2-1: ожидаем явного сигнала от ProxyService вместо delay(2500) + эвристики
|
|
||||||
val result = withTimeoutOrNull(5_000L) {
|
|
||||||
ProxyService.startupResult.filterNotNull().first()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Если proxyFailed уже обработал ошибку — не дублируем
|
|
||||||
if (_proxyState.value is ProxyState.Error) return@launch
|
|
||||||
|
|
||||||
when (result) {
|
|
||||||
null -> setErrorWithAutoReset("Прокси не запустился")
|
|
||||||
is StartupResult.Failed -> {
|
|
||||||
context.stopService(Intent(context, ProxyService::class.java))
|
|
||||||
setErrorWithAutoReset(result.message)
|
|
||||||
}
|
|
||||||
is StartupResult.Success -> _proxyState.value = ProxyState.Running
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setErrorWithAutoReset(message: String) {
|
|
||||||
HapticUtil.perform(getApplication(), HapticUtil.Pattern.ERROR)
|
|
||||||
_proxyState.value = ProxyState.Error(message)
|
|
||||||
viewModelScope.launch {
|
|
||||||
delay(3500)
|
|
||||||
if (_proxyState.value is ProxyState.Error) _proxyState.value = ProxyState.Idle
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopProxy(context: Context) {
|
fun stopProxy(context: Context) {
|
||||||
context.stopService(Intent(context, ProxyService::class.java))
|
proxyManager.stopProxy()
|
||||||
_proxyState.value = ProxyState.Idle
|
}
|
||||||
|
|
||||||
|
fun clearLogs() {
|
||||||
|
ProxyService.clearLogs()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Preferences ────────────────────────────────────────────────────────
|
// ── Preferences ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
fun saveClientConfig(config: ClientConfig) {
|
fun saveClientConfig(config: ClientConfig) {
|
||||||
viewModelScope.launch { prefs.saveClientConfig(config) }
|
viewModelScope.launch { prefs.saveClientConfig(config) }
|
||||||
}
|
}
|
||||||
@@ -458,33 +161,13 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
|||||||
viewModelScope.launch { prefs.setOnboardingDone(true) }
|
viewModelScope.launch { prefs.setOnboardingDone(true) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clearLogs() {
|
|
||||||
ProxyService.clearLogs()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Custom kernel ──────────────────────────────────────────────────────
|
// ── Custom kernel ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
fun setCustomKernel(uri: Uri) {
|
fun setCustomKernel(uri: Uri) {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch { proxyManager.setCustomKernel(uri) }
|
||||||
try {
|
|
||||||
val app = getApplication<Application>()
|
|
||||||
val dest = File(app.filesDir, "custom_vkturn")
|
|
||||||
app.contentResolver.openInputStream(uri)?.use { input ->
|
|
||||||
dest.outputStream().use { output -> input.copyTo(output) }
|
|
||||||
}
|
|
||||||
dest.setExecutable(true)
|
|
||||||
withContext(Dispatchers.Main) { _customKernelExists.value = true }
|
|
||||||
ProxyService.addLog("Кастомное ядро установлено: ${dest.length() / 1024} KB")
|
|
||||||
} catch (e: Exception) {
|
|
||||||
ProxyService.addLog("Ошибка установки ядра: ${e.message}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clearCustomKernel() {
|
fun clearCustomKernel() {
|
||||||
File(getApplication<Application>().filesDir, "custom_vkturn").delete()
|
proxyManager.clearCustomKernel()
|
||||||
_customKernelExists.value = false
|
|
||||||
ProxyService.addLog("Кастомное ядро удалено, используется встроенное")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun resetAllSettings(context: Context) {
|
fun resetAllSettings(context: Context) {
|
||||||
@@ -493,14 +176,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
|||||||
context.stopService(Intent(context, ProxyService::class.java))
|
context.stopService(Intent(context, ProxyService::class.java))
|
||||||
}
|
}
|
||||||
prefs.resetAll()
|
prefs.resetAll()
|
||||||
activeSshConfig = null
|
sshRepository.resetAll()
|
||||||
_sshState.value = SshConnectionState.Disconnected
|
proxyManager.clearState()
|
||||||
_serverState.value = ServerState.Unknown
|
|
||||||
_serverVersion.value = null
|
|
||||||
_serverLogs.value = null
|
|
||||||
_sshLog.value = emptyList()
|
|
||||||
_proxyState.value = ProxyState.Idle
|
|
||||||
_customKernelExists.value = false
|
|
||||||
ProxyService.clearLogs()
|
ProxyService.clearLogs()
|
||||||
|
|
||||||
val intent = (context as? android.app.Activity)?.intent
|
val intent = (context as? android.app.Activity)?.intent
|
||||||
|
|||||||
Reference in new issue
Block a user