ip过滤

    同样getty已经提供ip过滤插件IpFilterRuleHandler,只需注入到责任链即可方面的对连接的客户端ip进行过滤处理,该处理器只对服务器端有效

    1. AioServerStarter server = new AioServerStarter(8888);
    2. server.channelInitializer(new ChannelInitializer() {
    3. @Override
    4. public void initChannel(AioChannel channel) throws Exception {
    5. //获取责任链对象
    6. DefaultChannelPipeline defaultChannelPipeline = channel.getDefaultChannelPipeline();
    7. //需要过滤的ip起始段,可添加多个
    8. IpRange ir = new IpRange("192.168.1.100", "192.168.1.110");
    9. IpRange ir2 = new IpRange("192.168.1.110", "192.168.1.120");
    10. List<IpRange> list = new ArrayList<>();
    11. list.add(ir);
    12. list.add(ir2);
    13. //注入ip过滤器,IpFilterRuleType.ACCEPT表示接受,IpFilterRuleType.REJECT 表示拒绝
    14. //顾名思义就是运营上面配置的ip段连接,配置REJECT则拒绝连接
    15. defaultChannelPipeline.addLast(new IpFilterRuleHandler(list,IpFilterRuleType.ACCEPT));
    16. //添加 分隔符字符串处理器 按 "\r\n\" 进行消息分割
    17. defaultChannelPipeline.addLast(new DelimiterFrameDecoder(DelimiterFrameDecoder.lineDelimiter));
    18. //添加字符串解码器
    19. defaultChannelPipeline.addLast(new StringDecoder());
    20. //添加自定义的简单消息处理器
    21. defaultChannelPipeline.addLast(new SimpleHandler());
    22. }
    23. }).start();