mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-07 15:50:57 +03:00
Close any LocalPortForwarders with the SSHClient that produced them (assuming they're still open).
This commit is contained in:
@@ -139,6 +139,8 @@ public class SSHClient
|
|||||||
/** {@code ssh-connection} service */
|
/** {@code ssh-connection} service */
|
||||||
protected final Connection conn;
|
protected final Connection conn;
|
||||||
|
|
||||||
|
private List<LocalPortForwarder> forwarders;
|
||||||
|
|
||||||
/** Default constructor. Initializes this object using {@link DefaultConfig}. */
|
/** Default constructor. Initializes this object using {@link DefaultConfig}. */
|
||||||
public SSHClient() {
|
public SSHClient() {
|
||||||
this(new DefaultConfig());
|
this(new DefaultConfig());
|
||||||
@@ -431,6 +433,15 @@ public class SSHClient
|
|||||||
@Override
|
@Override
|
||||||
public void disconnect()
|
public void disconnect()
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
if (forwarders != null) {
|
||||||
|
for (LocalPortForwarder forwarder : forwarders) {
|
||||||
|
try {
|
||||||
|
forwarder.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn("Error closing forwarder", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
trans.disconnect();
|
trans.disconnect();
|
||||||
super.disconnect();
|
super.disconnect();
|
||||||
}
|
}
|
||||||
@@ -648,7 +659,12 @@ public class SSHClient
|
|||||||
*/
|
*/
|
||||||
public LocalPortForwarder newLocalPortForwarder(LocalPortForwarder.Parameters parameters,
|
public LocalPortForwarder newLocalPortForwarder(LocalPortForwarder.Parameters parameters,
|
||||||
ServerSocket serverSocket) {
|
ServerSocket serverSocket) {
|
||||||
return new LocalPortForwarder(conn, parameters, serverSocket);
|
if (forwarders == null) {
|
||||||
|
forwarders = new ArrayList<LocalPortForwarder>();
|
||||||
|
}
|
||||||
|
LocalPortForwarder forwarder = new LocalPortForwarder(conn, parameters, serverSocket);
|
||||||
|
forwarders.add(forwarder);
|
||||||
|
return forwarder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static com.hierynomus.sshj.backport.Sockets.asCloseable;
|
import static com.hierynomus.sshj.backport.Sockets.asCloseable;
|
||||||
@@ -134,11 +135,33 @@ public class LocalPortForwarder {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
log.info("Listening on {}", serverSocket.getLocalSocketAddress());
|
log.info("Listening on {}", serverSocket.getLocalSocketAddress());
|
||||||
while (!Thread.currentThread().isInterrupted()) {
|
while (!Thread.currentThread().isInterrupted()) {
|
||||||
final Socket socket = serverSocket.accept();
|
try {
|
||||||
log.debug("Got connection from {}", socket.getRemoteSocketAddress());
|
final Socket socket = serverSocket.accept();
|
||||||
startChannel(socket);
|
log.debug("Got connection from {}", socket.getRemoteSocketAddress());
|
||||||
|
startChannel(socket);
|
||||||
|
} catch (SocketException e) {
|
||||||
|
if (!serverSocket.isClosed()) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (serverSocket.isClosed()) {
|
||||||
|
log.debug("LocalPortForwarder closed");
|
||||||
|
} else {
|
||||||
|
log.debug("LocalPortForwarder interrupted!");
|
||||||
}
|
}
|
||||||
log.debug("Interrupted!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* Close the ServerSocket that's listening for connections to forward.
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void close() throws IOException {
|
||||||
|
if (!serverSocket.isClosed()) {
|
||||||
|
log.info("Closing listener on {}", serverSocket.getLocalSocketAddress());
|
||||||
|
serverSocket.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user