feat(ssh): support full multi-hop jump chain (#356) (#1058)

* feat(ssh): support full multi-hop jump chain (#356)

* fix(edit): validate jump cycle for new server saves
This commit is contained in:
lollipopkit🏳️‍⚧️
2026-02-28 00:12:03 +08:00
committed by GitHub
parent c3678f3df9
commit bc69686d16
8 changed files with 385 additions and 44 deletions

View File

@@ -8,19 +8,57 @@ class SftpReq {
String? privateKey;
Spi? jumpSpi;
String? jumpPrivateKey;
Map<String, Spi>? jumpSpisById;
Map<String, String>? privateKeysByKeyId;
Map<String, String>? knownHostFingerprints;
SftpReq(this.spi, this.remotePath, this.localPath, this.type) {
privateKeysByKeyId = {};
final keyId = spi.keyId;
if (keyId != null) {
privateKey = getPrivateKey(keyId);
privateKeysByKeyId![keyId] = privateKey!;
}
final allServers = {
for (final server in Stores.server.fetch()) server.id: server,
};
jumpSpisById = collectJumpServers(spi: spi, serversById: allServers);
if (spi.jumpId != null) {
jumpSpi = Stores.server.box.get(spi.jumpId);
jumpSpi = jumpSpisById?[spi.jumpId];
jumpPrivateKey = Stores.key.fetchOne(jumpSpi?.keyId)?.key;
if (jumpSpi?.keyId case final jumpKeyId?) {
if (jumpPrivateKey != null) {
privateKeysByKeyId![jumpKeyId] = jumpPrivateKey!;
}
}
}
for (final jump in jumpSpisById?.values ?? const <Spi>[]) {
final jumpKeyId = jump.keyId;
if (jumpKeyId == null || privateKeysByKeyId!.containsKey(jumpKeyId)) {
continue;
}
final key = Stores.key.fetchOne(jumpKeyId)?.key;
if (key == null) {
continue;
}
privateKeysByKeyId![jumpKeyId] = key;
}
if (jumpSpisById != null && jumpSpisById!.isEmpty) {
jumpSpisById = null;
}
if (privateKeysByKeyId != null && privateKeysByKeyId!.isEmpty) {
privateKeysByKeyId = null;
}
try {
knownHostFingerprints = Map<String, String>.from(Stores.setting.sshKnownHostFingerprints.get());
knownHostFingerprints = Map<String, String>.from(
Stores.setting.sshKnownHostFingerprints.get(),
);
} catch (e, s) {
Loggers.app.warning('Failed to load SSH known host fingerprints', e, s);
knownHostFingerprints = null;
@@ -46,8 +84,11 @@ class SftpReqStatus {
Exception? error;
Duration? spentTime;
SftpReqStatus({required this.req, required this.notifyListeners, this.completer})
: id = DateTime.now().microsecondsSinceEpoch {
SftpReqStatus({
required this.req,
required this.notifyListeners,
this.completer,
}) : id = DateTime.now().microsecondsSinceEpoch {
worker = SftpWorker(onNotify: onNotify, req: req)..init();
}

View File

@@ -6,6 +6,7 @@ import 'dart:typed_data';
import 'package:dartssh2/dartssh2.dart';
import 'package:easy_isolate/easy_isolate.dart';
import 'package:fl_lib/fl_lib.dart';
import 'package:server_box/core/utils/jump_chain.dart';
import 'package:server_box/core/utils/server.dart';
import 'package:server_box/data/model/server/server_private_info.dart';
import 'package:server_box/data/res/store.dart';
@@ -28,7 +29,11 @@ class SftpWorker {
/// the threads
Future<void> init() async {
if (worker.isInitialized) worker.dispose();
await worker.init(mainMessageHandler, isolateMessageHandler, errorHandler: print);
await worker.init(
mainMessageHandler,
isolateMessageHandler,
errorHandler: print,
);
worker.sendMessage(req);
}
@@ -39,7 +44,11 @@ class SftpWorker {
}
/// Handle the messages coming from the main
Future<void> isolateMessageHandler(dynamic data, SendPort mainSendPort, SendErrorFunction sendError) async {
Future<void> isolateMessageHandler(
dynamic data,
SendPort mainSendPort,
SendErrorFunction sendError,
) async {
switch (data) {
case final SftpReq val:
switch (val.type) {
@@ -56,7 +65,11 @@ Future<void> isolateMessageHandler(dynamic data, SendPort mainSendPort, SendErro
}
}
Future<void> _download(SftpReq req, SendPort mainSendPort, SendErrorFunction sendError) async {
Future<void> _download(
SftpReq req,
SendPort mainSendPort,
SendErrorFunction sendError,
) async {
try {
mainSendPort.send(SftpWorkerStatus.preparing);
final watch = Stopwatch()..start();
@@ -65,12 +78,17 @@ Future<void> _download(SftpReq req, SendPort mainSendPort, SendErrorFunction sen
privateKey: req.privateKey,
jumpSpi: req.jumpSpi,
jumpPrivateKey: req.jumpPrivateKey,
privateKeysByKeyId: req.privateKeysByKeyId,
jumpSpisById: req.jumpSpisById,
knownHostFingerprints: req.knownHostFingerprints,
);
mainSendPort.send(SftpWorkerStatus.sshConnectted);
/// Create the directory if not exists
final dirPath = req.localPath.substring(0, req.localPath.lastIndexOf(Pfs.seperator));
final dirPath = req.localPath.substring(
0,
req.localPath.lastIndexOf(Pfs.seperator),
);
await Directory(dirPath).create(recursive: true);
/// Use [FileMode.write] to overwrite the file
@@ -92,7 +110,9 @@ Future<void> _download(SftpReq req, SendPort mainSendPort, SendErrorFunction sen
while (totalRead < size) {
final remaining = size - totalRead;
final chunkSize = remaining > defaultChunkSize ? defaultChunkSize : remaining;
final chunkSize = remaining > defaultChunkSize
? defaultChunkSize
: remaining;
dprint('Size: $size, Total Read: $totalRead, Chunk Size: $chunkSize');
final fileData = file.read(offset: totalRead, length: chunkSize);
@@ -113,7 +133,11 @@ Future<void> _download(SftpReq req, SendPort mainSendPort, SendErrorFunction sen
}
}
Future<void> _upload(SftpReq req, SendPort mainSendPort, SendErrorFunction sendError) async {
Future<void> _upload(
SftpReq req,
SendPort mainSendPort,
SendErrorFunction sendError,
) async {
try {
mainSendPort.send(SftpWorkerStatus.preparing);
final watch = Stopwatch()..start();
@@ -122,6 +146,8 @@ Future<void> _upload(SftpReq req, SendPort mainSendPort, SendErrorFunction sendE
privateKey: req.privateKey,
jumpSpi: req.jumpSpi,
jumpPrivateKey: req.jumpPrivateKey,
privateKeysByKeyId: req.privateKeysByKeyId,
jumpSpisById: req.jumpSpisById,
knownHostFingerprints: req.knownHostFingerprints,
);
mainSendPort.send(SftpWorkerStatus.sshConnectted);
@@ -139,7 +165,10 @@ Future<void> _upload(SftpReq req, SendPort mainSendPort, SendErrorFunction sendE
// If remote exists, overwrite it
final file = await sftp.open(
req.remotePath,
mode: SftpFileOpenMode.truncate | SftpFileOpenMode.create | SftpFileOpenMode.write,
mode:
SftpFileOpenMode.truncate |
SftpFileOpenMode.create |
SftpFileOpenMode.write,
);
final writer = file.write(
localFile,