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.filter;
21  
22  import java.net.InetAddress;
23  import java.net.InetSocketAddress;
24  import java.net.SocketAddress;
25  import java.util.Collection;
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import org.apache.mina.common.IdleStatus;
30  import org.apache.mina.common.IoFilter;
31  import org.apache.mina.common.IoFilterAdapter;
32  import org.apache.mina.common.IoSession;
33  import org.apache.mina.util.SessionLog;
34  
35  /**
36   * A {@link IoFilter} which blocks connections from blacklisted remote
37   * address.
38   * 
39   * @author The Apache Directory Project (mina-dev@directory.apache.org)
40   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $
41   */
42  public class BlacklistFilter extends IoFilterAdapter {
43      private final Set blacklist = new HashSet();
44  
45      /**
46       * Sets the addresses to be blacklisted.
47       * 
48       * NOTE: this call will remove any previously blacklisted addresses.
49       * 
50       * @param addresses an array of addresses to be blacklisted.
51       */
52      public void setBlacklist(InetAddress[] addresses) {
53          if (addresses == null)
54              throw new NullPointerException("addresses");
55          blacklist.clear();
56          for (int i = 0; i < addresses.length; i++) {
57              InetAddress addr = addresses[i];
58              block(addr, "addresses[" + i + ']');
59          }
60      }
61  
62      /**
63       * Sets the addresses to be blacklisted.
64       * 
65       * NOTE: this call will remove any previously blacklisted addresses.
66       * 
67       * @param addresses a collection of InetAddress objects representing the 
68       *        addresses to be blacklisted.
69       * @throws IllegalArgumentException if the specified collections contains 
70       *         non-{@link InetAddress} objects.
71       */
72      public void setBlacklist(Collection addresses) {
73          if (addresses == null)
74              throw new NullPointerException("addresses");
75  
76          InetAddress[] inetAddresses = new InetAddress[addresses.size()];
77          try {
78              setBlacklist((InetAddress[]) addresses.toArray(inetAddresses));
79          } catch (ArrayStoreException ase) {
80              IllegalArgumentException iae = new IllegalArgumentException(
81                      "Collection of addresses must contain only InetAddress instances.");
82              iae.initCause(ase);
83              throw iae;
84          }
85      }
86  
87      /**
88       * Blocks the specified endpoint.
89       */
90      public synchronized void block(InetAddress address, String error_string) {
91          if (address == null)
92              throw new NullPointerException(error_string);
93          blacklist.add(address);
94      }
95  
96      /**
97       * Blocks the specified endpoint.
98       */
99      public synchronized void block(InetAddress address) {
100         block(address, "address");
101     }
102 
103     /**
104      * Unblocks the specified endpoint.
105      */
106     public synchronized void unblock(InetAddress address) {
107         if (address == null)
108             throw new NullPointerException("address");
109         blacklist.remove(address);
110     }
111 
112     public void sessionCreated(NextFilter nextFilter, IoSession session) {
113         if (!isBlocked(session)) {
114             // forward if not blocked
115             nextFilter.sessionCreated(session);
116         } else {
117             blockSession(session);
118         }
119     }
120 
121     public void sessionOpened(NextFilter nextFilter, IoSession session)
122             throws Exception {
123         if (!isBlocked(session)) {
124             // forward if not blocked
125             nextFilter.sessionOpened(session);
126         } else {
127             blockSession(session);
128         }
129     }
130 
131     public void sessionClosed(NextFilter nextFilter, IoSession session)
132             throws Exception {
133         if (!isBlocked(session)) {
134             // forward if not blocked
135             nextFilter.sessionClosed(session);
136         } else {
137             blockSession(session);
138         }
139     }
140 
141     public void sessionIdle(NextFilter nextFilter, IoSession session,
142             IdleStatus status) throws Exception {
143         if (!isBlocked(session)) {
144             // forward if not blocked
145             nextFilter.sessionIdle(session, status);
146         } else {
147             blockSession(session);
148         }
149     }
150 
151     public void messageReceived(NextFilter nextFilter, IoSession session,
152             Object message) {
153         if (!isBlocked(session)) {
154             // forward if not blocked
155             nextFilter.messageReceived(session, message);
156         } else {
157             blockSession(session);
158         }
159     }
160 
161     public void messageSent(NextFilter nextFilter, IoSession session,
162             Object message) throws Exception {
163         if (!isBlocked(session)) {
164             // forward if not blocked
165             nextFilter.messageSent(session, message);
166         } else {
167             blockSession(session);
168         }
169     }
170 
171     private void blockSession(IoSession session) {
172         SessionLog.info(session, "Remote address in the blacklist; closing.");
173         session.close();
174     }
175 
176     private boolean isBlocked(IoSession session) {
177         SocketAddress remoteAddress = session.getRemoteAddress();
178         if (remoteAddress instanceof InetSocketAddress) {
179             if (blacklist.contains(((InetSocketAddress) remoteAddress)
180                     .getAddress())) {
181                 return true;
182             }
183         }
184 
185         return false;
186     }
187 }