fix: capture SSH stderr, switch install to curl, check DONE marker

This commit is contained in:
samosvalishe committed 2026-03-29 01:53:35 +03:00
1 parent 6c7675e25c
commit c54b594c75
2 files changed
+37 -8

No files matched your search

@@ -8,6 +8,7 @@ import com.jcraft.jsch.Session
import com.jcraft.jsch.UserInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlin.concurrent.thread
import java.security.MessageDigest
import java.util.Base64
import java.util.Properties
@@ -49,12 +50,18 @@ class SSHManager {
val channel = tempSession.openChannel("exec") as ChannelExec
channel.setCommand(command)
channel.inputStream = null // не подключать stdin
channel.setErrStream(null)
val inStream = channel.inputStream
val errStream = channel.errStream
channel.connect()
val output = inStream.bufferedReader().use { reader ->
// Читаем stderr в отдельном потоке чтобы не было дедлока
val errBuilder = StringBuilder()
val errThread = thread {
errStream.bufferedReader().forEachLine { errBuilder.appendLine(it) }
}
val stdout = inStream.bufferedReader().use { reader ->
buildString {
var line: String?
while (reader.readLine().also { line = it } != null) {
@@ -62,8 +69,17 @@ class SSHManager {
}
}
}
errThread.join(3_000)
channel.disconnect()
output.trim()
val stderr = errBuilder.toString().trim()
buildString {
append(stdout.trim())
if (stderr.isNotEmpty()) {
if (stdout.isNotBlank()) append("\n")
append(stderr)
}
}
} catch (e: Exception) {
// Если отпечаток изменился — конкретное сообщение об угрозе
val isMitm = tofu.capturedFingerprint != null
@@ -213,15 +213,28 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
pkill -9 -f "server-linux-" 2>/dev/null;
ARCH=${'$'}(uname -m);
if [ "${'$'}ARCH" = "x86_64" ]; then BIN="server-linux-amd64"; else BIN="server-linux-arm64"; fi;
wget -qO ${'$'}BIN https://github.com/cacggghp/vk-turn-proxy/releases/latest/download/${'$'}BIN &&
chmod +x ${'$'}BIN && echo "DONE"
URL="https://github.com/cacggghp/vk-turn-proxy/releases/latest/download/${'$'}BIN";
if command -v curl >/dev/null 2>&1; then
curl -fsSL -o "${'$'}BIN" "${'$'}URL"
elif command -v wget >/dev/null 2>&1; then
wget -qO "${'$'}BIN" "${'$'}URL"
else
echo "ERROR: curl и wget не найдены на сервере"; exit 1
fi &&
chmod +x "${'$'}BIN" && echo "DONE"
""".trimIndent()
viewModelScope.launch {
sshManager.executeSilentCommand(cfg.ip, cfg.port, cfg.username, cfg.password, script,
val result = sshManager.executeSilentCommand(cfg.ip, cfg.port, cfg.username, cfg.password, script,
knownFingerprint = cfg.fp)
delay(1000)
checkServerState(cfg)
if (!result.contains("DONE")) {
_serverState.value = ServerState.Error(
result.lines().lastOrNull { it.isNotBlank() } ?: "Ошибка установки"
)
} else {
delay(500)
checkServerState(cfg)
}
}
}