|
19 | 19 |
|
20 | 20 | import com.google.common.annotations.VisibleForTesting; |
21 | 21 | import com.google.common.base.Preconditions; |
| 22 | +import com.velocitypowered.api.event.connection.ConnectionEstablishEvent; |
22 | 23 | import com.velocitypowered.api.event.connection.ConnectionHandshakeEvent; |
23 | 24 | import com.velocitypowered.api.network.ProtocolVersion; |
24 | 25 | import com.velocitypowered.proxy.VelocityServer; |
@@ -85,31 +86,57 @@ public boolean handle(LegacyHandshake packet) { |
85 | 86 | public boolean handle(Handshake handshake) { |
86 | 87 | InitialInboundConnection ic = new InitialInboundConnection(connection, |
87 | 88 | cleanVhost(handshake.getServerAddress()), handshake); |
88 | | - StateRegistry nextState = getStateForProtocol(handshake.getNextStatus()); |
89 | | - if (nextState == null) { |
90 | | - LOGGER.error("{} provided invalid protocol {}", ic, handshake.getNextStatus()); |
91 | | - connection.close(true); |
92 | | - } else { |
93 | | - connection.setProtocolVersion(handshake.getProtocolVersion()); |
94 | | - connection.setAssociation(ic); |
95 | | - |
96 | | - switch (nextState) { |
97 | | - case STATUS: |
98 | | - connection.setActiveSessionHandler(StateRegistry.STATUS, |
99 | | - new StatusSessionHandler(server, ic)); |
100 | | - break; |
101 | | - case LOGIN: |
102 | | - this.handleLogin(handshake, ic); |
103 | | - break; |
104 | | - default: |
105 | | - // If you get this, it's a bug in Velocity. |
106 | | - throw new AssertionError("getStateForProtocol provided invalid state!"); |
107 | | - } |
108 | | - } |
| 89 | + |
| 90 | + // Handle connection establish event. |
| 91 | + connection.setAutoReading(false); |
| 92 | + server.getEventManager() |
| 93 | + .fire(new ConnectionEstablishEvent( |
| 94 | + ic, getIntentionForStatus(handshake.getNextStatus()))) |
| 95 | + .thenAcceptAsync(result -> { |
| 96 | + // Clean up the disabling of auto-read. |
| 97 | + connection.setAutoReading(true); |
| 98 | + |
| 99 | + if (!result.getResult().isAllowed()) { |
| 100 | + connection.close(true); |
| 101 | + } else { |
| 102 | + StateRegistry nextState = getStateForProtocol(handshake.getNextStatus()); |
| 103 | + if (nextState == null) { |
| 104 | + LOGGER.error("{} provided invalid protocol {}", ic, handshake.getNextStatus()); |
| 105 | + connection.close(true); |
| 106 | + } else { |
| 107 | + connection.setProtocolVersion(handshake.getProtocolVersion()); |
| 108 | + connection.setAssociation(ic); |
| 109 | + |
| 110 | + switch (nextState) { |
| 111 | + case STATUS: |
| 112 | + connection.setActiveSessionHandler(StateRegistry.STATUS, |
| 113 | + new StatusSessionHandler(server, ic)); |
| 114 | + break; |
| 115 | + case LOGIN: |
| 116 | + this.handleLogin(handshake, ic); |
| 117 | + break; |
| 118 | + default: |
| 119 | + // If you get this, it's a bug in Velocity. |
| 120 | + throw new AssertionError("getStateForProtocol provided invalid state!"); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
109 | 125 |
|
110 | 126 | return true; |
111 | 127 | } |
112 | 128 |
|
| 129 | + private static ConnectionEstablishEvent.@Nullable Intention getIntentionForStatus(int status) { |
| 130 | + switch (status) { |
| 131 | + case StateRegistry.STATUS_ID: |
| 132 | + return ConnectionEstablishEvent.Intention.STATUS; |
| 133 | + case StateRegistry.LOGIN_ID: |
| 134 | + return ConnectionEstablishEvent.Intention.LOGIN; |
| 135 | + default: |
| 136 | + return null; |
| 137 | + } |
| 138 | + } |
| 139 | + |
113 | 140 | private static @Nullable StateRegistry getStateForProtocol(int status) { |
114 | 141 | switch (status) { |
115 | 142 | case StateRegistry.STATUS_ID: |
|
0 commit comments