| 
68
 | 
     1 /*
 | 
| 
 | 
     2 Copyright (c) 2009  Franklin Schmidt <fschmidt@gmail.com>
 | 
| 
 | 
     3 
 | 
| 
 | 
     4 Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
| 
 | 
     5 of this software and associated documentation files (the "Software"), to deal
 | 
| 
 | 
     6 in the Software without restriction, including without limitation the rights
 | 
| 
 | 
     7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
| 
 | 
     8 copies of the Software, and to permit persons to whom the Software is
 | 
| 
 | 
     9 furnished to do so, subject to the following conditions:
 | 
| 
 | 
    10 
 | 
| 
 | 
    11 The above copyright notice and this permission notice shall be included in
 | 
| 
 | 
    12 all copies or substantial portions of the Software.
 | 
| 
 | 
    13 
 | 
| 
 | 
    14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
| 
 | 
    15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
| 
 | 
    16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
| 
 | 
    17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
| 
 | 
    18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
| 
 | 
    19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
| 
 | 
    20 THE SOFTWARE.
 | 
| 
 | 
    21 */
 | 
| 
 | 
    22 
 | 
| 
 | 
    23 package fschmidt.tools;
 | 
| 
 | 
    24 
 | 
| 
 | 
    25 import fschmidt.util.java.IoUtils;
 | 
| 
 | 
    26 import org.slf4j.Logger;
 | 
| 
 | 
    27 import org.slf4j.LoggerFactory;
 | 
| 
 | 
    28 
 | 
| 
 | 
    29 import java.io.IOException;
 | 
| 
 | 
    30 import java.net.InetSocketAddress;
 | 
| 
 | 
    31 import java.net.ServerSocket;
 | 
| 
 | 
    32 import java.net.Socket;
 | 
| 
 | 
    33 import java.net.SocketAddress;
 | 
| 
 | 
    34 import java.util.concurrent.Executor;
 | 
| 
 | 
    35 import java.util.concurrent.Executors;
 | 
| 
 | 
    36 
 | 
| 
 | 
    37 
 | 
| 
 | 
    38 public final class TcpProxy {
 | 
| 
 | 
    39 	private static final Logger logger = LoggerFactory.getLogger(TcpProxy.class);
 | 
| 
 | 
    40 
 | 
| 
 | 
    41 	private static final Executor exec = Executors.newCachedThreadPool();
 | 
| 
 | 
    42 
 | 
| 
 | 
    43 	// never returns
 | 
| 
 | 
    44 	public static void proxy(SocketAddress from,final SocketAddress to) throws IOException {
 | 
| 
 | 
    45 		final ServerSocket server = new ServerSocket();
 | 
| 
 | 
    46 		server.bind(from);
 | 
| 
 | 
    47 		while(true) {
 | 
| 
 | 
    48 			final Socket client = server.accept();
 | 
| 
 | 
    49 			exec.execute(new Runnable(){public void run() {
 | 
| 
 | 
    50 				final Socket proxy = new Socket();
 | 
| 
 | 
    51 				try {
 | 
| 
 | 
    52 					proxy.connect(to);
 | 
| 
 | 
    53 					exec.execute(new Runnable(){public void run() {
 | 
| 
 | 
    54 						try {
 | 
| 
 | 
    55 							IoUtils.copyAll( proxy.getInputStream(), client.getOutputStream() );
 | 
| 
 | 
    56 						} catch(IOException e) {
 | 
| 
 | 
    57 							logger.debug("",e);
 | 
| 
 | 
    58 						}
 | 
| 
 | 
    59 					}});
 | 
| 
 | 
    60 					IoUtils.copyAll( client.getInputStream(), proxy.getOutputStream() );
 | 
| 
 | 
    61 				} catch(IOException e) {
 | 
| 
 | 
    62 					logger.debug("",e);
 | 
| 
 | 
    63 				} finally {
 | 
| 
 | 
    64 					try {
 | 
| 
 | 
    65 						proxy.close();
 | 
| 
 | 
    66 					} catch(IOException e) {}
 | 
| 
 | 
    67 					try {
 | 
| 
 | 
    68 						client.close();
 | 
| 
 | 
    69 					} catch(IOException e) {}
 | 
| 
 | 
    70 				}
 | 
| 
 | 
    71 			}});
 | 
| 
 | 
    72 		}
 | 
| 
 | 
    73 	}
 | 
| 
 | 
    74 
 | 
| 
 | 
    75 	private static SocketAddress parseSocketAddress(String s) {
 | 
| 
 | 
    76 		String[] a = s.split(":");
 | 
| 
 | 
    77 		return new InetSocketAddress( a[0], Integer.parseInt(a[1]) );
 | 
| 
 | 
    78 	}
 | 
| 
 | 
    79 
 | 
| 
 | 
    80 	public static void proxy(String from,String to) throws IOException {
 | 
| 
 | 
    81 		proxy(parseSocketAddress(from), parseSocketAddress(to));
 | 
| 
 | 
    82 	}
 | 
| 
 | 
    83 
 | 
| 
 | 
    84 	// better to write your own main()
 | 
| 
 | 
    85 	public static void main(String[] args) throws IOException {
 | 
| 
 | 
    86 		org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO);
 | 
| 
 | 
    87 		org.apache.log4j.BasicConfigurator.configure();
 | 
| 
 | 
    88 		proxy(args[0], args[1]);
 | 
| 
 | 
    89 	}
 | 
| 
 | 
    90 
 | 
| 
 | 
    91 }
 |