Class IpSubnetFilter
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
This class allows one to filter new Channel
s based on the
IpSubnetFilter
s passed to its constructor. If no rules are provided, all connections
will be accepted since acceptIfNotFound
is true
by default.
If you would like to explicitly take action on rejected Channel
s, you should override
AbstractRemoteAddressFilter.channelRejected(ChannelHandlerContext, SocketAddress)
.
Few Points to keep in mind:
- Since
IpSubnetFilter
uses Binary search algorithm, it's a good idea to insert IP addresses in incremental order. - Remove any over-lapping CIDR.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final boolean
private final IpFilterRuleType
private final IpFilterRuleType
private final IpSubnetFilterRule[]
private final IpSubnetFilterRule[]
-
Constructor Summary
ConstructorsConstructorDescriptionIpSubnetFilter
(boolean acceptIfNotFound, IpSubnetFilterRule... rules) Create newIpSubnetFilter
Instance with specifiedIpSubnetFilterRule
as array and specify if we'll accept a connection if we don't find it in the rule(s).IpSubnetFilter
(boolean acceptIfNotFound, List<IpSubnetFilterRule> rules) Create newIpSubnetFilter
Instance with specifiedIpSubnetFilterRule
asList
and specify if we'll accept a connection if we don't find it in the rule(s).IpSubnetFilter
(IpSubnetFilterRule... rules) Create newIpSubnetFilter
Instance with specifiedIpSubnetFilterRule
as array.IpSubnetFilter
(List<IpSubnetFilterRule> rules) -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
accept
(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) This method is called immediately after aChannel
gets registered.private static IpSubnetFilterRule[]
sortAndFilter
(List<IpSubnetFilterRule> rules) Sort the list Remove over-lapping subnet Sort the list againMethods inherited from class io.netty.handler.ipfilter.AbstractRemoteAddressFilter
channelAccepted, channelActive, channelRegistered, channelRejected
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelInactive, channelRead, channelReadComplete, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
Field Details
-
acceptIfNotFound
private final boolean acceptIfNotFound -
ipv4Rules
-
ipv6Rules
-
ipFilterRuleTypeIPv4
-
ipFilterRuleTypeIPv6
-
-
Constructor Details
-
IpSubnetFilter
Create new
IpSubnetFilter
Instance with specifiedIpSubnetFilterRule
as array.acceptIfNotFound
is set totrue
.- Parameters:
rules
-IpSubnetFilterRule
as an array
-
IpSubnetFilter
Create new
IpSubnetFilter
Instance with specifiedIpSubnetFilterRule
as array and specify if we'll accept a connection if we don't find it in the rule(s).- Parameters:
acceptIfNotFound
-true
if we'll accept connection if not found in rule(s).rules
-IpSubnetFilterRule
as an array
-
IpSubnetFilter
Create new
IpSubnetFilter
Instance with specifiedIpSubnetFilterRule
asList
.acceptIfNotFound
is set totrue
.- Parameters:
rules
-IpSubnetFilterRule
as aList
-
IpSubnetFilter
Create new
IpSubnetFilter
Instance with specifiedIpSubnetFilterRule
asList
and specify if we'll accept a connection if we don't find it in the rule(s).- Parameters:
acceptIfNotFound
-true
if we'll accept connection if not found in rule(s).rules
-IpSubnetFilterRule
as aList
-
-
Method Details
-
accept
Description copied from class:AbstractRemoteAddressFilter
This method is called immediately after aChannel
gets registered.- Specified by:
accept
in classAbstractRemoteAddressFilter<InetSocketAddress>
- Returns:
- Return true if connections from this IP address and port should be accepted. False otherwise.
-
sortAndFilter
- Sort the list
- Remove over-lapping subnet
- Sort the list again
-