1 2 29 30 import com.jcraft.jsch.*; 31 import com.jcraft.jhttptunnel.*; 32 33 import java.awt.*; 34 import java.awt.event.*; 35 import javax.swing.*; 36 37 import java.io.*; 38 import java.net.*; 39 40 public class JSchOverJHttpTunnel{ 41 public static void main(String [] arg){ 42 43 try{ 44 JSch jsch=new JSch(); 45 46 if(arg.length==0){ 47 System.err.println("Enter username@hostname[:port]"); 48 System.exit(1); 49 } 50 String host=arg[0]; 51 String user=host.substring(0, host.indexOf('@')); 52 host=host.substring(host.indexOf('@')+1); 53 int port=8888; 54 if(host.indexOf(':')!=-1){ 55 try{ 56 port=Integer.parseInt(host.substring(host.lastIndexOf(':') + 1)); 57 host=host.substring(0, host.lastIndexOf(':')); 58 } 59 catch (Exception e) { 60 System.err.println(e); 61 System.exit(1); 62 } 63 } 64 65 Session session=jsch.getSession(user, host, port); 66 68 UserInfo ui=new MyUserInfo(); 69 session.setUserInfo(ui); 70 71 session.setSocketFactory(new SocketFactory() { 72 InputStream in=null; 73 OutputStream out=null; 74 JHttpTunnelClient jhtc=null; 75 public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 76 jhtc=new JHttpTunnelClient(host, port); 77 79 jhtc.setInBound(new InBoundSocket()); 80 jhtc.setOutBound(new OutBoundSocket()); 81 82 jhtc.connect(); 83 return new Socket(); } 85 public InputStream getInputStream(Socket socket) throws IOException { 86 if(in==null) 87 in=jhtc.getInputStream(); 88 return in; 89 } 90 public OutputStream getOutputStream(Socket socket) throws IOException { 91 if(out==null) 92 out=jhtc.getOutputStream(); 93 return out; 94 } 95 }); 96 97 101 session.connect(); 102 103 Channel channel=session.openChannel("shell"); 104 105 channel.setInputStream(System.in); 106 channel.setOutputStream(System.out); 107 108 channel.connect(); 109 } 110 catch(Exception e){ 111 System.out.println(e); 112 } 113 } 114 115 public static class MyUserInfo implements UserInfo{ 116 public String getPassword(){ return passwd; } 117 public boolean promptYesNo(String str){ 118 Object [] options={ "yes", "no" }; 119 int foo=JOptionPane.showOptionDialog(null, 120 str, 121 "Warning", 122 JOptionPane.DEFAULT_OPTION, 123 JOptionPane.WARNING_MESSAGE, 124 null, options, options[0]); 125 return foo==0; 126 } 127 128 String passwd; 129 JTextField passwordField=(JTextField)new JPasswordField(20); 130 131 public String getPassphrase(){ return null; } 132 public boolean promptPassphrase(String message){ return true; } 133 public boolean promptPassword(String message){ 134 Object [] ob={passwordField}; 135 int result= 136 JOptionPane.showConfirmDialog(null, ob, message, 137 JOptionPane.OK_CANCEL_OPTION); 138 if(result==JOptionPane.OK_OPTION){ 139 passwd=passwordField.getText(); 140 return true; 141 } 142 else{ return false; } 143 } 144 public void showMessage(String message){ 145 JOptionPane.showMessageDialog(null, message); 146 } 147 } 148 149 } 150 | Popular Tags |