Skip to content

Commit

Permalink
opt.: alterUrl (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Aug 25, 2024
1 parent bcbf1fb commit 931c5f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
18 changes: 12 additions & 6 deletions lib/core/utils/server.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:dartssh2/dartssh2.dart';
import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/foundation.dart';
import 'package:server_box/data/model/app/error.dart';
import 'package:server_box/data/res/store.dart';
Expand Down Expand Up @@ -62,6 +63,8 @@ Future<SSHClient> genClient(
}) async {
onStatus?.call(GenSSHClientStatus.socket);

String? alterUser;

final socket = await () async {
// Proxy
final jumpSpi_ = () {
Expand Down Expand Up @@ -90,16 +93,19 @@ Future<SSHClient> genClient(
spi.port,
timeout: timeout,
);
} catch (e) {
} catch (e, s) {
Loggers.app.warning('genClient', e, s);
if (spi.alterUrl == null) rethrow;
try {
final ipPort = spi.fromStringUrl();
final res = spi.fromStringUrl();
alterUser = res.$2;
return await SSHSocket.connect(
ipPort.$1,
ipPort.$2,
res.$1,
res.$3,
timeout: timeout,
);
} catch (e) {
} catch (e, s) {
Loggers.app.warning('genClient alterUrl', e, s);
rethrow;
}
}
Expand All @@ -110,7 +116,7 @@ Future<SSHClient> genClient(
onStatus?.call(GenSSHClientStatus.pwd);
return SSHClient(
socket,
username: spi.user,
username: alterUser ?? spi.user,
onPasswordRequest: () => spi.pwd,
onUserInfoRequest: onKeyboardInteractive,
// printDebug: debugPrint,
Expand Down
5 changes: 3 additions & 2 deletions lib/data/model/server/server_private_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ extension Spix on Spi {
custom?.cmds != old.custom?.cmds;
}

(String, int) fromStringUrl() {
(String ip, String usr, int port) fromStringUrl() {
if (alterUrl == null) {
throw SSHErr(type: SSHErrType.connect, message: 'alterUrl is null');
}
final splited = alterUrl!.split('@');
if (splited.length != 2) {
throw SSHErr(type: SSHErrType.connect, message: 'alterUrl no @');
}
final usr = splited[0];
final splited2 = splited[1].split(':');
if (splited2.length != 2) {
throw SSHErr(type: SSHErrType.connect, message: 'alterUrl no :');
Expand All @@ -115,7 +116,7 @@ extension Spix on Spi {
if (port <= 0 || port > 65535) {
throw SSHErr(type: SSHErrType.connect, message: 'alterUrl port error');
}
return (ip_, port_);
return (ip_, usr, port_);
}

static const example = Spi(
Expand Down

0 comments on commit 931c5f0

Please sign in to comment.