1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /**
3  * This program will demonstrate the port forwarding like option -R of
4  * ssh command; the given port on the remote host will be forwarded to
5  * the given host and port  on the local side.
6  *   $ CLASSPATH=.:../build javac PortForwardingR.java
7  *   $ CLASSPATH=.:../build java PortForwardingR
8  * You will be asked username, hostname, port:host:hostport and passwd.
9  * If everything works fine, you will get the shell prompt.
10  * Try the port on remote host.
11  *
12  */
13 import com.jcraft.jsch.*;
14 import java.awt.*;
15 import javax.swing.*;
16 
17 public class PortForwardingR{
main(String[] arg)18   public static void main(String[] arg){
19 
20     int rport;
21     String lhost;
22     int lport;
23 
24     try{
25       JSch jsch=new JSch();
26 
27       String host=null;
28       if(arg.length>0){
29         host=arg[0];
30       }
31       else{
32         host=JOptionPane.showInputDialog("Enter username@hostname",
33                                          System.getProperty("user.name")+
34                                          "@localhost");
35       }
36       String user=host.substring(0, host.indexOf('@'));
37       host=host.substring(host.indexOf('@')+1);
38 
39       Session session=jsch.getSession(user, host, 22);
40 
41       String foo=JOptionPane.showInputDialog("Enter -R port:host:hostport",
42 					     "port:host:hostport");
43       rport=Integer.parseInt(foo.substring(0, foo.indexOf(':')));
44       foo=foo.substring(foo.indexOf(':')+1);
45       lhost=foo.substring(0, foo.indexOf(':'));
46       lport=Integer.parseInt(foo.substring(foo.indexOf(':')+1));
47 
48       // username and password will be given via UserInfo interface.
49       UserInfo ui=new MyUserInfo();
50       session.setUserInfo(ui);
51 
52       session.connect();
53 
54       // Channel channel=session.openChannel("shell");
55       // channel.connect();
56 
57       session.setPortForwardingR(rport, lhost, lport);
58 
59       System.out.println(host+":"+rport+" -> "+lhost+":"+lport);
60     }
61     catch(Exception e){
62       System.out.println(e);
63     }
64   }
65 
66   public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
getPassword()67     public String getPassword(){ return passwd; }
promptYesNo(String str)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 
getPassphrase()82     public String getPassphrase(){ return null; }
promptPassphrase(String message)83     public boolean promptPassphrase(String message){ return true; }
promptPassword(String message)84     public boolean promptPassword(String message){
85       Object[] ob={passwordField};
86       int result=
87 	  JOptionPane.showConfirmDialog(null, ob, message,
88 					JOptionPane.OK_CANCEL_OPTION);
89       if(result==JOptionPane.OK_OPTION){
90 	passwd=passwordField.getText();
91 	return true;
92       }
93       else{ return false; }
94     }
showMessage(String message)95     public void showMessage(String message){
96       JOptionPane.showMessageDialog(null, message);
97     }
98     final GridBagConstraints gbc =
99       new GridBagConstraints(0,0,1,1,1,1,
100                              GridBagConstraints.NORTHWEST,
101                              GridBagConstraints.NONE,
102                              new Insets(0,0,0,0),0,0);
103     private Container panel;
promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo)104     public String[] promptKeyboardInteractive(String destination,
105                                               String name,
106                                               String instruction,
107                                               String[] prompt,
108                                               boolean[] echo){
109       panel = new JPanel();
110       panel.setLayout(new GridBagLayout());
111 
112       gbc.weightx = 1.0;
113       gbc.gridwidth = GridBagConstraints.REMAINDER;
114       gbc.gridx = 0;
115       panel.add(new JLabel(instruction), gbc);
116       gbc.gridy++;
117 
118       gbc.gridwidth = GridBagConstraints.RELATIVE;
119 
120       JTextField[] texts=new JTextField[prompt.length];
121       for(int i=0; i<prompt.length; i++){
122         gbc.fill = GridBagConstraints.NONE;
123         gbc.gridx = 0;
124         gbc.weightx = 1;
125         panel.add(new JLabel(prompt[i]),gbc);
126 
127         gbc.gridx = 1;
128         gbc.fill = GridBagConstraints.HORIZONTAL;
129         gbc.weighty = 1;
130         if(echo[i]){
131           texts[i]=new JTextField(20);
132         }
133         else{
134           texts[i]=new JPasswordField(20);
135         }
136         panel.add(texts[i], gbc);
137         gbc.gridy++;
138       }
139 
140       if(JOptionPane.showConfirmDialog(null, panel,
141                                        destination+": "+name,
142                                        JOptionPane.OK_CANCEL_OPTION,
143                                        JOptionPane.QUESTION_MESSAGE)
144          ==JOptionPane.OK_OPTION){
145         String[] response=new String[prompt.length];
146         for(int i=0; i<prompt.length; i++){
147           response[i]=texts[i].getText();
148         }
149 	return response;
150       }
151       else{
152         return null;  // cancel
153       }
154     }
155   }
156 }
157