fix: jump server (#190)

This commit is contained in:
lollipopkit
2023-10-30 17:10:29 +08:00
parent 4971239bfc
commit bff799afd9
9 changed files with 103 additions and 73 deletions

View File

@@ -18,8 +18,9 @@ class ServerPrivateInfo {
final String user;
@HiveField(4)
final String? pwd;
/// [id] of private key
@HiveField(5)
final String? pubKeyId;
final String? keyId;
@HiveField(6)
final List<String>? tags;
@HiveField(7)
@@ -39,7 +40,7 @@ class ServerPrivateInfo {
required this.port,
required this.user,
required this.pwd,
this.pubKeyId,
this.keyId,
this.tags,
this.alterUrl,
this.autoConnect,
@@ -55,7 +56,7 @@ class ServerPrivateInfo {
name = json["name"]?.toString() ??
'${json["user"]?.toString() ?? 'root'}@${json["ip"].toString()}:${json["port"] ?? 22}',
pwd = json["authorization"]?.toString(),
pubKeyId = json["pubKeyId"]?.toString(),
keyId = json["pubKeyId"]?.toString(),
tags = json["tags"]?.cast<String>(),
alterUrl = json["alterUrl"]?.toString(),
autoConnect = json["autoConnect"],
@@ -68,7 +69,7 @@ class ServerPrivateInfo {
data["port"] = port;
data["user"] = user;
data["authorization"] = pwd;
data["pubKeyId"] = pubKeyId;
data["pubKeyId"] = keyId;
data["tags"] = tags;
data["alterUrl"] = alterUrl;
data["autoConnect"] = autoConnect;
@@ -82,7 +83,7 @@ class ServerPrivateInfo {
bool shouldReconnect(ServerPrivateInfo old) {
return id != old.id ||
pwd != old.pwd ||
pubKeyId != old.pubKeyId ||
keyId != old.keyId ||
alterUrl != old.alterUrl ||
jumpId != old.jumpId;
}

View File

@@ -22,7 +22,7 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
port: fields[2] as int,
user: fields[3] as String,
pwd: fields[4] as String?,
pubKeyId: fields[5] as String?,
keyId: fields[5] as String?,
tags: (fields[6] as List?)?.cast<String>(),
alterUrl: fields[7] as String?,
autoConnect: fields[8] as bool?,
@@ -45,7 +45,7 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
..writeByte(4)
..write(obj.pwd)
..writeByte(5)
..write(obj.pubKeyId)
..write(obj.keyId)
..writeByte(6)
..write(obj.tags)
..writeByte(7)

View File

@@ -1,5 +1,8 @@
import 'dart:async';
import 'package:toolbox/data/res/logger.dart';
import 'package:toolbox/data/res/store.dart';
import '../../../core/utils/server.dart';
import '../server/server_private_info.dart';
import 'worker.dart';
@@ -10,6 +13,8 @@ class SftpReq {
final String localPath;
final SftpReqType type;
String? privateKey;
ServerPrivateInfo? jumpSpi;
String? jumpPrivateKey;
SftpReq(
this.spi,
@@ -17,8 +22,13 @@ class SftpReq {
this.localPath,
this.type,
) {
if (spi.pubKeyId != null) {
privateKey = getPrivateKey(spi.pubKeyId!);
final keyId = spi.keyId;
if (keyId != null) {
privateKey = getPrivateKey(keyId);
}
if (spi.jumpId != null) {
jumpSpi = Stores.server.box.get(spi.jumpId);
jumpPrivateKey = Stores.key.get(jumpSpi?.keyId)?.key;
}
}
}
@@ -83,7 +93,9 @@ class SftpReqStatus {
break;
default:
error = Exception('sftp worker event: $event');
Loggers.app.warning(error);
dispose();
break;
}
notifyListeners();
}

View File

@@ -81,7 +81,12 @@ Future<void> _download(
try {
mainSendPort.send(SftpWorkerStatus.preparing);
final watch = Stopwatch()..start();
final client = await genClient(req.spi, privateKey: req.privateKey);
final client = await genClient(
req.spi,
privateKey: req.privateKey,
jumpSpi: req.jumpSpi,
jumpPrivateKey: req.jumpPrivateKey,
);
mainSendPort.send(SftpWorkerStatus.sshConnectted);
/// Create the directory if not exists
@@ -131,7 +136,12 @@ Future<void> _upload(
try {
mainSendPort.send(SftpWorkerStatus.preparing);
final watch = Stopwatch()..start();
final client = await genClient(req.spi, privateKey: req.privateKey);
final client = await genClient(
req.spi,
privateKey: req.privateKey,
jumpSpi: req.jumpSpi,
jumpPrivateKey: req.jumpPrivateKey,
);
mainSendPort.send(SftpWorkerStatus.sshConnectted);
final local = File(req.localPath);

View File

@@ -280,14 +280,11 @@ class ServerProvider extends ChangeNotifier {
_setServerState(s, ServerState.connecting);
final time1 = DateTime.now();
final jumpSpi =
spi.jumpId == null ? null : Stores.server.box.get(spi.jumpId);
try {
s.client = await genClient(
spi,
timeout: Stores.setting.timeoutD,
jumpSpi: jumpSpi,
);
} catch (e) {
_limiter.inc(sid);
@@ -304,8 +301,8 @@ class ServerProvider extends ChangeNotifier {
if (spi.jumpId == null) {
Loggers.app.info('Connected to ${spi.name} in $spentTime ms.');
} else {
Loggers.app.info(
'Connected to ${spi.name} via ${jumpSpi?.name} in $spentTime ms.');
Loggers.app
.info('Connected to ${spi.name} via jump server in $spentTime ms.');
}
_setServerState(s, ServerState.connected);

View File

@@ -2,9 +2,9 @@
class BuildData {
static const String name = "ServerBox";
static const int build = 612;
static const int build = 616;
static const String engine = "3.13.8";
static const String buildAt = "2023-10-28 17:13:40";
static const String buildAt = "2023-10-30 14:48:00";
static const int modifications = 5;
static const int script = 22;
static const int script = 23;
}