1
|
public
class
IpFilterRuleHandler
extends
IpFilteringHandlerImpl
|
1
|
public
abstract
class
IpFilteringHandlerImpl
implements
ChannelUpstreamHandler, IpFilteringHandler
|
该类和我们经常使用的解码器(decoder)以及逻辑处理handler一样都继承于ChannelUpstreamHandler,所以可以很方便的把它加入到我们的ChannelPipeline中。
例如:
1
2
3
4
5
|
ChannelPipeline p = Channels.pipeline();
//ip过滤
IpFilterRuleHandler ipFilterRuleHandler =
new
IpFilterRuleHandler();
ipFilterRuleHandler.addAll(
new
IpFilterRuleList(
"+i:192.168.*"
+
", -i:*"
));
p.addLast(
"ipFilter"
, ipFilterRuleHandler);
|
netty的ip过滤一共提供3中过滤:[i,n,c]
i对应的是ip地址,相应的 +i 表示allow(允许),-i 表示deny(否认)
n对应的是地址名称,相应的 +n 表示allow(允许),-n 表示deny(否认)
c对应的是CIDR (Classless Inter-Domain Routing)无分类域间路由选择,相应的 +c 表示allow(允许),-c表示deny(否认)
官方中实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package
org.jboss.netty.handler.ipfilter;
import
java.net.InetAddress;
import
java.net.InetSocketAddress;
public
class
IpFilterRuleTest {
public
static
boolean
accept(IpFilterRuleHandler h, InetSocketAddress addr)
throws
Exception {
return
h.accept(
null
,
null
, addr);
}
public
static
void
main(String[] args)
throws
Exception {
IpFilterRuleHandler h =
new
IpFilterRuleHandler();
h.addAll(
new
IpFilterRuleList(
"+n:localhost, -n:*"
));
InetSocketAddress addr =
new
InetSocketAddress(
InetAddress.getLocalHost(),
8080
);
System.out.println(accept(h, addr));
addr =
new
InetSocketAddress(InetAddress.getByName(
"127.0.0.2"
),
8080
);
System.out.println(accept(h, addr));
addr =
new
InetSocketAddress(InetAddress.getByName(InetAddress
.getLocalHost().getHostName()),
8080
);
System.out.println(accept(h, addr));
h.clear();
h.addAll(
new
IpFilterRuleList(
"+n:*"
+ InetAddress.getLocalHost().getHostName().substring(
1
)
+
", -n:*"
));
addr =
new
InetSocketAddress(InetAddress.getLocalHost(),
8080
);
System.out.println(accept(h, addr));
addr =
new
InetSocketAddress(InetAddress.getByName(
"127.0.0.2"
),
8080
);
System.out.println(accept(h, addr));
addr =
new
InetSocketAddress(InetAddress.getByName(InetAddress
.getLocalHost().getHostName()),
8080
);
System.out.println(accept(h, addr));
h.clear();
h.addAll(
new
IpFilterRuleList(
"+c:"
+ InetAddress.getLocalHost().getHostAddress() +
"/32, -n:*"
));
addr =
new
InetSocketAddress(InetAddress.getLocalHost(),
8080
);
System.out.println(accept(h, addr));
addr =
new
InetSocketAddress(InetAddress.getByName(
"127.0.0.2"
),
8080
);
System.out.println(accept(h, addr));
addr =
new
InetSocketAddress(InetAddress.getByName(InetAddress
.getLocalHost().getHostName()),
8080
);
System.out.println(accept(h, addr));
h.clear();
h.addAll(
new
IpFilterRuleList(
""
));
addr =
new
InetSocketAddress(InetAddress.getLocalHost(),
8080
);
System.out.println(accept(h, addr));
addr =
new
InetSocketAddress(InetAddress.getByName(
"127.0.0.2"
),
8080
);
System.out.println(accept(h, addr));
addr =
new
InetSocketAddress(InetAddress.getByName(InetAddress
.getLocalHost().getHostName()),
8080
);
System.out.println(accept(h, addr));
h.clear();
addr =
new
InetSocketAddress(InetAddress.getLocalHost(),
8080
);
System.out.println(accept(h, addr));
addr =
new
InetSocketAddress(InetAddress.getByName(
"127.0.0.2"
),
8080
);
System.out.println(accept(h, addr));
addr =
new
InetSocketAddress(InetAddress.getByName(InetAddress
.getLocalHost().getHostName()),
8080
);
System.out.println(accept(h, addr));
}
}
|
CIDR参考:http://blog.csdn.net/yaoyao4959/article/details/10084973
我在Netty4中用的是IpSubnetFilterRule来过滤对应的IP
看我的代码实例:
ChannelPipeline pipeline = ch.pipeline();
String[] ip = Server.getServerParam().getSlbAddress();
int count = ip.length;
IpSubnetFilterRule[] ipsf = new IpSubnetFilterRule[count];
for( int i=0;i
}
pipeline.addLast(new RuleBasedIpFilter(ipsf));