ip过滤
同样getty已经提供ip过滤插件IpFilterRuleHandler,只需注入到责任链即可方面的对连接的客户端ip进行过滤处理,该处理器只对服务器端有效
AioServerStarter server = new AioServerStarter(8888);
server.channelInitializer(new ChannelInitializer() {
@Override
public void initChannel(AioChannel channel) throws Exception {
//获取责任链对象
DefaultChannelPipeline defaultChannelPipeline = channel.getDefaultChannelPipeline();
//需要过滤的ip起始段,可添加多个
IpRange ir = new IpRange("192.168.1.100", "192.168.1.110");
IpRange ir2 = new IpRange("192.168.1.110", "192.168.1.120");
List<IpRange> list = new ArrayList<>();
list.add(ir);
list.add(ir2);
//注入ip过滤器,IpFilterRuleType.ACCEPT表示接受,IpFilterRuleType.REJECT 表示拒绝
//顾名思义就是运营上面配置的ip段连接,配置REJECT则拒绝连接
defaultChannelPipeline.addLast(new IpFilterRuleHandler(list,IpFilterRuleType.ACCEPT));
//添加 分隔符字符串处理器 按 "\r\n\" 进行消息分割
defaultChannelPipeline.addLast(new DelimiterFrameDecoder(DelimiterFrameDecoder.lineDelimiter));
//添加字符串解码器
defaultChannelPipeline.addLast(new StringDecoder());
//添加自定义的简单消息处理器
defaultChannelPipeline.addLast(new SimpleHandler());
}
}).start();