Search in sources :

Example 6 with ProxyHTTP

use of com.jcraft.jsch.ProxyHTTP in project pentaho-kettle by pentaho.

the class SFTPClient method setProxy.

public void setProxy(String host, String port, String user, String pass, String proxyType) throws KettleJobException {
    if (Utils.isEmpty(host) || Const.toInt(port, 0) == 0) {
        throw new KettleJobException("Proxy server name must be set and server port must be greater than zero.");
    }
    Proxy proxy = null;
    String proxyhost = host + ":" + port;
    if (proxyType.equals(PROXY_TYPE_HTTP)) {
        proxy = new ProxyHTTP(proxyhost);
        if (!Utils.isEmpty(user)) {
            ((ProxyHTTP) proxy).setUserPasswd(user, pass);
        }
    } else if (proxyType.equals(PROXY_TYPE_SOCKS5)) {
        proxy = new ProxySOCKS5(proxyhost);
        if (!Utils.isEmpty(user)) {
            ((ProxySOCKS5) proxy).setUserPasswd(user, pass);
        }
    }
    s.setProxy(proxy);
}
Also used : Proxy(com.jcraft.jsch.Proxy) ProxySOCKS5(com.jcraft.jsch.ProxySOCKS5) ProxyHTTP(com.jcraft.jsch.ProxyHTTP) KettleJobException(org.pentaho.di.core.exception.KettleJobException)

Example 7 with ProxyHTTP

use of com.jcraft.jsch.ProxyHTTP in project javautils by jiadongpo.

the class SFTPClient method setProxy.

public void setProxy(String host, String port, String user, String pass, String proxyType) throws Exception {
    if (isEmpty(host) || toInt(port, 0) == 0) {
        throw new Exception("Proxy server name must be set and server port must be greater than zero.");
    }
    Proxy proxy = null;
    String proxyhost = host + ":" + port;
    if (proxyType.equals(PROXY_TYPE_HTTP)) {
        proxy = new ProxyHTTP(proxyhost);
        if (!isEmpty(user)) {
            ((ProxyHTTP) proxy).setUserPasswd(user, pass);
        }
    } else if (proxyType.equals(PROXY_TYPE_SOCKS5)) {
        proxy = new ProxySOCKS5(proxyhost);
        if (!isEmpty(user)) {
            ((ProxySOCKS5) proxy).setUserPasswd(user, pass);
        }
    }
    s.setProxy(proxy);
}
Also used : Proxy(com.jcraft.jsch.Proxy) ProxySOCKS5(com.jcraft.jsch.ProxySOCKS5) ProxyHTTP(com.jcraft.jsch.ProxyHTTP) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException)

Example 8 with ProxyHTTP

use of com.jcraft.jsch.ProxyHTTP in project incubator-gobblin by apache.

the class SftpFsHelper method connect.

/**
 * Opens up a connection to specified host using the username. Connects to the source using a private key without
 * prompting for a password. This method does not support connecting to a source using a password, only by private
 * key
 * @throws org.apache.gobblin.source.extractor.filebased.FileBasedHelperException
 */
@Override
public void connect() throws FileBasedHelperException {
    String privateKey = PasswordManager.getInstance(this.state).readPassword(this.state.getProp(ConfigurationKeys.SOURCE_CONN_PRIVATE_KEY));
    String password = PasswordManager.getInstance(this.state).readPassword(this.state.getProp(ConfigurationKeys.SOURCE_CONN_PASSWORD));
    String knownHosts = this.state.getProp(ConfigurationKeys.SOURCE_CONN_KNOWN_HOSTS);
    String userName = this.state.getProp(ConfigurationKeys.SOURCE_CONN_USERNAME);
    String hostName = this.state.getProp(ConfigurationKeys.SOURCE_CONN_HOST_NAME);
    int port = this.state.getPropAsInt(ConfigurationKeys.SOURCE_CONN_PORT, ConfigurationKeys.SOURCE_CONN_DEFAULT_PORT);
    String proxyHost = this.state.getProp(ConfigurationKeys.SOURCE_CONN_USE_PROXY_URL);
    int proxyPort = this.state.getPropAsInt(ConfigurationKeys.SOURCE_CONN_USE_PROXY_PORT, -1);
    JSch.setLogger(new JSchLogger());
    JSch jsch = new JSch();
    log.info("Attempting to connect to source via SFTP with" + " privateKey: " + privateKey + " knownHosts: " + knownHosts + " userName: " + userName + " hostName: " + hostName + " port: " + port + " proxyHost: " + proxyHost + " proxyPort: " + proxyPort);
    try {
        if (!Strings.isNullOrEmpty(privateKey)) {
            List<IdentityStrategy> identityStrategies = ImmutableList.of(new LocalFileIdentityStrategy(), new DistributedCacheIdentityStrategy(), new HDFSIdentityStrategy());
            for (IdentityStrategy identityStrategy : identityStrategies) {
                if (identityStrategy.setIdentity(privateKey, jsch)) {
                    break;
                }
            }
        }
        this.session = jsch.getSession(userName, hostName, port);
        this.session.setConfig("PreferredAuthentications", "publickey,password");
        if (Strings.isNullOrEmpty(knownHosts)) {
            log.info("Known hosts path is not set, StrictHostKeyChecking will be turned off");
            this.session.setConfig("StrictHostKeyChecking", "no");
        } else {
            jsch.setKnownHosts(knownHosts);
        }
        if (!Strings.isNullOrEmpty(password)) {
            this.session.setPassword(password);
        }
        if (proxyHost != null && proxyPort >= 0) {
            this.session.setProxy(new ProxyHTTP(proxyHost, proxyPort));
        }
        UserInfo ui = new MyUserInfo();
        this.session.setUserInfo(ui);
        this.session.setDaemonThread(true);
        this.session.connect();
        log.info("Finished connecting to source");
    } catch (JSchException e) {
        if (this.session != null) {
            this.session.disconnect();
        }
        log.error(e.getMessage(), e);
        throw new FileBasedHelperException("Cannot connect to SFTP source", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) UserInfo(com.jcraft.jsch.UserInfo) JSch(com.jcraft.jsch.JSch) FileBasedHelperException(org.apache.gobblin.source.extractor.filebased.FileBasedHelperException) ProxyHTTP(com.jcraft.jsch.ProxyHTTP)

Aggregations

ProxyHTTP (com.jcraft.jsch.ProxyHTTP)8 ProxySOCKS5 (com.jcraft.jsch.ProxySOCKS5)5 JSchException (com.jcraft.jsch.JSchException)4 Proxy (com.jcraft.jsch.Proxy)4 SftpException (com.jcraft.jsch.SftpException)3 IOException (java.io.IOException)3 FileSystemException (org.apache.commons.vfs2.FileSystemException)3 JndiRegistry (org.apache.camel.impl.JndiRegistry)2 JSch (com.jcraft.jsch.JSch)1 ProxySOCKS4 (com.jcraft.jsch.ProxySOCKS4)1 UserInfo (com.jcraft.jsch.UserInfo)1 FileBasedHelperException (org.apache.gobblin.source.extractor.filebased.FileBasedHelperException)1 KettleJobException (org.pentaho.di.core.exception.KettleJobException)1