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

Quick Search    Search Deep

Source code: mindbright/ssh/SSHTunnelDialog.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:23 $
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 SSHTunnelDialog {
29      private static Dialog    basicTunnelsDialog = null;
30      private static List      tunnelList;
31      private static TextField remoteHost, remotePort, localPort;
32      private static Choice    protoChoice;
33      private final static String[] protos = { "general", "ftp", "telnet", "smtp", "http", "pop2", "pop3", "nntp", "imap" };
34      final static int[]    servs  = {  0, 21, 23, 25, 80, 109, 110, 119, 143 };
35  
36      private static SSHPropertyHandler propsHandler;
37      private static Frame              parent;
38      private static SSHClient          client;
39  
40      public static void show(String title, SSHClient cli,
41            SSHPropertyHandler props, Frame p) {
42    propsHandler = props;
43    parent       = p;
44    client       = cli;
45    if(basicTunnelsDialog == null) {
46        basicTunnelsDialog = new Dialog(parent, title, true);
47  
48        GridBagLayout       grid  = new GridBagLayout();
49        GridBagConstraints  gridc = new GridBagConstraints();
50        Label               lbl;
51        Button              b;
52        ActionListener      al;
53  
54        basicTunnelsDialog.setLayout(grid);
55  
56        gridc.fill      = GridBagConstraints.NONE;
57        gridc.anchor    = GridBagConstraints.WEST;
58        gridc.gridy     = 0;
59        gridc.insets    = new Insets(4, 4, 0, 4);
60  
61        lbl = new Label("Current local tunnels:");
62        gridc.gridwidth = 2;
63        grid.setConstraints(lbl, gridc);
64        basicTunnelsDialog.add(lbl);
65  
66        gridc.fill      = GridBagConstraints.BOTH;
67        gridc.anchor    = GridBagConstraints.WEST;
68        gridc.insets    = new Insets(4, 4, 4, 4);
69        gridc.weightx   = 1.0;
70        gridc.weighty   = 1.0;
71        gridc.gridwidth = 4;
72        gridc.gridy     = 1;
73  
74        tunnelList = new List(8);
75        grid.setConstraints(tunnelList, gridc);
76        basicTunnelsDialog.add(tunnelList);
77        tunnelList.addActionListener(new ActionListener() {
78      public void actionPerformed(ActionEvent e) {
79          int i = tunnelList.getSelectedIndex();
80          if(i != -1) {
81        SSHClient.LocalForward fwd = (SSHClient.LocalForward) client.localForwards.elementAt(i);
82        localPort.setText(String.valueOf(fwd.localPort));
83        remotePort.setText(String.valueOf(fwd.remotePort));
84        remoteHost.setText(fwd.remoteHost);
85        for(i = 1; i < servs.length; i++) {
86            if(fwd.remotePort == servs[i]) {
87          protoChoice.select(protos[i]);
88          break;
89            }
90        }
91        if(i == servs.length)
92            protoChoice.select("general");
93          }
94      }
95        });
96  
97        gridc.fill      = GridBagConstraints.NONE;
98        gridc.weighty   = 0;
99        gridc.gridy     = 2;
100       gridc.gridwidth = 1;
101 
102       lbl = new Label("Local port:");
103       grid.setConstraints(lbl, gridc);
104       basicTunnelsDialog.add(lbl);
105       gridc.fill      = GridBagConstraints.HORIZONTAL;
106       gridc.weightx   = 1.0;
107       localPort = new TextField("", 5);
108       grid.setConstraints(localPort, gridc);
109       basicTunnelsDialog.add(localPort);
110 
111       lbl = new Label("Protocol:");
112       grid.setConstraints(lbl, gridc);
113       gridc.fill      = GridBagConstraints.NONE;
114       basicTunnelsDialog.add(lbl);
115       protoChoice = new Choice();
116       for(int i = 0; i < protos.length; i++) {
117     protoChoice.add(protos[i]);
118       }
119       protoChoice.select("general");
120       grid.setConstraints(protoChoice, gridc);
121       basicTunnelsDialog.add(protoChoice);
122       protoChoice.addItemListener(new ItemListener() {
123     public void itemStateChanged(ItemEvent e) {
124         String it = (String)e.getItem();
125         int i;
126         for(i = 0; i < protos.length; i++)
127       if(it.equals(protos[i]))
128           break;
129         if(i > 0) {
130       remotePort.setText(String.valueOf(servs[i]));
131         }
132     }
133       });
134 
135       gridc.gridy     = 3;
136       lbl = new Label("Remote host:");
137       grid.setConstraints(lbl, gridc);
138       basicTunnelsDialog.add(lbl);
139       gridc.fill      = GridBagConstraints.HORIZONTAL;
140       gridc.weightx   = 1.0;
141       gridc.gridwidth = 3;
142       remoteHost = new TextField("", 16);
143       grid.setConstraints(remoteHost, gridc);
144       basicTunnelsDialog.add(remoteHost);
145       gridc.gridy     = 4;
146       gridc.fill      = GridBagConstraints.NONE;
147       gridc.gridwidth = 1;
148       lbl = new Label("Remote port:");
149       grid.setConstraints(lbl, gridc);
150       basicTunnelsDialog.add(lbl);
151       remotePort = new TextField("", 5);
152       gridc.fill      = GridBagConstraints.HORIZONTAL;
153       gridc.weightx   = 0.9;
154       grid.setConstraints(remotePort, gridc);
155       basicTunnelsDialog.add(remotePort);
156 
157       b = new Button("Add");
158       b.addActionListener(new ActionListener() {
159     public void actionPerformed(ActionEvent e) {
160         String rh = remoteHost.getText();
161         String plug = "general";
162         int    lp = -1, rp = -1;
163         try {
164       lp = Integer.valueOf(localPort.getText()).intValue();
165       rp = Integer.valueOf(remotePort.getText()).intValue();
166       if(lp < 1 || lp > 65535) {
167           lp = -1;
168           throw new NumberFormatException();
169       }
170       if(rp < 1 || rp > 65535) {
171           rp = -1;
172           throw new NumberFormatException();
173       }
174         } catch (NumberFormatException ee) {
175       if(lp == -1) {
176           localPort.setText("");
177           localPort.requestFocus();
178       } else {
179           remotePort.setText("");
180           remotePort.requestFocus();
181       }
182       return;
183         }
184         if(protoChoice.getSelectedItem().equals("ftp"))
185       plug = "ftp";
186         try {
187       propsHandler.setProperty("local" + client.localForwards.size(),
188               "/" + plug + "/" + lp + ":" + rh + ":" +  rp);
189       if(client.isOpened())
190           SSHMiscDialogs.alert("Tunnel Notice",
191              "Tunnel is now open and operational",
192              parent);
193         } catch (Throwable ee) {
194       SSHMiscDialogs.alert("Tunnel Notice",
195                "Could not open tunnel: " +
196                ee.getMessage(), parent);
197         }
198         updateTunnelList();
199     }
200       });
201       gridc.fill = GridBagConstraints.HORIZONTAL;
202       gridc.weightx   = 0.1;
203       grid.setConstraints(b, gridc);
204       basicTunnelsDialog.add(b);
205       b = new Button("Delete");
206       b.addActionListener(new ActionListener() {
207     public void actionPerformed(ActionEvent e) {
208         int i = tunnelList.getSelectedIndex();
209         if(i != -1) {
210       propsHandler.removeLocalTunnelAt(i, true);
211         }
212         updateTunnelList();
213     }
214       });
215       grid.setConstraints(b, gridc);
216       basicTunnelsDialog.add(b);
217       
218       b = new Button("Close Dialog");
219       b.addActionListener(new AWTConvenience.CloseAction(basicTunnelsDialog));
220       gridc.gridy     = 5;
221       gridc.fill      = GridBagConstraints.NONE;
222       gridc.gridwidth = GridBagConstraints.REMAINDER;
223       gridc.anchor    = GridBagConstraints.CENTER;
224       gridc.ipady     = 2;
225       gridc.ipadx     = 2;
226       grid.setConstraints(b, gridc);
227       basicTunnelsDialog.add(b);
228 
229       basicTunnelsDialog.addWindowListener(new AWTConvenience.CloseAdapter(b));
230 
231       AWTConvenience.setBackgroundOfChildren(basicTunnelsDialog);
232 
233       basicTunnelsDialog.setResizable(true);
234       basicTunnelsDialog.pack();
235   }
236   updateTunnelList();
237 
238   basicTunnelsDialog.setTitle(title);
239 
240   AWTConvenience.placeDialog(basicTunnelsDialog);
241   localPort.requestFocus();
242   basicTunnelsDialog.setVisible(true);
243     }
244 
245     private static void updateTunnelList() {
246   tunnelList.removeAll();
247   for(int i = 0; i < client.localForwards.size(); i++) {
248       SSHClient.LocalForward fwd = (SSHClient.LocalForward) client.localForwards.elementAt(i);
249       String plugStr = (fwd.plugin.equals("general") ? "" : " (plugin: " + fwd.plugin + ")");
250       tunnelList.add("local: " + fwd.localPort + " -> remote: " + fwd.remoteHost + "/" +
251          fwd.remotePort + plugStr);
252   }
253     }
254 
255 }