/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ import com.jcraft.jsch.*; import java.awt.*; import javax.swing.*; import java.util.zip.Deflater; import java.util.zip.Inflater; /** * This example demonstrates the packet compression without using jzlib[1]. * * The ssh protocol adopts zlib[2] for the packet compression. Fortunately, * JDK has provided wrapper classes for zlib(j.u.z.{Deflater, Inflater}), * but it does not expose enough functionality of zlib, unfortunately; * it must not allow to compress data with SYNC_FLUSH. So, JSch has been * using jzlib by the default. After 12 years of bug parade entry[3] filing, * Java7 has revised j.u.z.Deflater, and SYNC_FLUSH has been supported at last. * This example shows how to enable the packet compression by using JDK's * java.util.zip package. * * * [1] http://www.jcraft.com/jzlib/ * [2] http://www.zlib.net/ * [3] http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4206909 */ public class CompressionJUZ { /** * This is an implementation of com.jcraft.jsch.Compression with * java.util.zip.{Deflater, Inflater}. * Before Session#connect(), set the name of this class for "zlib" and * "zlib@openssh.com", for example, * * session.setConfig("zlib", "CompressionJUZ$MyCompression"); * session.setConfig("zlib@openssh.com", "CompressionJUZ$MyCompression"); */ public static class MyCompression implements Compression { private final int buffer_margin=32+20; // AES256 + HMACSHA1 private Deflater deflater = null; private Inflater inflater = null; private int type; private byte[] tmpbuf = new byte[4096]; private byte[] inflated_buf; public void init(int type, int level){ if(type==DEFLATER){ deflater = new Deflater(level); this.type = DEFLATER; } else { inflater = new Inflater(); inflated_buf=new byte[4096]; this.type = INFLATER; } } public byte[] compress(byte[] buf, int start, int[] end){ // There may be a bug in j.u.z.Deflater. // It seems to me that if the size of buffer for Deflater#deflate() is // not enough, that method will return weird value ;-( if(tmpbuf.length0); } catch(java.util.zip.DataFormatException e){ } if(buf.length0){ host=arg[0]; } else{ host=JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name")+ "@localhost"); } String user=host.substring(0, host.indexOf('@')); host=host.substring(host.indexOf('@')+1); Session session=jsch.getSession(user, host, 22); String passwd = JOptionPane.showInputDialog("Enter password"); session.setPassword(passwd); UserInfo ui = new MyUserInfo(){ public void showMessage(String message){ JOptionPane.showMessageDialog(null, message); } public boolean promptYesNo(String message){ Object[] options={ "yes", "no" }; int foo=JOptionPane.showOptionDialog(null, message, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); return foo==0; } }; session.setUserInfo(ui); session.setConfig("zlib", "CompressionJUZ$MyCompression"); session.setConfig("zlib@openssh.com", "CompressionJUZ$MyCompression"); session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none"); session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none"); session.setConfig("compression_level", "9"); session.connect(30000); Channel channel=session.openChannel("shell"); channel.setInputStream(System.in); /* // a hack for MS-DOS prompt on Windows. channel.setInputStream(new FilterInputStream(System.in){ public int read(byte[] b, int off, int len)throws IOException{ return in.read(b, off, (len>1024?1024:len)); } }); */ channel.setOutputStream(System.out); channel.connect(3*1000); } catch(Exception e){ System.out.println(e); } } public static abstract class MyUserInfo implements UserInfo, UIKeyboardInteractive{ public String getPassword(){ return null; } public boolean promptYesNo(String str){ return false; } public String getPassphrase(){ return null; } public boolean promptPassphrase(String message){ return false; } public boolean promptPassword(String message){ return false; } public void showMessage(String message){ } public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo){ return null; } } }