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.telnet;
21  
22  import java.net.InetSocketAddress;
23  import java.nio.charset.Charset;
24  import java.util.HashMap;
25  
26  import org.apache.mina.core.RuntimeIoException;
27  import org.apache.mina.core.future.ConnectFuture;
28  import org.apache.mina.core.session.IoSession;
29  import org.apache.mina.filter.codec.ProtocolCodecFilter;
30  import org.apache.mina.filter.codec.textline.LineDelimiter;
31  import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
32  import org.apache.mina.proxy.ProxyConnector;
33  import org.apache.mina.proxy.handlers.http.HttpProxyConstants;
34  import org.apache.mina.proxy.handlers.http.HttpProxyRequest;
35  import org.apache.mina.proxy.session.ProxyIoSession;
36  import org.apache.mina.transport.socket.nio.NioSocketConnector;
37  
38  /**
39   * ProxyTelnetTestClient.java - Tests a classical text communication through a proxy.
40   * Changing the params and request type will allow to test the multiple options
41   * (http or socks proxying, various authentications methods, ...).
42   * 
43   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
44   * @since MINA 2.0.0-M3
45   */
46  public class ProxyTelnetTestClient {
47      
48      /**
49       * The user login used to authenticate with the proxy.
50       */
51      public final static String USER = "TED_KODS";
52  
53      /**
54       * The password used to authenticate with the proxy.
55       */
56      public final static String PWD = "EDOUARD";
57  
58      /**
59       * The address we really want to connect to.
60       */
61      public final static InetSocketAddress serverAddress = new InetSocketAddress(
62              "localhost", 25);
63  
64      /**
65       * The address of the proxy server.
66       */
67      public final static InetSocketAddress proxyAddress = new InetSocketAddress(
68              "localhost", 8080);
69      
70      /**
71       * Connects to the endpoint running a text based protocol server through the
72       * proxy and allows user to type commands in the console to dialog with the
73       * server.
74       * 
75       * @throws Exception
76       */
77      public ProxyTelnetTestClient() throws Exception {
78          // Create proxy connector.
79          NioSocketConnector targetConnector = new NioSocketConnector(Runtime
80                  .getRuntime().availableProcessors() + 1);
81          ProxyConnector connector = new ProxyConnector(targetConnector);
82  
83          /*
84          // Example of socks v5 proxy use
85          SocksProxyRequest req = new SocksProxyRequest(
86                  SocksProxyConstants.SOCKS_VERSION_5,
87                  SocksProxyConstants.ESTABLISH_TCPIP_STREAM, serverAddress, USER);
88          req.setPassword(PWD);
89          */
90  
91          HttpProxyRequest req = new HttpProxyRequest(serverAddress);
92          HashMap<String, String> props = new HashMap<String, String>();
93          props.put(HttpProxyConstants.USER_PROPERTY, USER);
94          props.put(HttpProxyConstants.PWD_PROPERTY, PWD);
95          req.setProperties(props);        
96  
97          ProxyIoSession proxyIoSession = new ProxyIoSession(proxyAddress, req);
98          connector.setProxyIoSession(proxyIoSession);
99  
100         LineDelimiter delim = new LineDelimiter("\r\n");
101         targetConnector.getFilterChain().addLast(
102                 "codec",
103                 new ProtocolCodecFilter(new TextLineCodecFactory(Charset
104                         .forName("UTF-8"), delim, delim)));
105 
106         connector.setHandler(new TelnetSessionHandler());
107 
108         IoSession session;
109         for (;;) {
110             try {
111                 ConnectFuture future = connector.connect();
112                 future.awaitUninterruptibly();
113                 session = future.getSession();
114                 break;
115             } catch (RuntimeIoException e) {
116                 System.err.println("Failed to connect. Retrying in 5 secs ...");
117                 Thread.sleep(5000);
118             }
119         }
120 
121         // Wait until done
122         if (session != null) {
123             session.getCloseFuture().awaitUninterruptibly();
124         }
125         connector.dispose();
126         System.exit(0);
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     public static void main(String[] args) throws Exception {
133         new ProxyTelnetTestClient();
134     }
135 }