opt.: delete key after use

This commit is contained in:
lollipopkit
2023-08-07 16:51:47 +08:00
parent d37a1fbea7
commit c4925ee2c7

View File

@@ -447,36 +447,42 @@ class _ServerPageState extends State<ServerPage>
); );
} }
void gotoSSH(ServerPrivateInfo spi) { Future<void> gotoSSH(ServerPrivateInfo spi) async {
// as a `Mobile first` app -> handle mobile first // as a `Mobile first` app -> handle mobile first
if (!isDesktop) { if (!isDesktop) {
AppRoute(SSHPage(spi: spi), 'ssh page').go(context); AppRoute(SSHPage(spi: spi), 'ssh page').go(context);
return; return;
} }
List<String> extarArgs = []; List<String> extarArgs = [];
final path = "/tmp/.serverbox_pk_${spi.pubKeyId}";
final file = File(path);
if (spi.pubKeyId != null) { if (spi.pubKeyId != null) {
String path = "/tmp/.serverbox_pk_${spi.pubKeyId}"; await file.delete();
File(path).openWrite().write(getPrivateKey(spi.pubKeyId!)); await file.writeAsString(getPrivateKey(spi.pubKeyId!));
extarArgs += ["-i", path]; extarArgs += ["-i", path];
} }
List<String> sshCommand = ["ssh", "${spi.user}@${spi.ip}"] + extarArgs; List<String> sshCommand = ["ssh", "${spi.user}@${spi.ip}"] + extarArgs;
switch (Platform.operatingSystem) { final system = Platform.operatingSystem;
switch (system) {
case "windows": case "windows":
Process.start("cmd", ["/c", "start"] + sshCommand); await Process.start("cmd", ["/c", "start"] + sshCommand);
return; break;
case "linux": case "linux":
Process.start("x-terminal-emulator", ["-e"] + sshCommand); await Process.start("x-terminal-emulator", ["-e"] + sshCommand);
return; break;
case "macos": case "macos":
Process.start("osascript", [ await Process.start("osascript", [
"-e", "-e",
'tell application "Terminal" to do script "${sshCommand.join(" ")}"' 'tell application "Terminal" to do script "${sshCommand.join(" ")}"'
]); ]);
return; break;
default: default:
AppRoute(SSHPage(spi: spi), 'ssh page').go(context); showSnackBar(context, Text('Mismatch system: $system'));
return;
} }
// For security reason, delete the private key file after use
await file.delete();
} }
@override @override