Moved crawler to
[utils] / socketproxy / src / main / java / org / wamblee / socketproxy / SocketProxy.java
1 /*
2  * Copyright 2007 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.socketproxy;
17
18 /*
19  * Created on Apr 5, 2005
20  */
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.net.ServerSocket;
26 import java.net.Socket;
27
28 /**
29  * @author erik
30  * 
31  * TODO To change the template for this generated type comment go to Window -
32  * Preferences - Java - Code Style - Code Templates
33  *
34  * @author Erik Brakkee
35  */
36 public class SocketProxy {
37
38     public static void main( final String[] args ) throws IOException {
39         for ( int i = 0; i < args.length; i++ ) {
40             // System.out.println(i + " " + args[i]);
41             String[] fields = args[i].split( ":" );
42             final int localPort = Integer.parseInt( fields[0] );
43             final String host = fields[1];
44             final int remotePort = Integer.parseInt( fields[2] );
45             runSocketProxy( localPort, host, remotePort );
46         }
47     }
48
49     /**
50      * @param localPort
51      * @param host
52      * @param remotePort
53      */
54     private static void runSocketProxy( final int localPort,
55             final String host, final int remotePort ) {
56         new Thread( new Runnable( ) {
57             public void run( ) {
58                 try {
59                     new SocketProxy( localPort, host, remotePort );
60                 } catch ( IOException e ) {
61                     System.out.println( "Problem with socket " + localPort
62                             + ":" + host + ":" + remotePort );
63                     e.printStackTrace( );
64                 }
65             }
66         } ).start( );
67     }
68
69     public SocketProxy( int localPort, String remoteHost, int remotePort )
70             throws IOException {        
71         System.out.println( "Listening on port " + localPort );
72         ServerSocket server = new ServerSocket( localPort );
73         for ( ;; ) {
74             Socket socket = server.accept( );
75             System.out.println( "Got local connection on port "
76                     + localPort );
77             InputStream localIs = socket.getInputStream( );
78             OutputStream localOs = socket.getOutputStream( );
79             Socket clientSocket = new Socket( remoteHost, remotePort );
80             final String description = "Port forwarding: " + localPort
81                     + " -> " + remoteHost + ":" + remotePort;
82             System.out.println( description + " established." );
83             InputStream serverIs = clientSocket.getInputStream( );
84             OutputStream serverOs = clientSocket.getOutputStream( );
85             Barrier barrier = new Barrier(2);
86             final Thread t1 = runForwarder( barrier, "> ", localIs, serverOs );
87             final Thread t2 = runForwarder( barrier, "< ", serverIs, localOs );
88             waitForConnectionClose( description, t1, t2 );
89         }
90     }
91
92     /**
93      * @param description
94      * @param t1
95      * @param t2
96      */
97     private void waitForConnectionClose( final String description,
98             final Thread t1, final Thread t2 ) {
99         new Thread( new Runnable( ) {
100             public void run( ) {
101                 try {
102                     t1.join( );
103                     t2.join( );
104                 } catch ( InterruptedException e ) {
105                     e.printStackTrace( );
106                 }
107                 System.out.println( description + " closed" );
108             }
109         } ).start( );
110     }
111
112     private Thread runForwarder( final Barrier barrier, final String prefix,
113             final InputStream is, final OutputStream os ) {
114         Thread t = new ForwarderThread(prefix, barrier, is, os);
115         t.start( );
116         return t;
117     }
118 }