1 2 import com.jcraft.jsch.*; 3 import java.io.*; 4 import java.awt.*; 5 import javax.swing.*; 6 7 public class Daemon{ 8 public static void main(String [] arg){ 9 10 int rport; 11 12 try{ 13 JSch jsch=new JSch(); 14 15 String host=JOptionPane.showInputDialog("Enter username@hostname", 16 System.getProperty("user.name")+ 17 "@localhost"); 18 String user=host.substring(0, host.indexOf('@')); 19 host=host.substring(host.indexOf('@')+1); 20 21 Session session=jsch.getSession(user, host, 22); 22 23 String foo=JOptionPane.showInputDialog("Enter remote port number", 24 "8888"); 25 rport=Integer.parseInt(foo); 26 27 UserInfo ui=new MyUserInfo(); 29 session.setUserInfo(ui); 30 31 session.connect(); 32 33 session.setPortForwardingR(rport, "Daemon$Parrot"); 35 System.out.println(host+":"+rport+" <--> "+"Parrot"); 36 } 37 catch(Exception e){ 38 System.out.println(e); 39 } 40 } 41 42 public static class Parrot implements ForwardedTCPIPDaemon{ 43 ChannelForwardedTCPIP channel; 44 Object [] arg; 45 public void setChannel(ChannelForwardedTCPIP c){this.channel=c;} 46 public void setArg(Object [] arg){this.arg=arg;} 47 public void run(){ 48 System.out.println("remote port: "+channel.getRemotePort()); 49 System.out.println("remote host: "+channel.getSession().getHost()); 50 try{ 51 InputStream in=channel.getInputStream(); 52 OutputStream out=channel.getOutputStream(); 53 byte[] buf=new byte[1024]; 54 while(true){ 55 int i=in.read(buf, 0, buf.length); 56 if(i<=0)break; 57 out.write(buf, 0, i); 58 if(buf[0]=='.')break; 59 } 60 } 61 catch(IOException e){ 62 } 63 } 64 } 65 66 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 67 public String getPassword(){ return passwd; } 68 public boolean promptYesNo(String str){ 69 Object [] options={ "yes", "no" }; 70 int foo=JOptionPane.showOptionDialog(null, 71 str, 72 "Warning", 73 JOptionPane.DEFAULT_OPTION, 74 JOptionPane.WARNING_MESSAGE, 75 null, options, options[0]); 76 return foo==0; 77 } 78 79 String passwd; 80 JTextField passwordField=(JTextField)new JPasswordField(20); 81 82 public String getPassphrase(){ return null; } 83 public boolean promptPassphrase(String message){ return true; } 84 public boolean promptPassword(String message){ 85 Object [] ob={passwordField}; 86 int result=JOptionPane.showConfirmDialog(null, ob, message, 87 JOptionPane.OK_CANCEL_OPTION); 88 if(result==JOptionPane.OK_OPTION){ 89 passwd=passwordField.getText(); 90 return true; 91 } 92 else{ return false; } 93 } 94 public void showMessage(String message){ 95 JOptionPane.showMessageDialog(null, message); 96 } 97 final GridBagConstraints gbc = 98 new GridBagConstraints(0,0,1,1,1,1, 99 GridBagConstraints.NORTHWEST, 100 GridBagConstraints.NONE, 101 new Insets(0,0,0,0),0,0); 102 private Container panel; 103 public String [] promptKeyboardInteractive(String destination, 104 String name, 105 String instruction, 106 String [] prompt, 107 boolean[] echo){ 108 panel = new JPanel(); 109 panel.setLayout(new GridBagLayout()); 110 111 gbc.weightx = 1.0; 112 gbc.gridwidth = GridBagConstraints.REMAINDER; 113 gbc.gridx = 0; 114 panel.add(new JLabel(instruction), gbc); 115 gbc.gridy++; 116 117 gbc.gridwidth = GridBagConstraints.RELATIVE; 118 119 JTextField[] texts=new JTextField[prompt.length]; 120 for(int i=0; i<prompt.length; i++){ 121 gbc.fill = GridBagConstraints.NONE; 122 gbc.gridx = 0; 123 gbc.weightx = 1; 124 panel.add(new JLabel(prompt[i]),gbc); 125 126 gbc.gridx = 1; 127 gbc.fill = GridBagConstraints.HORIZONTAL; 128 gbc.weighty = 1; 129 if(echo[i]){ 130 texts[i]=new JTextField(20); 131 } 132 else{ 133 texts[i]=new JPasswordField(20); 134 } 135 panel.add(texts[i], gbc); 136 gbc.gridy++; 137 } 138 139 if(JOptionPane.showConfirmDialog(null, panel, 140 destination+": "+name, 141 JOptionPane.OK_CANCEL_OPTION, 142 JOptionPane.QUESTION_MESSAGE) 143 ==JOptionPane.OK_OPTION){ 144 String [] response=new String [prompt.length]; 145 for(int i=0; i<prompt.length; i++){ 146 response[i]=texts[i].getText(); 147 } 148 return response; 149 } 150 else{ 151 return null; } 153 } 154 } 155 } 156 | Popular Tags |