Skip to content

Commit

Permalink
Updated version command and moved away from HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed Mar 19, 2024
1 parent 3cd5caa commit d8b361a
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import static org.geysermc.floodgate.core.platform.command.Placeholder.literal;

import com.google.gson.JsonElement;
import it.unimi.dsi.fastutil.Pair;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
Expand All @@ -36,18 +35,16 @@
import java.util.function.BooleanSupplier;
import org.geysermc.floodgate.core.command.util.Permission;
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.MessageType;
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.incendo.cloud.context.CommandContext;

@Singleton
final class FirewallCheckSubcommand extends FloodgateSubCommand {
@Inject HttpClient httpProvider;
@Inject GlobalApiClient globalApiClient;

FirewallCheckSubcommand() {
super(
Expand All @@ -72,19 +69,7 @@ public void execute(CommandContext<UserAudience> context) {
}

private BooleanSupplier globalApiCheck(UserAudience sender) {
return executeFirewallText(
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()
));
}
});
return executeFirewallText(sender, "global api", () -> globalApiClient.health());
}

private BooleanSupplier executeFirewallText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@

import static org.geysermc.floodgate.core.platform.command.Placeholder.literal;

import com.google.gson.JsonElement;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import org.geysermc.floodgate.core.command.CommonCommandMessage;
import org.geysermc.floodgate.core.command.util.Permission;
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.platform.command.FloodgateSubCommand;
import org.geysermc.floodgate.core.platform.command.MessageType;
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.DynamicConstants;
import org.incendo.cloud.context.CommandContext;

@Singleton
public class VersionSubcommand extends FloodgateSubCommand {
@Inject HttpClient httpClient;
@Inject DownloadClient downloadClient;
@Inject FloodgateLogger logger;

VersionSubcommand() {
Expand All @@ -60,55 +60,33 @@ public void execute(CommandContext<UserAudience> context) {
UserAudience sender = context.sender();
sender.sendMessage(
Message.VERSION_INFO,
literal("version", Constants.FULL_VERSION),
literal("branch", Constants.GIT_BRANCH));
sender.sendMessage(Message.VERSION_FETCH_INFO);
literal("version", DynamicConstants.FULL_VERSION),
literal("branch", DynamicConstants.GIT_BRANCH));

String baseUrl = String.format(
"https://ci.opencollab.dev/job/GeyserMC/job/Floodgate/job/%s/lastSuccessfulBuild/",
Constants.GIT_BRANCH
);
sender.sendMessage(Message.VERSION_FETCH_INFO);

httpClient.asyncGet(
baseUrl + "buildNumber",
JsonElement.class
).whenComplete((result, error) -> {
downloadClient.latestBuildFor("floodgate").whenComplete((result, error) -> {
if (error != null) {
sender.sendMessage(Message.VERSION_FETCH_ERROR);
error.printStackTrace();
logger.error("An error occurred while fetching latest version", error);
return;
}

JsonElement response = result.getResponse();

logger.info(String.valueOf(response));
logger.info("{}", result.getHttpCode());

if (result.getHttpCode() == 404) {
if (result == null) {
sender.sendMessage(Message.VERSION_FETCH_NOT_FOUND);
return;
}

if (!result.isCodeOk()) {
//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();
int buildNumber = result.build();

if (buildNumber > Constants.BUILD_NUMBER) {
if (buildNumber > DynamicConstants.BUILD_NUMBER) {
sender.sendMessage(
Message.VERSION_OUTDATED,
literal("count", buildNumber - Constants.BUILD_NUMBER),
literal("url", baseUrl));
literal("count", buildNumber - DynamicConstants.BUILD_NUMBER),
literal("url", Constants.LATEST_DOWNLOAD_URL));
return;
}
if (buildNumber == Constants.BUILD_NUMBER) {
if (buildNumber == DynamicConstants.BUILD_NUMBER) {
sender.sendMessage(Message.VERSION_LATEST);
return;
}
Expand All @@ -119,7 +97,7 @@ public void execute(CommandContext<UserAudience> context) {
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_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_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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.geysermc.floodgate.core.http.downloads;

import io.micronaut.serde.annotation.Serdeable;

@Serdeable
public record LatestBuildResult(int build) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@

package org.geysermc.floodgate.core.http.xbox;

import static io.micronaut.http.HttpHeaders.ACCEPT;
import static io.micronaut.http.HttpHeaders.USER_AGENT;

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.NotNull;
import jakarta.validation.constraints.Size;
import java.util.concurrent.CompletableFuture;
import org.geysermc.floodgate.core.util.Constants;

@Client("${http.baseUrl}/v2/xbox")
@Header(name = USER_AGENT, value = "${http.userAgent}")
@Header(name = ACCEPT, value = "application/json")
@Client("${http.baseUrl.api}/v2/xbox")
@Header(name = HttpHeaders.USER_AGENT, value = Constants.USER_AGENT)
public interface XboxClient {
@Get("/xuid/{gamertag}")
CompletableFuture<GetXuidResult> xuidByGamertag(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.geysermc.floodgate.core.crypto.DataCodecType;
import org.geysermc.floodgate.core.crypto.topping.Base64Topping;
import org.geysermc.floodgate.core.crypto.topping.Topping;
import org.geysermc.floodgate.core.util.Constants;

@Factory
public class CommonModule {
Expand Down Expand Up @@ -71,20 +70,6 @@ public Topping topping() {
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
@Singleton
@Named("kickMessageAttribute")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
package org.geysermc.floodgate.core.util;

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 CONFIG_VERSION = 3;
Expand All @@ -38,25 +34,14 @@ public final class Constants {
public static final boolean DEBUG_MODE = 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";
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 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 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 =
"An internal error happened while handling Floodgate data." +
" Try logging in again or contact a server administrator if the issue persists.";
Expand Down
Loading

0 comments on commit d8b361a

Please sign in to comment.