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