fix(ssh): Ensure that the private key ends with a newline character (#1073)

* fix(ssh): Ensure that the private key ends with a newline character

Fixes an issue where SSH private key files were not written with a newline character at the end, preventing parsing errors in certain SSH clients

Update the package

* fix(server_func_btns): Fixed an issue with the order of SSH command parameters and added file permission settings

Added permission settings for private key files on non-Windows platforms to ensure secure access. Additionally, the order of SSH command parameters was adjusted to ensure that connection parameters are passed correctly.
This commit is contained in:
GT610
2026-03-18 16:37:12 +08:00
committed by GitHub
parent 0b7c1f2c99
commit 713662d5b6
3 changed files with 51 additions and 42 deletions

View File

@@ -216,6 +216,7 @@ void _gotoSSH(Spi spi, BuildContext context) async {
SSHPage.route.go(context, args);
return;
}
final extraArgs = <String>[];
if (spi.port != 22) {
extraArgs.addAll(['-p', '${spi.port}']);
@@ -227,17 +228,24 @@ void _gotoSSH(Spi spi, BuildContext context) async {
/// For security reason, save the private key file to app doc path
return Paths.doc.joinPath(tempKeyFileName);
}();
final file = File(path);
final shouldGenKey = spi.keyId != null;
if (shouldGenKey) {
if (await file.exists()) {
await file.delete();
}
await file.writeAsString(getPrivateKey(spi.keyId!));
final keyContent = getPrivateKey(spi.keyId!);
final keyContentWithNewline = keyContent.endsWith('\n') ? keyContent : '$keyContent\n';
await file.writeAsString(keyContentWithNewline);
if (!Platform.isWindows) {
await Process.run('chmod', ['600', path]);
}
extraArgs.addAll(['-i', path]);
}
final sshCommand = ['ssh', '${spi.user}@${spi.ip}'] + extraArgs;
final sshCommand = ['ssh'] + extraArgs + ['${spi.user}@${spi.ip}'];
final system = Pfs.type;
switch (system) {
case Pfs.windows: