From 23554239d0a804368e273eeeef17246888569cdc Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 23 Sep 2017 13:18:43 +1000 Subject: [PATCH 1/4] #2259: Have proxy handle keepalives --- .../main/java/net/md_5/bungee/ServerConnection.java | 6 ++++++ .../main/java/net/md_5/bungee/UserConnection.java | 3 --- .../md_5/bungee/connection/DownstreamBridge.java | 2 +- .../net/md_5/bungee/connection/UpstreamBridge.java | 13 ++++++++++++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java index b795bf9052..1be12dbce9 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java @@ -25,6 +25,12 @@ public class ServerConnection implements Server private boolean isObsolete; @Getter private final boolean forgeServer = false; + @Getter + @Setter + private boolean pingFailed; + @Getter + @Setter + private long sentPingId; private final Unsafe unsafe = new Unsafe() { diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 881a079a89..93df974770 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -91,9 +91,6 @@ public final class UserConnection implements ProxiedPlayer /*========================================================================*/ @Getter @Setter - private long sentPingId; - @Getter - @Setter private long sentPingTime; @Getter @Setter diff --git a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java index 4049e815af..6b8ae2452c 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java @@ -106,7 +106,7 @@ public void handle(PacketWrapper packet) throws Exception @Override public void handle(KeepAlive alive) throws Exception { - con.setSentPingId( alive.getRandomId() ); + server.setSentPingId( alive.getRandomId() ); con.setSentPingTime( System.currentTimeMillis() ); } diff --git a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java index 167f1f8867..29e763c779 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java @@ -116,11 +116,22 @@ public void handle(PacketWrapper packet) throws Exception @Override public void handle(KeepAlive alive) throws Exception { - if ( alive.getRandomId() == con.getSentPingId() ) + if ( alive.getRandomId() == con.getServer().getSentPingId() ) { int newPing = (int) ( System.currentTimeMillis() - con.getSentPingTime() ); con.getTabListHandler().onPingChange( newPing ); con.setPing( newPing ); + } else + { + if ( con.getServer().getSentPingId() != 0 && !con.getServer().isPingFailed() ) + { + alive.setRandomId( con.getServer().getSentPingId() ); + con.getServer().unsafe().sendPacket( alive ); + + con.getServer().setPingFailed( true ); + } + + throw CancelSendSignal.INSTANCE; } } From af8d1af635e12e5cf54734be6a3e20beb783a20b Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 23 Sep 2017 13:56:23 +1000 Subject: [PATCH 2/4] Initialize ping to -1 --- proxy/src/main/java/net/md_5/bungee/ServerConnection.java | 2 +- .../main/java/net/md_5/bungee/connection/UpstreamBridge.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java index 1be12dbce9..a302910681 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java @@ -30,7 +30,7 @@ public class ServerConnection implements Server private boolean pingFailed; @Getter @Setter - private long sentPingId; + private long sentPingId = -1; private final Unsafe unsafe = new Unsafe() { diff --git a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java index 29e763c779..ae3d900b22 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java @@ -123,7 +123,7 @@ public void handle(KeepAlive alive) throws Exception con.setPing( newPing ); } else { - if ( con.getServer().getSentPingId() != 0 && !con.getServer().isPingFailed() ) + if ( con.getServer().getSentPingId() != -1 && !con.getServer().isPingFailed() ) { alive.setRandomId( con.getServer().getSentPingId() ); con.getServer().unsafe().sendPacket( alive ); From 69bbc3a71e80545741df22384ace861716253575 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 23 Sep 2017 15:05:50 +1000 Subject: [PATCH 3/4] Invalidate ping when spoof sent --- .../src/main/java/net/md_5/bungee/connection/UpstreamBridge.java | 1 + 1 file changed, 1 insertion(+) diff --git a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java index ae3d900b22..ad7202d7bc 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java @@ -129,6 +129,7 @@ public void handle(KeepAlive alive) throws Exception con.getServer().unsafe().sendPacket( alive ); con.getServer().setPingFailed( true ); + con.getServer().setSentPingId( -1 ); } throw CancelSendSignal.INSTANCE; From 8d494242265790df1dc6d92121d1a37b726ac405 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 23 Sep 2017 17:14:03 +1000 Subject: [PATCH 4/4] #2261: Don't send any keep alive to server if invalid --- .../src/main/java/net/md_5/bungee/ServerConnection.java | 3 --- .../java/net/md_5/bungee/connection/UpstreamBridge.java | 9 --------- 2 files changed, 12 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java index a302910681..285e5b8f82 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java @@ -27,9 +27,6 @@ public class ServerConnection implements Server private final boolean forgeServer = false; @Getter @Setter - private boolean pingFailed; - @Getter - @Setter private long sentPingId = -1; private final Unsafe unsafe = new Unsafe() diff --git a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java index ad7202d7bc..bb43810bbc 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java @@ -123,15 +123,6 @@ public void handle(KeepAlive alive) throws Exception con.setPing( newPing ); } else { - if ( con.getServer().getSentPingId() != -1 && !con.getServer().isPingFailed() ) - { - alive.setRandomId( con.getServer().getSentPingId() ); - con.getServer().unsafe().sendPacket( alive ); - - con.getServer().setPingFailed( true ); - con.getServer().setSentPingId( -1 ); - } - throw CancelSendSignal.INSTANCE; } }