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 The Apache MINA Project (dev@mina.apache.org)
41   * @version $Rev$, $Date$
42   */
43  public class Main {
44  
45      public static void main(String[] args) throws Exception {
46          if (args.length != 3) {
47              System.out.println(Main.class.getName()
48                      + " <proxy-port> <server-hostname> <server-port>");
49              return;
50          }
51  
52          // Create TCP/IP acceptor.
53          NioSocketAcceptor acceptor = new NioSocketAcceptor();
54  
55          // Create TCP/IP connector.
56          IoConnector connector = new NioSocketConnector();
57  
58          // Set connect timeout.
59          connector.setConnectTimeoutMillis(30*1000L);
60  
61          ClientToProxyIoHandler handler = new ClientToProxyIoHandler(connector,
62                  new InetSocketAddress(args[1], Integer.parseInt(args[2])));
63  
64          // Start proxy.
65          acceptor.setHandler(handler);
66          acceptor.bind(new InetSocketAddress(Integer.parseInt(args[0])));
67  
68          System.out.println("Listening on port " + Integer.parseInt(args[0]));
69      }
70  
71  }