From 050a0ae7f788d1f336c513db31af007ed57922c0 Mon Sep 17 00:00:00 2001 From: Glease <4586901+Glease@users.noreply.github.com> Date: Fri, 4 Aug 2023 03:19:37 +0800 Subject: [PATCH] use openContainer instead of sent coords (#3644) --- .../li/cil/oc/common/container/Drone.scala | 2 +- .../li/cil/oc/server/PacketHandler.scala | 69 ++++++++++++------- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/main/scala/li/cil/oc/common/container/Drone.scala b/src/main/scala/li/cil/oc/common/container/Drone.scala index ba57aaa714..b242bd5873 100644 --- a/src/main/scala/li/cil/oc/common/container/Drone.scala +++ b/src/main/scala/li/cil/oc/common/container/Drone.scala @@ -8,7 +8,7 @@ import li.cil.oc.common.entity import net.minecraft.entity.player.InventoryPlayer import net.minecraft.inventory.IInventory -class Drone(playerInventory: InventoryPlayer, drone: entity.Drone) extends Player(playerInventory, drone.mainInventory) { +class Drone(playerInventory: InventoryPlayer, val drone: entity.Drone) extends Player(playerInventory, drone.mainInventory) { val deltaY = 0 for (i <- 0 to 1) { diff --git a/src/main/scala/li/cil/oc/server/PacketHandler.scala b/src/main/scala/li/cil/oc/server/PacketHandler.scala index 6d7deb06cd..d36ded6eea 100644 --- a/src/main/scala/li/cil/oc/server/PacketHandler.scala +++ b/src/main/scala/li/cil/oc/server/PacketHandler.scala @@ -3,12 +3,14 @@ package li.cil.oc.server import cpw.mods.fml.common.eventhandler.SubscribeEvent import cpw.mods.fml.common.network.FMLNetworkEvent.ServerCustomPacketEvent import li.cil.oc.Localization +import li.cil.oc.OpenComputers import li.cil.oc.api import li.cil.oc.api.internal.Server import li.cil.oc.api.machine.Machine import li.cil.oc.common.Achievement import li.cil.oc.common.PacketType import li.cil.oc.common.component.TextBuffer +import li.cil.oc.common.container import li.cil.oc.common.entity.Drone import li.cil.oc.common.item.Delegator import li.cil.oc.common.item.data.DriveData @@ -22,8 +24,14 @@ import net.minecraft.entity.player.EntityPlayerMP import net.minecraft.nbt.NBTTagCompound import net.minecraft.network.NetHandlerPlayServer import net.minecraftforge.common.DimensionManager +import org.apache.logging.log4j.MarkerManager object PacketHandler extends CommonPacketHandler { + private val securityMarker = MarkerManager.getMarker("SuspiciousPackets") + + private def logForgedPacket(player: EntityPlayerMP) = + OpenComputers.log.warn(securityMarker, "Player {} tried to send GUI packets without opening them", player.getGameProfile) + @SubscribeEvent def onPacket(e: ServerCustomPacketEvent) = onPacketData(e.packet.payload, e.handler.asInstanceOf[NetHandlerPlayServer].playerEntity) @@ -60,10 +68,14 @@ object PacketHandler extends CommonPacketHandler { def onComputerPower(p: PacketParser): Unit = { val entity = p.readTileEntity[Computer]() val setPower = p.readBoolean() - entity match { - case Some(t) => p.player match { - case player: EntityPlayerMP => trySetComputerPower(t.machine, setPower, player) - case _ => + p.player match { + case player: EntityPlayerMP => player.openContainer match { + case container: container.Player => (container.otherInventory, entity) match { + case (computer: Computer, Some(c2)) if c2.position == computer.position => + trySetComputerPower(computer.machine, setPower, player) + case _ => logForgedPacket(player) + } + case _ => logForgedPacket(player) } case _ => // Invalid packet. } @@ -72,17 +84,24 @@ object PacketHandler extends CommonPacketHandler { def onServerPower(p: PacketParser): Unit = { val entity = p.readTileEntity[Rack]() val index = p.readInt() - val setPower = p.readBoolean() - entity match { + val readServer = entity match { case Some(t) => - val mountableIndex = index - t.getMountable(mountableIndex) match { - case server: Server => p.player match { - case player: EntityPlayerMP => trySetComputerPower(server.machine, setPower, player) - case _ => // Invalid packet. - } - case _ => // Invalid packet. + t.getMountable(index) match { + case server: Server => server + case _ => return // probably just lag, not invalid packet + } + case _ => return + } + val setPower = p.readBoolean() + p.player match { + case player: EntityPlayerMP => player.openContainer match { + case container: container.Server => container.server match { + case Some(server) if server == readServer => + trySetComputerPower(server.machine, setPower, player) + case _ => logForgedPacket(player) } + case _ => logForgedPacket(player) + } case _ => // Invalid packet. } } @@ -122,16 +141,17 @@ object PacketHandler extends CommonPacketHandler { def onDronePower(p: PacketParser): Unit = { val entity = p.readEntity[Drone]() val power = p.readBoolean() - entity match { - case Some(drone) => p.player match { - case player: EntityPlayerMP => + p.player match { + case player: EntityPlayerMP => (player.openContainer, entity) match { + case (c: container.Drone, Some(readDrone)) if c.drone == readDrone => + val drone = c.drone if (power) { drone.preparePowerUp() } trySetComputerPower(drone.machine, power, player) - case _ => + case _ => logForgedPacket(player) } - case _ => // Invalid packet. + case _ => } } @@ -249,13 +269,14 @@ object PacketHandler extends CommonPacketHandler { val mountableIndex = p.readInt() val nodeIndex = p.readInt() val side = p.readDirection() - entity match { - case Some(t) => p.player match { - case player: EntityPlayerMP if t.isUseableByPlayer(player) => - t.connect(mountableIndex, nodeIndex - 1, side) - case _ => + p.player match { + case player: EntityPlayerMP => (player.openContainer, entity) match { + case (container: container.Rack, Some(readRack)) if readRack == container.rack => + if (container.rack.isUseableByPlayer(player)) + container.rack.connect(mountableIndex, nodeIndex - 1, side) + case _ => logForgedPacket(player) } - case _ => // Invalid packet. + case _ => } }