From e96ae1bc4a7382130a0242ffe7ec767e527f0f4f Mon Sep 17 00:00:00 2001 From: ProfMobius Date: Wed, 1 Oct 2014 19:51:51 +0200 Subject: [PATCH] Basic rcon server. --- src/main/java/mcp/mobius/opis/modOpis.java | 5 +- .../opis/network/rconserver/RConServer.java | 79 +++++++++++++++++++ .../mcp/mobius/opis/proxy/ProxyServer.java | 10 ++- 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java diff --git a/src/main/java/mcp/mobius/opis/modOpis.java b/src/main/java/mcp/mobius/opis/modOpis.java index 74daf50..f468735 100644 --- a/src/main/java/mcp/mobius/opis/modOpis.java +++ b/src/main/java/mcp/mobius/opis/modOpis.java @@ -79,6 +79,8 @@ public class modOpis { public static boolean profilerRun = false; public static boolean profilerRunClient = false; public static int profilerMaxTicks = 250; + public static int rconport = 25566; + public static boolean rconactive = false; public static boolean microseconds = true; private static int lagGenID = -1; public static CoordinatesBlock selectedBlock = null; @@ -99,7 +101,8 @@ public class modOpis { lagGenID = config.get(Configuration.CATEGORY_GENERAL, "laggenerator_id", -1).getInt(); profilerMaxTicks = config.get(Configuration.CATEGORY_GENERAL, "profiler.maxpts", 250).getInt(); microseconds = config.get(Configuration.CATEGORY_GENERAL, "display.microseconds", true).getBoolean(true); - + rconport = config.get(Configuration.CATEGORY_GENERAL, "opisrcon.port", 25566).getInt(); + rconactive = config.get(Configuration.CATEGORY_GENERAL, "opisrcon.active", false).getBoolean(false); String[] users = config.get("ACCESS_RIGHTS", "privileged", new String[]{}, commentPrivileged).getStringList(); AccessLevel minTables = AccessLevel.PRIVILEGED; diff --git a/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java b/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java new file mode 100644 index 0000000..239e539 --- /dev/null +++ b/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java @@ -0,0 +1,79 @@ +package mcp.mobius.opis.network.rconserver; + +import mcp.mobius.opis.modOpis; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +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.util.ReferenceCountUtil; + +public class RConServer extends ChannelInboundHandlerAdapter implements Runnable { + + public final static RConServer instance = new RConServer(); + private int port = -1; + + + private RConServer(){ + this.port = modOpis.rconport; + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + //((ByteBuf) msg).release(); + + try { + // Do something with msg + } finally { + ReferenceCountUtil.release(msg); + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } + + @Override + public void run(){ + modOpis.log.info("Starting Opis remote control server."); + + EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1) + EventLoopGroup workerGroup = new NioEventLoopGroup(); + try { + ServerBootstrap b = new ServerBootstrap(); // (2) + b.group(bossGroup, workerGroup) + .channel(NioServerSocketChannel.class) // (3) + .childHandler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(RConServer.instance); + } + }) + .option(ChannelOption.SO_BACKLOG, 128) // (5) + .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) + + // Bind and start to accept incoming connections. + ChannelFuture f = b.bind(port).sync(); // (7) + + // Wait until the server socket is closed. + // In this example, this does not happen, but you can do that to gracefully + // shut down your server. + f.channel().closeFuture().sync(); + } catch (Exception e){ + modOpis.log.severe("Error in Opis remote control server."); + e.printStackTrace(); + } finally { + workerGroup.shutdownGracefully(); + bossGroup.shutdownGracefully(); + } + } + +} diff --git a/src/main/java/mcp/mobius/opis/proxy/ProxyServer.java b/src/main/java/mcp/mobius/opis/proxy/ProxyServer.java index d1dba45..a376a42 100644 --- a/src/main/java/mcp/mobius/opis/proxy/ProxyServer.java +++ b/src/main/java/mcp/mobius/opis/proxy/ProxyServer.java @@ -3,14 +3,22 @@ package mcp.mobius.opis.proxy; import java.util.ArrayList; import java.util.HashMap; +import mcp.mobius.opis.modOpis; import mcp.mobius.opis.api.IMessageHandler; import mcp.mobius.opis.gui.overlay.OverlayStatus; import mcp.mobius.opis.network.PacketBase; import mcp.mobius.opis.network.enums.Message; +import mcp.mobius.opis.network.rconserver.RConServer; public class ProxyServer implements IMessageHandler{ - public void init(){} + public void init(){ + if (modOpis.rconactive){ + Thread rconserver = new Thread(RConServer.instance); + rconserver.setName("Opis Server"); + rconserver.start(); + } + } @Override public boolean handleMessage(Message msg, PacketBase rawdata) {