From 8b4aa3b46dd058342c9076f52a9e7bdc45b81242 Mon Sep 17 00:00:00 2001 From: Nikita Date: Sat, 16 Nov 2013 13:14:18 +0400 Subject: [PATCH] Wrong urls handler. Issue #81 --- .../socketio/SocketIOChannelInitializer.java | 22 +++++++---- .../socketio/handler/AuthorizeHandler.java | 1 + .../socketio/handler/WrongUrlHandler.java | 39 +++++++++++++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/corundumstudio/socketio/handler/WrongUrlHandler.java diff --git a/src/main/java/com/corundumstudio/socketio/SocketIOChannelInitializer.java b/src/main/java/com/corundumstudio/socketio/SocketIOChannelInitializer.java index 26a6f0c..33acbbb 100644 --- a/src/main/java/com/corundumstudio/socketio/SocketIOChannelInitializer.java +++ b/src/main/java/com/corundumstudio/socketio/SocketIOChannelInitializer.java @@ -15,6 +15,14 @@ */ package com.corundumstudio.socketio; +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequestDecoder; +import io.netty.handler.codec.http.HttpResponseEncoder; +import io.netty.handler.ssl.SslHandler; + import java.io.InputStream; import java.security.KeyStore; import java.security.Security; @@ -24,14 +32,6 @@ import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; -import io.netty.channel.Channel; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelPipeline; -import io.netty.handler.codec.http.HttpObjectAggregator; -import io.netty.handler.codec.http.HttpRequestDecoder; -import io.netty.handler.codec.http.HttpResponseEncoder; -import io.netty.handler.ssl.SslHandler; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +39,7 @@ import com.corundumstudio.socketio.ack.AckManager; import com.corundumstudio.socketio.handler.AuthorizeHandler; import com.corundumstudio.socketio.handler.PacketHandler; import com.corundumstudio.socketio.handler.ResourceHandler; +import com.corundumstudio.socketio.handler.WrongUrlHandler; import com.corundumstudio.socketio.misc.CompositeIterable; import com.corundumstudio.socketio.misc.IterableCollection; import com.corundumstudio.socketio.namespace.NamespacesHub; @@ -66,6 +67,7 @@ public class SocketIOChannelInitializer extends ChannelInitializer impl public static final String SSL_HANDLER = "ssl"; public static final String FLASH_POLICY_HANDLER = "flashPolicyHandler"; public static final String RESOURCE_HANDLER = "resourceHandler"; + public static final String WRONG_URL_HANDLER = "wrongUrlBlocker"; private final Logger log = LoggerFactory.getLogger(getClass()); @@ -80,6 +82,7 @@ public class SocketIOChannelInitializer extends ChannelInitializer impl private final FlashPolicyHandler flashPolicyHandler = new FlashPolicyHandler(); private ResourceHandler resourceHandler; private SocketIOEncoder socketIOEncoder; + private WrongUrlHandler wrongUrlHandler; private CancelableScheduler scheduler; @@ -119,6 +122,7 @@ public class SocketIOChannelInitializer extends ChannelInitializer impl flashSocketTransport = new FlashSocketTransport(connectPath, isSsl, ackManager, this, authorizeHandler, heartbeatHandler); resourceHandler = new ResourceHandler(configuration.getContext()); socketIOEncoder = new SocketIOEncoder(encoder); + wrongUrlHandler = new WrongUrlHandler(); } public Collection getAllClients() { @@ -159,6 +163,8 @@ public class SocketIOChannelInitializer extends ChannelInitializer impl pipeline.addLast(FLASH_SOCKET_TRANSPORT, flashSocketTransport); pipeline.addLast(SOCKETIO_ENCODER, socketIOEncoder); + + pipeline.addLast(WRONG_URL_HANDLER, wrongUrlHandler); } private SSLContext createSSLContext(InputStream keyStoreFile, String keyStoreFilePassword) throws Exception { diff --git a/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java b/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java index 39f7c20..5a8fee7 100644 --- a/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java +++ b/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java @@ -85,6 +85,7 @@ public class AuthorizeHandler extends ChannelInboundHandlerAdapter implements Di ChannelFuture f = channel.write(res); f.addListener(ChannelFutureListener.CLOSE); req.release(); + log.warn("Blocked wrong request! url: {}, ip: {}", queryDecoder.path(), channel.remoteAddress()); return; } if (queryDecoder.path().equals(connectPath)) { diff --git a/src/main/java/com/corundumstudio/socketio/handler/WrongUrlHandler.java b/src/main/java/com/corundumstudio/socketio/handler/WrongUrlHandler.java new file mode 100644 index 0000000..ac81cad --- /dev/null +++ b/src/main/java/com/corundumstudio/socketio/handler/WrongUrlHandler.java @@ -0,0 +1,39 @@ +package com.corundumstudio.socketio.handler; + +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.handler.codec.http.DefaultHttpResponse; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.HttpResponse; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.QueryStringDecoder; + +@Sharable +public class WrongUrlHandler extends ChannelInboundHandlerAdapter { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + if (msg instanceof FullHttpRequest) { + FullHttpRequest req = (FullHttpRequest) msg; + Channel channel = ctx.channel(); + QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri()); + + HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST); + ChannelFuture f = channel.write(res); + f.addListener(ChannelFutureListener.CLOSE); + req.release(); + log.warn("Blocked wrong socket.io-context request! url: {}, ip: {}", queryDecoder.path(), channel.remoteAddress()); + } + } + +}