3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-09-29 06:30:16 +02:00

Migrate to using built-in http client (#1192)

Dieser Commit ist enthalten in:
Shane Freeder 2024-01-17 15:02:36 +00:00 committet von GitHub
Ursprung fe052e5163
Commit b22b2dc458
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
2 geänderte Dateien mit 64 neuen und 60 gelöschten Zeilen

Datei anzeigen

@ -71,6 +71,7 @@ import io.netty.channel.EventLoopGroup;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.http.HttpClient;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.AccessController; import java.security.AccessController;
@ -143,6 +144,7 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
private final ConnectionManager cm; private final ConnectionManager cm;
private final ProxyOptions options; private final ProxyOptions options;
private final HttpClient httpClient;
private @MonotonicNonNull VelocityConfiguration configuration; private @MonotonicNonNull VelocityConfiguration configuration;
private @MonotonicNonNull KeyPair serverKeyPair; private @MonotonicNonNull KeyPair serverKeyPair;
private final ServerMap servers; private final ServerMap servers;
@ -168,6 +170,7 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
scheduler = new VelocityScheduler(pluginManager); scheduler = new VelocityScheduler(pluginManager);
console = new VelocityConsole(this); console = new VelocityConsole(this);
cm = new ConnectionManager(this); cm = new ConnectionManager(this);
httpClient = HttpClient.newHttpClient();
servers = new ServerMap(this); servers = new ServerMap(this);
serverListPingHandler = new ServerListPingHandler(this); serverListPingHandler = new ServerListPingHandler(this);
this.options = options; this.options = options;
@ -591,6 +594,10 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
this.cm.closeEndpoints(false); this.cm.closeEndpoints(false);
} }
public HttpClient getHttpClient() {
return httpClient;
}
public AsyncHttpClient getAsyncHttpClient() { public AsyncHttpClient getAsyncHttpClient() {
return cm.getHttpClient(); return cm.getHttpClient();
} }

Datei anzeigen

@ -42,19 +42,19 @@ import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse;
import com.velocitypowered.proxy.protocol.packet.ServerLogin; import com.velocitypowered.proxy.protocol.packet.ServerLogin;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.asynchttpclient.ListenableFuture;
import org.asynchttpclient.Response;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** /**
@ -209,14 +209,20 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
url += "&ip=" + urlFormParameterEscaper().escape(playerIp); url += "&ip=" + urlFormParameterEscaper().escape(playerIp);
} }
ListenableFuture<Response> hasJoinedResponse = server.getAsyncHttpClient().prepareGet(url) HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(url)).build();
.execute(); server.getHttpClient().sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString())
hasJoinedResponse.addListener(() -> { .whenCompleteAsync((response, throwable) -> {
if (mcConnection.isClosed()) { if (mcConnection.isClosed()) {
// The player disconnected after we authenticated them. // The player disconnected after we authenticated them.
return; return;
} }
if (throwable != null) {
logger.error("Unable to authenticate player", throwable);
inbound.disconnect(Component.translatable("multiplayer.disconnect.authservers_down"));
return;
}
// Go ahead and enable encryption. Once the client sends EncryptionResponse, encryption // Go ahead and enable encryption. Once the client sends EncryptionResponse, encryption
// is enabled. // is enabled.
try { try {
@ -229,10 +235,8 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
return; return;
} }
try { if (response.statusCode() == 200) {
Response profileResponse = hasJoinedResponse.get(); final GameProfile profile = GENERAL_GSON.fromJson(response.body(),
if (profileResponse.getStatusCode() == 200) {
final GameProfile profile = GENERAL_GSON.fromJson(profileResponse.getResponseBody(),
GameProfile.class); GameProfile.class);
// Not so fast, now we verify the public key for 1.19.1+ // Not so fast, now we verify the public key for 1.19.1+
if (inbound.getIdentifiedKey() != null if (inbound.getIdentifiedKey() != null
@ -247,7 +251,7 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
// All went well, initialize the session. // All went well, initialize the session.
mcConnection.setActiveSessionHandler(StateRegistry.LOGIN, mcConnection.setActiveSessionHandler(StateRegistry.LOGIN,
new AuthSessionHandler(server, inbound, profile, true)); new AuthSessionHandler(server, inbound, profile, true));
} else if (profileResponse.getStatusCode() == 204) { } else if (response.statusCode() == 204) {
// Apparently an offline-mode user logged onto this online-mode proxy. // Apparently an offline-mode user logged onto this online-mode proxy.
inbound.disconnect( inbound.disconnect(
Component.translatable("velocity.error.online-mode-only", NamedTextColor.RED)); Component.translatable("velocity.error.online-mode-only", NamedTextColor.RED));
@ -255,16 +259,9 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
// Something else went wrong // Something else went wrong
logger.error( logger.error(
"Got an unexpected error code {} whilst contacting Mojang to log in {} ({})", "Got an unexpected error code {} whilst contacting Mojang to log in {} ({})",
profileResponse.getStatusCode(), login.getUsername(), playerIp); response.sslSession(), login.getUsername(), playerIp);
inbound.disconnect(Component.translatable("multiplayer.disconnect.authservers_down")); inbound.disconnect(Component.translatable("multiplayer.disconnect.authservers_down"));
} }
} catch (ExecutionException e) {
logger.error("Unable to authenticate with Mojang", e);
inbound.disconnect(Component.translatable("multiplayer.disconnect.authservers_down"));
} catch (InterruptedException e) {
// not much we can do usefully
Thread.currentThread().interrupt();
}
}, mcConnection.eventLoop()); }, mcConnection.eventLoop());
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
logger.error("Unable to enable encryption", e); logger.error("Unable to enable encryption", e);