apache / mina-sshd

Apache MINA sshd is a comprehensive Java library for client- and server-side SSH.
https://mina.apache.org/sshd-project/
Apache License 2.0
847 stars 353 forks source link

Can port forwarding be maintained? Currently, it is closed immediately after forwarding. #508

Closed hengboy closed 1 month ago

hengboy commented 1 month ago

Version

2.12.2

Bug description

try (SshClient client = SshClient.setUpDefaultClient()) {
            client.start();
            clientSession = client.connect(config.getUsername(), config.getServerIp(), config.getSshPort()).verify().getSession();
            boolean result = clientSession.auth().verify().await();
            if (!result) {
                throw new AgentException("Remote server ssh login failed.");
            } else {
                log.info("Connection to the remote server [{}] is successful.", config.getServerIp());
                // Local and remote port number binding forwarding
                this.portForwardingTracker = clientSession.createLocalPortForwardingTracker(new SshdSocketAddress(FORWARD_LOCAL_IP, config.getLocalPort()),
                        new SshdSocketAddress(config.getForwardTargetIp(), config.getForwardTargetPort()));
                log.info("Port forwarding binding is completed, local port : {}, forward IP: {}, forward port : {}",
                        config.getLocalPort(), config.getForwardTargetIp(), config.getForwardTargetPort());
            }
        } catch (Exception e) {
            throw new AgentException("Remote server port number forwarding failed.", e);
        }

Actual behavior

sshd logs

May 27 06:37:35 node1 sshd[1367281]: Accepted publickey for root from xxx.xxx.xx port 51881 ssh2: ED25519 SHA256:k0velrxirD8HY9rq/GMBqoBI......
May 27 06:37:35 node1 sshd[1367281]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
May 27 06:37:35 node1 systemd-logind[743]: New session 244 of user root.
May 27 06:37:35 node1 sshd[1367281]: pam_unix(sshd:session): session closed for user root
May 27 06:37:35 node1 systemd-logind[743]: Session 244 logged out. Waiting for processes to exit.
May 27 06:37:35 node1 systemd-logind[743]: Removed session 244.

Expected behavior

If the close method is not executed, the forwarding will continue.

Relevant log output

No response

Other information

No response

tomaswolf commented 1 month ago

You close the SshClient, which closes the session.

hengboy commented 1 month ago

@tomaswolf ClientSession#close method not called

hengboy commented 1 month ago

@tomaswolf Thank you very much, the problem is solved