Skip to content

Commit 0cc54ef

Browse files
committed
feature: Cancellable connection establish event
1 parent 7f776ab commit 0cc54ef

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2018-2021 Velocity Contributors
3+
*
4+
* The Velocity API is licensed under the terms of the MIT License. For more details,
5+
* reference the LICENSE file in the api top-level directory.
6+
*/
7+
8+
package com.velocitypowered.api.event.connection;
9+
10+
import com.google.common.base.Preconditions;
11+
import com.velocitypowered.api.event.ResultedEvent;
12+
import com.velocitypowered.api.event.annotation.AwaitingEvent;
13+
import com.velocitypowered.api.proxy.InboundConnection;
14+
import org.jetbrains.annotations.NotNull;
15+
16+
/**
17+
* Event called when a connection is initially established with the proxy.
18+
* Velocity will wait for this event to fire before continuing with the connection.
19+
*/
20+
@AwaitingEvent
21+
public class ConnectionEstablishEvent implements ResultedEvent<ResultedEvent.GenericResult> {
22+
private final InboundConnection connection;
23+
private GenericResult result = GenericResult.allowed();
24+
25+
public ConnectionEstablishEvent(final @NotNull InboundConnection connection) {
26+
this.connection = Preconditions.checkNotNull(connection, "connection");
27+
}
28+
29+
/**
30+
* Returns the inbound connection that is being established.
31+
* @return the connection
32+
*/
33+
public @NotNull InboundConnection getConnection() {
34+
return this.connection;
35+
}
36+
37+
@Override
38+
public GenericResult getResult() {
39+
return this.result;
40+
}
41+
42+
@Override
43+
public void setResult(final @NotNull GenericResult result) {
44+
this.result = Preconditions.checkNotNull(result, "result");
45+
}
46+
}

proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.annotations.VisibleForTesting;
2121
import com.google.common.base.Preconditions;
22+
import com.velocitypowered.api.event.connection.ConnectionEstablishEvent;
2223
import com.velocitypowered.api.event.connection.ConnectionHandshakeEvent;
2324
import com.velocitypowered.api.network.ProtocolVersion;
2425
import com.velocitypowered.proxy.VelocityServer;
@@ -85,6 +86,13 @@ public boolean handle(LegacyHandshake packet) {
8586
public boolean handle(Handshake handshake) {
8687
InitialInboundConnection ic = new InitialInboundConnection(connection,
8788
cleanVhost(handshake.getServerAddress()), handshake);
89+
90+
ConnectionEstablishEvent event = server.getEventManager().fire(new ConnectionEstablishEvent(ic)).join();
91+
if (!event.getResult().isAllowed()) {
92+
connection.close(true);
93+
return true;
94+
}
95+
8896
StateRegistry nextState = getStateForProtocol(handshake.getNextStatus());
8997
if (nextState == null) {
9098
LOGGER.error("{} provided invalid protocol {}", ic, handshake.getNextStatus());

0 commit comments

Comments
 (0)