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  
23  import java.net.InetSocketAddress;
24  
25  import org.apache.mina.core.service.IoConnector;
26  import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
27  import org.apache.mina.transport.socket.nio.NioSocketConnector;
28  
29  
30  /**
31   * (<b>Entry point</b>) Demonstrates how to write a very simple tunneling proxy
32   * using MINA. The proxy only logs all data passing through it. This is only
33   * suitable for text based protocols since received data will be converted into
34   * strings before being logged.
35   * <p>
36   * Start a proxy like this:<br>
37   * <code>org.apache.mina.example.proxy.Main 12345 www.google.com 80</code><br>
38   * and open <a href="http://localhost:12345">http://localhost:12345</a> in a
39   * browser window.
40   *
41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
42   */
43  public class Main
44  {
45  
46      public static void main( String[] args ) throws Exception
47      {
48          if ( args.length != 3 )
49          {
50              System.out.println( Main.class.getName()
51                  + " <proxy-port> <server-hostname> <server-port>" );
52              return;
53          }
54  
55          // Create TCP/IP acceptor.
56          NioSocketAcceptorioSocketAcceptor.html#NioSocketAcceptor">NioSocketAcceptor acceptor = new NioSocketAcceptor();
57  
58          // Create TCP/IP connector.
59          IoConnector connector = new NioSocketConnector();
60  
61          // Set connect timeout.
62          connector.setConnectTimeoutMillis( 30 * 1000L );
63  
64          ClientToProxyIoHandleryIoHandler.html#ClientToProxyIoHandler">ClientToProxyIoHandler handler = new ClientToProxyIoHandler( connector,
65              new InetSocketAddress( args[1], Integer.parseInt( args[2] ) ) );
66  
67          // Start proxy.
68          acceptor.setHandler( handler );
69          acceptor.bind( new InetSocketAddress( Integer.parseInt( args[0] ) ) );
70  
71          System.out.println( "Listening on port " + Integer.parseInt( args[0] ) );
72      }
73  
74  }