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 The Apache MINA Project (dev@mina.apache.org)
44   * @version $Rev$, $Date$
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(Charset
105                         .forName("UTF-8"), delim, delim)));
106 
107         connector.setHandler(new TelnetSessionHandler());
108 
109         IoSession session;
110         for (;;) {
111             try {
112                 ConnectFuture future = connector.connect();
113                 future.awaitUninterruptibly();
114                 session = future.getSession();
115                 break;
116             } catch (RuntimeIoException e) {
117                 System.err.println("Failed to connect. Retrying in 5 secs ...");
118                 Thread.sleep(5000);
119             }
120         }
121 
122         // Wait until done
123         if (session != null) {
124             session.getCloseFuture().awaitUninterruptibly();
125         }
126         connector.dispose();
127         System.exit(0);
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     public static void main(String[] args) throws Exception {
134         new ProxyTelnetTestClient();
135     }
136 }