Skip to content

Commit 351be0d

Browse files
authored
Implement wildcard support for forced hosts and passthrough
This iteration also contains improvements to the raw fallback handler and eliminates abstract/obscure code that could slowdown user connections when attempting to connect to unavailable instances.
1 parent aef4bf0 commit 351be0d

2 files changed

Lines changed: 45 additions & 46 deletions

File tree

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

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
import com.velocitypowered.proxy.connection.player.resourcepack.VelocityResourcePackInfo;
6767
import com.velocitypowered.proxy.connection.player.resourcepack.handler.ResourcePackHandler;
6868
import com.velocitypowered.proxy.connection.util.ConnectionMessages;
69-
import com.velocitypowered.proxy.connection.util.ConnectionRequestResults;
7069
import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl;
7170
import com.velocitypowered.proxy.connection.util.VelocityInboundConnection;
7271
import com.velocitypowered.proxy.protocol.StateRegistry;
@@ -106,10 +105,10 @@
106105
import io.netty.buffer.Unpooled;
107106
import java.net.InetSocketAddress;
108107
import java.util.Collection;
109-
import java.util.Collections;
110108
import java.util.HashSet;
111109
import java.util.List;
112110
import java.util.Locale;
111+
import java.util.Map;
113112
import java.util.Optional;
114113
import java.util.Set;
115114
import java.util.UUID;
@@ -195,7 +194,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
195194
private @MonotonicNonNull List<String> serversToTry = null;
196195
private final ResourcePackHandler resourcePackHandler;
197196
private final BundleDelimiterHandler bundleHandler = new BundleDelimiterHandler(this);
198-
private boolean connectionInProgress;
199197
private boolean dontRemoveFromRedis;
200198
private @Nullable String clientBrand;
201199
private @Nullable Locale effectiveLocale;
@@ -750,8 +748,12 @@ public void handleConnectionException(final RegisteredServer server, final Throw
750748
friendlyError = Component.translatable("velocity.error.connected-server-error",
751749
Component.text(server.getServerInfo().getName()));
752750
} else {
753-
logger.error("{}: unable to connect to server {}", this, server.getServerInfo().getName(),
754-
wrapped);
751+
if (Boolean.getBoolean("velocity.suppress-connection-timeout-logs")) {
752+
logger.error("{}: unable to connect to server {}", this, server.getServerInfo().getName());
753+
} else {
754+
logger.error("{}: unable to connect to server {}", this, server.getServerInfo().getName(),
755+
wrapped);
756+
}
755757
friendlyError = Component.translatable("velocity.error.connecting-server-error",
756758
Component.text(server.getServerInfo().getName()));
757759
}
@@ -945,20 +947,24 @@ public Optional<RegisteredServer> getNextServerToTry() {
945947
* @return the next server to try
946948
*/
947949
private Optional<RegisteredServer> getNextServerToTry(@Nullable final RegisteredServer current) {
948-
if (serversToTry == null) {
950+
if (serversToTry == null || serversToTry.isEmpty()) {
949951
String virtualHostStr = getVirtualHost().map(InetSocketAddress::getHostString)
950952
.orElse("")
951953
.toLowerCase(Locale.ROOT);
952-
serversToTry = server.getConfiguration().getForcedHosts().getOrDefault(virtualHostStr,
953-
Collections.emptyList());
954-
}
955954

956-
if (serversToTry.isEmpty()) {
957-
List<String> connOrder = server.getConfiguration().getAttemptConnectionOrder();
958-
if (connOrder.isEmpty()) {
959-
return Optional.empty();
960-
} else {
961-
serversToTry = connOrder;
955+
serversToTry = server.getConfiguration().getForcedHosts().get(virtualHostStr);
956+
if (serversToTry == null || serversToTry.isEmpty()) {
957+
for (Map.Entry<String, List<String>> entry : server.getConfiguration().getForcedHosts().entrySet()) {
958+
String pattern = entry.getKey().toLowerCase(Locale.ROOT);
959+
if (pattern.startsWith("*.") && virtualHostStr.endsWith(pattern.substring(1))) {
960+
serversToTry = entry.getValue();
961+
break;
962+
}
963+
}
964+
}
965+
966+
if (serversToTry == null || serversToTry.isEmpty()) {
967+
serversToTry = server.getConfiguration().getAttemptConnectionOrder();
962968
}
963969
}
964970

@@ -982,20 +988,20 @@ private Optional<RegisteredServer> getNextServerToTry(@Nullable final Registered
982988

983989
if (selectedServer.isEmpty()) {
984990
if (strategy.equalsIgnoreCase("FIRST_AVAILABLE")) {
985-
tryIndex = i + 1;
991+
tryIndex = i;
986992
return Optional.of(registeredServer);
987993
}
988994
selectedServer = Optional.of(registeredServer);
989-
tryIndex = i + 1;
995+
tryIndex = i;
990996
} else if (strategy.equalsIgnoreCase("MOST_POPULATED")) {
991997
if (registeredServer.getTotalPlayerCount() > selectedServer.get().getTotalPlayerCount()) {
992998
selectedServer = Optional.of(registeredServer);
993-
tryIndex = i + 1;
999+
tryIndex = i;
9941000
}
9951001
} else if (strategy.equalsIgnoreCase("LEAST_POPULATED")) {
9961002
if (registeredServer.getTotalPlayerCount() < selectedServer.get().getTotalPlayerCount()) {
9971003
selectedServer = Optional.of(registeredServer);
998-
tryIndex = i + 1;
1004+
tryIndex = i;
9991005
}
10001006
}
10011007
}
@@ -1576,11 +1582,6 @@ private void resetIfInFlightIs(final VelocityServerConnection establishedConnect
15761582

15771583
@Override
15781584
public CompletableFuture<Result> connect() {
1579-
if (connectionInProgress) {
1580-
return completedFuture(ConnectionRequestResults.plainResult(Status.CONNECTION_CANCELLED, toConnect));
1581-
}
1582-
1583-
connectionInProgress = true;
15841585
return this.internalConnect().whenCompleteAsync((status, throwable) -> {
15851586
if (status != null && !status.isSuccessful()) {
15861587
if (!status.isSafe()) {
@@ -1603,22 +1604,11 @@ public CompletableFuture<Result> connect() {
16031604
}
16041605
}
16051606
}
1606-
1607-
connectionInProgress = false;
1608-
}, connection.eventLoop())
1609-
.exceptionally((ex) -> {
1610-
connectionInProgress = false;
1611-
return null;
1612-
}).thenApply(x -> x);
1607+
}, connection.eventLoop()).thenApply(x -> x);
16131608
}
16141609

16151610
@Override
16161611
public CompletableFuture<Boolean> connectWithIndication() {
1617-
if (connectionInProgress) {
1618-
return completedFuture(false);
1619-
}
1620-
1621-
connectionInProgress = true;
16221612
return internalConnect().whenCompleteAsync((status, throwable) -> {
16231613
if (throwable != null) {
16241614
// TODO: The exception handling from this is not very good. Find a better way.
@@ -1649,19 +1639,13 @@ public CompletableFuture<Boolean> connectWithIndication() {
16491639
}
16501640
}
16511641
default -> {
1652-
// The only remaining value is successful (no need to do anything!)
1642+
// In this case, the default handler removes the user on server switch.
16531643
if (server.getConfiguration().getQueue().isRemovePlayerOnServerSwitch()) {
16541644
server.getQueueManager().removeFromAll(get());
16551645
}
16561646
}
16571647
}
1658-
1659-
connectionInProgress = false;
1660-
}, connection.eventLoop())
1661-
.exceptionally((ex) -> {
1662-
connectionInProgress = false;
1663-
return null;
1664-
}).thenApply(Result::isSuccessful);
1648+
}, connection.eventLoop()).thenApply(Result::isSuccessful);
16651649
}
16661650

16671651
@Override

proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Collections;
3333
import java.util.List;
3434
import java.util.Locale;
35+
import java.util.Map;
3536
import java.util.Optional;
3637
import java.util.UUID;
3738
import java.util.concurrent.CompletableFuture;
@@ -227,8 +228,22 @@ public CompletableFuture<ServerPing> getInitialPing(final VelocityInboundConnect
227228
String virtualHostStr = connection.getVirtualHost().map(InetSocketAddress::getHostString)
228229
.map(str -> str.toLowerCase(Locale.ROOT))
229230
.orElse("");
230-
List<String> serversToTry = server.getConfiguration().getForcedHosts().getOrDefault(
231-
virtualHostStr, server.getConfiguration().getAttemptConnectionOrder());
231+
232+
List<String> serversToTry = server.getConfiguration().getForcedHosts().get(virtualHostStr);
233+
if (serversToTry == null || serversToTry.isEmpty()) {
234+
for (Map.Entry<String, List<String>> entry : server.getConfiguration().getForcedHosts().entrySet()) {
235+
String pattern = entry.getKey().toLowerCase(Locale.ROOT);
236+
if (pattern.startsWith("*.") && virtualHostStr.endsWith(pattern.substring(1))) {
237+
serversToTry = entry.getValue();
238+
break;
239+
}
240+
}
241+
}
242+
243+
if (serversToTry == null || serversToTry.isEmpty()) {
244+
serversToTry = server.getConfiguration().getAttemptConnectionOrder();
245+
}
246+
232247
return attemptPingPassthrough(connection, passthroughMode, serversToTry, shownVersion, virtualHostStr);
233248
}
234249
}

0 commit comments

Comments
 (0)