View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.example.proxy;
    21
    22  import java.net.InetSocketAddress;
    23
    24  import org.apache.mina.core.service.IoConnector;
    25  import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
    26  import org.apache.mina.transport.socket.nio.NioSocketConnector;
    27
    28  /**
    29   * (<b>Entry point</b>) Demonstrates how to write a very simple tunneling proxy
    30   * using MINA. The proxy only logs all data passing through it. This is only
    31   * suitable for text based protocols since received data will be converted into
    32   * strings before being logged.
    33   * <p>
    34   * Start a proxy like this:<br/>
    35   * <code>org.apache.mina.example.proxy.Main 12345 www.google.com 80</code><br/>
    36   * and open <a href="http://localhost:12345">http://localhost:12345</a> in a
    37   * browser window.
    38   * </p>
    39   *
    40   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    41   */
    42  public class Main {
    43
    44      public static void main(String[] args) throws Exception {
    45          if (args.length != 3) {
    46              System.out.println(Main.class.getName()
    47                      + " <proxy-port> <server-hostname> <server-port>");
    48              return;
    49          }
    50
    51          // Create TCP/IP acceptor.
    52          NioSocketAcceptor acceptor = new NioSocketAcceptor();
    53
    54          // Create TCP/IP connector.
    55          IoConnector connector = new NioSocketConnector();
    56
    57          // Set connect timeout.
    58          connector.setConnectTimeoutMillis(30*1000L);
    59
    60          ClientToProxyIoHandler handler = new ClientToProxyIoHandler(connector,
    61                  new InetSocketAddress(args[1], Integer.parseInt(args[2])));
    62
    63          // Start proxy.
    64          acceptor.setHandler(handler);
    65          acceptor.bind(new InetSocketAddress(Integer.parseInt(args[0])));
    66
    67          System.out.println("Listening on port " + Integer.parseInt(args[0]));
    68      }
    69
    70  }