2 * Copyright 2007 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.socketproxy;
19 * Created on Apr 5, 2005
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;
31 * TODO To change the template for this generated type comment go to Window -
32 * Preferences - Java - Code Style - Code Templates
34 * @author Erik Brakkee
36 public class SocketProxy {
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 );
54 private static void runSocketProxy( final int localPort,
55 final String host, final int remotePort ) {
56 new Thread( new Runnable( ) {
59 new SocketProxy( localPort, host, remotePort );
60 } catch ( IOException e ) {
61 System.out.println( "Problem with socket " + localPort
62 + ":" + host + ":" + remotePort );
69 public SocketProxy( int localPort, String remoteHost, int remotePort )
71 System.out.println( "Listening on port " + localPort );
72 ServerSocket server = new ServerSocket( localPort );
74 Socket socket = server.accept( );
75 System.out.println( "Got local connection on port "
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 );
97 private void waitForConnectionClose( final String description,
98 final Thread t1, final Thread t2 ) {
99 new Thread( new Runnable( ) {
104 } catch ( InterruptedException e ) {
105 e.printStackTrace( );
107 System.out.println( description + " closed" );
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);