Added support for a password.

This commit is contained in:
ProfMobius committed 2014-10-04 15:41:10 +02:00
1 parent 7c693ab480
commit 7d837824fd
6 files changed
+117 -16

No files matched your search

+4 -2
View File
@@ -82,6 +82,7 @@ public class modOpis {
public static int profilerMaxTicks = 250;
public static int rconport = 25566;
public static boolean rconactive = false;
public static String rconpass = "";
public static boolean microseconds = true;
private static int lagGenID = -1;
public static CoordinatesBlock selectedBlock = null;
@@ -103,8 +104,9 @@ 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);
rconport = config.get("REMOTE_CONSOLE", "opisrcon.port", 25566).getInt();
rconactive = config.get("REMOTE_CONSOLE", "opisrcon.active", false).getBoolean(false);
rconpass = config.get("REMOTE_CONSOLE", "opisrcon.password", "").getString();
String[] users = config.get("ACCESS_RIGHTS", "privileged", new String[]{}, commentPrivileged).getStringList();
AccessLevel minTables = AccessLevel.PRIVILEGED;
@@ -0,0 +1,32 @@
package mcp.mobius.opis.network.rconserver;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
import mcp.mobius.opis.network.PacketBase;
import com.google.common.io.ByteStreams;
public class RConHandshakeDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 4) { return; } // Size of the password char array
int nChars = in.getInt(in.readerIndex());
if (in.readableBytes() < 4 + 2*nChars) { return; } // The total size of the packet is 4 (int) + 2 (char) * nChars
in.readInt();
char[] passchars = new char[nChars];
for (int i = 0; i < nChars; i++){
passchars[i] = in.readChar();
}
out.add(new String(passchars));
}
}
@@ -0,0 +1,65 @@
package mcp.mobius.opis.network.rconserver;
import java.util.UUID;
import mcp.mobius.opis.modOpis;
import mcp.mobius.opis.data.holders.basetypes.SerialLong;
import mcp.mobius.opis.data.managers.StringCache;
import mcp.mobius.opis.events.PlayerTracker;
import mcp.mobius.opis.network.PacketManager;
import mcp.mobius.opis.network.enums.Message;
import mcp.mobius.opis.network.packets.server.NetDataValue;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.FakePlayerFactory;
import com.mojang.authlib.GameProfile;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.compression.JdkZlibDecoder;
import io.netty.handler.codec.compression.JdkZlibEncoder;
import io.netty.util.ReferenceCountUtil;
public class RConHandshakeHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
// Here we should check the password against what the client is sending and do all the user registration part
// if the password is valid. We should close the socket immediately otherwise.
try{
modOpis.log.info(String.format("Connection to rcon from %s detected", ctx.channel().remoteAddress().toString()));
if (((String)msg).equals(modOpis.rconpass)){
ByteBuf buf = ctx.alloc().buffer();
buf.writeBoolean(true);
ctx.writeAndFlush(buf);
ctx.pipeline().remove(RConHandshakeDecoder.class);
ctx.pipeline().remove(RConHandshakeHandler.class);
ctx.pipeline().addLast(new JdkZlibDecoder());
ctx.pipeline().addLast(new JdkZlibEncoder());
ctx.pipeline().addLast(new RConMsgDecoder());
ctx.pipeline().addLast(new RConInboundHandler());
} else {
modOpis.log.info(String.format("Password error. Closing socket"));
ByteBuf buf = ctx.alloc().buffer();
buf.writeBoolean(false);
ctx.writeAndFlush(buf);
ctx.close();
}
} finally {
ReferenceCountUtil.release(msg);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
@@ -26,20 +26,20 @@ class RConInboundHandler extends ChannelInboundHandlerAdapter{
// One object of this type is created by connection
// There is one permanent context per connection !
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// This method is called when the connection is created.
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
UUID fakeUUID = UUID.randomUUID();
FakePlayer fakePlayer = FakePlayerFactory.get(DimensionManager.getWorld(0), new GameProfile(fakeUUID, ctx.name()));
RConServer.instance.fakePlayers.put(fakePlayer, ctx);
PlayerTracker.INSTANCE.playersSwing.add(fakePlayer);
FakePlayer fakePlayer = FakePlayerFactory.get(DimensionManager.getWorld(0), new GameProfile(fakeUUID, ctx.name()));
RConServer.instance.fakePlayers.put(fakePlayer, ctx);
PlayerTracker.INSTANCE.playersSwing.add(fakePlayer);
PacketManager.validateAndSend(new NetDataValue(Message.STATUS_CURRENT_TIME, new SerialLong(System.currentTimeMillis())), fakePlayer);
StringCache.INSTANCE.syncCache(fakePlayer);
modOpis.log.info(String.format("Connection to rcon detected. FakePlayer %s with uuid %s created.", ctx.name(), fakeUUID));
modOpis.log.info(String.format("FakePlayer %s with uuid %s created.", ctx.name(), fakeUUID));
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
@@ -10,7 +10,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
public class RconMsgDecoder extends ByteToMessageDecoder {
public class RConMsgDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
@@ -62,10 +62,12 @@ public class RConServer implements Runnable {
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());
ch.pipeline().addLast(new RConHandshakeDecoder());
ch.pipeline().addLast(new RConHandshakeHandler());
//ch.pipeline().addLast(new JdkZlibDecoder());
//ch.pipeline().addLast(new JdkZlibEncoder());
//ch.pipeline().addLast(new RconMsgDecoder());
//ch.pipeline().addLast(new RConInboundHandler());
}
}