1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38
39
40
41
42 public class BlacklistFilter extends IoFilterAdapter {
43 private final Set blacklist = new HashSet();
44
45
46
47
48
49
50
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
64
65
66
67
68
69
70
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
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
98
99 public synchronized void block(InetAddress address) {
100 block(address, "address");
101 }
102
103
104
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
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
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
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
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
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
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 }