support wss directly

This commit is contained in:
Dank074 2021-01-27 23:42:15 -06:00
parent 1a4b8ef8f9
commit e866dd2c16
4 changed files with 47 additions and 31 deletions

View File

@ -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));
}
}

View File

@ -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<SocketChannel> {
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<SocketChannel>
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<SocketChannel>
ch.pipeline().addLast(new GameServerMessageLogger());
}
}
public boolean isSSL() {
return isSSL;
}
}

View File

@ -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<Object> 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);
}
}

View File

@ -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;
}
}