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

Quick Search    Search Deep

Source code: org/jbpm/util/tcp/TcpProxy.java


1   package org.jbpm.util.tcp;
2   
3   import java.net.*;
4   import java.io.*;
5   import java.util.*;
6   import java.text.*;
7   import java.awt.*;
8   import java.awt.event.*;
9   
10  /**
11   * This is a modified version of the TcpTunnelGui utility
12   * borrowed from the xml.apache.org project.
13   * A <code>TcpTunnelGui</code> object listens on the given port,
14   * and once <code>Start</code> is pressed, will forward all bytes
15   * to the given host and port. All traffic is displayed in a
16   * UI.
17   *
18   * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
19   */
20  public class TcpProxy extends Frame {
21    int listenPort;
22    String tunnelHost;
23    int tunnelPort;
24    TextArea listenText, tunnelText;
25    Label status;
26    Relay inRelay, outRelay;
27  
28    public TcpProxy(int listenPort, String tunnelHost, int tunnelPort) {
29      Panel p;
30  
31      this.listenPort = listenPort;
32      this.tunnelHost = tunnelHost;
33      this.tunnelPort = tunnelPort;
34  
35      addWindowListener(new WindowAdapter() {
36        public void windowClosing(WindowEvent e) {
37          System.exit(0);
38        }
39      });
40  
41      // show info
42      setTitle("TCP Tunnel/Monitor: Tunneling localhost:" + listenPort + " to " + tunnelHost + ":" + tunnelPort);
43  
44      // labels
45      p = new Panel();
46      p.setLayout(new BorderLayout());
47      Label l1, l2;
48      p.add("West", l1 = new Label("From localhost:" + listenPort, Label.CENTER));
49      p.add("East", l2 = new Label("From " + tunnelHost + ":" + tunnelPort, Label.CENTER));
50      add("North", p);
51  
52      // the monitor part
53      p = new Panel();
54      p.setLayout(new GridLayout(-1, 2));
55      p.add(listenText = new TextArea());
56      p.add(tunnelText = new TextArea());
57      add("Center", p);
58  
59      // clear and status
60      Panel p2 = new Panel();
61      p2.setLayout(new BorderLayout());
62  
63      p = new Panel();
64      Button b = new Button("Clear");
65      b.addActionListener(new ActionListener() {
66        public void actionPerformed(ActionEvent e) {
67          listenText.setText("");
68          tunnelText.setText("");
69        }
70      });
71      p.add(b);
72      p2.add("Center", p);
73  
74      p2.add("South", status = new Label());
75      add("South", p2);
76  
77      pack();
78      show();
79  
80      Font f = l1.getFont();
81      l1.setFont(new Font(f.getName(), Font.BOLD, f.getSize()));
82      l2.setFont(new Font(f.getName(), Font.BOLD, f.getSize()));
83    }
84  
85    public int getListenPort() {
86      return listenPort;
87    }
88  
89    public String getTunnelHost() {
90      return tunnelHost;
91    }
92  
93    public int getTunnelPort() {
94      return tunnelPort;
95    }
96  
97    public TextArea getListenText() {
98      return listenText;
99    }
100 
101   public TextArea getTunnelText() {
102     return tunnelText;
103   }
104 
105   public Label getStatus() {
106     return status;
107   }
108 
109   public static void main(String args[]) throws IOException {
110     if (args.length != 3) {
111       System.err.println("Usage: java ProxyGui listenport tunnelhost " + "tunnelport");
112       System.exit(1);
113     }
114 
115     int listenPort = Integer.parseInt(args[0]);
116     String tunnelHost = args[1];
117     int tunnelPort = Integer.parseInt(args[2]);
118     final TcpProxy ttg = new TcpProxy(listenPort, tunnelHost, tunnelPort);
119 
120     // create the server thread
121     Thread server = new Thread() {
122       public void run() {
123         ServerSocket ss = null;
124         Label status = ttg.getStatus();
125         try {
126           ss = new ServerSocket(ttg.getListenPort());
127         } catch (Exception e) {
128           e.printStackTrace();
129           System.exit(1);
130         }
131         while (true) {
132           try {
133             status.setText("Listening for connections on port " + ttg.getListenPort() + " ...");
134             // accept the connection from my client
135             Socket sc = ss.accept();
136 
137             // connect to the thing I'm tunnelling for
138             Socket st = new Socket(ttg.getTunnelHost(), ttg.getTunnelPort());
139 
140             status.setText(
141               "Tunnelling port " + ttg.getListenPort() + " to port " + ttg.getTunnelPort() + " on host " + ttg.getTunnelHost() + " ...");
142 
143             // relay the stuff thru
144             Thread fromBrowserToServer = new Relay(sc.getInputStream(), st.getOutputStream(), "<<< B2S <<<", ttg.getListenText());
145             Thread fromServerToBrowser = new Relay(st.getInputStream(), sc.getOutputStream(), ">>> S2B >>>", ttg.getTunnelText());
146 
147             fromBrowserToServer.start();
148             fromServerToBrowser.start();
149 
150             /* 
151             // Serialize tcp-connections
152             fromBrowserToServer.join();        
153             fromServerToBrowser.join();        
154             */
155 
156           } catch (Exception ee) {
157             status.setText("Ouch! [See console for details]: " + ee.getMessage());
158             ee.printStackTrace();
159           }
160         }
161       }
162     };
163     server.start();
164   }
165 
166   public static class Relay extends Thread {
167     private InputStream in;
168     private OutputStream out;
169     private String prefix;
170     private final static int BUFSIZ = 4096;
171     private byte buf[] = new byte[BUFSIZ];
172     private TextArea textArea = null;
173 
174     public Relay(InputStream in, OutputStream out, String prefix, TextArea textArea) {
175       this.in = in;
176       this.out = out;
177       this.prefix = prefix;
178       this.textArea = textArea;
179     }
180 
181     public void run() {
182       int n = 0;
183 
184       try {
185         textArea.append(
186           "\n\n=== START OF A TRANSMISSION : " + dateFormat.format(new Date()) + " =======================================\n");
187 
188         while ((n = in.read(buf)) > 0) {
189           out.write(buf, 0, n);
190           out.flush();
191 
192           for (int i = 0; i < n; i++) {
193             if (buf[i] == 7)
194               buf[i] = '#';
195           }
196 
197           String msg = new String(buf, 0, n);
198           System.out.println(prefix + " : " + msg.length());
199           textArea.append(msg);
200         }
201       } catch (IOException e) {
202         e.printStackTrace();
203       } finally {
204         try {
205           in.close();
206           out.close();
207         } catch (IOException e) {
208           e.printStackTrace();
209         }
210       }
211 
212       System.out.println("Quiting stream proxy " + prefix + "...");
213     }
214   }
215 
216   private static final Format dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss,SSS");
217 }