Java源码示例:com.jcraft.jsch.Proxy

示例1
@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);
}
 
示例2
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;
}
 
示例3
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];
}
 
示例4
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);
	}
}
 
示例5
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;
}
 
示例6
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;
}
 
示例7
@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();
        }
    }
}
 
示例8
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 );
}
 
示例9
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() );
}
 
示例10
@Override
public Proxy getProxy() {
    return proxy;
}
 
示例11
/**
 * Stores the proxy to use.
 *
 * @param proxy The proxy to use.
 * @return This object.
 */
public SFTPEnvironment withProxy(Proxy proxy) {
    put(PROXY, proxy);
    return this;
}
 
示例12
/**
 * Sets the proxy through which all connections will be piped.
 * 
 * @param proxy
 *            The proxy
 */
public void setProxy( Proxy proxy ) {
    this.proxy = proxy;
}
 
示例13
/**
 * 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();
 
示例14
/**
 * 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;
}