Added SSL support to Nexus Client

This commit is contained in:
ProfMobius committed 2014-10-13 18:43:24 +02:00
1 parent ea7ee7d2c8
commit 663d616f21
4 files changed
+64 -11

No files matched your search

@@ -12,6 +12,7 @@ import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
@@ -47,21 +48,20 @@ import mcp.mobius.opis.network.rcon.server.RConServer;
public class NexusClient implements Runnable {
class ChannelInit extends ChannelInitializer<SocketChannel>{
//private final SslContext sslCtx;
//public ChannelInit(SslContext sslCtx) {
// this.sslCtx = sslCtx;
//}
private final SslContext sslCtx;
public ChannelInit(SslContext sslCtx) { this.sslCtx = sslCtx; }
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), RConClient.instance.host, RConClient.instance.port));
//ch.pipeline().addLast(new RConHandshakeDecoder());
//ch.pipeline().addLast(new RConHandshakeHandler());
ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), NexusClient.instance.host, NexusClient.instance.port));
ch.pipeline().addLast(new NexusHandshakeDecoder());
ch.pipeline().addLast(new NexusHandshakeHandler());
//ch.pipeline().addLast(new JdkZlibDecoder());
//ch.pipeline().addLast(new JdkZlibEncoder());
//ch.pipeline().addLast(new WriteTimeoutHandler(5));
ch.pipeline().addLast(new NexusMsgDecoder());
ch.pipeline().addLast(new NexusInboundHandler());
//ch.pipeline().addLast(new NexusMsgDecoder());
//ch.pipeline().addLast(new NexusInboundHandler());
}
}
@@ -87,6 +87,13 @@ public class NexusClient implements Runnable {
modOpis.log.info(String.format("Connecting to OpixNexus %s:%s", this.host, this.port));
SslContext sslCtx = null;
try {
sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
} catch (SSLException e) {
e.printStackTrace();
}
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
@@ -94,7 +101,7 @@ public class NexusClient implements Runnable {
b.group(workerGroup); // (2)
b.channel(NioSocketChannel.class); // (3)
b.option(ChannelOption.SO_KEEPALIVE, true); // (4)
b.handler(new ChannelInit());
b.handler(new ChannelInit(sslCtx));
ChannelFuture f = b.connect(this.host, this.port).sync(); // (5)
// Wait until the connection is closed.
@@ -0,0 +1,17 @@
package mcp.mobius.opis.network.rcon.nexus;
import java.util.List;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
public class NexusHandshakeDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 1) { return; }
out.add(in.readBoolean());
}
}
@@ -0,0 +1,29 @@
package mcp.mobius.opis.network.rcon.nexus;
import mcp.mobius.opis.modOpis;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.channel.Channel;
public class NexusHandshakeHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
modOpis.log.info("SSL encryption activated.");
ctx.pipeline().remove(NexusHandshakeDecoder.class);
ctx.pipeline().remove(NexusHandshakeHandler.class);
ctx.pipeline().addLast(new NexusMsgDecoder());
ctx.pipeline().addLast(new NexusInboundHandler());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
@@ -30,7 +30,7 @@ import io.netty.handler.codec.ByteToMessageDecoder;
public class NexusInboundHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(final ChannelHandlerContext ctx) {
public void handlerAdded(final ChannelHandlerContext ctx) {
UUID fakeUUID = UUID.randomUUID();
FakePlayer fakePlayer = FakePlayerFactory.get(DimensionManager.getWorld(0), new GameProfile(fakeUUID, ctx.name()));