Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tunnel device with ssh server in flutter?

Tags:

flutter

I have ssh access and connect that with the dartssh2 package. Like this:

final client = SSHClient(
  await SSHSocket.connect('address', port),
  username: 'username',
  onPasswordRequest: () => 'password',
);

Now, I want to create a VPN profile to tunnel devices with get permission access from the user and tunnel traffic with an ssh server. How can I do that?

like image 923
Marlen Schreiner Avatar asked Oct 15 '25 09:10

Marlen Schreiner


1 Answers

(Despite ChatGPT saying there's no way to do it, your question helped me)

I was able to do it this way, I have to connect to my server and forward all traffic through the port 27700.

      final String keyPem = """
-----BEGIN OPENSSH PRIVATE KEY-----
<YOUR_KEY>
-----END OPENSSH PRIVATE KEY-----
"""; // You can also grab it from a File, check the `dartssh2` documentation
      final String keyPemPassphrase = "<YOUR_PASSPHRASE_FOR_THAT_KEY_PEM>";

      final SSHSocket socket = await SSHSocket.connect("<DOMAIN_TO_CONNECT>", $PORT_TO_CONNECT);
      final SSHClient client = SSHClient(
        socket,
        username: "<USERNAME_FOR_SSH>",
        identities: [
          ...SSHKeyPair.fromPem(keyPem, keyPemPassphrase),
        ]
      );

      final ServerSocket serverSocket = await ServerSocket.bind(
        'localhost', // Where you want to forward it
        27700, // Port where you want to forward it
      );

      await for (final Socket socket in serverSocket) {
        final SSHForwardChannel forward = await client.forwardLocal(
          '192.168.202.17', // Internal port at the domain of the computer
          27700, // It's port
        );
        forward.stream.cast<List<int>>().pipe(socket);
        socket.pipe(forward.sink);
      }

works like a charm

like image 104
Rafael Ruiz Muñoz Avatar answered Oct 17 '25 01:10

Rafael Ruiz Muñoz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!