From 21e72556c964154deee5ed7e5f81690ddaf17a1e Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 11 Aug 2018 06:46:40 -0400 Subject: [PATCH] Correctly handle rapid disconnects. Fixes #31 --- .../proxy/connection/MinecraftConnection.java | 15 +++++++++------ .../backend/BackendPlaySessionHandler.java | 14 ++++++++++++++ .../proxy/connection/client/ConnectedPlayer.java | 1 - 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java index 6d9738414..a87c95e40 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -104,18 +104,21 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { } public void write(Object msg) { - ensureOpen(); - channel.writeAndFlush(msg, channel.voidPromise()); + if (channel.isActive()) { + channel.writeAndFlush(msg, channel.voidPromise()); + } } public void delayedWrite(Object msg) { - ensureOpen(); - channel.write(msg, channel.voidPromise()); + if (channel.isActive()) { + channel.write(msg, channel.voidPromise()); + } } public void flush() { - ensureOpen(); - channel.flush(); + if (channel.isActive()) { + channel.flush(); + } } public void closeWith(Object msg) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index bbf407952..b8975a0b3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -18,6 +18,13 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { @Override public void handle(MinecraftPacket packet) { + if (!connection.getProxyPlayer().isActive()) { + // Connection was left open accidentally. Close it so as to avoid "You logged in from another location" + // errors. + connection.getMinecraftConnection().close(); + return; + } + ClientPlaySessionHandler playerHandler = (ClientPlaySessionHandler) connection.getProxyPlayer().getConnection().getSessionHandler(); if (packet instanceof KeepAlive) { @@ -69,6 +76,13 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { @Override public void handleUnknown(ByteBuf buf) { + if (!connection.getProxyPlayer().isActive()) { + // Connection was left open accidentally. Close it so as to avoid "You logged in from another location" + // errors. + connection.getMinecraftConnection().close(); + return; + } + ClientPlaySessionHandler playerHandler = (ClientPlaySessionHandler) connection.getProxyPlayer().getConnection().getSessionHandler(); ByteBuf remapped = playerHandler.getIdRemapper().remap(buf, ProtocolConstants.Direction.CLIENTBOUND); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index 2cf2b887f..bf4f05916 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -132,7 +132,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { String error = ThrowableUtils.briefDescription(throwable); String userMessage; if (connectedServer != null && connectedServer.getServerInfo().equals(info)) { - logger.error("{}: exception occurred in connection to {}", this, info.getName(), throwable); userMessage = "Exception in server " + info.getName(); } else { logger.error("{}: unable to connect to server {}", this, info.getName(), throwable);