hierynomus / hierynomus/sshj

Correctly disconnecting when using local and remote port forwarding

Open
#317 7 comments 4 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.7k
Forks
620
Avg merge
3d 23h
Merged PRs (30d)
11

Description

I am currently writing an application that involves connecting to an Amazon EC2 instance and then using SSHJ to tunnel RMI over SSH. I managed to get the port forwarding set up and working from looking at the provided tutorials but I get errors when disconnecting from the SSHClient.
I am starting the local port forwarder in a separate thread like so:
```java
executor.submit(() -> {
Parameters params = new Parameters("127.0.0.1", port, "127.0.0.1", port);
try(ServerSocket ss = new ServerSocket()){
ss.bind(new InetSocketAddress(params.getLocalHost(), params.getLocalPort()));
client.newLocalPortForwarder(params, ss).listen();
}
catch(IOException ex){
ex.printStackTrace(System.out);
System.out.println("problem (probably serversocket)");
}
});
```
I have also tried just using ``new Thread(...).start();`` instead of ``ExecutorService`` with the same results.

And the remote port forwarder (I was originally running this in it's own thread as well and calling ``client.getTransport().join()`` but it seemed to work fine without it):
```java
client.getRemotePortForwarder().bind(new Forward("127.0.0.1", 8080), new SocketForwardingConnectListener(new InetSocketAddress("127.0.0.1", 8080)));
client.getConnection().getKeepAlive().setKeepAliveInterval(30);
```

I am getting the following error when ``client.disconnect();`` is called:
```java
[main] INFO net.schmizz.sshj.connection.channel.direct.LocalPortForwarder - Closing listener on /127.0.0.1:5000
[main] INFO net.schmizz.sshj.transport.TransportImpl - Disconnected - BY_APPLICATION
[chan2soc] ERROR net.schmizz.sshj.common.StreamCopier - In pipe from < ChannelInputStream for Channel #8 > to java.net.SocketOutputStream@74480065
net.schmizz.sshj.transport.TransportException: [BY_APPLICATION] Disconnected
at net.schmizz.sshj.transport.TransportImpl.disconnect(TransportImpl.java:410)
at net.schmizz.sshj.transport.TransportImpl.disconnect(TransportImpl.java:401)
at net.schmizz.sshj.transport.TransportImpl.disconnect(TransportImpl.java:396)
at net.schmizz.sshj.SSHClient.disconnect(SSHClient.java:434)
at masterapplication.CloudConnection.closeConnection(CloudConnection.java:128)
at masterapplication.CommunicationController.cleanup(CommunicationController.java:401)
at view.ViewController.waitForGUI(ViewController.java:64)
at masterapplication.CommunicationController.main(CommunicationController.java:449)
```
Sometimes it will also give this error immediately after the one above (seems to only happen if I start the remote port forwarder):
```java
[sockmon] ERROR net.schmizz.concurrent.Promise - <> woke to: net.schmizz.sshj.transport.TransportException: [BY_APPLICATION] Disconnected
[soc2chan] ERROR net.schmizz.sshj.common.StreamCopier - In pipe from java.net.SocketInputStream@70ff915 to < ChannelOutputStream for Channel #8 >
java.net.SocketException: Socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.net.SocketInputStream.read(SocketInputStream.java:127)
at net.schmizz.sshj.common.StreamCopier.copy(StreamCopier.java:131)
at net.schmizz.sshj.common.StreamCopier$3.run(StreamCopier.java:110)
```

I assume that I'm just not closing something properly but I have tried things like keeping a reference to the returned ``LocalPortForwarder`` from ``client.newLocalPortForwarder(params, ss);`` and then closing it before calling ``client.disconnect();``

Any help with working out why this is happening would be greatly appreciated. It seems to be more of an annoyance than anything else because the tunnelling itself does work.
If you need any more information please let me know.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.