From 6d7335b21fb25054dbfd871b099a71c0eb6d7c11 Mon Sep 17 00:00:00 2001 From: Adrian <68704415+4drian3d@users.noreply.github.com> Date: Wed, 17 Jan 2024 10:20:27 -0500 Subject: [PATCH] Fully replaced AsyncHttpClient (#1194) --- gradle/libs.versions.toml | 1 - proxy/build.gradle.kts | 1 - .../velocitypowered/proxy/VelocityServer.java | 5 --- .../client/InitialLoginSessionHandler.java | 6 +++- .../proxy/network/ConnectionManager.java | 32 ++++--------------- 5 files changed, 12 insertions(+), 33 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 25f58c3ac..1e7328dfd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -15,7 +15,6 @@ spotless = "com.diffplug.spotless:6.12.0" adventure-bom = "net.kyori:adventure-bom:4.15.0-SNAPSHOT" adventure-facet = "net.kyori:adventure-platform-facet:4.3.0" asm = "org.ow2.asm:asm:9.5" -asynchttpclient = "org.asynchttpclient:async-http-client:2.12.3" brigadier = "com.velocitypowered:velocity-brigadier:1.0.0-SNAPSHOT" bstats = "org.bstats:bstats-base:3.0.1" caffeine = "com.github.ben-manes.caffeine:caffeine:3.1.5" diff --git a/proxy/build.gradle.kts b/proxy/build.gradle.kts index 643689d40..b3026977a 100644 --- a/proxy/build.gradle.kts +++ b/proxy/build.gradle.kts @@ -119,7 +119,6 @@ dependencies { implementation(platform(libs.adventure.bom)) implementation("net.kyori:adventure-nbt") implementation(libs.adventure.facet) - implementation(libs.asynchttpclient) implementation(libs.completablefutures) implementation(libs.nightconfig) implementation(libs.bstats) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index ddf302e78..6efe04e25 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -101,7 +101,6 @@ import net.kyori.adventure.translation.GlobalTranslator; import net.kyori.adventure.translation.TranslationRegistry; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.asynchttpclient.AsyncHttpClient; import org.checkerframework.checker.nullness.qual.EnsuresNonNull; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.NonNull; @@ -595,10 +594,6 @@ public class VelocityServer implements ProxyServer, ForwardingAudience { } public HttpClient getHttpClient() { - return httpClient; - } - - public AsyncHttpClient getAsyncHttpClient() { return cm.getHttpClient(); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java index 53fe21cad..6ba98ae0e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java @@ -209,7 +209,11 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler { url += "&ip=" + urlFormParameterEscaper().escape(playerIp); } - HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(url)).build(); + final HttpRequest httpRequest = HttpRequest.newBuilder() + .setHeader("User-Agent", + server.getVersion().getName() + "/" + server.getVersion().getVersion()) + .uri(URI.create(url)) + .build(); server.getHttpClient().sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()) .whenCompleteAsync((response, throwable) -> { if (mcConnection.isClosed()) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java index 6175f82e2..23fd55c7d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java @@ -17,9 +17,6 @@ package com.velocitypowered.proxy.network; -import static org.asynchttpclient.Dsl.asyncHttpClient; -import static org.asynchttpclient.Dsl.config; - import com.google.common.base.Preconditions; import com.velocitypowered.api.event.proxy.ListenerBoundEvent; import com.velocitypowered.api.event.proxy.ListenerCloseEvent; @@ -37,15 +34,11 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.WriteBufferWaterMark; import io.netty.util.concurrent.GlobalEventExecutor; import java.net.InetSocketAddress; +import java.net.http.HttpClient; import java.util.HashMap; import java.util.Map; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.asynchttpclient.AsyncHttpClient; -import org.asynchttpclient.RequestBuilder; -import org.asynchttpclient.filter.FilterContext; -import org.asynchttpclient.filter.FilterContext.FilterContextBuilder; -import org.asynchttpclient.filter.RequestFilter; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -69,7 +62,7 @@ public final class ConnectionManager { public final BackendChannelInitializerHolder backendChannelInitializer; private final SeparatePoolInetNameResolver resolver; - private final AsyncHttpClient httpClient; + private final HttpClient httpClient; /** * Initalizes the {@code ConnectionManager}. @@ -86,20 +79,9 @@ public final class ConnectionManager { this.backendChannelInitializer = new BackendChannelInitializerHolder( new BackendChannelInitializer(this.server)); this.resolver = new SeparatePoolInetNameResolver(GlobalEventExecutor.INSTANCE); - this.httpClient = asyncHttpClient(config() - .setEventLoopGroup(this.workerGroup) - .setUserAgent(server.getVersion().getName() + "/" + server.getVersion().getVersion()) - .addRequestFilter(new RequestFilter() { - @Override - public FilterContext filter(FilterContext ctx) { - return new FilterContextBuilder<>(ctx) - .request(new RequestBuilder(ctx.getRequest()) - .setNameResolver(resolver) - .build()) - .build(); - } - }) - .build()); + this.httpClient = HttpClient.newBuilder() + .executor(this.workerGroup) + .build(); } public void logChannelInformation() { @@ -256,8 +238,8 @@ public final class ConnectionManager { return this.serverChannelInitializer; } - public AsyncHttpClient getHttpClient() { - return httpClient; + public HttpClient getHttpClient() { + return this.httpClient; } public BackendChannelInitializerHolder getBackendChannelInitializer() {