Waterfall/BungeeCord-Patches/0049-ConnectionInitEvent.patch

124 lines
4.4 KiB
Diff
Raw Normal View History

2023-09-21 12:33:23 +02:00
From 888d05f5fba61d8aa230eebe869bce7e5c19fe82 Mon Sep 17 00:00:00 2001
2019-12-02 12:36:13 +01:00
From: Shane Freeder <theboyetronic@gmail.com>
Date: Mon, 2 Dec 2019 11:35:17 +0000
Subject: [PATCH] ConnectionInitEvent
diff --git a/api/src/main/java/io/github/waterfallmc/waterfall/event/ConnectionInitEvent.java b/api/src/main/java/io/github/waterfallmc/waterfall/event/ConnectionInitEvent.java
new file mode 100644
2020-02-22 15:22:27 +01:00
index 00000000..6e79675f
2019-12-02 12:36:13 +01:00
--- /dev/null
+++ b/api/src/main/java/io/github/waterfallmc/waterfall/event/ConnectionInitEvent.java
@@ -0,0 +1,56 @@
2019-12-02 12:36:13 +01:00
+package io.github.waterfallmc.waterfall.event;
+
+import net.md_5.bungee.api.Callback;
+import net.md_5.bungee.api.config.ListenerInfo;
2019-12-02 12:36:13 +01:00
+import net.md_5.bungee.api.event.AsyncEvent;
+import net.md_5.bungee.api.event.ClientConnectEvent;
2019-12-02 12:36:13 +01:00
+import net.md_5.bungee.api.plugin.Cancellable;
+
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
2019-12-02 12:36:13 +01:00
+
+import lombok.ToString;
+
+/**
+ * Represents a brand new connection made to the proxy, allowing for plugins to
+ * efficiently close a connection, useful for connection throttlers, etc
+ */
+@ToString
2020-02-22 15:22:27 +01:00
+public class ConnectionInitEvent extends AsyncEvent<ConnectionInitEvent> implements Cancellable {
2019-12-02 12:36:13 +01:00
+
+ private final SocketAddress remoteAddress;
+ private final ListenerInfo listener;
2019-12-02 12:36:13 +01:00
+ private boolean isCancelled = false;
+
2020-02-22 15:22:27 +01:00
+ public ConnectionInitEvent(SocketAddress remoteAddress, ListenerInfo listener, Callback<ConnectionInitEvent> done) {
+ super(done);
2019-12-02 12:36:13 +01:00
+ this.remoteAddress = remoteAddress;
+ this.listener = listener;
2019-12-02 12:36:13 +01:00
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return this.isCancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.isCancelled = cancel;
+ }
+
+ /**
+ * @return the INetSocketAddress of the connection being opened
+ * @deprecated BungeeCord can accept connections via Unix domain sockets
+ */
+ @Deprecated
+ public InetSocketAddress getRemoteAddress() {
+ return (InetSocketAddress) remoteAddress;
+ }
+
+ /**
+ * @return the SocketAddress of the connection being opened
+ */
+ public SocketAddress getRemoteSocketAddress() {
+ return remoteAddress;
+ }
2019-12-02 12:36:13 +01:00
+}
diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java
index 3f30da0c..af65e192 100644
2019-12-02 12:36:13 +01:00
--- a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java
+++ b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java
@@ -1,6 +1,7 @@
2019-12-02 12:36:13 +01:00
package net.md_5.bungee.netty;
import com.google.common.base.Preconditions;
2019-12-02 12:36:13 +01:00
+import io.github.waterfallmc.waterfall.event.ConnectionInitEvent;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelException;
@@ -63,7 +64,6 @@ public class PipelineUtils
ch.close();
2019-12-02 12:36:13 +01:00
return;
}
-
ListenerInfo listener = ch.attr( LISTENER ).get();
2019-12-02 12:36:13 +01:00
if ( BungeeCord.getInstance().getPluginManager().callEvent( new ClientConnectEvent( remoteAddress, listener ) ).isCancelled() )
@@ -72,7 +72,21 @@ public class PipelineUtils
return;
}
2019-12-02 12:36:13 +01:00
+ ConnectionInitEvent connectionInitEvent = new ConnectionInitEvent(ch.remoteAddress(), listener, (result, throwable) -> { // Waterfall
+
+ if (result.isCancelled()) {
+ ch.close();
+ return;
+ }
+
+
2019-12-02 12:36:13 +01:00
+ try {
BASE.initChannel( ch );
+ } catch (Exception e) {
+ e.printStackTrace();
+ ch.close();
+ return;
+ }
ch.pipeline().addBefore( FRAME_DECODER, LEGACY_DECODER, new LegacyDecoder() );
ch.pipeline().addAfter( FRAME_DECODER, PACKET_DECODER, new MinecraftDecoder( Protocol.HANDSHAKE, true, ProxyServer.getInstance().getProtocolVersion() ) );
ch.pipeline().addAfter( FRAME_PREPENDER, PACKET_ENCODER, new MinecraftEncoder( Protocol.HANDSHAKE, true, ProxyServer.getInstance().getProtocolVersion() ) );
@@ -83,6 +97,9 @@ public class PipelineUtils
2019-12-02 12:36:13 +01:00
{
ch.pipeline().addFirst( new HAProxyMessageDecoder() );
}
+ }); // Waterfall
+
+ BungeeCord.getInstance().getPluginManager().callEvent(connectionInitEvent);
}
};
public static final Base BASE = new Base( false );
2019-12-02 12:36:13 +01:00
--
2023-09-21 12:33:23 +02:00
2.42.0
2019-12-02 12:36:13 +01:00