From 12b3eb07becd97846db5a4d3f3335bd5a0db64d7 Mon Sep 17 00:00:00 2001 From: Trumeet Date: Wed, 13 Jul 2022 13:44:20 -0700 Subject: Fix HTTP connection not closed after writing error codes in AuthHandler Signed-off-by: Trumeet --- src/main/java/moe/ymc/acron/net/AuthHandler.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/moe/ymc/acron/net/AuthHandler.java b/src/main/java/moe/ymc/acron/net/AuthHandler.java index 47abf9f..3e42e14 100644 --- a/src/main/java/moe/ymc/acron/net/AuthHandler.java +++ b/src/main/java/moe/ymc/acron/net/AuthHandler.java @@ -1,5 +1,6 @@ package moe.ymc.acron.net; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.*; @@ -24,7 +25,8 @@ public class AuthHandler extends SimpleChannelInboundHandler { LOGGER.debug("channelRead0: {}", msg.uri()); if (msg.method() != HttpMethod.GET) { ctx.channel().writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.BAD_REQUEST)); + HttpResponseStatus.BAD_REQUEST)) + .addListener(ChannelFutureListener.CLOSE); return; } HttpHeaders headers = msg.headers(); @@ -32,7 +34,8 @@ public class AuthHandler extends SimpleChannelInboundHandler { if (!"Upgrade".equalsIgnoreCase(headers.get(HttpHeaderNames.CONNECTION)) || !"WebSocket".equalsIgnoreCase(headers.get(HttpHeaderNames.UPGRADE))) { ctx.channel().writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.BAD_REQUEST)); + HttpResponseStatus.BAD_REQUEST)) + .addListener(ChannelFutureListener.CLOSE); return; } @@ -47,7 +50,8 @@ public class AuthHandler extends SimpleChannelInboundHandler { decoder.parameters().get("token") == null || decoder.parameters().get("token").size() != 1) { ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.BAD_REQUEST)); + HttpResponseStatus.BAD_REQUEST)) + .addListener(ChannelFutureListener.CLOSE); return; } @@ -59,12 +63,14 @@ public class AuthHandler extends SimpleChannelInboundHandler { try { if (Integer.parseInt(versionRaw) != 0) { ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.BAD_REQUEST)); + HttpResponseStatus.BAD_REQUEST)) + .addListener(ChannelFutureListener.CLOSE); return; } } catch (NumberFormatException ignored) { ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.BAD_REQUEST)); + HttpResponseStatus.BAD_REQUEST)) + .addListener(ChannelFutureListener.CLOSE); return; } @@ -72,7 +78,8 @@ public class AuthHandler extends SimpleChannelInboundHandler { if (client == null || !client.token().equals(DigestUtils.sha256Hex(token))) { ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.UNAUTHORIZED)); + HttpResponseStatus.UNAUTHORIZED)) + .addListener(ChannelFutureListener.CLOSE); return; } ctx.channel().attr(Attributes.ID).set(new ClientIdentification(0, client)); -- cgit v1.2.3