mirror of https://github.com/apache/jclouds.git
add support for jsch proxy
This commit is contained in:
parent
c5e3b24d3e
commit
3afdc3550e
|
@ -50,6 +50,7 @@ import org.jclouds.http.handlers.BackoffLimitedRetryHandler;
|
||||||
import org.jclouds.io.Payload;
|
import org.jclouds.io.Payload;
|
||||||
import org.jclouds.io.Payloads;
|
import org.jclouds.io.Payloads;
|
||||||
import org.jclouds.logging.Logger;
|
import org.jclouds.logging.Logger;
|
||||||
|
import org.jclouds.proxy.ProxyConfig;
|
||||||
import org.jclouds.rest.AuthorizationException;
|
import org.jclouds.rest.AuthorizationException;
|
||||||
import org.jclouds.ssh.SshClient;
|
import org.jclouds.ssh.SshClient;
|
||||||
import org.jclouds.ssh.SshException;
|
import org.jclouds.ssh.SshException;
|
||||||
|
@ -123,7 +124,8 @@ public class JschSshClient implements SshClient {
|
||||||
final String user;
|
final String user;
|
||||||
final String host;
|
final String host;
|
||||||
|
|
||||||
public JschSshClient(BackoffLimitedRetryHandler backoffLimitedRetryHandler, HostAndPort socket,
|
|
||||||
|
public JschSshClient(ProxyConfig proxyConfig, BackoffLimitedRetryHandler backoffLimitedRetryHandler, HostAndPort socket,
|
||||||
LoginCredentials loginCredentials, int timeout) {
|
LoginCredentials loginCredentials, int timeout) {
|
||||||
this.user = checkNotNull(loginCredentials, "loginCredentials").getUser();
|
this.user = checkNotNull(loginCredentials, "loginCredentials").getUser();
|
||||||
this.host = checkNotNull(socket, "socket").getHostText();
|
this.host = checkNotNull(socket, "socket").getHostText();
|
||||||
|
@ -142,7 +144,7 @@ public class JschSshClient implements SshClient {
|
||||||
fingerPrint, sha1, host, socket.getPort());
|
fingerPrint, sha1, host, socket.getPort());
|
||||||
}
|
}
|
||||||
sessionConnection = SessionConnection.builder().hostAndPort(HostAndPort.fromParts(host, socket.getPort())).loginCredentials(
|
sessionConnection = SessionConnection.builder().hostAndPort(HostAndPort.fromParts(host, socket.getPort())).loginCredentials(
|
||||||
loginCredentials).connectTimeout(timeout).sessionTimeout(timeout).build();
|
loginCredentials).proxy(checkNotNull(proxyConfig, "proxyConfig")).connectTimeout(timeout).sessionTimeout(timeout).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -467,8 +469,8 @@ public class JschSshClient implements SshClient {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ExecChannel create() throws Exception {
|
public ExecChannel create() throws Exception {
|
||||||
this.sessionConnection = acquire(SessionConnection.builder().fromSessionConnection(
|
this.sessionConnection = acquire(SessionConnection.builder().from(JschSshClient.this.sessionConnection)
|
||||||
JschSshClient.this.sessionConnection).sessionTimeout(0).build());
|
.sessionTimeout(0).build());
|
||||||
String channel = "exec";
|
String channel = "exec";
|
||||||
executor = (ChannelExec) sessionConnection.openChannel(channel);
|
executor = (ChannelExec) sessionConnection.openChannel(channel);
|
||||||
executor.setCommand(command);
|
executor.setCommand(command);
|
||||||
|
|
|
@ -20,26 +20,35 @@ package org.jclouds.ssh.jsch;
|
||||||
|
|
||||||
import static com.google.common.base.Objects.equal;
|
import static com.google.common.base.Objects.equal;
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
|
import org.jclouds.domain.Credentials;
|
||||||
import org.jclouds.domain.LoginCredentials;
|
import org.jclouds.domain.LoginCredentials;
|
||||||
|
import org.jclouds.javax.annotation.Nullable;
|
||||||
|
import org.jclouds.proxy.ProxyConfig;
|
||||||
import org.jclouds.ssh.jsch.JschSshClient.Connection;
|
import org.jclouds.ssh.jsch.JschSshClient.Connection;
|
||||||
|
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
|
import com.google.common.base.Optional;
|
||||||
import com.google.common.net.HostAndPort;
|
import com.google.common.net.HostAndPort;
|
||||||
import com.jcraft.jsch.JSch;
|
import com.jcraft.jsch.JSch;
|
||||||
|
import com.jcraft.jsch.Proxy;
|
||||||
|
import com.jcraft.jsch.ProxyHTTP;
|
||||||
|
import com.jcraft.jsch.ProxySOCKS5;
|
||||||
import com.jcraft.jsch.Session;
|
import com.jcraft.jsch.Session;
|
||||||
|
|
||||||
public class SessionConnection implements Connection<Session> {
|
public final class SessionConnection implements Connection<Session> {
|
||||||
public static Builder builder() {
|
public static Builder builder() {
|
||||||
return new Builder();
|
return new Builder();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public final static class Builder {
|
||||||
|
|
||||||
protected HostAndPort hostAndPort;
|
private HostAndPort hostAndPort;
|
||||||
protected LoginCredentials loginCredentials;
|
private LoginCredentials loginCredentials;
|
||||||
protected int connectTimeout;
|
private Optional<Proxy> proxy = Optional.absent();
|
||||||
protected int sessionTimeout;
|
private int connectTimeout;
|
||||||
|
private int sessionTimeout;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see SessionConnection#getHostAndPort()
|
* @see SessionConnection#getHostAndPort()
|
||||||
|
@ -57,6 +66,39 @@ public class SessionConnection implements Connection<Session> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see SessionConnection#getProxy()
|
||||||
|
*/
|
||||||
|
public Builder proxy(Proxy proxy) {
|
||||||
|
this.proxy = Optional.fromNullable(proxy);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see #proxy(Proxy)
|
||||||
|
*/
|
||||||
|
public Builder proxy(ProxyConfig proxyConfig) {
|
||||||
|
Optional<HostAndPort> proxyEndpoint = proxyConfig.getProxy();
|
||||||
|
if (!proxyEndpoint.isPresent())
|
||||||
|
return proxy((Proxy) null);
|
||||||
|
|
||||||
|
Optional<Credentials> creds = proxyConfig.getCredentials();
|
||||||
|
switch (proxyConfig.getType()) {
|
||||||
|
case HTTP:
|
||||||
|
ProxyHTTP httpProxy = new ProxyHTTP(proxyEndpoint.get().getHostText(), proxyEndpoint.get().getPort());
|
||||||
|
if (creds.isPresent())
|
||||||
|
httpProxy.setUserPasswd(creds.get().identity, creds.get().credential);
|
||||||
|
return proxy(httpProxy);
|
||||||
|
case SOCKS:
|
||||||
|
ProxySOCKS5 socksProxy = new ProxySOCKS5(proxyEndpoint.get().getHostText(), proxyEndpoint.get().getPort());
|
||||||
|
if (creds.isPresent())
|
||||||
|
socksProxy.setUserPasswd(creds.get().identity, creds.get().credential);
|
||||||
|
return proxy(socksProxy);
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException(proxyConfig.getType() + " not supported");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see SessionConnection#getConnectTimeout()
|
* @see SessionConnection#getConnectTimeout()
|
||||||
*/
|
*/
|
||||||
|
@ -74,27 +116,30 @@ public class SessionConnection implements Connection<Session> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public SessionConnection build() {
|
public SessionConnection build() {
|
||||||
return new SessionConnection(hostAndPort, loginCredentials, connectTimeout, sessionTimeout);
|
return new SessionConnection(hostAndPort, loginCredentials, proxy, connectTimeout, sessionTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Builder fromSessionConnection(SessionConnection in) {
|
public Builder from(SessionConnection in) {
|
||||||
return hostAndPort(in.getHostAndPort()).connectTimeout(in.getConnectTimeout()).loginCredentials(
|
return hostAndPort(in.hostAndPort).loginCredentials(in.loginCredentials).proxy(in.proxy.orNull())
|
||||||
in.getLoginCredentials());
|
.connectTimeout(in.connectTimeout).sessionTimeout(in.sessionTimeout);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private SessionConnection(HostAndPort hostAndPort, LoginCredentials loginCredentials, int connectTimeout,
|
}
|
||||||
int sessionTimeout) {
|
|
||||||
this.hostAndPort = hostAndPort;
|
private SessionConnection(HostAndPort hostAndPort, LoginCredentials loginCredentials, Optional<Proxy> proxy,
|
||||||
this.loginCredentials = loginCredentials;
|
int connectTimeout, int sessionTimeout) {
|
||||||
|
this.hostAndPort = checkNotNull(hostAndPort, "hostAndPort");
|
||||||
|
this.loginCredentials = checkNotNull(loginCredentials, "loginCredentials for %", hostAndPort);
|
||||||
this.connectTimeout = connectTimeout;
|
this.connectTimeout = connectTimeout;
|
||||||
this.sessionTimeout = sessionTimeout;
|
this.sessionTimeout = sessionTimeout;
|
||||||
|
this.proxy = checkNotNull(proxy, "proxy for %", hostAndPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final byte[] emptyPassPhrase = new byte[0];
|
private static final byte[] emptyPassPhrase = new byte[0];
|
||||||
|
|
||||||
private final HostAndPort hostAndPort;
|
private final HostAndPort hostAndPort;
|
||||||
private final LoginCredentials loginCredentials;
|
private final LoginCredentials loginCredentials;
|
||||||
|
private final Optional<Proxy> proxy;
|
||||||
private final int connectTimeout;
|
private final int connectTimeout;
|
||||||
private final int sessionTimeout;
|
private final int sessionTimeout;
|
||||||
|
|
||||||
|
@ -126,6 +171,8 @@ public class SessionConnection implements Connection<Session> {
|
||||||
java.util.Properties config = new java.util.Properties();
|
java.util.Properties config = new java.util.Properties();
|
||||||
config.put("StrictHostKeyChecking", "no");
|
config.put("StrictHostKeyChecking", "no");
|
||||||
session.setConfig(config);
|
session.setConfig(config);
|
||||||
|
if (proxy.isPresent())
|
||||||
|
session.setProxy(proxy.get());
|
||||||
session.connect(connectTimeout);
|
session.connect(connectTimeout);
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
@ -145,6 +192,14 @@ public class SessionConnection implements Connection<Session> {
|
||||||
return loginCredentials;
|
return loginCredentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return proxy used for this connection
|
||||||
|
*/
|
||||||
|
public Optional<Proxy> getProxy() {
|
||||||
|
return proxy;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return how long to wait for the initial connection to be made
|
* @return how long to wait for the initial connection to be made
|
||||||
|
@ -165,6 +220,7 @@ public class SessionConnection implements Connection<Session> {
|
||||||
*
|
*
|
||||||
* @return the current session or {@code null} if not connected
|
* @return the current session or {@code null} if not connected
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
public Session getSession() {
|
public Session getSession() {
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
@ -187,10 +243,12 @@ public class SessionConnection implements Connection<Session> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return Objects.toStringHelper("").add("hostAndPort", hostAndPort).add("loginUser", loginCredentials.getUser())
|
return Objects.toStringHelper("").omitNullValues()
|
||||||
.add("session", session != null ? session.hashCode() : null).add("connectTimeout", connectTimeout).add(
|
.add("hostAndPort", hostAndPort).add("loginUser", loginCredentials.getUser())
|
||||||
"sessionTimeout", sessionTimeout).toString();
|
.add("session", session != null ? session.hashCode() : null)
|
||||||
|
.add("connectTimeout", connectTimeout)
|
||||||
|
.add("proxy", proxy.orNull())
|
||||||
|
.add("sessionTimeout", sessionTimeout).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,11 +18,14 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.ssh.jsch.config;
|
package org.jclouds.ssh.jsch.config;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
|
|
||||||
import org.jclouds.Constants;
|
import org.jclouds.Constants;
|
||||||
import org.jclouds.domain.LoginCredentials;
|
import org.jclouds.domain.LoginCredentials;
|
||||||
import org.jclouds.http.handlers.BackoffLimitedRetryHandler;
|
import org.jclouds.http.handlers.BackoffLimitedRetryHandler;
|
||||||
|
import org.jclouds.proxy.ProxyConfig;
|
||||||
import org.jclouds.ssh.SshClient;
|
import org.jclouds.ssh.SshClient;
|
||||||
import org.jclouds.ssh.config.ConfiguresSshClient;
|
import org.jclouds.ssh.config.ConfiguresSshClient;
|
||||||
import org.jclouds.ssh.jsch.JschSshClient;
|
import org.jclouds.ssh.jsch.JschSshClient;
|
||||||
|
@ -49,18 +52,20 @@ public class JschSshClientModule extends AbstractModule {
|
||||||
@Inject(optional = true)
|
@Inject(optional = true)
|
||||||
int timeout = 60000;
|
int timeout = 60000;
|
||||||
|
|
||||||
|
private final ProxyConfig proxyConfig;
|
||||||
private final BackoffLimitedRetryHandler backoffLimitedRetryHandler;
|
private final BackoffLimitedRetryHandler backoffLimitedRetryHandler;
|
||||||
private final Injector injector;
|
private final Injector injector;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public Factory(BackoffLimitedRetryHandler backoffLimitedRetryHandler, Injector injector) {
|
public Factory(ProxyConfig proxyConfig, BackoffLimitedRetryHandler backoffLimitedRetryHandler, Injector injector) {
|
||||||
this.backoffLimitedRetryHandler = backoffLimitedRetryHandler;
|
this.proxyConfig = checkNotNull(proxyConfig, "proxyConfig");
|
||||||
this.injector = injector;
|
this.backoffLimitedRetryHandler = checkNotNull(backoffLimitedRetryHandler, "backoffLimitedRetryHandler");
|
||||||
|
this.injector = checkNotNull(injector, "injector");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SshClient create(HostAndPort socket, LoginCredentials credentials) {
|
public SshClient create(HostAndPort socket, LoginCredentials credentials) {
|
||||||
SshClient client = new JschSshClient(backoffLimitedRetryHandler, socket, credentials, timeout);
|
SshClient client = new JschSshClient(proxyConfig, backoffLimitedRetryHandler, socket, credentials, timeout);
|
||||||
injector.injectMembers(client);// add logger
|
injector.injectMembers(client);// add logger
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue