1 /*
2 * Copyright 2014 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.handler.ipfilter;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelFutureListener;
21 import io.netty.channel.ChannelHandlerContext;
22 import io.netty.channel.ChannelInboundHandlerAdapter;
23 import io.netty.util.internal.PlatformDependent;
24
25 import java.net.SocketAddress;
26
27 /**
28 * This class provides the functionality to either accept or reject new {@link Channel}s
29 * based on their IP address.
30 * <p>
31 * You should inherit from this class if you would like to implement your own IP-based filter. Basically you have to
32 * implement {@link #accept(ChannelHandlerContext, SocketAddress)} to decided whether you want to accept or reject
33 * a connection from the remote address.
34 * <p>
35 * Furthermore overriding {@link #channelRejected(ChannelHandlerContext, SocketAddress)} gives you the
36 * flexibility to respond to rejected (denied) connections. If you do not want to send a response, just have it return
37 * null. Take a look at {@link RuleBasedIpFilter} for details.
38 */
39 public abstract class AbstractRemoteAddressFilter<T extends SocketAddress> extends ChannelInboundHandlerAdapter {
40
41 @Override
42 public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
43 handleNewChannel(ctx);
44 ctx.fireChannelRegistered();
45 }
46
47 @Override
48 public void channelActive(ChannelHandlerContext ctx) throws Exception {
49 if (!handleNewChannel(ctx)) {
50 throw new IllegalStateException("cannot determine to accept or reject a channel: " + ctx.channel());
51 } else {
52 ctx.fireChannelActive();
53 }
54 }
55
56 private boolean handleNewChannel(ChannelHandlerContext ctx) throws Exception {
57 @SuppressWarnings("unchecked")
58 T remoteAddress = (T) ctx.channel().remoteAddress();
59
60 // If the remote address is not available yet, defer the decision.
61 if (remoteAddress == null) {
62 return false;
63 }
64
65 // Evaluate the decision before removing this handler from the pipeline, so that if accept(...) throws,
66 // the channel is closed instead of silently continuing through the rest of the pipeline unfiltered.
67 boolean accepted;
68 try {
69 accepted = accept(ctx, remoteAddress);
70 } catch (Throwable cause) {
71 ctx.close();
72 PlatformDependent.throwException(cause);
73 return true;
74 }
75
76 // No need to keep this handler in the pipeline anymore because the decision has been made.
77 // Also, this will prevent the subsequent events from being handled by this handler.
78 ctx.pipeline().remove(this);
79
80 if (accepted) {
81 channelAccepted(ctx, remoteAddress);
82 } else {
83 ChannelFuture rejectedFuture = channelRejected(ctx, remoteAddress);
84 if (rejectedFuture != null) {
85 rejectedFuture.addListener(ChannelFutureListener.CLOSE);
86 } else {
87 ctx.close();
88 }
89 }
90
91 return true;
92 }
93
94 /**
95 * This method is called immediately after a {@link io.netty.channel.Channel} gets registered.
96 *
97 * @return Return true if connections from this IP address and port should be accepted. False otherwise.
98 */
99 protected abstract boolean accept(ChannelHandlerContext ctx, T remoteAddress) throws Exception;
100
101 /**
102 * This method is called if {@code remoteAddress} gets accepted by
103 * {@link #accept(ChannelHandlerContext, SocketAddress)}. You should override it if you would like to handle
104 * (e.g. respond to) accepted addresses.
105 */
106 @SuppressWarnings("UnusedParameters")
107 protected void channelAccepted(ChannelHandlerContext ctx, T remoteAddress) { }
108
109 /**
110 * This method is called if {@code remoteAddress} gets rejected by
111 * {@link #accept(ChannelHandlerContext, SocketAddress)}. You should override it if you would like to handle
112 * (e.g. respond to) rejected addresses.
113 *
114 * @return A {@link ChannelFuture} if you perform I/O operations, so that
115 * the {@link Channel} can be closed once it completes. Null otherwise.
116 */
117 @SuppressWarnings("UnusedParameters")
118 protected ChannelFuture channelRejected(ChannelHandlerContext ctx, T remoteAddress) {
119 return null;
120 }
121 }