mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
refactor: SSHClientX.exec
This commit is contained in:
@@ -13,65 +13,66 @@ typedef OnStdin = void Function(SSHSession session);
|
||||
typedef PwdRequestFunc = Future<String?> Function(String? user);
|
||||
|
||||
extension SSHClientX on SSHClient {
|
||||
/// TODO: delete [exec]
|
||||
Future<SSHSession> exec(
|
||||
String cmd, {
|
||||
OnStdout? onStderr,
|
||||
Future<(SSHSession, String)> exec(
|
||||
OnStdin onStdin, {
|
||||
String? entry,
|
||||
SSHPtyConfig? pty,
|
||||
OnStdout? onStdout,
|
||||
OnStdin? stdin,
|
||||
bool redirectToBash = false, // not working yet. do not use
|
||||
OnStdout? onStderr,
|
||||
bool stdout = true,
|
||||
bool stderr = true,
|
||||
Map<String, String>? env,
|
||||
}) async {
|
||||
final session = await execute(redirectToBash ? 'head -1 | bash' : cmd);
|
||||
|
||||
if (redirectToBash) {
|
||||
session.stdin.add('$cmd\n'.uint8List);
|
||||
}
|
||||
final session = await execute(
|
||||
entry ?? 'cat | sh',
|
||||
pty: pty,
|
||||
environment: env,
|
||||
);
|
||||
|
||||
final result = BytesBuilder(copy: false);
|
||||
final stdoutDone = Completer<void>();
|
||||
final stderrDone = Completer<void>();
|
||||
|
||||
if (onStdout != null) {
|
||||
session.stdout.listen(
|
||||
(e) => onStdout(e.string, session),
|
||||
onDone: stdoutDone.complete,
|
||||
);
|
||||
} else {
|
||||
stdoutDone.complete();
|
||||
}
|
||||
session.stdout.listen(
|
||||
(e) {
|
||||
onStdout?.call(e.string, session);
|
||||
if (stdout) result.add(e);
|
||||
},
|
||||
onDone: stdoutDone.complete,
|
||||
onError: stderrDone.completeError,
|
||||
);
|
||||
|
||||
if (onStderr != null) {
|
||||
session.stderr.listen(
|
||||
(e) => onStderr(e.string, session),
|
||||
onDone: stderrDone.complete,
|
||||
);
|
||||
} else {
|
||||
stderrDone.complete();
|
||||
}
|
||||
session.stderr.listen(
|
||||
(e) {
|
||||
onStderr?.call(e.string, session);
|
||||
if (stderr) result.add(e);
|
||||
},
|
||||
onDone: stderrDone.complete,
|
||||
onError: stderrDone.completeError,
|
||||
);
|
||||
|
||||
if (stdin != null) {
|
||||
stdin(session);
|
||||
}
|
||||
onStdin(session);
|
||||
|
||||
await stdoutDone.future;
|
||||
await stderrDone.future;
|
||||
|
||||
session.close();
|
||||
return session;
|
||||
return (session, result.takeBytes().string);
|
||||
}
|
||||
|
||||
Future<int?> execWithPwd(
|
||||
String cmd, {
|
||||
String script, {
|
||||
String? entry,
|
||||
BuildContext? context,
|
||||
OnStdout? onStdout,
|
||||
OnStdout? onStderr,
|
||||
OnStdin? stdin,
|
||||
bool redirectToBash = false, // not working yet. do not use
|
||||
required String id,
|
||||
}) async {
|
||||
var isRequestingPwd = false;
|
||||
final session = await exec(
|
||||
cmd,
|
||||
redirectToBash: redirectToBash,
|
||||
final (session, _) = await exec(
|
||||
(sess) {
|
||||
sess.stdin.add('$script\n'.uint8List);
|
||||
sess.stdin.close();
|
||||
},
|
||||
onStderr: (data, session) async {
|
||||
onStderr?.call(data, session);
|
||||
if (isRequestingPwd) return;
|
||||
@@ -84,88 +85,38 @@ extension SSHClientX on SSHClient {
|
||||
? await context.showPwdDialog(title: user, id: id)
|
||||
: null;
|
||||
if (pwd == null || pwd.isEmpty) {
|
||||
session.kill(SSHSignal.TERM);
|
||||
session.stdin.close();
|
||||
} else {
|
||||
session.stdin.add('$pwd\n'.uint8List);
|
||||
}
|
||||
isRequestingPwd = false;
|
||||
}
|
||||
},
|
||||
onStdout: (data, sink) async {
|
||||
onStdout?.call(data, sink);
|
||||
},
|
||||
stdin: stdin,
|
||||
onStdout: onStdout,
|
||||
entry: entry,
|
||||
);
|
||||
return session.exitCode;
|
||||
}
|
||||
|
||||
Future<Uint8List> runForOutput(
|
||||
String command, {
|
||||
bool runInPty = false,
|
||||
Future<String> execForOutput(
|
||||
String script, {
|
||||
SSHPtyConfig? pty,
|
||||
bool stdout = true,
|
||||
bool stderr = true,
|
||||
Map<String, String>? environment,
|
||||
Future<void> Function(SSHSession)? action,
|
||||
String? entry,
|
||||
Map<String, String>? env,
|
||||
}) async {
|
||||
final session = await execute(
|
||||
command,
|
||||
pty: runInPty ? const SSHPtyConfig() : null,
|
||||
environment: environment,
|
||||
final ret = await exec(
|
||||
(session) {
|
||||
session.stdin.add('$script\n'.uint8List);
|
||||
session.stdin.close();
|
||||
},
|
||||
pty: pty,
|
||||
env: env,
|
||||
stdout: stdout,
|
||||
stderr: stderr,
|
||||
entry: entry,
|
||||
);
|
||||
|
||||
final result = BytesBuilder(copy: false);
|
||||
final stdoutDone = Completer<void>();
|
||||
final stderrDone = Completer<void>();
|
||||
|
||||
session.stdout.listen(
|
||||
stdout ? result.add : (_) {},
|
||||
onDone: stdoutDone.complete,
|
||||
onError: stderrDone.completeError,
|
||||
);
|
||||
|
||||
session.stderr.listen(
|
||||
stderr ? result.add : (_) {},
|
||||
onDone: stderrDone.complete,
|
||||
onError: stderrDone.completeError,
|
||||
);
|
||||
|
||||
if (action != null) await action(session);
|
||||
|
||||
await stdoutDone.future;
|
||||
await stderrDone.future;
|
||||
|
||||
return result.takeBytes();
|
||||
}
|
||||
|
||||
Future<String> runScriptIn(
|
||||
String cmd, {
|
||||
String shell = '/bin/sh',
|
||||
bool stdout = true,
|
||||
bool stderr = true,
|
||||
}) async {
|
||||
final session = await execute('cat | $shell');
|
||||
|
||||
final result = BytesBuilder(copy: false);
|
||||
final stdoutDone = Completer<void>();
|
||||
final stderrDone = Completer<void>();
|
||||
|
||||
session.stdout.listen(
|
||||
stdout ? result.add : (_) {},
|
||||
onDone: stdoutDone.complete,
|
||||
onError: stderrDone.completeError,
|
||||
);
|
||||
session.stderr.listen(
|
||||
stderr ? result.add : (_) {},
|
||||
onDone: stderrDone.complete,
|
||||
onError: stderrDone.completeError,
|
||||
);
|
||||
|
||||
session.stdin.add('$cmd\n'.uint8List);
|
||||
session.stdin.close();
|
||||
|
||||
await stdoutDone.future;
|
||||
await stderrDone.future;
|
||||
|
||||
return result.takeBytes().string;
|
||||
return ret.$2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,8 +95,8 @@ Future<SSHClient> genClient(
|
||||
try {
|
||||
final ipPort = spi.fromStringUrl();
|
||||
return await SSHSocket.connect(
|
||||
ipPort.ip,
|
||||
ipPort.port,
|
||||
ipPort.$1,
|
||||
ipPort.$2,
|
||||
timeout: timeout,
|
||||
);
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user