Ibrahim GUNDUZ - 2012-04-10

Hello,
I am trying to implement proxy interface for stream forwarding. My aim is running SFTP channel over an SSH gateway.
Using jsch-0.1.47.jar with the following code gets an error; com.jcraft.jsch.JSchException: channel is not opened.
My code is:

import java.net.Socket;
import java.io.*;
import com.jcraft.jsch.*;
public class StreamForwarding {
    public static void main(String[] arg) {
        try {
            JSch jsch=new JSch();
            JSch.setLogger(new SSHLogger());
            String gwhost = "gatewayhost";
            String gwuser = "gatewayuser";
            Session gateway = jsch.getSession(gwuser, gwhost, 22);
            gateway.setPassword("gatewaypass");
            gateway.setConfig("StrictHostKeyChecking", "no");
            gateway.connect();
            String rhost = "remotehost";
            String ruser = "remoteuser";
            Session session = jsch.getSession(ruser, rhost, 22);
            session.setPassword("remotepass");
            session.setConfig("StrictHostKeyChecking", "no");
            session.setProxy(new ProxySSH(gateway));
            session.connect();
        } catch(Exception e) {
            System.out.println(e);
        }
    }
    public static class ProxySSH implements Proxy {
        public ProxySSH(Session gateway) {
            this.gateway = gateway;
        }
        private Session gateway;
        private ChannelDirectTCPIP channel;
        private InputStream iStream;
        private OutputStream oStream;
        public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws Exception {
            try {
                channel = (ChannelDirectTCPIP)gateway.openChannel("direct-tcpip");
                channel.setHost(host);
                channel.setPort(port);
                iStream = ((ChannelDirectTCPIP)channel).getInputStream();
                oStream = ((ChannelDirectTCPIP)channel).getOutputStream();
                channel.connect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public InputStream getInputStream() { return iStream; }
        public OutputStream getOutputStream() { return oStream; }
        public Socket getSocket() { return null; }
        public void close() {
            try {
                if(iStream!=null)iStream.close();
                if(oStream!=null)oStream.close();
            } catch(Exception  e) {
                e.printStackTrace();
            }
            iStream = null;
            oStream = null;
            channel.disconnect();
        }
    }
    public static class SSHLogger implements com.jcraft.jsch.Logger {
        public boolean isEnabled(int level) {
            return true;
        }
        public void log(int level, String message) {
            switch(level) {
                case DEBUG: System.out.println("DEBUG:" + message); break;
                case INFO: System.out.println("INFO:" + message); break;
                case WARN: System.out.println("WARNING:" + message); break;
                case ERROR: System.out.println("ERROR:" + message); break;
                case FATAL: System.out.println("FATAL:" + message); break;
            }
        }       
    }
}

And the output:

java -classpath jsch-0.1.47.jar;. StreamForwarding
INFO:Connecting to ssh-gateway port 22
INFO:Connection established
INFO:Remote version string: SSH-2.0-OpenSSH_4.2
INFO:Local version string: SSH-2.0-JSCH-0.1.47
INFO:CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO:aes256-ctr is not available.
INFO:aes192-ctr is not available.
INFO:aes256-cbc is not available.
INFO:aes192-cbc is not available.
INFO:arcfour256 is not available.
INFO:CheckKexes: diffie-hellman-group14-sha1
INFO:diffie-hellman-group14-sha1 is not available.
INFO:SSH_MSG_KEXINIT sent
INFO:SSH_MSG_KEXINIT received
INFO:kex: server: diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
INFO:kex: server: ssh-rsa,ssh-dss
INFO:kex: server: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr
INFO:kex: server: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr
INFO:kex: server: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO:kex: server: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO:kex: server: none,zlib@openssh.com,zlib
INFO:kex: server: none,zlib@openssh.com,zlib
INFO:kex: server:
INFO:kex: server:
INFO:kex: client: diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1
INFO:kex: client: ssh-rsa,ssh-dss
INFO:kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
INFO:kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
INFO:kex: client: hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96
INFO:kex: client: hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96
INFO:kex: client: none
INFO:kex: client: none
INFO:kex: client:
INFO:kex: client:
INFO:kex: server->client aes128-ctr hmac-md5 none
INFO:kex: client->server aes128-ctr hmac-md5 none
INFO:SSH_MSG_KEXDH_INIT sent
INFO:expecting SSH_MSG_KEXDH_REPLY
INFO:ssh_rsa_verify: signature true
WARNING:Permanently added 'ssh-gateway' (RSA) to the list of known hosts.
INFO:SSH_MSG_NEWKEYS sent
INFO:SSH_MSG_NEWKEYS received
INFO:SSH_MSG_SERVICE_REQUEST sent
INFO:SSH_MSG_SERVICE_ACCEPT received
INFO:Authentications that can continue: publickey,keyboard-interactive,password
INFO:Next authentication method: publickey
INFO:Authentications that can continue: password
INFO:Next authentication method: password
INFO:Authentication succeeded (password).
INFO:Connecting to 10.0.0.21 port 22
com.jcraft.jsch.JSchException: channel is not opened.
        at com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:673)
        at com.jcraft.jsch.ChannelDirectTCPIP.connect(ChannelDirectTCPIP.java:73)
        at StreamForwarding$ProxySSH.connect(StreamForwarding.java:41)
        at com.jcraft.jsch.Session.connect(Session.java:208)
        at com.jcraft.jsch.Session.connect(Session.java:160)
        at StreamForwarding.main(StreamForwarding.java:21)
INFO:Connection established
com.jcraft.jsch.JSchException: Session.connect: java.io.IOException: failed to initialize the channel.

Any help is appreciated.
Best Regards,
Ibrahim