3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-06 00:00:47 +01:00

Save /velocity dump output into a local file instead.

dump.velocitypowered.com is going to go offline on January 15th.
Dieser Commit ist enthalten in:
Andrew Steinborn 2023-01-01 11:26:45 -05:00
Ursprung c9e0e14eb1
Commit 118c4b451c

Datei anzeigen

@ -20,11 +20,8 @@ package com.velocitypowered.proxy.command.builtin;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.permission.Tristate;
@ -35,15 +32,20 @@ import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.util.ProxyVersion; import com.velocitypowered.api.util.ProxyVersion;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.util.InformationUtils; import com.velocitypowered.proxy.util.InformationUtils;
import java.net.ConnectException; import java.io.BufferedWriter;
import java.net.UnknownHostException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import net.kyori.adventure.identity.Identity; import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -56,10 +58,6 @@ import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
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.AsyncHttpClient;
import org.asynchttpclient.BoundRequestBuilder;
import org.asynchttpclient.ListenableFuture;
import org.asynchttpclient.Response;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
public class VelocityCommand implements SimpleCommand { public class VelocityCommand implements SimpleCommand {
@ -347,7 +345,7 @@ public class VelocityCommand implements SimpleCommand {
@Override @Override
public void execute(CommandSource source, String @NonNull [] args) { public void execute(CommandSource source, String @NonNull [] args) {
if (args.length != 0) { if (args.length != 0) {
source.sendMessage(Identity.nil(), Component.text("/velocity dump", NamedTextColor.RED)); source.sendMessage(Component.text("/velocity dump", NamedTextColor.RED));
return; return;
} }
@ -376,93 +374,25 @@ public class VelocityCommand implements SimpleCommand {
dump.add("config", proxyConfig); dump.add("config", proxyConfig);
dump.add("plugins", InformationUtils.collectPluginInfo(server)); dump.add("plugins", InformationUtils.collectPluginInfo(server));
source.sendMessage(Component.text().content("Uploading gathered information...").build()); Path dumpPath = Paths.get("velocity-dump-"
AsyncHttpClient httpClient = ((VelocityServer) server).getAsyncHttpClient(); + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
+ ".json");
try (BufferedWriter bw = Files.newBufferedWriter(
dumpPath, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW)) {
bw.write(InformationUtils.toHumanReadableString(dump));
BoundRequestBuilder request = source.sendMessage(Component.text(
httpClient.preparePost("https://dump.velocitypowered.com/documents"); "An anonymised report containing useful information about "
request.setHeader("Content-Type", "text/plain"); + "this proxy has been saved at " + dumpPath.toAbsolutePath(), NamedTextColor.GREEN));
request.addHeader("User-Agent", server.getVersion().getName() + "/" } catch (IOException e) {
+ server.getVersion().getVersion()); logger.error("Failed to complete dump command, "
request.setBody( + "the executor was interrupted: " + e.getMessage());
InformationUtils.toHumanReadableString(dump).getBytes(StandardCharsets.UTF_8)); e.printStackTrace();
source.sendMessage(Component.text(
ListenableFuture<Response> future = request.execute(); "We could not save the anonymized dump. Check the console for more details.",
future.addListener(() -> { NamedTextColor.RED)
try { );
Response response = future.get(); }
if (response.getStatusCode() != 200) {
source.sendMessage(Component.text()
.content("An error occurred while communicating with the Velocity servers. "
+ "The servers may be temporarily unavailable or there is an issue "
+ "with your network settings. You can find more information in the "
+ "log or console of your Velocity server.")
.color(NamedTextColor.RED).build());
logger.error("Invalid status code while POST-ing Velocity dump: "
+ response.getStatusCode());
logger.error("Headers: \n--------------BEGIN HEADERS--------------\n"
+ response.getHeaders().toString()
+ "\n---------------END HEADERS---------------");
return;
}
JsonObject key = InformationUtils.parseString(
response.getResponseBody(StandardCharsets.UTF_8));
if (!key.has("key")) {
throw new JsonSyntaxException("Missing Dump-Url-response");
}
String url = "https://dump.velocitypowered.com/"
+ key.get("key").getAsString() + ".json";
source.sendMessage(Component.text()
.content("Created an anonymised report containing useful information about "
+ "this proxy. If a developer requested it, you may share the "
+ "following link with them:")
.append(Component.newline())
.append(Component.text(">> " + url)
.color(NamedTextColor.GREEN)
.clickEvent(ClickEvent.openUrl(url)))
.append(Component.newline())
.append(Component.text("Note: This link is only valid for a few days")
.color(NamedTextColor.GRAY)
).build());
} catch (InterruptedException e) {
source.sendMessage(Component.text()
.content("Could not complete the request, the command was interrupted."
+ "Please refer to the proxy-log or console for more information.")
.color(NamedTextColor.RED).build());
logger.error("Failed to complete dump command, "
+ "the executor was interrupted: " + e.getMessage());
e.printStackTrace();
} catch (ExecutionException e) {
TextComponent.Builder message = Component.text()
.content("An error occurred while attempting to upload the gathered "
+ "information to the Velocity servers.")
.append(Component.newline())
.color(NamedTextColor.RED);
if (e.getCause() instanceof UnknownHostException
|| e.getCause() instanceof ConnectException) {
message.append(Component.text(
"Likely cause: Invalid system DNS settings or no internet connection"));
}
source.sendMessage(message
.append(Component.newline()
.append(Component.text(
"Error details can be found in the proxy log / console"))
).build());
logger.error("Failed to complete dump command, "
+ "the executor encountered an Exception: " + e.getCause().getMessage());
e.getCause().printStackTrace();
} catch (JsonParseException e) {
source.sendMessage(Component.text()
.content("An error occurred on the Velocity servers and the dump could not "
+ "be completed. Please contact the Velocity staff about this problem. "
+ "If you do, provide the details about this error from the Velocity "
+ "console or server log.")
.color(NamedTextColor.RED).build());
logger.error("Invalid response from the Velocity servers: " + e.getMessage());
e.printStackTrace();
}
}, MoreExecutors.directExecutor());
} }
@Override @Override