hierynomus / hierynomus/sshj

Hitting erros when using local and remote port forwarding

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

Description

Hi,I am currently writing an application and then using the port forwarding. but I get errors when disconnecting from the SSHClient. **The version are:**
```
jdk: 1.8.0_381
SSHJ: 0.31.0
```

And when I call the close() method in `SSHJProxyGateWay.java`, I hit erros when `gatewaySession.disconnect()` is called ,the erros in log are as follows:

```
2024-12-24 11:00:14.763 [http-nio-8880-exec-4]-INFO -net.schmizz.sshj.transport.TransportImpl - Disconnected - BY_APPLICATION
2024-12-24 11:00:22.851 [http-nio-8880-exec-4]-INFO -net.schmizz.sshj.connection.channel.direct.LocalPortForwarder - Closing listener on /127.0.0.1:52229
2024-12-24 11:00:22.852 [http-nio-8880-exec-4]-INFO -net.schmizz.sshj.transport.TransportImpl - Disconnected - BY_APPLICATION
2024-12-24 11:00:22.853 [chan2soc]-ERROR-net.schmizz.sshj.common.StreamCopier - In pipe from < ChannelInputStream for Channel #0 > to java.net.SocketOutputStream@7d79d57c
net.schmizz.sshj.transport.TransportException: Disconnected
at net.schmizz.sshj.transport.TransportImpl.disconnect(TransportImpl.java:428)
at net.schmizz.sshj.transport.TransportImpl.disconnect(TransportImpl.java:419)
at net.schmizz.sshj.transport.TransportImpl.disconnect(TransportImpl.java:414)
at net.schmizz.sshj.SSHClient.disconnect(SSHClient.java:435)
at com.SSHJProxyGateWay.close(SSHJProxyGateWay.java:80)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:750)
2024-12-24 11:00:27.850 [sockmon]-ERROR-net.schmizz.concurrent.Promise - <> woke to: net.schmizz.sshj.transport.TransportException: [BY_APPLICATION] Disconnected
```

I am starting the local port forwarder in a separate thread. The main codes are as follows:

**proxy.java:**
```
int proxyPort = 0;
SSHClient proxyssh = new SSHClient();
proxyssh.addHostKeyVerifier(new PromiscuousVerifier());
proxyssh.connect(gateWayInfo.getIp(),gateWayInfo.getPort());
proxyssh.authPassword(gateWayInfo.getUserName(),gateWayInfo.getPassword());
Parameters params = new Parameters("127.0.0.1", 0, parentProxyInfo.getIp(), parentProxyInfo.getPort());
final ServerSocket ss = new ServerSocket();
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress(params.getLocalHost(), params.getLocalPort()));
SSHJPortForwarder portForwarder = new SSHJPortForwarder(proxyssh,ss,params);
Thread thread = new Thread(portForwarder);
thread.start();
proxyPort = ss.getLocalPort();
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect("127.0.0.1",proxyPort);
ssh.authPassword(username,password);
Session session = ssh.startSession();
SSHJProxyGateWay proxyGateWay = new SSHJProxyGateWay(proxyssh,proxyPort,session,ssh);
return proxyGateWay;
```

**SSHJPortForwarder.java:**
```
public class SSHJPortForwarder extends Thread implements Closeable {
private SSHClient sshClient;
private ServerSocket serverSocket;
private Parameters parameters;
public SSHJPortForwarder(SSHClient sshClient, ServerSocket serverSocket,Parameters parameters) {
this.sshClient = sshClient;
this.serverSocket = serverSocket;
this.parameters = parameters;
}
@Override
public void run() {
sshClient.newLocalPortForwarder(parameters, serverSocket).listen();
}

@Override
public void close() throws IOException {
serverSocket.close();
this.join();
}
}
```

**SSHJProxyGateWay.java**
```
public class SSHJProxyGateWay {
private SHClient gatewaySession;
private Integer localPort;
private Session proxySession;
private SSHClient proxyCliet;

public SSHJProxyGateWay(SSHClient gatewaySession, Integer localPort, Session proxySession,SSHClient proxyCliet){
this.gatewaySession = gatewaySession;
this.localPort = localPort;
this.proxySession = proxySession;
this.proxyCliet = proxyCliet;
}

public void close() {
if (proxyCliet != null) {
proxyCliet.disconnect();
}
if (gatewaySession != null && gatewaySession.isConnected()) {
gatewaySession.disconnect();
}
}
}
```

I found that issue in [Correctly disconnecting when using local and remote port forwarding #317](https://github.com/hierynomus/sshj/issues/317) are the same, and has fixed it,but I still hit the erros.So,any help with working out why this is happening would be greatly appreciated.

@hierynomus

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.