com.jcraft.jsch.Proxy Java Examples

The following examples show how to use com.jcraft.jsch.Proxy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SFTPEnvironmentTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitializeSessionFull() throws IOException, JSchException {
    SFTPEnvironment env = new SFTPEnvironment();
    initializeFully(env);

    Session session = mock(Session.class);
    env.initialize(session);

    verify(session).setProxy((Proxy) env.get("proxy"));
    verify(session).setUserInfo((UserInfo) env.get("userInfo"));
    verify(session).setPassword(new String((char[]) env.get("password")));
    verify(session).setConfig((Properties) env.get("config"));
    verify(session).setSocketFactory((SocketFactory) env.get("socketFactory"));
    verify(session).setTimeout((int) env.get("timeOut"));
    verify(session).setClientVersion((String) env.get("clientVersion"));
    verify(session).setHostKeyAlias((String) env.get("hostKeyAlias"));
    verify(session).setServerAliveInterval((int) env.get("serverAliveInterval"));
    verify(session).setServerAliveCountMax((int) env.get("serverAliveCountMax"));
    verifyNoMoreInteractions(session);
}
 
Example #2
Source File: SftpClientFactory.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
private static Proxy createStreamProxy(final String proxyHost, final int proxyPort,
        final FileSystemOptions fileSystemOptions, final SftpFileSystemConfigBuilder builder) {
    Proxy proxy;
    // Use a stream proxy, i.e. it will use a remote host as a proxy
    // and run a command (e.g. netcat) that forwards input/output
    // to the target host.

    // Here we get the settings for connecting to the proxy:
    // user, password, options and a command
    final String proxyUser = builder.getProxyUser(fileSystemOptions);
    final String proxyPassword = builder.getProxyPassword(fileSystemOptions);
    final FileSystemOptions proxyOptions = builder.getProxyOptions(fileSystemOptions);

    final String proxyCommand = builder.getProxyCommand(fileSystemOptions);

    // Create the stream proxy
    proxy = new SftpStreamProxy(proxyCommand, proxyUser, proxyHost, proxyPort, proxyPassword, proxyOptions);
    return proxy;
}
 
Example #3
Source File: PooledSFTPConnection.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Proxy[] getProxies() {
	String proxyString = authority.getProxyString();
	if (!StringUtils.isEmpty(proxyString)) {
		java.net.Proxy proxy = FileUtils.getProxy(authority.getProxyString());
		if ((proxy != null) && (proxy.type() != Type.DIRECT)) {
			URI proxyUri = URI.create(proxyString);
			String hostName = proxyUri.getHost();
			int port = proxyUri.getPort();
			String userInfo = proxyUri.getUserInfo();
			org.jetel.util.protocols.UserInfo proxyCredentials = null;
			if (userInfo != null) {
				proxyCredentials = new org.jetel.util.protocols.UserInfo(userInfo);
			}
			switch (proxy.type()) {
			case HTTP:
				ProxyHTTP proxyHttp = (port >= 0) ? new ProxyHTTP(hostName, port) : new ProxyHTTP(hostName);
				if (proxyCredentials != null) {
					proxyHttp.setUserPasswd(proxyCredentials.getUser(), proxyCredentials.getPassword());
				}
				return new Proxy[] {proxyHttp};
			case SOCKS:
				ProxySOCKS4 proxySocks4 = (port >= 0) ? new ProxySOCKS4(hostName, port) : new ProxySOCKS4(hostName);
				ProxySOCKS5 proxySocks5 = (port >= 0) ? new ProxySOCKS5(hostName, port) : new ProxySOCKS5(hostName);
				if (proxyCredentials != null) {
					proxySocks4.setUserPasswd(proxyCredentials.getUser(), proxyCredentials.getPassword());
					proxySocks5.setUserPasswd(proxyCredentials.getUser(), proxyCredentials.getPassword());
				}
				return new Proxy[] {proxySocks5, proxySocks4};
			case DIRECT:
				return new Proxy[1];
			}
		}
	}
	
	return new Proxy[1];
}
 
Example #4
Source File: PooledSFTPConnection.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Session getSession(JSch jsch, String username, UserInfo password, Proxy[] proxies) throws IOException {
	assert (proxies != null) && (proxies.length > 0);
	
	Session session;
	try {
		if (authority.getPort() == 0) {
			session = jsch.getSession(username, authority.getHost());
		} else {
			session = jsch.getSession(username, authority.getHost(), authority.getPort() == -1 ? DEFAULT_PORT : authority.getPort());
		}

		// password will be given via UserInfo interface.
		session.setUserInfo(password);
		
		Exception exception = null;
		for (Proxy proxy: proxies) {
			if (proxy != null) {
				session.setProxy(proxy);
			}
			try {
				session.connect();
				return session;
			} catch (Exception e) {
				exception = e;
			}
		}
		
		throw exception;
	} catch (Exception e) {
		if (log.isDebugEnabled()) {
			ConnectionPool pool = ConnectionPool.getInstance();
			synchronized (pool) {
				log.debug(MessageFormat.format("Connection pool status: {0} idle, {1} active", pool.getNumIdle(), pool.getNumActive()));
			}
		}
		throw new IOException(e);
	}
}
 
Example #5
Source File: DefaultSessionFactory.java    From jsch-extension with MIT License 5 votes vote down vote up
private DefaultSessionFactory( JSch jsch, String username, String hostname, int port, Proxy proxy ) {
    this.jsch = jsch;
    this.username = username;
    this.hostname = hostname;
    this.port = port;
    this.proxy = proxy;
}
 
Example #6
Source File: SessionFactory.java    From jsch-extension with MIT License 5 votes vote down vote up
protected SessionFactoryBuilder( JSch jsch, String username, String hostname, int port, Proxy proxy, Map<String, String> config, UserInfo userInfo ) {
    this.jsch = jsch;
    this.username = username;
    this.hostname = hostname;
    this.port = port;
    this.proxy = proxy;
    this.config = config;
    this.userInfo = userInfo;
}
 
Example #7
Source File: SshProxyTest.java    From jsch-extension with MIT License 5 votes vote down vote up
@Test
public void testSshProxy() {
    Proxy proxy = null;
    Session session = null;
    Channel channel = null;
    try {
        SessionFactory proxySessionFactory = sessionFactory.newSessionFactoryBuilder()
                .setHostname( "localhost" ).setPort( SessionFactory.SSH_PORT ).build();
        SessionFactory destinationSessionFactory = sessionFactory.newSessionFactoryBuilder()
                .setProxy( new SshProxy( proxySessionFactory ) ).build();
        session = destinationSessionFactory.newSession();

        session.connect();

        channel = session.openChannel( "exec" );
        ((ChannelExec)channel).setCommand( "echo " + expected );
        InputStream inputStream = channel.getInputStream();
        channel.connect();

        // echo adds \n
        assertEquals( expected + "\n", IOUtils.copyToString( inputStream, UTF8 ) );
    }
    catch ( Exception e ) {
        logger.error( "failed for proxy {}: {}", proxy, e );
        logger.debug( "failed:", e );
        fail( e.getMessage() );
    }
    finally {
        if ( channel != null && channel.isConnected() ) {
            channel.disconnect();
        }
        if ( session != null && session.isConnected() ) {
            session.disconnect();
        }
    }
}
 
Example #8
Source File: UnixSshSftpHybridFileSystem.java    From jsch-nio with MIT License 5 votes vote down vote up
public UnixSshSftpHybridFileSystem( UnixSshFileSystemProvider provider, URI uri, Map<String, ?> environment ) throws IOException {
    super( provider, uri, environment );

    // Construct a new sessionFactory from the URI authority, path, and
    // optional environment proxy
    SessionFactory defaultSessionFactory = (SessionFactory) environment.get( "defaultSessionFactory" );
    if ( defaultSessionFactory == null ) {
        throw new IllegalArgumentException( "defaultSessionFactory environment parameter is required" );
    }
    SessionFactoryBuilder builder = defaultSessionFactory.newSessionFactoryBuilder();
    String username = uri.getUserInfo();
    if ( username != null ) {
        builder.setUsername( username );
    }
    String hostname = uri.getHost();
    if ( hostname != null ) {
        builder.setHostname( hostname );
    }
    int port = uri.getPort();
    if ( port != -1 ) {
        builder.setPort( port );
    }
    Proxy proxy = (Proxy) environment.get( "proxy" );
    if ( proxy != null ) {
        builder.setProxy( proxy );
    }
    logger.debug( "Building SftpRunner" );
    this.sftpRunner = new SftpRunner( builder.build() );

    this.defaultDirectory = new UnixSshPath( this, uri.getPath() );
    if ( !defaultDirectory.isAbsolute() ) {
        throw new RuntimeException( "default directory must be absolute" );
    }

    rootDirectory = new UnixSshPath( this, PATH_SEPARATOR_STRING );
}
 
Example #9
Source File: AbstractSshFileSystem.java    From jsch-nio with MIT License 5 votes vote down vote up
public AbstractSshFileSystem( AbstractSshFileSystemProvider provider, URI uri, Map<String, ?> environment ) throws IOException {
    this.provider = provider;
    this.uri = uri;
    this.environment = environment;

    String binDirKey = "dir.bin";
    if ( environment.containsKey( binDirKey ) ) {
        binDir = (String) environment.get( binDirKey );
    }

    // Construct a new sessionFactory from the URI authority, path, and
    // optional environment proxy
    SessionFactory defaultSessionFactory = (SessionFactory) environment.get( "defaultSessionFactory" );
    if ( defaultSessionFactory == null ) {
        defaultSessionFactory = new DefaultSessionFactory();
    }
    SessionFactoryBuilder builder = defaultSessionFactory.newSessionFactoryBuilder();
    String username = uri.getUserInfo();
    if ( username != null ) {
        builder.setUsername( username );
    }
    String hostname = uri.getHost();
    if ( hostname != null ) {
        builder.setHostname( hostname );
    }
    int port = uri.getPort();
    if ( port != -1 ) {
        builder.setPort( port );
    }
    Proxy proxy = (Proxy) environment.get( "proxy" );
    if ( proxy != null ) {
        builder.setProxy( proxy );
    }
    this.commandRunner = new CommandRunner( builder.build() );
}
 
Example #10
Source File: DefaultSessionFactory.java    From jsch-extension with MIT License 4 votes vote down vote up
@Override
public Proxy getProxy() {
    return proxy;
}
 
Example #11
Source File: SFTPEnvironment.java    From sftp-fs with Apache License 2.0 2 votes vote down vote up
/**
 * Stores the proxy to use.
 *
 * @param proxy The proxy to use.
 * @return This object.
 */
public SFTPEnvironment withProxy(Proxy proxy) {
    put(PROXY, proxy);
    return this;
}
 
Example #12
Source File: DefaultSessionFactory.java    From jsch-extension with MIT License 2 votes vote down vote up
/**
 * Sets the proxy through which all connections will be piped.
 * 
 * @param proxy
 *            The proxy
 */
public void setProxy( Proxy proxy ) {
    this.proxy = proxy;
}
 
Example #13
Source File: SessionFactory.java    From jsch-extension with MIT License 2 votes vote down vote up
/**
 * Returns the proxy that sessions built by this factory will connect
 * through, if any. If none was configured, <code>null</code> will be
 * returned.
 * 
 * @return The proxy or null
 */
public Proxy getProxy();
 
Example #14
Source File: SessionFactory.java    From jsch-extension with MIT License 2 votes vote down vote up
/**
 * Replaces the current proxy with <code>proxy</code>
 * 
 * @param proxy
 *            The new proxy
 * @return This builder
 * 
 * @see com.pastdev.jsch.DefaultSessionFactory#setProxy(Proxy)
 */
public SessionFactoryBuilder setProxy( Proxy proxy ) {
    this.proxy = proxy;
    return this;
}