mirror of
https://github.com/GeyserMC/Floodgate.git
synced 2025-12-24 01:09:19 +00:00
Updated version command and moved away from HttpClient
This commit is contained in:
@@ -27,7 +27,6 @@ package org.geysermc.floodgate.core.command.main;
|
|||||||
|
|
||||||
import static org.geysermc.floodgate.core.platform.command.Placeholder.literal;
|
import static org.geysermc.floodgate.core.platform.command.Placeholder.literal;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import it.unimi.dsi.fastutil.Pair;
|
import it.unimi.dsi.fastutil.Pair;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import jakarta.inject.Singleton;
|
import jakarta.inject.Singleton;
|
||||||
@@ -36,18 +35,16 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
import java.util.function.BooleanSupplier;
|
import java.util.function.BooleanSupplier;
|
||||||
import org.geysermc.floodgate.core.command.util.Permission;
|
import org.geysermc.floodgate.core.command.util.Permission;
|
||||||
import org.geysermc.floodgate.core.connection.audience.UserAudience;
|
import org.geysermc.floodgate.core.connection.audience.UserAudience;
|
||||||
|
import org.geysermc.floodgate.core.http.api.GlobalApiClient;
|
||||||
import org.geysermc.floodgate.core.platform.command.FloodgateSubCommand;
|
import org.geysermc.floodgate.core.platform.command.FloodgateSubCommand;
|
||||||
import org.geysermc.floodgate.core.platform.command.MessageType;
|
import org.geysermc.floodgate.core.platform.command.MessageType;
|
||||||
import org.geysermc.floodgate.core.platform.command.TranslatableMessage;
|
import org.geysermc.floodgate.core.platform.command.TranslatableMessage;
|
||||||
import org.geysermc.floodgate.core.util.Constants;
|
|
||||||
import org.geysermc.floodgate.core.util.HttpClient;
|
|
||||||
import org.geysermc.floodgate.core.util.HttpClient.HttpResponse;
|
|
||||||
import org.geysermc.floodgate.core.util.Utils;
|
import org.geysermc.floodgate.core.util.Utils;
|
||||||
import org.incendo.cloud.context.CommandContext;
|
import org.incendo.cloud.context.CommandContext;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
final class FirewallCheckSubcommand extends FloodgateSubCommand {
|
final class FirewallCheckSubcommand extends FloodgateSubCommand {
|
||||||
@Inject HttpClient httpProvider;
|
@Inject GlobalApiClient globalApiClient;
|
||||||
|
|
||||||
FirewallCheckSubcommand() {
|
FirewallCheckSubcommand() {
|
||||||
super(
|
super(
|
||||||
@@ -72,19 +69,7 @@ final class FirewallCheckSubcommand extends FloodgateSubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private BooleanSupplier globalApiCheck(UserAudience sender) {
|
private BooleanSupplier globalApiCheck(UserAudience sender) {
|
||||||
return executeFirewallText(
|
return executeFirewallText(sender, "global api", () -> globalApiClient.health());
|
||||||
sender, "global api",
|
|
||||||
() -> {
|
|
||||||
HttpResponse<JsonElement> response =
|
|
||||||
httpProvider.get(Constants.HEALTH_URL, JsonElement.class);
|
|
||||||
|
|
||||||
if (!response.isCodeOk()) {
|
|
||||||
throw new IllegalStateException(String.format(
|
|
||||||
"Didn't receive an 'ok' http code. Got: %s, response: %s",
|
|
||||||
response.getHttpCode(), response.getResponse()
|
|
||||||
));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private BooleanSupplier executeFirewallText(
|
private BooleanSupplier executeFirewallText(
|
||||||
|
|||||||
@@ -27,23 +27,23 @@ package org.geysermc.floodgate.core.command.main;
|
|||||||
|
|
||||||
import static org.geysermc.floodgate.core.platform.command.Placeholder.literal;
|
import static org.geysermc.floodgate.core.platform.command.Placeholder.literal;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import jakarta.inject.Singleton;
|
import jakarta.inject.Singleton;
|
||||||
import org.geysermc.floodgate.core.command.CommonCommandMessage;
|
import org.geysermc.floodgate.core.command.CommonCommandMessage;
|
||||||
import org.geysermc.floodgate.core.command.util.Permission;
|
import org.geysermc.floodgate.core.command.util.Permission;
|
||||||
import org.geysermc.floodgate.core.connection.audience.UserAudience;
|
import org.geysermc.floodgate.core.connection.audience.UserAudience;
|
||||||
|
import org.geysermc.floodgate.core.http.downloads.DownloadClient;
|
||||||
import org.geysermc.floodgate.core.logger.FloodgateLogger;
|
import org.geysermc.floodgate.core.logger.FloodgateLogger;
|
||||||
import org.geysermc.floodgate.core.platform.command.FloodgateSubCommand;
|
import org.geysermc.floodgate.core.platform.command.FloodgateSubCommand;
|
||||||
import org.geysermc.floodgate.core.platform.command.MessageType;
|
import org.geysermc.floodgate.core.platform.command.MessageType;
|
||||||
import org.geysermc.floodgate.core.platform.command.TranslatableMessage;
|
import org.geysermc.floodgate.core.platform.command.TranslatableMessage;
|
||||||
import org.geysermc.floodgate.core.util.Constants;
|
import org.geysermc.floodgate.core.util.Constants;
|
||||||
import org.geysermc.floodgate.core.util.HttpClient;
|
import org.geysermc.floodgate.core.util.DynamicConstants;
|
||||||
import org.incendo.cloud.context.CommandContext;
|
import org.incendo.cloud.context.CommandContext;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
public class VersionSubcommand extends FloodgateSubCommand {
|
public class VersionSubcommand extends FloodgateSubCommand {
|
||||||
@Inject HttpClient httpClient;
|
@Inject DownloadClient downloadClient;
|
||||||
@Inject FloodgateLogger logger;
|
@Inject FloodgateLogger logger;
|
||||||
|
|
||||||
VersionSubcommand() {
|
VersionSubcommand() {
|
||||||
@@ -60,55 +60,33 @@ public class VersionSubcommand extends FloodgateSubCommand {
|
|||||||
UserAudience sender = context.sender();
|
UserAudience sender = context.sender();
|
||||||
sender.sendMessage(
|
sender.sendMessage(
|
||||||
Message.VERSION_INFO,
|
Message.VERSION_INFO,
|
||||||
literal("version", Constants.FULL_VERSION),
|
literal("version", DynamicConstants.FULL_VERSION),
|
||||||
literal("branch", Constants.GIT_BRANCH));
|
literal("branch", DynamicConstants.GIT_BRANCH));
|
||||||
|
|
||||||
sender.sendMessage(Message.VERSION_FETCH_INFO);
|
sender.sendMessage(Message.VERSION_FETCH_INFO);
|
||||||
|
|
||||||
String baseUrl = String.format(
|
downloadClient.latestBuildFor("floodgate").whenComplete((result, error) -> {
|
||||||
"https://ci.opencollab.dev/job/GeyserMC/job/Floodgate/job/%s/lastSuccessfulBuild/",
|
|
||||||
Constants.GIT_BRANCH
|
|
||||||
);
|
|
||||||
|
|
||||||
httpClient.asyncGet(
|
|
||||||
baseUrl + "buildNumber",
|
|
||||||
JsonElement.class
|
|
||||||
).whenComplete((result, error) -> {
|
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
sender.sendMessage(Message.VERSION_FETCH_ERROR);
|
sender.sendMessage(Message.VERSION_FETCH_ERROR);
|
||||||
error.printStackTrace();
|
logger.error("An error occurred while fetching latest version", error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonElement response = result.getResponse();
|
if (result == null) {
|
||||||
|
|
||||||
logger.info(String.valueOf(response));
|
|
||||||
logger.info("{}", result.getHttpCode());
|
|
||||||
|
|
||||||
if (result.getHttpCode() == 404) {
|
|
||||||
sender.sendMessage(Message.VERSION_FETCH_NOT_FOUND);
|
sender.sendMessage(Message.VERSION_FETCH_NOT_FOUND);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result.isCodeOk()) {
|
int buildNumber = result.build();
|
||||||
//todo make it more generic instead of using a Whitelist command strings
|
|
||||||
logger.error(
|
|
||||||
"Got an error from requesting the latest Floodgate version: {}",
|
|
||||||
response.toString()
|
|
||||||
);
|
|
||||||
sender.sendMessage(CommonCommandMessage.UNEXPECTED_ERROR);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int buildNumber = response.getAsInt();
|
if (buildNumber > DynamicConstants.BUILD_NUMBER) {
|
||||||
|
|
||||||
if (buildNumber > Constants.BUILD_NUMBER) {
|
|
||||||
sender.sendMessage(
|
sender.sendMessage(
|
||||||
Message.VERSION_OUTDATED,
|
Message.VERSION_OUTDATED,
|
||||||
literal("count", buildNumber - Constants.BUILD_NUMBER),
|
literal("count", buildNumber - DynamicConstants.BUILD_NUMBER),
|
||||||
literal("url", baseUrl));
|
literal("url", Constants.LATEST_DOWNLOAD_URL));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (buildNumber == Constants.BUILD_NUMBER) {
|
if (buildNumber == DynamicConstants.BUILD_NUMBER) {
|
||||||
sender.sendMessage(Message.VERSION_LATEST);
|
sender.sendMessage(Message.VERSION_LATEST);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -119,7 +97,7 @@ public class VersionSubcommand extends FloodgateSubCommand {
|
|||||||
public static final class Message {
|
public static final class Message {
|
||||||
public static final TranslatableMessage VERSION_INFO = new TranslatableMessage("floodgate.command.main.version.info", MessageType.NORMAL);
|
public static final TranslatableMessage VERSION_INFO = new TranslatableMessage("floodgate.command.main.version.info", MessageType.NORMAL);
|
||||||
public static final TranslatableMessage VERSION_FETCH_INFO = new TranslatableMessage("floodgate.command.main.version.fetch.info", MessageType.INFO);
|
public static final TranslatableMessage VERSION_FETCH_INFO = new TranslatableMessage("floodgate.command.main.version.fetch.info", MessageType.INFO);
|
||||||
public static final TranslatableMessage VERSION_FETCH_ERROR = new TranslatableMessage("floodgate.command.main.version.fetch.error", MessageType.ERROR);
|
public static final TranslatableMessage VERSION_FETCH_ERROR = new TranslatableMessage("floodgate.command.main.version.fetch.error " + CommonCommandMessage.CHECK_CONSOLE, MessageType.ERROR);
|
||||||
public static final TranslatableMessage VERSION_FETCH_NOT_FOUND = new TranslatableMessage("floodgate.command.main.version.fetch.not_found", MessageType.ERROR);
|
public static final TranslatableMessage VERSION_FETCH_NOT_FOUND = new TranslatableMessage("floodgate.command.main.version.fetch.not_found", MessageType.ERROR);
|
||||||
public static final TranslatableMessage VERSION_OUTDATED = new TranslatableMessage("floodgate.command.main.version.fetch.result.outdated", MessageType.NORMAL);
|
public static final TranslatableMessage VERSION_OUTDATED = new TranslatableMessage("floodgate.command.main.version.fetch.result.outdated", MessageType.NORMAL);
|
||||||
public static final TranslatableMessage VERSION_LATEST = new TranslatableMessage("floodgate.command.main.version.fetch.result.latest", MessageType.SUCCESS);
|
public static final TranslatableMessage VERSION_LATEST = new TranslatableMessage("floodgate.command.main.version.fetch.result.latest", MessageType.SUCCESS);
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package org.geysermc.floodgate.core.http.api;
|
||||||
|
|
||||||
|
import io.micronaut.http.HttpHeaders;
|
||||||
|
import io.micronaut.http.annotation.Get;
|
||||||
|
import io.micronaut.http.annotation.Header;
|
||||||
|
import io.micronaut.http.client.annotation.Client;
|
||||||
|
import org.geysermc.floodgate.core.util.Constants;
|
||||||
|
|
||||||
|
@Client("${http.baseUrl.api}")
|
||||||
|
@Header(name = HttpHeaders.USER_AGENT, value = Constants.USER_AGENT)
|
||||||
|
public interface GlobalApiClient {
|
||||||
|
/**
|
||||||
|
* Checks if it can connect to the Global Api, any other status code than 204 or 404 will throw
|
||||||
|
*/
|
||||||
|
@Get("/healthy")
|
||||||
|
void health();
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.geysermc.floodgate.core.http.downloads;
|
||||||
|
|
||||||
|
import io.micronaut.http.HttpHeaders;
|
||||||
|
import io.micronaut.http.annotation.Get;
|
||||||
|
import io.micronaut.http.annotation.Header;
|
||||||
|
import io.micronaut.http.client.annotation.Client;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import org.geysermc.floodgate.core.util.Constants;
|
||||||
|
|
||||||
|
@Client("${http.baseUrl.download}")
|
||||||
|
@Header(name = HttpHeaders.USER_AGENT, value = Constants.USER_AGENT)
|
||||||
|
public interface DownloadClient {
|
||||||
|
@Get("/v2/projects/{project}/versions/latest/builds/latest")
|
||||||
|
CompletableFuture<LatestBuildResult> latestBuildFor(@NotBlank String project);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.geysermc.floodgate.core.http.downloads;
|
||||||
|
|
||||||
|
import io.micronaut.serde.annotation.Serdeable;
|
||||||
|
|
||||||
|
@Serdeable
|
||||||
|
public record LatestBuildResult(int build) {
|
||||||
|
}
|
||||||
@@ -25,19 +25,17 @@
|
|||||||
|
|
||||||
package org.geysermc.floodgate.core.http.xbox;
|
package org.geysermc.floodgate.core.http.xbox;
|
||||||
|
|
||||||
import static io.micronaut.http.HttpHeaders.ACCEPT;
|
import io.micronaut.http.HttpHeaders;
|
||||||
import static io.micronaut.http.HttpHeaders.USER_AGENT;
|
|
||||||
|
|
||||||
import io.micronaut.http.annotation.Get;
|
import io.micronaut.http.annotation.Get;
|
||||||
import io.micronaut.http.annotation.Header;
|
import io.micronaut.http.annotation.Header;
|
||||||
import io.micronaut.http.client.annotation.Client;
|
import io.micronaut.http.client.annotation.Client;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import org.geysermc.floodgate.core.util.Constants;
|
||||||
|
|
||||||
@Client("${http.baseUrl}/v2/xbox")
|
@Client("${http.baseUrl.api}/v2/xbox")
|
||||||
@Header(name = USER_AGENT, value = "${http.userAgent}")
|
@Header(name = HttpHeaders.USER_AGENT, value = Constants.USER_AGENT)
|
||||||
@Header(name = ACCEPT, value = "application/json")
|
|
||||||
public interface XboxClient {
|
public interface XboxClient {
|
||||||
@Get("/xuid/{gamertag}")
|
@Get("/xuid/{gamertag}")
|
||||||
CompletableFuture<GetXuidResult> xuidByGamertag(
|
CompletableFuture<GetXuidResult> xuidByGamertag(
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ import org.geysermc.api.connection.Connection;
|
|||||||
import org.geysermc.floodgate.core.crypto.DataCodecType;
|
import org.geysermc.floodgate.core.crypto.DataCodecType;
|
||||||
import org.geysermc.floodgate.core.crypto.topping.Base64Topping;
|
import org.geysermc.floodgate.core.crypto.topping.Base64Topping;
|
||||||
import org.geysermc.floodgate.core.crypto.topping.Topping;
|
import org.geysermc.floodgate.core.crypto.topping.Topping;
|
||||||
import org.geysermc.floodgate.core.util.Constants;
|
|
||||||
|
|
||||||
@Factory
|
@Factory
|
||||||
public class CommonModule {
|
public class CommonModule {
|
||||||
@@ -71,20 +70,6 @@ public class CommonModule {
|
|||||||
return new Base64Topping();
|
return new Base64Topping();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Singleton
|
|
||||||
@Named("gitBranch")
|
|
||||||
public String gitBranch() {
|
|
||||||
return Constants.GIT_BRANCH;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Singleton
|
|
||||||
@Named("buildNumber")
|
|
||||||
public int buildNumber() {
|
|
||||||
return Constants.BUILD_NUMBER;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Singleton
|
@Singleton
|
||||||
@Named("kickMessageAttribute")
|
@Named("kickMessageAttribute")
|
||||||
|
|||||||
@@ -26,10 +26,6 @@
|
|||||||
package org.geysermc.floodgate.core.util;
|
package org.geysermc.floodgate.core.util;
|
||||||
|
|
||||||
public final class Constants {
|
public final class Constants {
|
||||||
public static final String FULL_VERSION = "@fullVersion@";
|
|
||||||
public static final String VERSION = "@version@";
|
|
||||||
public static final int BUILD_NUMBER = Integer.parseInt("@buildNumber@");
|
|
||||||
public static final String GIT_BRANCH = "@branch@";
|
|
||||||
public static final int METRICS_ID = 14649;
|
public static final int METRICS_ID = 14649;
|
||||||
|
|
||||||
public static final int CONFIG_VERSION = 3;
|
public static final int CONFIG_VERSION = 3;
|
||||||
@@ -38,25 +34,14 @@ public final class Constants {
|
|||||||
public static final boolean DEBUG_MODE = false;
|
public static final boolean DEBUG_MODE = false;
|
||||||
public static final boolean PRINT_ALL_PACKETS = false;
|
public static final boolean PRINT_ALL_PACKETS = false;
|
||||||
|
|
||||||
public static final String DATABASE_NAME_FORMAT = "^floodgate-[a-zA-Z0-9_]{0,16}-database.jar$";
|
|
||||||
|
|
||||||
|
|
||||||
public static final String USER_AGENT = "GeyserMC/Floodgate";
|
public static final String USER_AGENT = "GeyserMC/Floodgate";
|
||||||
private static final String API_BASE_URL = "s://api.geysermc.org";
|
private static final String API_BASE_URL = "s://api.geysermc.org";
|
||||||
public static final String HEALTH_URL = "http" + API_BASE_URL + "/health";
|
public static final String HEALTH_URL = "http" + API_BASE_URL + "/health";
|
||||||
|
|
||||||
public static final String WEBSOCKET_URL = "ws" + API_BASE_URL + "/ws";
|
|
||||||
public static final String GET_XUID_URL = "http" + API_BASE_URL + "/v2/xbox/xuid/";
|
|
||||||
public static final String GET_GAMERTAG_URL = "http" + API_BASE_URL + "/v2/xbox/gamertag/";
|
|
||||||
public static final String NEWS_OVERVIEW_URL = "http" + API_BASE_URL + "/v2/news/";
|
|
||||||
public static final String GET_BEDROCK_LINK = "http" + API_BASE_URL + "/v2/link/bedrock/";
|
|
||||||
|
|
||||||
public static final String LINK_INFO_URL = "https://link.geysermc.org/";
|
public static final String LINK_INFO_URL = "https://link.geysermc.org/";
|
||||||
|
public static final String LATEST_DOWNLOAD_URL = "https://geysermc.org/download";
|
||||||
|
|
||||||
public static final String NEWS_PROJECT_NAME = "floodgate";
|
|
||||||
|
|
||||||
|
|
||||||
public static final String NTP_SERVER = "time.cloudflare.com";
|
|
||||||
public static final String INTERNAL_ERROR_MESSAGE =
|
public static final String INTERNAL_ERROR_MESSAGE =
|
||||||
"An internal error happened while handling Floodgate data." +
|
"An internal error happened while handling Floodgate data." +
|
||||||
" Try logging in again or contact a server administrator if the issue persists.";
|
" Try logging in again or contact a server administrator if the issue persists.";
|
||||||
@@ -1,182 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*
|
|
||||||
* @author GeyserMC
|
|
||||||
* @link https://github.com/GeyserMC/Floodgate
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.geysermc.floodgate.core.util;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import jakarta.inject.Inject;
|
|
||||||
import jakarta.inject.Named;
|
|
||||||
import jakarta.inject.Singleton;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import lombok.AccessLevel;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
||||||
|
|
||||||
// resources are properly closed and ignoring the original stack trace is intended
|
|
||||||
@SuppressWarnings({"PMD.CloseResource", "PMD.PreserveStackTrace"})
|
|
||||||
@Singleton
|
|
||||||
public class HttpClient {
|
|
||||||
private final Gson gson = new Gson();
|
|
||||||
@Inject
|
|
||||||
@Named("commonPool")
|
|
||||||
ExecutorService executorService;
|
|
||||||
|
|
||||||
public CompletableFuture<DefaultHttpResponse> asyncGet(String urlString) {
|
|
||||||
return CompletableFuture.supplyAsync(() -> get(urlString), executorService);
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T> CompletableFuture<HttpResponse<T>> asyncGet(String urlString, Class<T> response) {
|
|
||||||
return CompletableFuture.supplyAsync(() -> get(urlString, response), executorService);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DefaultHttpResponse get(String urlString) {
|
|
||||||
return readDefaultResponse(request(urlString));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T> HttpResponse<T> get(String urlString, Class<T> clazz) {
|
|
||||||
return readResponse(request(urlString), clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T> HttpResponse<T> getSilent(String urlString, Class<T> clazz) {
|
|
||||||
return readResponseSilent(request(urlString), clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
private HttpURLConnection request(String urlString) {
|
|
||||||
HttpURLConnection connection;
|
|
||||||
|
|
||||||
try {
|
|
||||||
URL url = new URL(urlString.replace(" ", "%20")); // Encode spaces correctly
|
|
||||||
connection = (HttpURLConnection) url.openConnection();
|
|
||||||
} catch (Exception exception) {
|
|
||||||
throw new RuntimeException("Failed to create connection", exception);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
connection.setRequestMethod("GET");
|
|
||||||
connection.setUseCaches(false);
|
|
||||||
connection.setRequestProperty("User-Agent", Constants.USER_AGENT);
|
|
||||||
connection.setConnectTimeout(3000);
|
|
||||||
connection.setReadTimeout(5000);
|
|
||||||
} catch (Exception exception) {
|
|
||||||
throw new RuntimeException("Failed to create request", exception);
|
|
||||||
}
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
private <T> HttpResponse<T> readResponse(HttpURLConnection connection, Class<T> clazz) {
|
|
||||||
InputStreamReader streamReader = createReader(connection);
|
|
||||||
if (streamReader == null) {
|
|
||||||
return new HttpResponse<>(-1, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
int responseCode = -1;
|
|
||||||
try {
|
|
||||||
responseCode = connection.getResponseCode();
|
|
||||||
T response = gson.fromJson(streamReader, clazz);
|
|
||||||
return new HttpResponse<>(responseCode, response);
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
// e.g. when the response isn't JSON
|
|
||||||
return new HttpResponse<>(responseCode, null);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
streamReader.close();
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
private <T> HttpResponse<T> readResponseSilent(HttpURLConnection connection, Class<T> clazz) {
|
|
||||||
try {
|
|
||||||
return readResponse(connection, clazz);
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
return new HttpResponse<>(-1, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
private DefaultHttpResponse readDefaultResponse(HttpURLConnection connection) {
|
|
||||||
InputStreamReader streamReader = createReader(connection);
|
|
||||||
if (streamReader == null) {
|
|
||||||
return new DefaultHttpResponse(-1, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
int responseCode = connection.getResponseCode();
|
|
||||||
JsonObject response = gson.fromJson(streamReader, JsonObject.class);
|
|
||||||
return new DefaultHttpResponse(responseCode, response);
|
|
||||||
} catch (Exception exception) {
|
|
||||||
throw new RuntimeException("Failed to read response", exception);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
streamReader.close();
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private InputStreamReader createReader(HttpURLConnection connection) {
|
|
||||||
InputStream stream;
|
|
||||||
try {
|
|
||||||
stream = connection.getInputStream();
|
|
||||||
} catch (Exception exception) {
|
|
||||||
stream = connection.getErrorStream();
|
|
||||||
}
|
|
||||||
|
|
||||||
// it's null for example when it couldn't connect to the server
|
|
||||||
if (stream != null) {
|
|
||||||
return new InputStreamReader(stream);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
|
||||||
public static class HttpResponse<T> {
|
|
||||||
private final int httpCode;
|
|
||||||
private final T response;
|
|
||||||
|
|
||||||
public boolean isCodeOk() {
|
|
||||||
return httpCode >= 200 && httpCode < 300;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final class DefaultHttpResponse extends HttpResponse<JsonObject> {
|
|
||||||
DefaultHttpResponse(int httpCode, JsonObject response) {
|
|
||||||
super(httpCode, response);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -101,7 +101,7 @@ public final class Metrics {
|
|||||||
);
|
);
|
||||||
|
|
||||||
metricsBase.addCustomChart(
|
metricsBase.addCustomChart(
|
||||||
new SimplePie("floodgate_version", () -> Constants.FULL_VERSION)
|
new SimplePie("floodgate_version", () -> DynamicConstants.FULL_VERSION)
|
||||||
);
|
);
|
||||||
|
|
||||||
metricsBase.addCustomChart(
|
metricsBase.addCustomChart(
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
http:
|
http:
|
||||||
baseUrl: https://api.geysermc.org
|
baseUrl:
|
||||||
userAgent: GeyserMC/Floodgate
|
api: https://api.geysermc.org
|
||||||
|
download: https://download.geysermc.org
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author GeyserMC
|
||||||
|
* @link https://github.com/GeyserMC/Floodgate
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.geysermc.floodgate.core.util;
|
||||||
|
|
||||||
|
public final class DynamicConstants {
|
||||||
|
public static final String FULL_VERSION = "@fullVersion@";
|
||||||
|
public static final String VERSION = "@version@";
|
||||||
|
public static final int BUILD_NUMBER = Integer.parseInt("@buildNumber@");
|
||||||
|
public static final String GIT_BRANCH = "@branch@";
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user