mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
opt.: ssh page sftp path checking (#763)
This commit is contained in:
@@ -27,8 +27,6 @@ part 'init.dart';
|
|||||||
part 'keyboard.dart';
|
part 'keyboard.dart';
|
||||||
part 'virt_key.dart';
|
part 'virt_key.dart';
|
||||||
|
|
||||||
const _echoPWD = 'echo \$PWD';
|
|
||||||
|
|
||||||
final class SshPageArgs {
|
final class SshPageArgs {
|
||||||
final Spi spi;
|
final Spi spi;
|
||||||
final String? initCmd;
|
final String? initCmd;
|
||||||
|
|||||||
@@ -69,22 +69,60 @@ extension _VirtKey on SSHPageState {
|
|||||||
snippet.runInTerm(_terminal, widget.args.spi);
|
snippet.runInTerm(_terminal, widget.args.spi);
|
||||||
break;
|
break;
|
||||||
case VirtualKeyFunc.file:
|
case VirtualKeyFunc.file:
|
||||||
// get $PWD from SSH session
|
// get $PWD from SSH session with unique markers
|
||||||
_terminal.textInput(_echoPWD);
|
const marker = 'ServerBoxOutput';
|
||||||
|
const markerEnd = 'ServerBoxEnd';
|
||||||
|
const pwdCommand = 'echo "$marker:\$PWD:$markerEnd"';
|
||||||
|
_terminal.textInput(pwdCommand);
|
||||||
_terminal.keyInput(TerminalKey.enter);
|
_terminal.keyInput(TerminalKey.enter);
|
||||||
|
|
||||||
|
// Wait for output with timeout
|
||||||
|
String? initPath;
|
||||||
|
await Future.delayed(const Duration(milliseconds: 700));
|
||||||
|
final startTime = DateTime.now();
|
||||||
|
final timeout = const Duration(seconds: 3);
|
||||||
|
|
||||||
|
while (initPath == null) {
|
||||||
|
// Check if we've exceeded timeout
|
||||||
|
if (DateTime.now().difference(startTime) > timeout) {
|
||||||
|
contextSafe?.showRoundDialog(
|
||||||
|
title: libL10n.error,
|
||||||
|
child: Text(libL10n.empty),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search for marked output in terminal buffer
|
||||||
final cmds = _terminal.buffer.lines.toList();
|
final cmds = _terminal.buffer.lines.toList();
|
||||||
// wait for the command to finish
|
for (final line in cmds.reversed) {
|
||||||
await Future.delayed(const Duration(milliseconds: 777));
|
final lineStr = line.toString();
|
||||||
// the line below `echo $PWD` is the current path
|
if (lineStr.contains(marker) && lineStr.contains(markerEnd)) {
|
||||||
final idx = cmds.lastIndexWhere((e) => e.toString().contains(_echoPWD));
|
// Extract path between markers
|
||||||
final initPath = cmds.elementAtOrNull(idx + 1)?.toString();
|
final start = lineStr.indexOf(marker) + marker.length + 1; // +1 for ':'
|
||||||
if (initPath == null || !initPath.startsWith('/')) {
|
final end = lineStr.indexOf(markerEnd) - 1; // -1 for ':'
|
||||||
|
if (start < end) {
|
||||||
|
initPath = lineStr.substring(start, end);
|
||||||
|
if (initPath.isEmpty || initPath == '\$PWD') {
|
||||||
|
initPath = null;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Short wait before checking again
|
||||||
|
await Future.delayed(const Duration(milliseconds: 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initPath.startsWith('/')) {
|
||||||
context.showRoundDialog(
|
context.showRoundDialog(
|
||||||
title: libL10n.error,
|
title: libL10n.error,
|
||||||
child: Text('${l10n.remotePath}: $initPath'),
|
child: Text('${l10n.remotePath}: $initPath'),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final args = SftpPageArgs(spi: widget.args.spi, initPath: initPath);
|
final args = SftpPageArgs(spi: widget.args.spi, initPath: initPath);
|
||||||
SftpPage.route.go(context, args);
|
SftpPage.route.go(context, args);
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user