001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.example.proxy;
021
022
023import java.net.InetSocketAddress;
024
025import org.apache.mina.core.service.IoConnector;
026import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
027import org.apache.mina.transport.socket.nio.NioSocketConnector;
028
029
030/**
031 * (<b>Entry point</b>) Demonstrates how to write a very simple tunneling proxy
032 * using MINA. The proxy only logs all data passing through it. This is only
033 * suitable for text based protocols since received data will be converted into
034 * strings before being logged.
035 * <p>
036 * Start a proxy like this:<br>
037 * <code>org.apache.mina.example.proxy.Main 12345 www.google.com 80</code><br>
038 * and open <a href="http://localhost:12345">http://localhost:12345</a> in a
039 * browser window.
040 *
041 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
042 */
043public class Main
044{
045
046    public static void main( String[] args ) throws Exception
047    {
048        if ( args.length != 3 )
049        {
050            System.out.println( Main.class.getName()
051                + " <proxy-port> <server-hostname> <server-port>" );
052            return;
053        }
054
055        // Create TCP/IP acceptor.
056        NioSocketAcceptor acceptor = new NioSocketAcceptor();
057
058        // Create TCP/IP connector.
059        IoConnector connector = new NioSocketConnector();
060
061        // Set connect timeout.
062        connector.setConnectTimeoutMillis( 30 * 1000L );
063
064        ClientToProxyIoHandler handler = new ClientToProxyIoHandler( connector,
065            new InetSocketAddress( args[1], Integer.parseInt( args[2] ) ) );
066
067        // Start proxy.
068        acceptor.setHandler( handler );
069        acceptor.bind( new InetSocketAddress( Integer.parseInt( args[0] ) ) );
070
071        System.out.println( "Listening on port " + Integer.parseInt( args[0] ) );
072    }
073
074}