通过SOCKS5代理从java客户端(代码)连接到SFTP服务器,无需代理身份验证

时间:2017-08-08 11:44:51

标签: java proxy sftp socks j2ssh

我正在尝试通过socks5代理服务器从独立的sftpclient连接到SFTP服务器,我正在使用maverick-all库。我的代码运行正常。以下是代码段:

private SshClient getSSHClient() throws Exception {
    SshClient ssh = null;
    SshClient forwardedConnection = null;
    /**
     * Create an SshConnector instance
     */
    SshConnector con = SshConnector.getInstance();
    Ssh2Context context = (Ssh2Context) con.getContext(SshConnector.SSH2);
    setupContext(context);
    context.setHostKeyVerification(new ConsoleKnownHostsKeyVerification());
    SocketTransport t = new SocketTransport(hostname, port);
    t.setTcpNoDelay(true);
    //System.out.println("t.isConnected() ->" + t.isConnected());
    ssh = con.connect(t, username, true);

    PasswordAuthentication pwd = new PasswordAuthentication();
    pwd.setPassword(password);
    ssh.authenticate(pwd);


    if (ssh.isAuthenticated()) {    
        System.out.println("authenticated");
        // Connect via socks5 proxy
        SshTransport spt = SocksProxyTransport.connectViaSocks5Proxy(hostname, port, proxyServerHost, proxyServerPort, proxyServerUsername,proxyServerPassword);

        forwardedConnection = con.connect(spt, username);
        PasswordAuthentication pwd2 = new PasswordAuthentication();
        pwd2.setPassword(password);
        forwardedConnection.authenticate(pwd2);
    }
    return forwardedConnection;
}

我现在想要不使用代理身份验证,即不使用代理用户名和密码,因为代理服务器(CCproxy)允许这样的配置。如果我们将方法指定为'0',我查看了库代码。代码可以在github中找到,下面是URL

https://github.com/sshtools/j2ssh-maverick/blob/master/j2ssh-maverick/src/main/java/com/sshtools/net/SocksProxyTransport.java

connectViaSocks5Proxy方法。

第204行。

那么有没有办法指定身份验证方法?

0 个答案:

没有答案