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.nio.charset.StandardCharsets;
25  import java.util.HashMap;
26  
27  import org.apache.mina.core.RuntimeIoException;
28  import org.apache.mina.core.future.ConnectFuture;
29  import org.apache.mina.core.session.IoSession;
30  import org.apache.mina.filter.codec.ProtocolCodecFilter;
31  import org.apache.mina.filter.codec.textline.LineDelimiter;
32  import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
33  import org.apache.mina.proxy.ProxyConnector;
34  import org.apache.mina.proxy.handlers.http.HttpProxyConstants;
35  import org.apache.mina.proxy.handlers.http.HttpProxyRequest;
36  import org.apache.mina.proxy.session.ProxyIoSession;
37  import org.apache.mina.transport.socket.nio.NioSocketConnector;
38  
39  /**
40   * ProxyTelnetTestClient.java - Tests a classical text communication through a proxy.
41   * Changing the params and request type will allow to test the multiple options
42   * (http or socks proxying, various authentications methods, ...).
43   * 
44   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
45   * @since MINA 2.0.0-M3
46   */
47  public class ProxyTelnetTestClient {
48      
49      /**
50       * The user login used to authenticate with the proxy.
51       */
52      public final static String USER = "TED_KODS";
53  
54      /**
55       * The password used to authenticate with the proxy.
56       */
57      public final static String PWD = "EDOUARD";
58  
59      /**
60       * The address we really want to connect to.
61       */
62      public final static InetSocketAddress serverAddress = new InetSocketAddress(
63              "localhost", 25);
64  
65      /**
66       * The address of the proxy server.
67       */
68      public final static InetSocketAddress proxyAddress = new InetSocketAddress(
69              "localhost", 8080);
70      
71      /**
72       * Connects to the endpoint running a text based protocol server through the
73       * proxy and allows user to type commands in the console to dialog with the
74       * server.
75       * 
76       * @throws Exception
77       */
78      public ProxyTelnetTestClient() throws Exception {
79          // Create proxy connector.
80          NioSocketConnector targetConnector = new NioSocketConnector(Runtime
81                  .getRuntime().availableProcessors() + 1);
82          ProxyConnector connector = new ProxyConnector(targetConnector);
83  
84          /*
85          // Example of socks v5 proxy use
86          SocksProxyRequest req = new SocksProxyRequest(
87                  SocksProxyConstants.SOCKS_VERSION_5,
88                  SocksProxyConstants.ESTABLISH_TCPIP_STREAM, serverAddress, USER);
89          req.setPassword(PWD);
90          */
91  
92          HttpProxyRequest req = new HttpProxyRequest(serverAddress);
93          HashMap<String, String> props = new HashMap<String, String>();
94          props.put(HttpProxyConstants.USER_PROPERTY, USER);
95          props.put(HttpProxyConstants.PWD_PROPERTY, PWD);
96          req.setProperties(props);        
97  
98          ProxyIoSession proxyIoSession = new ProxyIoSession(proxyAddress, req);
99          connector.setProxyIoSession(proxyIoSession);
100 
101         LineDelimiter delim = new LineDelimiter("\r\n");
102         targetConnector.getFilterChain().addLast(
103                 "codec",
104                 new ProtocolCodecFilter(new TextLineCodecFactory(StandardCharsets.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 }