java – Netty – 如何生存DDOS?

我正在使用netty 4.1作为MMORPG游戏的NIO套接字服务器.它运行良好多年,但最近我们正遭受DDOS攻击.我打了很长时间,但目前,我对如何改进它没有更多的想法. Ddoser通过来自世界各地成千上万的ips的新连接发送垃圾邮件.在网络级别上很难削减它,因为攻击看起来与普通玩家非常相似.与HTTP服务器上的攻击相比,攻击不是很大,但足以让我们的游戏崩溃.

我如何使用netty:

public void startServer() {

    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();

    try {
        int timeout = (Settings.SOCKET_TIMEOUT*1000);
        bootstrap = new ServerBootstrap();

        int bufferSize = 65536;
        bootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.SO_TIMEOUT, timeout)
                .childOption(ChannelOption.SO_RCVBUF, bufferSize)
                .childOption(ChannelOption.SO_SNDBUF, bufferSize)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new CustomInitalizer(sslCtx));


        ChannelFuture bind = bootstrap.bind(DrServerAdmin.port);
        bossChannel = bind.sync();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

Initalizer:

public class CustomInitalizer extends ChannelInitializer<SocketChannel> {

    public static  DefaultEventExecutorGroup normalGroup = new DefaultEventExecutorGroup(16);
    public static  DefaultEventExecutorGroup loginGroup = new DefaultEventExecutorGroup(8);
    public static  DefaultEventExecutorGroup commandsGroup = new DefaultEventExecutorGroup(4);

    private final SslContext sslCtx;

    public CustomInitalizer(SslContext sslCtx) {
        this.sslCtx = sslCtx;
    }

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        if (sslCtx != null) {
            pipeline.addLast(sslCtx.newHandler(ch.alloc()));
        }

        pipeline.addLast(new CustomFirewall()); //it is AbstractRemoteAddressFilter<InetSocketAddress>
        int limit = 32768;        
        pipeline.addLast(new DelimiterBasedFrameDecoder(limit, Delimiters.nulDelimiter()));
        pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
        pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));

        pipeline.addLast(new CustomReadTimeoutHandler(Settings.SOCKET_TIMEOUT));

        int id = DrServerNetty.getDrServer().getIdClient();
        CustomHandler normalHandler = new CustomHandler();
        FlashClientNetty client = new FlashClientNetty(normalHandler,id);
        normalHandler.setClient(client);

        pipeline.addLast(normalGroup,"normalHandler",normalHandler);

        CustomLoginHandler loginHandler = new CustomLoginHandler(client);
        pipeline.addLast(loginGroup,"loginHandler",loginHandler);


        CustomCommandsHandler commandsHandler = new CustomCommandsHandler(loginHandler.client);
        pipeline.addLast(commandsGroup, "commandsHandler", commandsHandler);

    }
}

我正在使用5组:

> bootstrap bossGroup – 用于新连接
> bootstrap workerGroup – 用于传递消息
> normalGroup – 适用于大多数消息
> loginGroup – 用于繁重的登录过程
>命令组 – 用于某些重要的逻辑

我正在监视新连接和消息的数量,以便我可以立即发现是否有攻击.在攻击期间,我不再接受新的连接:我在自定义防火墙(AbstractRemoteAddressFilter)中返回false.

protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) throws Exception {
    if(ddosDetected())
       return false;
    else
        return true;
}

但即使我正在放弃新的连接,我的工作组也会过载. PendingTasks for worker group(所有其他组都很好)正在增长,导致普通玩家的通信时间越来越长,最后,他们被socket_timeouts踢了.我不确定为什么会这样.在正常的服务器使用期间,最繁忙的组是登录和正常组.在网络级服务器很好 – 它只使用其带宽限制的约10%.攻击期间CPU和RAM的使用率也不是很高.但经过几分钟的攻击后,我的所有玩家都被踢出游戏,无法连接.

有没有更好的方法来立即丢弃所有传入的连接并保护已连接的用户?

最佳答案 我认为你需要通过例如iptables在内核级别“修复此问题”.否则你只能在你已经接受之后关闭连接,这在这种情况下听起来不够好.

点赞