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
022import java.net.InetSocketAddress;
023
024import org.apache.mina.core.service.IoConnector;
025import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
026import org.apache.mina.transport.socket.nio.NioSocketConnector;
027
028/**
029 * (<b>Entry point</b>) Demonstrates how to write a very simple tunneling proxy
030 * using MINA. The proxy only logs all data passing through it. This is only
031 * suitable for text based protocols since received data will be converted into
032 * strings before being logged.
033 * <p>
034 * Start a proxy like this:<br/>
035 * <code>org.apache.mina.example.proxy.Main 12345 www.google.com 80</code><br/>
036 * and open <a href="http://localhost:12345">http://localhost:12345</a> in a
037 * browser window.
038 * </p>
039 *
040 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
041 */
042public class Main {
043
044    public static void main(String[] args) throws Exception {
045        if (args.length != 3) {
046            System.out.println(Main.class.getName()
047                    + " <proxy-port> <server-hostname> <server-port>");
048            return;
049        }
050
051        // Create TCP/IP acceptor.
052        NioSocketAcceptor acceptor = new NioSocketAcceptor();
053
054        // Create TCP/IP connector.
055        IoConnector connector = new NioSocketConnector();
056
057        // Set connect timeout.
058        connector.setConnectTimeoutMillis(30*1000L);
059
060        ClientToProxyIoHandler handler = new ClientToProxyIoHandler(connector,
061                new InetSocketAddress(args[1], Integer.parseInt(args[2])));
062
063        // Start proxy.
064        acceptor.setHandler(handler);
065        acceptor.bind(new InetSocketAddress(Integer.parseInt(args[0])));
066
067        System.out.println("Listening on port " + Integer.parseInt(args[0]));
068    }
069
070}