KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > util > net > TcpTunnelGui


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "SOAP" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.util.net;
59
60 import java.net.*;
61 import java.io.*;
62 import java.awt.*;
63 import java.awt.event.*;
64
65 /**
66  * A <code>TcpTunnelGui</code> object listens on the given port,
67  * and once <code>Start</code> is pressed, will forward all bytes
68  * to the given host and port. All traffic is displayed in a
69  * UI.
70  *
71  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
72  */

73 public class TcpTunnelGui extends Frame {
74   int listenPort;
75   String JavaDoc tunnelHost;
76   int tunnelPort;
77   TextArea listenText, tunnelText;
78   Label status;
79   Relay inRelay, outRelay;
80
81   public TcpTunnelGui (int listenPort, String JavaDoc tunnelHost, int tunnelPort) {
82     Panel p;
83     
84     this.listenPort = listenPort;
85     this.tunnelHost = tunnelHost;
86     this.tunnelPort = tunnelPort;
87
88     addWindowListener (new WindowAdapter () {
89       public void windowClosing (WindowEvent e) {
90         System.exit (0);
91       }
92     });
93
94     // show info
95
setTitle ("TCP Tunnel/Monitor: Tunneling localhost:" + listenPort +
96               " to " + tunnelHost + ":" + tunnelPort);
97
98     // labels
99
p = new Panel ();
100     p.setLayout (new BorderLayout ());
101     Label l1, l2;
102     p.add ("West",
103            l1 = new Label ("From localhost:" + listenPort, Label.CENTER));
104     p.add ("East",
105            l2 = new Label ("From " + tunnelHost + ":" + tunnelPort,
106                            Label.CENTER));
107     add ("North", p);
108
109     // the monitor part
110
p = new Panel ();
111     p.setLayout (new GridLayout (-1,2));
112     p.add (listenText = new TextArea ());
113     p.add (tunnelText = new TextArea ());
114     add ("Center", p);
115
116     // clear and status
117
Panel p2 = new Panel ();
118     p2.setLayout (new BorderLayout ());
119
120     p = new Panel ();
121     Button b = new Button ("Clear");
122     b.addActionListener (new ActionListener () {
123       public void actionPerformed (ActionEvent e) {
124         listenText.setText ("");
125         tunnelText.setText ("");
126       }
127     });
128     p.add (b);
129     p2.add ("Center", p);
130
131     p2.add ("South", status = new Label ());
132     add ("South", p2);
133
134     pack ();
135     show ();
136
137     Font f = l1.getFont ();
138     l1.setFont (new Font (f.getName (), Font.BOLD, f.getSize ()));
139     l2.setFont (new Font (f.getName (), Font.BOLD, f.getSize ()));
140   }
141
142   public int getListenPort () {
143     return listenPort;
144   }
145
146   public String JavaDoc getTunnelHost () {
147     return tunnelHost;
148   }
149
150   public int getTunnelPort () {
151     return tunnelPort;
152   }
153   
154   public TextArea getListenText () {
155     return listenText;
156   }
157
158   public TextArea getTunnelText () {
159     return tunnelText;
160   }
161
162   public Label getStatus () {
163     return status;
164   }
165
166   public static void main (String JavaDoc args[]) throws IOException {
167     if (args.length != 3) {
168       System.err.println ("Usage: java TcpTunnelGui listenport tunnelhost " +
169                           "tunnelport");
170       System.exit (1);
171     }
172     
173     int listenPort = Integer.parseInt (args[0]);
174     String JavaDoc tunnelHost = args[1];
175     int tunnelPort = Integer.parseInt (args[2]);
176     final TcpTunnelGui ttg =
177       new TcpTunnelGui (listenPort, tunnelHost, tunnelPort);
178
179     // create the server thread
180
Thread JavaDoc server = new Thread JavaDoc () {
181       public void run () {
182         ServerSocket ss = null;
183         Label status = ttg.getStatus ();
184         try {
185           ss = new ServerSocket (ttg.getListenPort ());
186         } catch (Exception JavaDoc e) {
187           e.printStackTrace ();
188           System.exit (1);
189         }
190         while (true) {
191           try {
192             status.setText ("Listening for connections on port " +
193                             ttg.getListenPort () + " ...");
194             // accept the connection from my client
195
Socket sc = ss.accept ();
196             
197             // connect to the thing I'm tunnelling for
198
Socket st = new Socket (ttg.getTunnelHost (),
199                                     ttg.getTunnelPort ());
200             
201             status.setText ("Tunnelling port " + ttg.getListenPort () +
202                             " to port " + ttg.getTunnelPort () +
203                             " on host " + ttg.getTunnelHost () + " ...");
204             
205             // relay the stuff thru
206
new Relay (sc.getInputStream (), st.getOutputStream (),
207                        ttg.getListenText ()).start ();
208             new Relay (st.getInputStream (), sc.getOutputStream (),
209                        ttg.getTunnelText ()).start ();
210             
211             // that's it .. they're off; now I go back to my stuff.
212
} catch (Exception JavaDoc ee) {
213             status.setText ("Ouch! [See console for details]: " +
214                             ee.getMessage ());
215             ee.printStackTrace ();
216           }
217         }
218       }
219     };
220     server.start ();
221   }
222 }
223
Popular Tags