1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.ipfilter;
17
18 import java.net.InetSocketAddress;
19
20 import org.jboss.netty.channel.ChannelEvent;
21 import org.jboss.netty.channel.ChannelFuture;
22 import org.jboss.netty.channel.ChannelFutureListener;
23 import org.jboss.netty.channel.ChannelHandlerContext;
24 import org.jboss.netty.channel.ChannelStateEvent;
25 import org.jboss.netty.channel.ChannelUpstreamHandler;
26 import org.jboss.netty.channel.Channels;
27
28
29
30
31 public abstract class IpFilteringHandlerImpl implements ChannelUpstreamHandler, IpFilteringHandler {
32
33 private IpFilterListener listener;
34
35
36
37
38
39
40
41
42 protected abstract boolean accept(ChannelHandlerContext ctx, ChannelEvent e, InetSocketAddress inetSocketAddress)
43 throws Exception;
44
45
46
47
48
49
50
51
52
53
54
55 protected ChannelFuture handleRefusedChannel(ChannelHandlerContext ctx, ChannelEvent e,
56 InetSocketAddress inetSocketAddress) throws Exception {
57 if (listener == null) {
58 return null;
59 }
60 return listener.refused(ctx, e, inetSocketAddress);
61 }
62
63 protected ChannelFuture handleAllowedChannel(ChannelHandlerContext ctx, ChannelEvent e,
64 InetSocketAddress inetSocketAddress) throws Exception {
65 if (listener == null) {
66 return null;
67 }
68 return listener.allowed(ctx, e, inetSocketAddress);
69 }
70
71
72
73
74
75
76 protected boolean isBlocked(ChannelHandlerContext ctx) {
77 return ctx.getAttachment() != null;
78 }
79
80
81
82
83
84
85
86
87
88
89
90 protected boolean continues(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
91 if (listener != null) {
92 return listener.continues(ctx, e);
93 } else {
94 return false;
95 }
96 }
97
98
99 public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
100 if (e instanceof ChannelStateEvent) {
101 ChannelStateEvent evt = (ChannelStateEvent) e;
102 switch (evt.getState()) {
103 case OPEN:
104 case BOUND:
105
106
107 if (isBlocked(ctx) && !continues(ctx, evt)) {
108
109 return;
110 } else {
111 ctx.sendUpstream(e);
112 return;
113 }
114 case CONNECTED:
115 if (evt.getValue() != null) {
116
117 InetSocketAddress inetSocketAddress = (InetSocketAddress) e.getChannel().getRemoteAddress();
118 if (!accept(ctx, e, inetSocketAddress)) {
119 ctx.setAttachment(Boolean.TRUE);
120 ChannelFuture future = handleRefusedChannel(ctx, e, inetSocketAddress);
121 if (future != null) {
122 future.addListener(ChannelFutureListener.CLOSE);
123 } else {
124 Channels.close(e.getChannel());
125 }
126 if (isBlocked(ctx) && !continues(ctx, evt)) {
127
128 return;
129 }
130 } else {
131 handleAllowedChannel(ctx, e, inetSocketAddress);
132 }
133
134 ctx.setAttachment(null);
135 } else {
136
137 if (isBlocked(ctx) && !continues(ctx, evt)) {
138
139 return;
140 }
141 }
142 break;
143 }
144 }
145 if (isBlocked(ctx) && !continues(ctx, e)) {
146
147 return;
148 }
149
150 ctx.sendUpstream(e);
151 }
152
153
154 public void setIpFilterListener(IpFilterListener listener) {
155 this.listener = listener;
156 }
157
158
159 public void removeIpFilterListener() {
160 listener = null;
161
162 }
163
164 }