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