diff --git a/src/main/java/org/krews/plugin/nitro/main.java b/src/main/java/org/krews/plugin/nitro/main.java index c0cf407..df881f1 100644 --- a/src/main/java/org/krews/plugin/nitro/main.java +++ b/src/main/java/org/krews/plugin/nitro/main.java @@ -35,11 +35,12 @@ public class main extends HabboPlugin implements EventListener { Emulator.getConfig().register("ws.nitro.host", "0.0.0.0"); Emulator.getConfig().register("ws.nitro.port", "2096"); - Emulator.getGameServer().getServerBootstrap().childHandler(new NetworkChannelInitializer()); + NetworkChannelInitializer wsChannelHandler = new NetworkChannelInitializer(); + Emulator.getGameServer().getServerBootstrap().childHandler(wsChannelHandler); Emulator.getGameServer().getServerBootstrap().bind(Emulator.getConfig().getValue("ws.nitro.host", "0.0.0.0"), Emulator.getConfig().getInt("ws.nitro.port", 2096)).sync(); LOGGER.info("OFFICIAL PLUGIN - Nitro Websockets has started!"); - LOGGER.info("Nitro Websockets Listening on " + Emulator.getConfig().getValue("ws.nitro.host", "0.0.0.0") + ":" + Emulator.getConfig().getInt("ws.nitro.port", 2096)); + LOGGER.info("Nitro Websockets Listening on " + (wsChannelHandler.isSSL() ? "wss://" : "ws://") + Emulator.getConfig().getValue("ws.nitro.host", "0.0.0.0") + ":" + Emulator.getConfig().getInt("ws.nitro.port", 2096)); } } diff --git a/src/main/java/org/krews/plugin/nitro/websockets/NetworkChannelInitializer.java b/src/main/java/org/krews/plugin/nitro/websockets/NetworkChannelInitializer.java index ccdfeea..07cafdd 100644 --- a/src/main/java/org/krews/plugin/nitro/websockets/NetworkChannelInitializer.java +++ b/src/main/java/org/krews/plugin/nitro/websockets/NetworkChannelInitializer.java @@ -5,13 +5,25 @@ import com.eu.habbo.networking.gameserver.decoders.*; import com.eu.habbo.networking.gameserver.encoders.GameServerMessageEncoder; import com.eu.habbo.networking.gameserver.encoders.GameServerMessageLogger; import com.eu.habbo.networking.gameserver.handlers.IdleTimeoutHandler; -import org.krews.plugin.nitro.websockets.handlers.MessageInterceptorHandler; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.logging.LoggingHandler; +import io.netty.handler.ssl.SslContext; import io.netty.handler.timeout.IdleStateHandler; +import org.krews.plugin.nitro.websockets.codec.WebSocketCodec; +import org.krews.plugin.nitro.websockets.ssl.SSLCertificateLoader; public class NetworkChannelInitializer extends ChannelInitializer { + private final SslContext context; + private final boolean isSSL; + + public NetworkChannelInitializer() { + context = SSLCertificateLoader.getContext(); + isSSL = context != null; + } @Override public void initChannel(SocketChannel ch) { @@ -20,7 +32,13 @@ public class NetworkChannelInitializer extends ChannelInitializer ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(60, 30, 0)); ch.pipeline().addAfter("idleStateHandler", "idleEventHandler", new IdleTimeoutHandler()); - ch.pipeline().addLast("messageInterceptor", new MessageInterceptorHandler()); + if(isSSL) { + ch.pipeline().addLast(context.newHandler(ch.alloc())); + } + ch.pipeline().addLast("httpCodec", new HttpServerCodec()); + ch.pipeline().addLast("objectAggregator", new HttpObjectAggregator(65536)); + ch.pipeline().addLast("protocolHandler", new WebSocketServerProtocolHandler("/", true)); + ch.pipeline().addLast("websocketCodec", new WebSocketCodec()); // Decoders. ch.pipeline().addLast(new GamePolicyDecoder()); @@ -41,4 +59,8 @@ public class NetworkChannelInitializer extends ChannelInitializer ch.pipeline().addLast(new GameServerMessageLogger()); } } + + public boolean isSSL() { + return isSSL; + } } diff --git a/src/main/java/org/krews/plugin/nitro/websockets/handlers/MessageInterceptorHandler.java b/src/main/java/org/krews/plugin/nitro/websockets/handlers/MessageInterceptorHandler.java deleted file mode 100644 index 5f5ad86..0000000 --- a/src/main/java/org/krews/plugin/nitro/websockets/handlers/MessageInterceptorHandler.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.krews.plugin.nitro.websockets.handlers; - -import org.krews.plugin.nitro.websockets.codec.WebSocketCodec; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; -import io.netty.handler.codec.http.HttpObjectAggregator; -import io.netty.handler.codec.http.HttpServerCodec; -import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; -import io.netty.util.CharsetUtil; - -import java.util.List; - -public class MessageInterceptorHandler extends ByteToMessageDecoder { - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - if(in.toString(CharsetUtil.UTF_8).startsWith("GET")) { - // this is a websocket upgrade request, so add the appropriate decoders/encoders - ctx.pipeline().addAfter("messageInterceptor", "websocketCodec", new WebSocketCodec()); - ctx.pipeline().addAfter("messageInterceptor", "protocolHandler", new WebSocketServerProtocolHandler("/", true)); - ctx.pipeline().addAfter("messageInterceptor", "objectAggregator", new HttpObjectAggregator(65536)); - ctx.pipeline().addAfter("messageInterceptor", "httpCodec", new HttpServerCodec()); - } - // Remove ourselves - ctx.pipeline().remove(this); - } -} \ No newline at end of file diff --git a/src/main/java/org/krews/plugin/nitro/websockets/ssl/SSLCertificateLoader.java b/src/main/java/org/krews/plugin/nitro/websockets/ssl/SSLCertificateLoader.java new file mode 100644 index 0000000..80d49d3 --- /dev/null +++ b/src/main/java/org/krews/plugin/nitro/websockets/ssl/SSLCertificateLoader.java @@ -0,0 +1,20 @@ +package org.krews.plugin.nitro.websockets.ssl; + +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; + +import java.io.File; + +public class SSLCertificateLoader { + private static final String filePath = "ssl"; + + public static SslContext getContext() { + SslContext context; + try { + context = SslContextBuilder.forServer(new File( filePath + File.separator + "cert.pem" ), new File( filePath + File.separator + "privkey.pem" )).build(); + } catch ( Exception e ) { + context = null; + } + return context; + } +}