Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Modified Source of TcpTunnelGui to remove logging textboxes

By: KevinLaity on Nov 30th, 2012  |  syntax: None  |  size: 5.17 KB  |  views: 88  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.net.*;
  2. import java.io.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. /**
  7.  * A <code>TcpTunnelGui</code> object listens on the given port,
  8.  * and once <code>Start</code> is pressed, will forward all bytes
  9.  * to the given host and port. All traffic is displayed in a
  10.  * UI.
  11.  *
  12.  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
  13.  *
  14.  * Modified by Kevin Laity to remove logging textboxes which slow down the app
  15.  */
  16. public class TcpTunnelGui extends Frame {
  17.  
  18.   // Change this value to change the size of the text displayed in the
  19.   // "listen" and "tunnel" panels of the UI. (Added by JCBeatty)
  20.   int mainPanelTextSize = 14;
  21.  
  22.   int listenPort;
  23.   String tunnelHost;
  24.   int tunnelPort;
  25.   TextArea listenText, tunnelText;
  26.   Label status;
  27.   Relay inRelay, outRelay;
  28.  
  29.   public TcpTunnelGui (int listenPort, String tunnelHost, int tunnelPort) {
  30.  
  31.     Panel p;
  32.    
  33.     this.listenPort = listenPort;
  34.     this.tunnelHost = tunnelHost;
  35.     this.tunnelPort = tunnelPort;
  36.  
  37.     addWindowListener (new WindowAdapter () {
  38.       public void windowClosing (WindowEvent e) {
  39.         System.exit (0);
  40.       }
  41.     });
  42.  
  43.     // show info
  44.     setTitle ("TCP Tunnel/Monitor: Tunneling localhost:" + listenPort +
  45.               " to " + tunnelHost + ":" + tunnelPort);
  46.  
  47.     // labels
  48.     p = new Panel ();
  49.     p.setLayout (new BorderLayout ());
  50.     Label l1, l2;
  51.     p.add ("West",
  52.            l1 = new Label ("From localhost:" + listenPort, Label.CENTER));
  53.     p.add ("East",
  54.            l2 = new Label ("From " + tunnelHost + ":" + tunnelPort,
  55.                            Label.CENTER));
  56.     add ("North", p);
  57.  
  58.     /*// the monitor part
  59.     p = new Panel ();
  60.     p.setLayout (new GridLayout (-1,2));
  61.     p.add (listenText = new TextArea ());
  62.     p.add (tunnelText = new TextArea ());
  63.     add ("Center", p);*/
  64.    
  65.     /*// Added by JCBeatty
  66.     Font mainPanelFont = new Font( "SansSerif", Font.PLAIN, mainPanelTextSize );
  67.     listenText.setFont( mainPanelFont );
  68.     tunnelText.setFont( mainPanelFont );*/
  69.  
  70.     // clear and status
  71.     Panel p2 = new Panel ();
  72.     p2.setLayout (new BorderLayout ());
  73.  
  74.     p = new Panel ();
  75.     /*Button b = new Button ("Clear");
  76.     b.addActionListener (new ActionListener () {
  77.       public void actionPerformed (ActionEvent e) {
  78.         listenText.setText ("");
  79.         tunnelText.setText ("");
  80.       }
  81.     });
  82.     p.add (b);*/
  83.     p2.add ("Center", p);
  84.  
  85.     p2.add ("South", status = new Label ());
  86.     add ("South", p2);
  87.  
  88.     pack ();
  89.     show ();
  90.  
  91.     Font f = l1.getFont ();
  92.     l1.setFont (new Font (f.getName (), Font.BOLD, f.getSize ()));
  93.     l2.setFont (new Font (f.getName (), Font.BOLD, f.getSize ()));
  94.   }
  95.  
  96.   public int getListenPort () {
  97.     return listenPort;
  98.   }
  99.  
  100.   public String getTunnelHost () {
  101.     return tunnelHost;
  102.   }
  103.  
  104.   public int getTunnelPort () {
  105.     return tunnelPort;
  106.   }
  107.  
  108.   public TextArea getListenText () {
  109.     return listenText;
  110.   }
  111.  
  112.   public TextArea getTunnelText () {
  113.     return tunnelText;
  114.   }
  115.  
  116.   public Label getStatus () {
  117.     return status;
  118.   }
  119.  
  120.   public static void main (String args[]) throws IOException {
  121.  
  122.     if (args.length != 3) {
  123.       System.err.println ("Usage: java TcpTunnelGui listenport tunnelhost " +
  124.                           "tunnelport");
  125.       System.exit (1);
  126.     }
  127.    
  128.     int listenPort = Integer.parseInt (args[0]);
  129.     String tunnelHost = args[1];
  130.     int tunnelPort = Integer.parseInt (args[2]);
  131.     final TcpTunnelGui ttg =
  132.       new TcpTunnelGui (listenPort, tunnelHost, tunnelPort);
  133.  
  134.     // create the server thread
  135.     Thread server = new Thread () {
  136.       public void run () {
  137.         ServerSocket ss = null;
  138.         Label status = ttg.getStatus ();
  139.         try {
  140.           ss = new ServerSocket (ttg.getListenPort ());
  141.         } catch (Exception e) {
  142.           e.printStackTrace ();
  143.           System.exit (1);
  144.         }
  145.         while (true) {
  146.           try {
  147.             status.setText ("Listening for connections on port " +
  148.                             ttg.getListenPort () + " ...");
  149.             // accept the connection from my client
  150.             Socket sc = ss.accept ();
  151.            
  152.             // connect to the thing I'm tunnelling for
  153.             Socket st = new Socket (ttg.getTunnelHost (),
  154.                                     ttg.getTunnelPort ());
  155.            
  156.             status.setText ("Tunnelling port " + ttg.getListenPort () +
  157.                             " to port " + ttg.getTunnelPort () +
  158.                             " on host " + ttg.getTunnelHost () + " ...");
  159.            
  160.             // relay the stuff thru
  161.             new Relay (sc.getInputStream (), st.getOutputStream (),
  162.                        null).start ();//ttg.getListenText ()
  163.             new Relay (st.getInputStream (), sc.getOutputStream (),
  164.                        null).start ();//ttg.getTunnelText ()
  165.            
  166.             // that's it .. they're off; now I go back to my stuff.
  167.           } catch (Exception ee) {
  168.             status.setText ("Ouch! [See console for details]: " +
  169.                             ee.getMessage ());
  170.             ee.printStackTrace ();
  171.           }
  172.         }
  173.       }
  174.     };
  175.     server.start ();
  176.   }
  177. }