fix: cache active SSH config in memory, strip CRLF from commands
This commit is contained in:
1 parent
00a506b948
commit
b473387cd6
2 files changed
+20
-7
No files matched your search
@@ -48,7 +48,9 @@ class SSHManager {
|
||||
lastSeenFingerprint = tofu.capturedFingerprint
|
||||
|
||||
val channel = tempSession.openChannel("exec") as ChannelExec
|
||||
channel.setCommand(command)
|
||||
// Strip carriage returns: Kotlin string literals in CRLF source files
|
||||
// contain \r which corrupts variable values on the remote Linux shell
|
||||
channel.setCommand(command.replace("\r\n", "\n").replace("\r", "\n"))
|
||||
channel.inputStream = null // не подключать stdin
|
||||
|
||||
val inStream = channel.inputStream
|
||||
|
||||
@@ -57,6 +57,13 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
private val prefs = AppPreferences(application)
|
||||
private val sshManager = SSHManager()
|
||||
|
||||
/**
|
||||
* Конфиг, с которым было выполнено последнее успешное SSH-подключение.
|
||||
* Хранит пароль в памяти, не зависит от асинхронного чтения EncryptedSharedPreferences.
|
||||
* Все операции с сервером используют его вместо sshConfig.value.
|
||||
*/
|
||||
@Volatile private var activeSshConfig: SshConfig? = null
|
||||
|
||||
private val _sshState = MutableStateFlow<SshConnectionState>(SshConnectionState.Disconnected)
|
||||
val sshState: StateFlow<SshConnectionState> = _sshState.asStateFlow()
|
||||
|
||||
@@ -141,9 +148,13 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
)
|
||||
if (result.trim() == "OK") {
|
||||
// При первом подключении сохраняем отпечаток хоста
|
||||
val fp = sshManager.lastSeenFingerprint ?: config.hostFingerprint
|
||||
if (config.hostFingerprint.isEmpty()) {
|
||||
sshManager.lastSeenFingerprint?.let { prefs.saveSshFingerprint(it) }
|
||||
}
|
||||
// Кешируем полный конфиг (включая пароль) в памяти, чтобы
|
||||
// installServer/startServer/stopServer не зависели от EncryptedSharedPreferences
|
||||
activeSshConfig = config.copy(hostFingerprint = fp)
|
||||
HapticUtil.perform(getApplication(), HapticUtil.Pattern.SUCCESS)
|
||||
_sshState.value = SshConnectionState.Connected(config.ip)
|
||||
checkServerState(config)
|
||||
@@ -155,14 +166,14 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
|
||||
fun reconnectSsh() {
|
||||
val cfg = sshConfig.value
|
||||
val cfg = activeSshConfig ?: sshConfig.value
|
||||
if (cfg.ip.isNotEmpty()) connectSsh(cfg)
|
||||
}
|
||||
|
||||
// ── Server management ──────────────────────────────────────────────────
|
||||
|
||||
fun checkServerState(config: SshConfig? = null) {
|
||||
val cfg = config ?: sshConfig.value
|
||||
val cfg = config ?: activeSshConfig ?: sshConfig.value
|
||||
if (cfg.ip.isEmpty()) return
|
||||
_serverState.value = ServerState.Checking
|
||||
|
||||
@@ -188,7 +199,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
|
||||
fun fetchServerVersion(config: SshConfig? = null) {
|
||||
val cfg = config ?: sshConfig.value
|
||||
val cfg = config ?: activeSshConfig ?: sshConfig.value
|
||||
if (cfg.ip.isEmpty()) return
|
||||
val cmd = """
|
||||
cd /opt/vk-turn 2>/dev/null &&
|
||||
@@ -204,7 +215,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
|
||||
fun installServer() {
|
||||
val cfg = sshConfig.value
|
||||
val cfg = activeSshConfig ?: sshConfig.value
|
||||
if (cfg.ip.isEmpty()) return
|
||||
_serverState.value = ServerState.Working("Установка vk-turn-proxy...")
|
||||
|
||||
@@ -246,7 +257,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
|
||||
fun startServer() {
|
||||
val cfg = sshConfig.value
|
||||
val cfg = activeSshConfig ?: sshConfig.value
|
||||
if (cfg.ip.isEmpty()) return
|
||||
_serverState.value = ServerState.Working("Запуск сервера...")
|
||||
|
||||
@@ -269,7 +280,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
|
||||
fun stopServer() {
|
||||
val cfg = sshConfig.value
|
||||
val cfg = activeSshConfig ?: sshConfig.value
|
||||
if (cfg.ip.isEmpty()) return
|
||||
_serverState.value = ServerState.Working("Остановка сервера...")
|
||||
|
||||
|
||||
Reference in new issue
Block a user