new: jump serevr (#190)

This commit is contained in:
lollipopkit
2023-10-22 14:20:13 +08:00
parent e89ccd7ad4
commit 307ec25524
25 changed files with 183 additions and 58 deletions

View File

@@ -45,16 +45,18 @@ Future<SSHClient> genClient(
ServerPrivateInfo spi, {
void Function(GenSSHClientStatus)? onStatus,
String? privateKey,
Duration? timeout,
Duration timeout = const Duration(seconds: 5),
/// [ServerPrivateInfo] of the jump server
ServerPrivateInfo? jumpSpi,
}) async {
onStatus?.call(GenSSHClientStatus.socket);
late SSHSocket socket;
final duration = timeout ?? const Duration(seconds: 5);
SSHSocket? socket;
try {
socket = await SSHSocket.connect(
spi.ip,
spi.port,
timeout: duration,
timeout: timeout,
);
} catch (e) {
if (spi.alterUrl == null) rethrow;
@@ -63,17 +65,29 @@ Future<SSHClient> genClient(
socket = await SSHSocket.connect(
ipPort.ip,
ipPort.port,
timeout: duration,
timeout: timeout,
);
} catch (e) {
rethrow;
}
}
final forward = await () async {
if (jumpSpi != null) {
final jumpClient = await genClient(
jumpSpi,
privateKey: privateKey,
timeout: timeout,
);
// Use `0.0.0.0` as localhost to use all interfaces.
return await jumpClient.forwardLocal(spi.ip, spi.port, localHost: '0.0.0.0');
}
}();
if (spi.pubKeyId == null) {
onStatus?.call(GenSSHClientStatus.pwd);
return SSHClient(
socket,
forward ?? socket,
username: spi.user,
onPasswordRequest: () => spi.pwd,
);
@@ -82,7 +96,7 @@ Future<SSHClient> genClient(
onStatus?.call(GenSSHClientStatus.key);
return SSHClient(
socket,
forward ?? socket,
username: spi.user,
identities: await compute(loadIndentity, privateKey),
);

View File

@@ -27,6 +27,10 @@ class ServerPrivateInfo {
@HiveField(8)
final bool? autoConnect;
/// [id] of the jump server
@HiveField(9)
final String? jumpId;
final String id;
const ServerPrivateInfo({
@@ -39,9 +43,9 @@ class ServerPrivateInfo {
this.tags,
this.alterUrl,
this.autoConnect,
this.jumpId,
}) : id = '$user@$ip:$port';
/// TODO: if any field is changed, remember to update [id] [name]
ServerPrivateInfo.fromJson(Map<String, dynamic> json)
: ip = json["ip"].toString(),
port = json["port"] ?? 22,
@@ -54,7 +58,8 @@ class ServerPrivateInfo {
pubKeyId = json["pubKeyId"]?.toString(),
tags = json["tags"]?.cast<String>(),
alterUrl = json["alterUrl"]?.toString(),
autoConnect = json["autoConnect"];
autoConnect = json["autoConnect"],
jumpId = json["jumpId"];
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
@@ -67,16 +72,19 @@ class ServerPrivateInfo {
data["tags"] = tags;
data["alterUrl"] = alterUrl;
data["autoConnect"] = autoConnect;
data["jumpId"] = jumpId;
return data;
}
Server? get server => Pros.server.pick(spi: this);
Server? get jumpServer => Pros.server.pick(id: jumpId);
bool shouldReconnect(ServerPrivateInfo old) {
return id != old.id ||
pwd != old.pwd ||
pubKeyId != old.pubKeyId ||
alterUrl != old.alterUrl;
alterUrl != old.alterUrl ||
jumpId != old.jumpId;
}
_IpPort fromStringUrl() {

View File

@@ -26,13 +26,14 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
tags: (fields[6] as List?)?.cast<String>(),
alterUrl: fields[7] as String?,
autoConnect: fields[8] as bool?,
jumpId: fields[9] as String?,
);
}
@override
void write(BinaryWriter writer, ServerPrivateInfo obj) {
writer
..writeByte(9)
..writeByte(10)
..writeByte(0)
..write(obj.name)
..writeByte(1)
@@ -50,7 +51,9 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
..writeByte(7)
..write(obj.alterUrl)
..writeByte(8)
..write(obj.autoConnect);
..write(obj.autoConnect)
..writeByte(9)
..write(obj.jumpId);
}
@override

View File

@@ -117,7 +117,8 @@ class ServerProvider extends ChangeNotifier {
await _getData(spi);
return;
}
await Future.wait(_servers.values.map((s) async {
Future<void> connectFn(Server s) async {
if (onlyFailed) {
if (s.state != ServerState.failed) return;
_limiter.reset(s.spi.id);
@@ -133,7 +134,19 @@ class ServerProvider extends ChangeNotifier {
return;
}
return await _getData(s.spi);
}));
}
final directServers = <Server>[];
final proxyServers = <Server>[];
for (final s in _servers.values) {
if (s.spi.jumpId == null) {
directServers.add(s);
} else {
proxyServers.add(s);
}
}
await Future.wait(directServers.map(connectFn));
await Future.wait(proxyServers.map(connectFn));
}
Future<void> startAutoRefresh() async {
@@ -223,10 +236,6 @@ class ServerProvider extends ChangeNotifier {
// Only reconnect if neccessary
if (newSpi.shouldReconnect(old)) {
_servers[newSpi.id]?.client = await genClient(
newSpi,
timeout: Stores.setting.timeoutD,
);
refreshData(spi: newSpi);
}
@@ -262,6 +271,7 @@ class ServerProvider extends ChangeNotifier {
s.client = await genClient(
spi,
timeout: Stores.setting.timeoutD,
jumpSpi: spi.jumpId == null ? null : Stores.server.box.get(spi.jumpId),
);
} catch (e) {
_limiter.inc(sid);

View File

@@ -2,9 +2,9 @@
class BuildData {
static const String name = "ServerBox";
static const int build = 603;
static const int build = 606;
static const String engine = "3.13.7";
static const String buildAt = "2023-10-18 20:37:38";
static const int modifications = 7;
static const String buildAt = "2023-10-21 19:26:34";
static const int modifications = 4;
static const int script = 22;
}

View File

@@ -39,5 +39,7 @@ class GithubIds {
'fanzhebufan1',
'wcbing',
'balh55y',
'wc7086',
'michaelsara'
};
}

View File

@@ -104,6 +104,7 @@
"invalidVersion": "Ungültige Version",
"invalidVersionHelp": "Bitte stelle sicher, dass Docker korrekt installiert ist oder dass du eine nicht selbstkompilierte Version verwendest. Wenn du die oben genannten Probleme nicht hast, melde bitte einen Fehler auf {url}.",
"isBusy": "Is busy now",
"jumpServer": "Server springen",
"keepForeground": "Stelle sicher, dass die App geöffnet bleibt.",
"keyAuth": "Schlüsselauthentifzierung",
"keyboardCompatibility": "Mögliche Verbesserungen bei der Kompatibilität der Eingabemethoden",

View File

@@ -104,6 +104,7 @@
"invalidVersion": "Invalid version",
"invalidVersionHelp": "Please make sure that docker is installed correctly, or that you are using a non-self-compiled version. If you don't have the above issues, please submit an issue on {url}.",
"isBusy": "Is busy now",
"jumpServer": "Jump server",
"keepForeground": "Keep app foreground!",
"keyAuth": "Key Auth",
"keyboardCompatibility": "Possible to improve input method compatibility",

View File

@@ -104,6 +104,7 @@
"invalidVersion": "Versi tidak valid",
"invalidVersionHelp": "Pastikan Docker diinstal dengan benar, atau Anda menggunakan versi yang tidak dikompilasi. Jika Anda tidak memiliki masalah di atas, silakan kirimkan masalah pada {url}.",
"isBusy": "Sibuk sekarang",
"jumpServer": "Lompat server",
"keepForeground": "Simpan Aplikasi Foreground!",
"keyAuth": "Auth kunci",
"keyboardCompatibility": "Mungkin untuk meningkatkan kompatibilitas metode input",

View File

@@ -104,6 +104,7 @@
"invalidVersion": "不支持的版本",
"invalidVersionHelp": "请确保正确安装了docker或者使用的非自编译版本。如果没有以上问题请在 {url} 提交问题。",
"isBusy": "当前正忙",
"jumpServer": "跳板服务器",
"keepForeground": "请保持应用处于前台!",
"keyAuth": "密钥认证",
"keyboardCompatibility": "可能会改善输入法兼容性",

View File

@@ -104,6 +104,7 @@
"invalidVersion": "不支持的版本",
"invalidVersionHelp": "請確保正確安裝了docker或者使用的非自編譯版本。如果沒有以上問題請在 {url} 提交問題。",
"isBusy": "當前正忙",
"jumpServer": "跳板服務器",
"keepForeground": "請保持應用處於前台!",
"keyAuth": "密鑰認證",
"keyboardCompatibility": "可能會改善輸入法兼容性",

View File

@@ -95,11 +95,11 @@ void _setupProviders() {
Future<void> _initHive() async {
await Hive.initFlutter();
// 以 typeId 为顺序
Hive.registerAdapter(PrivateKeyInfoAdapter());
Hive.registerAdapter(SnippetAdapter());
Hive.registerAdapter(ServerPrivateInfoAdapter());
Hive.registerAdapter(VirtKeyAdapter());
Hive.registerAdapter(NetViewTypeAdapter());
Hive.registerAdapter(PrivateKeyInfoAdapter()); // 1
Hive.registerAdapter(SnippetAdapter()); // 2
Hive.registerAdapter(ServerPrivateInfoAdapter()); // 3
Hive.registerAdapter(VirtKeyAdapter()); // 4
Hive.registerAdapter(NetViewTypeAdapter()); // 5
}
void _setupLogger() {

View File

@@ -5,6 +5,7 @@ import 'package:toolbox/core/extension/context/dialog.dart';
import 'package:toolbox/core/extension/context/locale.dart';
import 'package:toolbox/core/extension/context/snackbar.dart';
import 'package:toolbox/data/res/provider.dart';
import 'package:toolbox/view/widget/expand_tile.dart';
import '../../../core/route.dart';
import '../../../data/model/server/private_key_info.dart';
@@ -43,6 +44,7 @@ class _ServerEditPageState extends State<ServerEditPage> {
final _keyIdx = ValueNotifier<int?>(null);
final _autoConnect = ValueNotifier(true);
final _jumpServer = ValueNotifier<String?>(null);
var _tags = <String>[];
@@ -72,6 +74,9 @@ class _ServerEditPageState extends State<ServerEditPage> {
if (widget.spi?.autoConnect != null) {
_autoConnect.value = widget.spi!.autoConnect!;
}
if (widget.spi?.jumpId != null) {
_jumpServer.value = widget.spi!.jumpId;
}
}
}
@@ -189,6 +194,7 @@ class _ServerEditPageState extends State<ServerEditPage> {
onRenameTag: Pros.server.renameTag,
),
_buildAuth(),
_buildJumpServer(),
ListTile(
title: Text(l10n.autoConnect),
trailing: ValueBuilder(
@@ -316,6 +322,45 @@ class _ServerEditPageState extends State<ServerEditPage> {
);
}
Widget _buildJumpServer() {
return ValueBuilder(
listenable: _jumpServer,
build: () {
final children = Pros.server.servers
.where((element) => element.spi.jumpId == null)
.where((element) => element.spi.id != widget.spi?.id)
.map(
(e) => ListTile(
title: Text(e.spi.name),
subtitle: Text(e.spi.id, style: UIs.textGrey),
trailing: Radio<String>(
groupValue: _jumpServer.value,
value: e.spi.id,
onChanged: (val) => _jumpServer.value = val,
),
onTap: () {
_jumpServer.value = e.spi.id;
},
),
)
.toList();
children.add(ListTile(
title: Text(l10n.clear),
trailing: const Icon(Icons.clear),
onTap: () => _jumpServer.value = null,
));
return CardX(
ExpandTile(
leading: const Icon(Icons.map),
initiallyExpanded: _jumpServer.value != null,
title: Text(l10n.jumpServer),
children: children,
),
);
},
);
}
void _onSave() async {
if (_ipController.text.isEmpty) {
context.showSnackBar(l10n.plzEnterHost);
@@ -364,6 +409,7 @@ class _ServerEditPageState extends State<ServerEditPage> {
tags: _tags,
alterUrl: _altUrlController.text.isEmpty ? null : _altUrlController.text,
autoConnect: _autoConnect.value,
jumpId: _jumpServer.value,
);
if (widget.spi == null) {

View File

@@ -5,6 +5,7 @@ const _shape = Border();
class ExpandTile extends ExpansionTile {
const ExpandTile({
super.key,
super.leading,
required super.title,
super.children,
super.subtitle,