Added author to all java files with class comments.
[utils] / socketproxy / src / main / java / org / wamblee / socketproxy / SocketProxy.java
1 package org.wamblee.socketproxy;
2
3 /*
4  * Created on Apr 5, 2005
5  */
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.net.ServerSocket;
11 import java.net.Socket;
12
13 /**
14  * @author erik
15  * 
16  * TODO To change the template for this generated type comment go to Window -
17  * Preferences - Java - Code Style - Code Templates
18  *
19  * @author Erik Brakkee
20  */
21 public class SocketProxy {
22
23     public static void main( final String[] args ) throws IOException {
24         for ( int i = 0; i < args.length; i++ ) {
25             // System.out.println(i + " " + args[i]);
26             String[] fields = args[i].split( ":" );
27             final int localPort = Integer.parseInt( fields[0] );
28             final String host = fields[1];
29             final int remotePort = Integer.parseInt( fields[2] );
30             runSocketProxy( localPort, host, remotePort );
31         }
32     }
33
34     /**
35      * @param localPort
36      * @param host
37      * @param remotePort
38      */
39     private static void runSocketProxy( final int localPort,
40             final String host, final int remotePort ) {
41         new Thread( new Runnable( ) {
42             public void run( ) {
43                 try {
44                     new SocketProxy( localPort, host, remotePort );
45                 } catch ( IOException e ) {
46                     System.out.println( "Problem with socket " + localPort
47                             + ":" + host + ":" + remotePort );
48                     e.printStackTrace( );
49                 }
50             }
51         } ).start( );
52     }
53
54     public SocketProxy( int localPort, String remoteHost, int remotePort )
55             throws IOException {        
56         System.out.println( "Listening on port " + localPort );
57         ServerSocket server = new ServerSocket( localPort );
58         for ( ;; ) {
59             Socket socket = server.accept( );
60             System.out.println( "Got local connection on port "
61                     + localPort );
62             InputStream localIs = socket.getInputStream( );
63             OutputStream localOs = socket.getOutputStream( );
64             Socket clientSocket = new Socket( remoteHost, remotePort );
65             final String description = "Port forwarding: " + localPort
66                     + " -> " + remoteHost + ":" + remotePort;
67             System.out.println( description + " established." );
68             InputStream serverIs = clientSocket.getInputStream( );
69             OutputStream serverOs = clientSocket.getOutputStream( );
70             Barrier barrier = new Barrier(2);
71             final Thread t1 = runForwarder( barrier, "> ", localIs, serverOs );
72             final Thread t2 = runForwarder( barrier, "< ", serverIs, localOs );
73             waitForConnectionClose( description, t1, t2 );
74         }
75     }
76
77     /**
78      * @param description
79      * @param t1
80      * @param t2
81      */
82     private void waitForConnectionClose( final String description,
83             final Thread t1, final Thread t2 ) {
84         new Thread( new Runnable( ) {
85             public void run( ) {
86                 try {
87                     t1.join( );
88                     t2.join( );
89                 } catch ( InterruptedException e ) {
90                     e.printStackTrace( );
91                 }
92                 System.out.println( description + " closed" );
93             }
94         } ).start( );
95     }
96
97     private Thread runForwarder( final Barrier barrier, final String prefix,
98             final InputStream is, final OutputStream os ) {
99         Thread t = new ForwarderThread(prefix, barrier, is, os);
100         t.start( );
101         return t;
102     }
103 }