From 35199ed09473cb0f667e2d72f1e6bd74e8d611d5 Mon Sep 17 00:00:00 2001 From: hstyi Date: Mon, 28 Jul 2025 09:51:00 +0800 Subject: [PATCH] chore: RDP encrypt password on Windows --- .../internal/rdp/RDPProtocolProvider.kt | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/app/termora/plugin/internal/rdp/RDPProtocolProvider.kt b/src/main/kotlin/app/termora/plugin/internal/rdp/RDPProtocolProvider.kt index 06cad96..211a3db 100644 --- a/src/main/kotlin/app/termora/plugin/internal/rdp/RDPProtocolProvider.kt +++ b/src/main/kotlin/app/termora/plugin/internal/rdp/RDPProtocolProvider.kt @@ -82,24 +82,42 @@ internal class RDPProtocolProvider private constructor() : GenericProtocolProvid } } - val file = FileUtils.getFile(Application.getTemporaryDir(), randomUUID() + ".rdp") - file.outputStream().use { IOUtils.write(sb.toString(), it, Charsets.UTF_8) } - if (host.authentication.type == AuthenticationType.Password) { - val systemClipboard = windowScope.window.toolkit.systemClipboard val password = host.authentication.password - systemClipboard.setContents(StringSelection(password), null) - // clear password - swingCoroutineScope.launch(Dispatchers.IO) { - delay(30.seconds) - if (systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) { - if (systemClipboard.getData(DataFlavor.stringFlavor) == password) { - systemClipboard.setContents(StringSelection(StringUtils.EMPTY), null) + var ep = StringUtils.EMPTY + + if (SystemInfo.isWindows) { + val cmd = "ConvertTo-SecureString '${password}' -AsPlainText -Force | ConvertFrom-SecureString" + val process = ProcessBuilder("powershell.exe", "-Command", cmd).start() + if (process.waitFor() == 0) { + ep = String(process.inputStream.readAllBytes()) + } + } + + if (ep.isNotBlank()) { + sb.append("password 51:b:").append(ep).appendLine() + } + + // 如果获取加密密码失败,那么依然要走剪切板 + if (ep.isBlank() || SystemInfo.isMacOS) { + val systemClipboard = windowScope.window.toolkit.systemClipboard + systemClipboard.setContents(StringSelection(password), null) + // clear password + swingCoroutineScope.launch(Dispatchers.IO) { + delay(30.seconds) + if (systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) { + if (systemClipboard.getData(DataFlavor.stringFlavor) == password) { + systemClipboard.setContents(StringSelection(StringUtils.EMPTY), null) + } } } } } + + val file = FileUtils.getFile(Application.getTemporaryDir(), randomUUID() + ".rdp") + file.outputStream().use { IOUtils.write(sb.toString(), it, Charsets.UTF_8) } + if (SystemInfo.isMacOS) { ProcessBuilder("open", file.absolutePath).start() } else if (SystemInfo.isWindows) {