Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: mindbright/ssh/SSHProxyDialog.java


1   /******************************************************************************
2    *
3    * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
4    *                 www.mindbright.se, info@mindbright.se
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   *****************************************************************************
17   * $Author: nallen $
18   * $Date: 2001/11/12 16:31:21 $
19   * $Name:  $
20   *****************************************************************************/
21  package mindbright.ssh;
22  
23  import java.awt.*;
24  import java.awt.event.*;
25  
26  import mindbright.util.AWTConvenience;
27  
28  public final class SSHProxyDialog {
29      private static Dialog     proxyDialog = null;
30      private static Choice     choicePrxType;
31      private static Checkbox   cbNeedAuth;
32      private static TextField  textPrxHost, textPrxPort, textPrxUser, textPrxPasswd;
33      private static String[]   prxTypes;
34  
35      private static SSHPropertyHandler propsHandler;
36  
37      public static void show(String title, Frame parent,
38            SSHPropertyHandler props) {
39    propsHandler = props;
40    if(proxyDialog == null) {
41        prxTypes = SSH.getProxyTypes();
42  
43        proxyDialog = new Dialog(parent, title, true);
44  
45        Label              lbl;
46        GridBagLayout      grid  = new GridBagLayout();
47        GridBagConstraints gridc = new GridBagConstraints();
48        proxyDialog.setLayout(grid);
49        Button             b;
50        ItemListener       il;
51  
52        gridc.fill   = GridBagConstraints.HORIZONTAL;
53        gridc.anchor = GridBagConstraints.WEST;
54        gridc.gridwidth = 1;
55        gridc.insets = new Insets(4, 4, 0, 4);
56  
57        gridc.gridy = 0;
58        lbl = new Label("Proxy type:");
59        gridc.gridwidth = 2;
60        grid.setConstraints(lbl, gridc);
61        proxyDialog.add(lbl);
62        choicePrxType = new Choice();
63        for(int i = 0; i < prxTypes.length; i++) {
64      choicePrxType.add(prxTypes[i]);
65        }
66        grid.setConstraints(choicePrxType, gridc);
67        proxyDialog.add(choicePrxType);
68        choicePrxType.addItemListener(il = new ItemListener() {
69      public void itemStateChanged(ItemEvent e) {
70          if(e.getSource() == choicePrxType) {
71        textPrxPort.setText(String.valueOf(SSH.defaultProxyPorts[SSH.getProxyType(choicePrxType.getSelectedItem())]));
72          }
73          updateFromType();
74      }
75        });
76  
77        gridc.gridy = 1;
78        lbl = new Label("Server:");
79        grid.setConstraints(lbl, gridc);
80        proxyDialog.add(lbl);
81        gridc.gridwidth = 4;
82        textPrxHost = new TextField("", 16);
83        grid.setConstraints(textPrxHost, gridc);
84        proxyDialog.add(textPrxHost);
85        gridc.gridwidth = 1;
86        lbl = new Label("Port:");
87        grid.setConstraints(lbl, gridc);
88        proxyDialog.add(lbl);
89        textPrxPort = new TextField("", 4);
90        grid.setConstraints(textPrxPort, gridc);
91        proxyDialog.add(textPrxPort);
92  
93        gridc.gridy = 2;
94        gridc.gridwidth = 2;
95        lbl = new Label("Username:");
96        grid.setConstraints(lbl, gridc);
97        proxyDialog.add(lbl);
98        textPrxUser = new TextField("", 10);
99        grid.setConstraints(textPrxUser, gridc);
100       proxyDialog.add(textPrxUser);
101       lbl = new Label("Password:");
102       grid.setConstraints(lbl, gridc);
103       proxyDialog.add(lbl);
104       textPrxPasswd = new TextField("", 10);
105       textPrxPasswd.setEchoChar('*');
106       grid.setConstraints(textPrxPasswd, gridc);
107       proxyDialog.add(textPrxPasswd);
108 
109       gridc.gridy = 3;
110       gridc.gridwidth = 4;
111       cbNeedAuth = new Checkbox("Need authentication");
112       grid.setConstraints(cbNeedAuth, gridc);
113       proxyDialog.add(cbNeedAuth);
114       cbNeedAuth.addItemListener(il);
115 
116       Panel bp = new Panel(new FlowLayout());
117       bp.add(b = new Button("OK"));
118       b.addActionListener(new ActionListener() {
119     public void actionPerformed(ActionEvent e) {
120         try {
121       String prxTypeStr = choicePrxType.getSelectedItem();
122       propsHandler.setProperty("proxytype", prxTypeStr);
123       if(!"none".equalsIgnoreCase(prxTypeStr)) {
124           propsHandler.setProperty("proxyhost", textPrxHost.getText());
125           propsHandler.setProperty("proxyport", textPrxPort.getText());
126       }
127       if(cbNeedAuth.getState()) {
128           propsHandler.setProperty("proxyuser", textPrxUser.getText());
129           propsHandler.setProperty("prxpassword", textPrxPasswd.getText());
130       } else if("socks4".equals(prxTypeStr)) {
131           propsHandler.setProperty("proxyuser", textPrxUser.getText());
132       }
133       proxyDialog.setVisible(false);
134         } catch (Exception ee) {
135       // !!!
136         }
137     }
138       });
139       bp.add(b = new Button("Cancel"));
140       b.addActionListener(new AWTConvenience.CloseAction(proxyDialog));
141 
142       gridc.gridy = 4;
143       gridc.anchor = GridBagConstraints.CENTER;
144       gridc.fill = GridBagConstraints.NONE;
145       gridc.gridwidth = GridBagConstraints.REMAINDER;
146       grid.setConstraints(bp, gridc);
147       proxyDialog.add(bp);
148 
149       proxyDialog.addWindowListener(new AWTConvenience.CloseAdapter(b));
150 
151       AWTConvenience.setBackgroundOfChildren(proxyDialog);
152 
153       proxyDialog.setResizable(true);
154       proxyDialog.pack();
155   }
156 
157   proxyDialog.setTitle(title);
158 
159   String prxType = propsHandler.getProperty("proxytype");
160   choicePrxType.select(prxType);
161   String prxUser = propsHandler.getProperty("proxyuser");
162   boolean needAuth = (prxUser != null && (prxUser.trim().length() > 0));
163   cbNeedAuth.setState(needAuth);
164   textPrxHost.setText(propsHandler.getProperty("proxyhost"));
165   textPrxPort.setText(propsHandler.getProperty("proxyport"));
166   textPrxUser.setText(propsHandler.getProperty("proxyuser"));
167 
168   updateFromType();
169 
170   AWTConvenience.placeDialog(proxyDialog);
171 
172   proxyDialog.setVisible(true);
173     }
174 
175     private static void updateFromType() {
176   boolean proxyEnable = false;
177   boolean authEnable  = false;
178   String  proxyType   = choicePrxType.getSelectedItem();
179   int     type        = 0;
180 
181   try {
182       type = SSH.getProxyType(proxyType);
183       switch(type) {
184       case SSH.PROXY_NONE:
185     break;
186       case SSH.PROXY_HTTP:
187       case SSH.PROXY_SOCKS5_DNS:
188       case SSH.PROXY_SOCKS5_IP:
189     authEnable = true;
190     // Fall through
191       case SSH.PROXY_SOCKS4:
192     proxyEnable = true;
193     break;
194       }
195   } catch (Exception ee) {
196       // !!!
197   }
198   textPrxHost.setEnabled(proxyEnable);
199   textPrxPort.setEnabled(proxyEnable);
200   cbNeedAuth.setEnabled(authEnable);
201 
202   if(!authEnable)
203       cbNeedAuth.setState(false);
204 
205   boolean needAuth = cbNeedAuth.getState();
206 
207   textPrxUser.setEnabled(needAuth);
208   textPrxPasswd.setEnabled(needAuth);
209 
210   if(proxyEnable) {
211       if(textPrxHost.getText().length() == 0)
212     textPrxHost.setText(propsHandler.getProperty("proxyhost"));
213       if(textPrxPort.getText().length() == 0)
214     textPrxPort.setText(propsHandler.getProperty("proxyport"));
215   } else {
216       textPrxHost.setText("");
217       textPrxPort.setText("");
218   }
219 
220   if(needAuth) {
221       if(textPrxUser.getText().length() == 0)
222     textPrxUser.setText(propsHandler.getProperty("proxyuser"));
223   } else {
224       textPrxUser.setText("");
225       textPrxPasswd.setText("");
226   }
227 
228   if(type == SSH.PROXY_SOCKS4) {
229       textPrxUser.setEnabled(true);
230       String user = propsHandler.getProperty("proxyuser");
231       if(textPrxUser.getText().length() == 0) {
232     if(user == null)
233         user = "anonymous";
234     textPrxUser.setText(user);
235       }
236   }
237 
238   if(proxyEnable)
239       textPrxHost.requestFocus();
240     }
241 }