1 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 73 public class TcpTunnelGui extends Frame { 74 int listenPort; 75 String tunnelHost; 76 int tunnelPort; 77 TextArea listenText, tunnelText; 78 Label status; 79 Relay inRelay, outRelay; 80 81 public TcpTunnelGui (int listenPort, String 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 setTitle ("TCP Tunnel/Monitor: Tunneling localhost:" + listenPort + 96 " to " + tunnelHost + ":" + tunnelPort); 97 98 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 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 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 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 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 tunnelHost = args[1]; 175 int tunnelPort = Integer.parseInt (args[2]); 176 final TcpTunnelGui ttg = 177 new TcpTunnelGui (listenPort, tunnelHost, tunnelPort); 178 179 Thread server = new Thread () { 181 public void run () { 182 ServerSocket ss = null; 183 Label status = ttg.getStatus (); 184 try { 185 ss = new ServerSocket (ttg.getListenPort ()); 186 } catch (Exception 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 Socket sc = ss.accept (); 196 197 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 new Relay (sc.getInputStream (), st.getOutputStream (), 207 ttg.getListenText ()).start (); 208 new Relay (st.getInputStream (), sc.getOutputStream (), 209 ttg.getTunnelText ()).start (); 210 211 } catch (Exception 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 |