Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue while Local Port Forwarding #92

Open
Gabriel-Hobart opened this issue Nov 8, 2023 · 2 comments
Open

Issue while Local Port Forwarding #92

Gabriel-Hobart opened this issue Nov 8, 2023 · 2 comments

Comments

@Gabriel-Hobart
Copy link

Gabriel-Hobart commented Nov 8, 2023

I'm following the forward_local.dart example, but I keep getting the following error no matter what I do.

StateError (Bad state: Stream was already listened to)
#0      _RawServerSocket.listen (dart:io-patch/socket_patch.dart:1770:7)
#1      new _ForwardingStreamSubscription (dart:async/stream_pipe.dart:114:10)
#2      _ForwardingStream._createSubscription (dart:async/stream_pipe.dart:86:16)
#3      _ForwardingStream.listen (dart:async/stream_pipe.dart:81:12)
#4      _ServerSocket.listen (dart:io-patch/socket_patch.dart:2053:71)
#5      _StreamIterator._initializeOrDone (dart:async/stream_impl.dart:942:33)
#6      _StreamIterator.moveNext (dart:async/stream_impl.dart:918:12)
#7      _MyHomePageState.sshConnect (package:vnc/main.dart)
<asynchronous suspension>

Here is a snippet of my code.

int port = 0;

  SSHClient? _client;
  SSHSocket? _socket;
  ServerSocket? _serverSocket;

  bool authenticated = false;
  bool forwarded = false;

  bool connect = false;

  sshConnect(String targetIp) async {
    bool result = false;
    Object? exception;
    try {
      _socket = await SSHSocket.connect(targetIp, 22);

      _client = SSHClient(_socket!,
          username: SSH_USER,
          onPasswordRequest: () => SSH_PASSWORD,
          onAuthenticated: () => setState(() => authenticated = true));

      await _client!.authenticated;

      _serverSocket = await ServerSocket.bind(LOCALHOST, port);
      print('Listening on ${_serverSocket!.address.address}:${_serverSocket!.port}');

      port = _serverSocket!.port;

      await for (final socket in _serverSocket!) {
        final forward = await _client!.forwardLocal("locahost", 5900,
            localHost: LOCALHOST, localPort: port);
        forward.stream.cast<List<int>>().pipe(socket);
        socket.cast<List<int>>().pipe(forward.sink);
      }
      result = true;
    } catch (e) {
      await _serverSocket!.close();
      _client!.close();
      await _client!.done;
      _socket!.close();
      await _socket!.done;
      result = false;
      exception = e;
    }

    setState(() {
      if (result) {
        forwarded = true;
      } else {
        connect = false;
        authenticated = false;
        forwarded = false;

        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            duration: const Duration(seconds: 30),
            content: Text('Error: $exception'),
          ),
        );
      }
    });
  }

  @override
  void dispose() {
    if (_client?.isClosed ?? false) _client!.close();
    if (_serverSocket != null) _serverSocket!.close();
    if (_socket != null) _socket!.close();
    super.dispose();
  }
@Gabriel-Hobart
Copy link
Author

I'm running the code as a Windows app, and I have the dartssh2 2.9.1-pre package.

@Gabriel-Hobart
Copy link
Author

A summarized version of the relevant code would be this.

     var _socket = await SSHSocket.connect(TARGET_IP, 22);

     var  _client = SSHClient(_socket,
          username: SSH_USER,
          onPasswordRequest: () => SSH_PASSWORD);

      await _client.authenticated;

      var _serverSocket = await ServerSocket.bind(LOCALHOST, 0);
      print('Listening on ${_serverSocket.address.address}:${_serverSocket.port}');

      await for (final socket in _serverSocket) {
        final forward = await _client!.forwardLocal("locahost", 5900,
            localHost: LOCALHOST, localPort:  _serverSocket.port);
        forward.stream.cast<List<int>>().pipe(socket);
        socket.cast<List<int>>().pipe(forward.sink);
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant