mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-08 08:10:55 +03:00
Merge pull request #263 from joval/master
Modifications for working with SSH gateways
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 final List<LocalPortForwarder> forwarders = new ArrayList<LocalPortForwarder>();
|
||||||
|
|
||||||
/** 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,14 @@ public class SSHClient
|
|||||||
@Override
|
@Override
|
||||||
public void disconnect()
|
public void disconnect()
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
for (LocalPortForwarder forwarder : forwarders) {
|
||||||
|
try {
|
||||||
|
forwarder.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn("Error closing forwarder", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
forwarders.clear();
|
||||||
trans.disconnect();
|
trans.disconnect();
|
||||||
super.disconnect();
|
super.disconnect();
|
||||||
}
|
}
|
||||||
@@ -648,7 +658,9 @@ 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);
|
LocalPortForwarder forwarder = new LocalPortForwarder(conn, parameters, serverSocket);
|
||||||
|
forwarders.add(forwarder);
|
||||||
|
return forwarder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -48,12 +48,50 @@ public abstract class SocketClient {
|
|||||||
this.defaultPort = defaultPort;
|
this.defaultPort = defaultPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect(InetAddress host, int port) throws IOException {
|
/**
|
||||||
socket = socketFactory.createSocket();
|
* Connect to a host via a proxy.
|
||||||
socket.connect(new InetSocketAddress(host, port), connectTimeout);
|
* @param hostname The host name to connect to.
|
||||||
|
* @param proxy The proxy to connect via.
|
||||||
|
* @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory}
|
||||||
|
* into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public void connect(String hostname, Proxy proxy) throws IOException {
|
||||||
|
connect(hostname, defaultPort, proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to a host via a proxy.
|
||||||
|
* @param hostname The host name to connect to.
|
||||||
|
* @param port The port to connect to.
|
||||||
|
* @param proxy The proxy to connect via.
|
||||||
|
* @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory}
|
||||||
|
* into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public void connect(String hostname, int port, Proxy proxy) throws IOException {
|
||||||
|
this.hostname = hostname;
|
||||||
|
if (JavaVersion.isJava7OrEarlier() && proxy.type() == Proxy.Type.HTTP) {
|
||||||
|
// Java7 and earlier have no support for HTTP Connect proxies, return our custom socket.
|
||||||
|
socket = new Jdk7HttpProxySocket(proxy);
|
||||||
|
} else {
|
||||||
|
socket = new Socket(proxy);
|
||||||
|
}
|
||||||
|
socket.connect(new InetSocketAddress(hostname, port), connectTimeout);
|
||||||
onConnect();
|
onConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to a host via a proxy.
|
||||||
|
* @param host The host address to connect to.
|
||||||
|
* @param proxy The proxy to connect via.
|
||||||
|
* @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory}
|
||||||
|
* into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public void connect(InetAddress host, Proxy proxy) throws IOException {
|
||||||
|
connect(host, defaultPort, proxy);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connect to a host via a proxy.
|
* Connect to a host via a proxy.
|
||||||
@@ -75,23 +113,41 @@ public abstract class SocketClient {
|
|||||||
onConnect();
|
onConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect(String hostname, int port) throws IOException {
|
public void connect(String hostname) throws IOException {
|
||||||
this.hostname = hostname;
|
connect(hostname, defaultPort);
|
||||||
connect(InetAddress.getByName(hostname), port);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void connect(String hostname, int port) throws IOException {
|
||||||
* Connect to a host via a proxy.
|
if (hostname == null) {
|
||||||
* @param hostname The host name to connect to.
|
connect(InetAddress.getByName(null), port);
|
||||||
* @param port The port to connect to.
|
} else {
|
||||||
* @param proxy The proxy to connect via.
|
this.hostname = hostname;
|
||||||
* @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory}
|
socket = socketFactory.createSocket();
|
||||||
* into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor.
|
socket.connect(new InetSocketAddress(hostname, port), connectTimeout);
|
||||||
*/
|
onConnect();
|
||||||
@Deprecated
|
}
|
||||||
public void connect(String hostname, int port, Proxy proxy) throws IOException {
|
}
|
||||||
this.hostname = hostname;
|
|
||||||
connect(InetAddress.getByName(hostname), port, proxy);
|
public void connect(String hostname, int port, InetAddress localAddr, int localPort) throws IOException {
|
||||||
|
if (hostname == null) {
|
||||||
|
connect(InetAddress.getByName(null), port, localAddr, localPort);
|
||||||
|
} else {
|
||||||
|
this.hostname = hostname;
|
||||||
|
socket = socketFactory.createSocket();
|
||||||
|
socket.bind(new InetSocketAddress(localAddr, localPort));
|
||||||
|
socket.connect(new InetSocketAddress(hostname, port), connectTimeout);
|
||||||
|
onConnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connect(InetAddress host) throws IOException {
|
||||||
|
connect(host, defaultPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connect(InetAddress host, int port) throws IOException {
|
||||||
|
socket = socketFactory.createSocket();
|
||||||
|
socket.connect(new InetSocketAddress(host, port), connectTimeout);
|
||||||
|
onConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect(InetAddress host, int port, InetAddress localAddr, int localPort)
|
public void connect(InetAddress host, int port, InetAddress localAddr, int localPort)
|
||||||
@@ -102,43 +158,6 @@ public abstract class SocketClient {
|
|||||||
onConnect();
|
onConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect(String hostname, int port, InetAddress localAddr, int localPort) throws IOException {
|
|
||||||
this.hostname = hostname;
|
|
||||||
connect(InetAddress.getByName(hostname), port, localAddr, localPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connect(InetAddress host) throws IOException {
|
|
||||||
connect(host, defaultPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connect(String hostname) throws IOException {
|
|
||||||
connect(hostname, defaultPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connect to a host via a proxy.
|
|
||||||
* @param host The host address to connect to.
|
|
||||||
* @param proxy The proxy to connect via.
|
|
||||||
* @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory}
|
|
||||||
* into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public void connect(InetAddress host, Proxy proxy) throws IOException {
|
|
||||||
connect(host, defaultPort, proxy);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connect to a host via a proxy.
|
|
||||||
* @param hostname The host name to connect to.
|
|
||||||
* @param proxy The proxy to connect via.
|
|
||||||
* @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory}
|
|
||||||
* into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public void connect(String hostname, Proxy proxy) throws IOException {
|
|
||||||
connect(hostname, defaultPort, proxy);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void disconnect() throws IOException {
|
public void disconnect() throws IOException {
|
||||||
if (socket != null) {
|
if (socket != null) {
|
||||||
socket.close();
|
socket.close();
|
||||||
|
|||||||
@@ -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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
}
|
}
|
||||||
log.debug("Interrupted!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user