From 7c693ab48089e3ce16dc2f9f01b574eaa7028d2b Mon Sep 17 00:00:00 2001 From: ProfMobius Date: Fri, 3 Oct 2014 22:46:52 +0200 Subject: [PATCH] Added shaded netty 4.0.23 Added support for SSL encryption. --- build.gradle | 1 + .../opis/network/rconserver/RConServer.java | 36 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index d43ad8c..1179406 100644 --- a/build.gradle +++ b/build.gradle @@ -51,6 +51,7 @@ dependencies { shade 'org.jfree:jfreechart:1.0.17' shade 'com.miglayout:miglayout-swing:4.2' shade 'net.sf.opencsv:opencsv:2.3' + shade 'io.netty:netty-all:4.0.23.Final' compile "mcp.mobius.mobiuscore:MobiusCore:1.2.4_1.7.10:dev" deployerJars "org.apache.maven.wagon:wagon-ssh:2.2" } diff --git a/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java b/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java index a1fa965..225fa49 100644 --- a/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java +++ b/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java @@ -2,10 +2,13 @@ package mcp.mobius.opis.network.rconserver; import static cpw.mods.fml.relauncher.Side.SERVER; +import java.security.cert.CertificateException; import java.util.ArrayList; import java.util.HashMap; import java.util.Timer; +import javax.net.ssl.SSLException; + import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.google.common.io.ByteArrayDataOutput; @@ -34,6 +37,13 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.compression.JdkZlibDecoder; +import io.netty.handler.codec.compression.JdkZlibEncoder; +import io.netty.handler.codec.compression.ZlibCodecFactory; +import io.netty.handler.codec.compression.ZlibDecoder; +import io.netty.handler.codec.compression.ZlibWrapper; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.util.SelfSignedCertificate; import io.netty.handler.timeout.IdleStateHandler; import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.util.ReferenceCountUtil; @@ -41,9 +51,22 @@ import io.netty.util.ReferenceCountUtil; public class RConServer implements Runnable { class ChannelInit extends ChannelInitializer{ + private final SslContext sslCtx; + + public ChannelInit(SslContext sslCtx) { + this.sslCtx = sslCtx; + } + @Override protected void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(new ReadTimeoutHandler(10), new RconMsgDecoder(), new RConInboundHandler()); + + ch.pipeline().addLast(sslCtx.newHandler(ch.alloc())); + ch.pipeline().addLast(new ReadTimeoutHandler(10)); + ch.pipeline().addLast(new JdkZlibDecoder()); + ch.pipeline().addLast(new JdkZlibEncoder()); + ch.pipeline().addLast(new RconMsgDecoder()); + ch.pipeline().addLast(new RConInboundHandler()); + } } @@ -68,6 +91,15 @@ public class RConServer implements Runnable { public void run(){ modOpis.log.info("Starting Opis remote control server."); + + SelfSignedCertificate ssc = null; + SslContext sslCtx = null; + try { + ssc = new SelfSignedCertificate(); + sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); + } catch (CertificateException e1) { e1.printStackTrace(); + } catch (SSLException e1) { e1.printStackTrace(); } + EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); @@ -75,7 +107,7 @@ public class RConServer implements Runnable { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) - .childHandler(new ChannelInit()) + .childHandler(new ChannelInit(sslCtx)) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true);