01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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.common.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.setConnectTimeout(30);
60:
61: ClientToProxyIoHandler handler = new ClientToProxyIoHandler(
62: connector, new InetSocketAddress(args[1], Integer
63: .parseInt(args[2])));
64:
65: // Start proxy.
66: acceptor.setHandler(handler);
67: acceptor.bind(new InetSocketAddress(Integer.parseInt(args[0])));
68:
69: System.out.println("Listening on port "
70: + Integer.parseInt(args[0]));
71: }
72:
73: }
|