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.proxy;
21  
22  import java.net.InetSocketAddress;
23  import java.net.SocketAddress;
24  import java.util.concurrent.Executor;
25  
26  import org.apache.mina.core.buffer.IoBuffer;
27  import org.apache.mina.core.file.FileRegion;
28  import org.apache.mina.core.filterchain.IoFilter;
29  import org.apache.mina.core.future.ConnectFuture;
30  import org.apache.mina.core.future.DefaultConnectFuture;
31  import org.apache.mina.core.future.IoFuture;
32  import org.apache.mina.core.service.AbstractIoConnector;
33  import org.apache.mina.core.service.DefaultTransportMetadata;
34  import org.apache.mina.core.service.IoHandler;
35  import org.apache.mina.core.service.TransportMetadata;
36  import org.apache.mina.core.session.IoSession;
37  import org.apache.mina.core.session.IoSessionConfig;
38  import org.apache.mina.core.session.IoSessionInitializer;
39  import org.apache.mina.proxy.filter.ProxyFilter;
40  import org.apache.mina.proxy.handlers.socks.SocksProxyRequest;
41  import org.apache.mina.proxy.session.ProxyIoSession;
42  import org.apache.mina.proxy.session.ProxyIoSessionInitializer;
43  import org.apache.mina.transport.socket.DefaultSocketSessionConfig;
44  import org.apache.mina.transport.socket.SocketConnector;
45  import org.apache.mina.transport.socket.SocketSessionConfig;
46  
47  /**
48   * ProxyConnector.java - Decorator for {@link SocketConnector} to provide proxy support, 
49   * as suggested by MINA list discussions.
50   * <p>
51   * Operates by intercepting connect requests and replacing the endpoint address with the 
52   * proxy address, then adding a {@link ProxyFilter} as the first {@link IoFilter} which 
53   * performs any necessary handshaking with the proxy before allowing data to flow 
54   * normally. During the handshake, any outgoing write requests are buffered.
55   * 
56   * @see        http://www.nabble.com/Meta-Transport%3A-an-idea-on-implementing-reconnection-and-proxy-td12969001.html
57   * @see        http://issues.apache.org/jira/browse/DIRMINA-415
58   * 
59   * @author The Apache MINA Project (dev@mina.apache.org)
60   * @since MINA 2.0.0-M3
61   */
62  public class ProxyConnector extends AbstractIoConnector {
63      private static final TransportMetadata METADATA = new DefaultTransportMetadata(
64              "proxy", "proxyconnector", false, true, InetSocketAddress.class,
65              SocketSessionConfig.class, IoBuffer.class, FileRegion.class);
66  
67      /**
68       * Wrapped connector to use for outgoing TCP connections.
69       */
70      private SocketConnector connector = null;
71  
72      /**
73       * Proxy filter instance.
74       */
75      private final ProxyFilter proxyFilter = new ProxyFilter();
76  
77      /**
78       * The {@link ProxyIoSession} in use.
79       */
80      private ProxyIoSession proxyIoSession;
81  
82      /**
83       * This future will notify it's listeners when really connected to the target
84       */
85      private DefaultConnectFuture future;
86  
87      /**
88       * Creates a new proxy connector.
89       */
90      public ProxyConnector() {
91          super(new DefaultSocketSessionConfig(), null);
92      }
93  
94      /**
95       * Creates a new proxy connector.
96       * 
97       * @param connector Connector used to establish proxy connections.
98       */
99      public ProxyConnector(final SocketConnector connector) {        
100         this(connector, new DefaultSocketSessionConfig(), null);
101     }
102 
103     /**
104      * Creates a new proxy connector. 
105      * @see AbstractIoConnector(IoSessionConfig, Executor).
106      */
107     public ProxyConnector(final SocketConnector connector, IoSessionConfig config, Executor executor) {
108         super(config, executor);
109         setConnector(connector);
110     }    
111     
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public IoSessionConfig getSessionConfig() {
117         return connector.getSessionConfig();
118     }
119 
120     /**
121      * Returns the {@link ProxyIoSession} linked with this connector.
122      */
123     public ProxyIoSession getProxyIoSession() {
124         return proxyIoSession;
125     }
126 
127     /**
128      * Sets the proxy session object of this connector.
129      * @param proxyIoSession the configuration of this connector.
130      */
131     public void setProxyIoSession(ProxyIoSession proxyIoSession) {
132         if (proxyIoSession == null) {
133             throw new NullPointerException("proxySession object cannot be null");
134         }
135 
136         if (proxyIoSession.getProxyAddress() == null) {
137             throw new NullPointerException(
138                     "proxySession.proxyAddress cannot be null");
139         }
140 
141         proxyIoSession.setConnector(this);
142         setDefaultRemoteAddress(proxyIoSession.getProxyAddress());
143         this.proxyIoSession = proxyIoSession;
144     }
145 
146     /**
147      * Connects to the specified <code>address</code>.  If communication starts
148      * successfully, events are fired to the connector's <code>handler</code>.
149      * 
150      * @param remoteAddress the remote address to connect to
151      * @param localAddress the local address
152      * @param sessionInitializer the session initializer
153      * @return {@link ConnectFuture} that will tell the result of the connection attempt
154      */
155     @SuppressWarnings("unchecked")
156     @Override
157     protected ConnectFuture connect0(
158             final SocketAddress remoteAddress,
159             final SocketAddress localAddress,
160             final IoSessionInitializer<? extends ConnectFuture> sessionInitializer) {
161         if (!proxyIoSession.isReconnectionNeeded()) {
162             // First connection
163             IoHandler handler = getHandler();
164             if (!(handler instanceof AbstractProxyIoHandler)) {
165                 throw new IllegalArgumentException(
166                         "IoHandler must be an instance of AbstractProxyIoHandler");
167             }
168 
169             connector.setHandler(handler);
170             future = new DefaultConnectFuture();
171         }
172 
173         ConnectFuture conFuture = connector.connect(proxyIoSession
174                 .getProxyAddress(), new ProxyIoSessionInitializer(
175                 sessionInitializer, proxyIoSession));
176 
177         // If proxy does not use reconnection like socks the connector's 
178         // future is returned. If we're in the middle of a reconnection
179         // then we send back the connector's future which is only used
180         // internally while <code>future</code> will be used to notify
181         // the user of the connection state.
182         if (proxyIoSession.getRequest() instanceof SocksProxyRequest
183                 || proxyIoSession.isReconnectionNeeded()) {
184             return conFuture;
185         }
186 
187         return future;
188     }
189 
190     /**
191      * Cancels the real connection when reconnection is in use.
192      */
193     public void cancelConnectFuture() {
194         future.cancel();
195     }
196 
197     /**
198      * Fires the connection event on the real future to notify the client.
199      * 
200      * @param session the current session
201      * @return the future holding the connected session
202      */
203     protected ConnectFuture fireConnected(final IoSession session) {
204         future.setSession(session);
205         return future;
206     }
207 
208     /**
209      * Get the {@link SocketConnector} to be used for connections
210      * to the proxy server.
211      */
212     public final SocketConnector getConnector() {
213         return connector;
214     }
215 
216     /**
217      * Sets the {@link SocketConnector} to be used for connections
218      * to the proxy server.
219      * 
220      * @param connector the connector to use
221      */
222     private final void setConnector(final SocketConnector connector) {
223         if (connector == null) {
224             throw new NullPointerException("connector cannot be null");
225         }
226 
227         this.connector = connector;
228         String className = ProxyFilter.class.getName();
229 
230         // Removes an old ProxyFilter instance from the chain
231         if (connector.getFilterChain().contains(className)) {
232             connector.getFilterChain().remove(className);
233         }
234 
235         // Insert the ProxyFilter as the first filter in the filter chain builder        
236         connector.getFilterChain().addFirst(className, proxyFilter);
237     }
238 
239     /**
240      * {@inheritDoc}
241      */
242     @Override
243     protected IoFuture dispose0() throws Exception {
244         if (connector != null) {
245             connector.dispose();
246         }
247         return null;
248     }
249 
250     /**
251      * {@inheritDoc}
252      */
253     public TransportMetadata getTransportMetadata() {
254         return METADATA;
255     }
256 }